使用 R 将纬度和经度转换为城市名称
Convert latitude and longitude to city names, using R
我有一个文件数据框,其中包含巴西一些河流的生化需氧量信息。我想将我拥有的数据(以地理坐标表示)转换为城市名称。我怎样才能做到这一点?我知道有一个名为 "ggmap" 的软件包可以帮助我,但我确定它是否正确
这里是数据的link:https://www.dropbox.com/s/rgkkqw50qqymil6/dbo.xls?dl=0
state river lat long year contagem mean
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AL Rio Mundaú -9.60 -35.8 2007 5 3
2 AL Rio Mundaú -9.60 -35.8 2010 5 2
3 AL Rio Mundaú -9.60 -35.8 2011 9 3.78
4 AL Zona dos Canais -9.71 -35.8 2007 5 2.2
5 AL Zona dos Canais -9.71 -35.8 2010 7 2
6 AL Zona dos Canais -9.71 -35.8 2011 9 2.11
这可以使用从光栅中提取函数来实现。或者也可以使用 sp 包中的 over 函数。我在下面采用的方法是使用提取函数完成的:
我先用你上面提供的数据做了一个数据框,然后得到了巴西的shapefile,用它从提供的坐标中提取了城市名称。这是我的代码和注释:
library(raster)
library(sp)
#### Coordinates that will be used to search ###
state = c('AL','AL','AL','AL','AL','AL')
river = c('Rio Mundaú', 'Rio Mundaú', 'Rio Mundaú' , 'Zona dos Can', 'Zona dos Can', 'Zona dos Canais')
lat = c(-9.60, -9.60, -9.60, -9.71, -9.71, -9.71)
long = c(-35.8, -35.8, -35.8, -35.8, -35.8, -35.8)
year = c(2007, 2010, 2011, 2007, 2010, 2011)
contagem = c(5, 5, 9, 5, 7, 9)
mean = c(3, 2, 3.78, 2.2, 2, 2.11)
brazil_data = data.frame(state, river, lat, long, year, contagem, mean)
### Getting the brazil shapefile
brazil = getData('GADM', country = 'Brazil', level = 3, type = "sp")
### Extracting the attributes from the shapefile for the given points
city_names = extract(brazil, brazil_data[, c("long", "lat")])[,12]
### Adding the city names to the Brazil data frame, with the coordinates
brazil_data$City = city_names
这是我们最后得到的:
> brazil_data
state river lat long year contagem mean City
1 AL Rio Mundaú -9.60 -35.8 2007 5 3.00 Santa Luzia do Norte
2 AL Rio Mundaú -9.60 -35.8 2010 5 2.00 Santa Luzia do Norte
3 AL Rio Mundaú -9.60 -35.8 2011 9 3.78 Santa Luzia do Norte
4 AL Zona dos Can -9.71 -35.8 2007 5 2.20 Marechal deodoro
5 AL Zona dos Can -9.71 -35.8 2010 7 2.00 Marechal deodoro
6 AL Zona dos Canais -9.71 -35.8 2011 9 2.11 Marechal deodoro
希望对您有所帮助!
我有一个文件数据框,其中包含巴西一些河流的生化需氧量信息。我想将我拥有的数据(以地理坐标表示)转换为城市名称。我怎样才能做到这一点?我知道有一个名为 "ggmap" 的软件包可以帮助我,但我确定它是否正确
这里是数据的link:https://www.dropbox.com/s/rgkkqw50qqymil6/dbo.xls?dl=0
state river lat long year contagem mean
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AL Rio Mundaú -9.60 -35.8 2007 5 3
2 AL Rio Mundaú -9.60 -35.8 2010 5 2
3 AL Rio Mundaú -9.60 -35.8 2011 9 3.78
4 AL Zona dos Canais -9.71 -35.8 2007 5 2.2
5 AL Zona dos Canais -9.71 -35.8 2010 7 2
6 AL Zona dos Canais -9.71 -35.8 2011 9 2.11
这可以使用从光栅中提取函数来实现。或者也可以使用 sp 包中的 over 函数。我在下面采用的方法是使用提取函数完成的:
我先用你上面提供的数据做了一个数据框,然后得到了巴西的shapefile,用它从提供的坐标中提取了城市名称。这是我的代码和注释:
library(raster)
library(sp)
#### Coordinates that will be used to search ###
state = c('AL','AL','AL','AL','AL','AL')
river = c('Rio Mundaú', 'Rio Mundaú', 'Rio Mundaú' , 'Zona dos Can', 'Zona dos Can', 'Zona dos Canais')
lat = c(-9.60, -9.60, -9.60, -9.71, -9.71, -9.71)
long = c(-35.8, -35.8, -35.8, -35.8, -35.8, -35.8)
year = c(2007, 2010, 2011, 2007, 2010, 2011)
contagem = c(5, 5, 9, 5, 7, 9)
mean = c(3, 2, 3.78, 2.2, 2, 2.11)
brazil_data = data.frame(state, river, lat, long, year, contagem, mean)
### Getting the brazil shapefile
brazil = getData('GADM', country = 'Brazil', level = 3, type = "sp")
### Extracting the attributes from the shapefile for the given points
city_names = extract(brazil, brazil_data[, c("long", "lat")])[,12]
### Adding the city names to the Brazil data frame, with the coordinates
brazil_data$City = city_names
这是我们最后得到的:
> brazil_data
state river lat long year contagem mean City
1 AL Rio Mundaú -9.60 -35.8 2007 5 3.00 Santa Luzia do Norte
2 AL Rio Mundaú -9.60 -35.8 2010 5 2.00 Santa Luzia do Norte
3 AL Rio Mundaú -9.60 -35.8 2011 9 3.78 Santa Luzia do Norte
4 AL Zona dos Can -9.71 -35.8 2007 5 2.20 Marechal deodoro
5 AL Zona dos Can -9.71 -35.8 2010 7 2.00 Marechal deodoro
6 AL Zona dos Canais -9.71 -35.8 2011 9 2.11 Marechal deodoro
希望对您有所帮助!