简体   繁体   English

使用 R 将纬度和经度转换为城市名称

[英]Convert latitude and longitude to city names, using R

I have a file data frame with informations about biochemical oxygen demand of some rivers, in Brazil.我有一个文件数据框,其中包含有关巴西某些河流的生化需氧量的信息。 I'd like to transform the data that I have (in geographic coordinates) to the name of the city.我想将我拥有的数据(以地理坐标表示)转换为城市名称。 How can I do that?我怎样才能做到这一点? I know there is a package called "ggmap" that could help me, but I'm note sure if it's the right one我知道有一个名为“ggmap”的软件包可以帮助我,但我不确定它是否正确

Here is the link for the data: https://www.dropbox.com/s/rgkkqw50qqymil6/dbo.xls?dl=0这是数据的链接: 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

This is possible using the extract function from raster.这可以使用栅格中的提取函数来实现。 Or also possible with the over function from the sp package.或者也可以使用 sp 包中的 over 函数。 The approach I have taken below is done using the extract function:我在下面采用的方法是使用 extract 函数完成的:

I first used your data provided above to make a data frame, and then got the Brazil shapefile, and used it to extract the city name from the coordinates provided.我首先使用你上面提供的数据制作了一个数据框,然后得到了巴西的shapefile,并用它从提供的坐标中提取了城市名称。 Here is my code with comments:这是我的代码和评论:

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

This is what we get at the end:这就是我们最后得到的:

> 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

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM