【问题标题】:How can I batch geocode street addresses from a csv file in to R?如何将 csv 文件中的街道地址批量地理编码到 R 中?
【发布时间】:2013-08-27 02:39:30
【问题描述】:

编辑;在下面回答。

可以使用 ggmap 像这样进行批量地理编码,文件名是我的。代码改编自David Smith's Revolutions Blog Post

library(ggmap)
#Read in csv file
FDNYHouse = read.csv("Path to your csv file here.csv")
#Get column header names if you don't already have them
names(FDNYHouse)
#Create a file of just addresses that need to be geocoded
#You can require a state by replacing State below with New York if state was missing
#Everything inside paste() is a column header from the csv file 
FDNYAddresses = with(FDNYHouse, paste(FacilityAddress, Borough, State, sep = ","))
#Now we can geocode the addresses
FDNYLocations = geocode(FDNYAddresses)
#The FDNYLocations file will have a lon and lat column representing your geocoded data
#My next problem is getting the shape file projection to match my geocoded points

我使用 ggplot2 和 shape 文件创建了纽约市人口普查区地图。接下来,我想使用我在此处下载的 csv 文件创建一个数据框,使用消防站的街道地址放置在地图顶部:

FDNY Firehouse Locations

人口普查区的形状文件位于此处(黑色为 2010 年版本):

NYC Shape File

我的问题是数据没有列出城市和州,而且我不知道如何编写一个函数来获取这些地址并使用 Google 对它们进行地理编码使用类似 ggmap

我们将不胜感激任何朝着正确方向提出的建议或推动。我是 R 和 stackoverflow 的新手,所以请放轻松。

编辑:是否有人将其标记为已经问过 A)查看我的实际数据或 B)意识到您认为我重复的问题是 3 岁?猜猜在过去的 3 年里,R 中没有发生任何新的事情,对吧?世界是平的,跟着人走。 /咆哮

我可以使用 ggmap 和 geocode() 函数来获取纬度和经度,而无需创建函数。

#As an example
install.packages("ggmap")
library(ggmap)
geocode("San Francisco")

再次,问题是如何告诉 R 读取我的 csv 文件,该文件缺少城市和州数据,以便它可以创建我需要的 200+ 纬度和经度测量值,而无需我对 1 个地址进行地理编码时间。

第二个问题是获取这些数据,制作一个数据框并将其添加到我已经拥有的 NYC 形状文件中。

对于没有看过这篇文章的大多数人的经验的人来说,3 年前的这个答案是复杂而令人困惑的……我也相信它不能回答我的问题。

【问题讨论】:

  • 消防站位置数据包含街道地址和自治市镇。 shapefile 给出了消防站编号、营和师。我见过询问如何将数据分配给邮政编码的问题,但从来没有遇到过询问如何将街道地址分配给多边形的问题。您最终可能不得不手动将每个消防站的邮政编码和/或经纬度坐标输入到街道地址文件中。但这是一个猜测。也许有一种方法可以将街道地址分配给 GIS 图层。抱歉,我无法提供更好的建议。
  • 从 RJ 提供的链接中,我发现这个网站也许可以将邮政地址分配给没有经纬度坐标的 GIS 图层:geoservices.tamu.edu/Services/Geocode/Default.aspx
  • 按照我发布的链接,您可以使用FDNY Firehouse Locations 数据对地址进行地理编码。虽然它没有列出城市或州,但您可以限制位置以获得更准确的地理编码。见developers.google.com/maps/documentation/geocoding/index。之后,您可以对 NYC shapefile 进行多边形测试。
  • 感谢您提供的信息;我去看看。

标签: r csv ggplot2 ggmap


【解决方案1】:

我最近解决了一个类似的问题。下面是两段代码。第一个函数将地址转换为纬度/经度(如果您不能遵守 Google 的使用条款,请寻找数据科学工具包作为 geo-coding 的良好独立替代品。)第二个函数查看给定的纬度/经度对并确定哪个多边形(人口普查区)包含这些坐标。对于制作choropleth 地图非常有用。

library("RJSONIO") #Load Library
library("plyr")
library("RODBC")
library(maptools)

getGeoCode <- function(gcStr)
{ gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
  #Open Connection
  connectStr <- paste('http://http://maps.googleapis.com/maps/api/geocode/json?address=',gcStr, sep="") 
  con <- url(connectStr)
  data.json <- fromJSON(paste(readLines(con, warn = FALSE), collapse=""))
  close(con)
  #Flatten the received JSON
  data.json <- unlist(data.json)

  if (data.json["status"] == "OK" && data.json["results.geometry.location_type"] == "ROOFTOP") {
    address <- data.json["results.formatted_address"]
    lat <- data.json["results.geometry.location.lat"]
    lon <- data.json["results.geometry.location.lng"]
    gcodes <- data.frame("Address" = address, "Lon" = as.numeric(lon), "Lat" =     as.numeric(lat))
    return (gcodes)
  } else return ()
}

# Testing...
geoCodes <- getGeoCode("Palo Alto,California")
geoCodes
# "-122.1430195" "37.4418834" 



# Required for TractLookup
Washington <-readShapePoly("g:/USCensus/tl_2012_53_tract/tl_2012_53_tract")       
# US Census tract files (includes shape and data files)


tractLookup <- function(x) {
  # pt <- SpatialPoints(data.frame(x = -80.1, y = 26.3))
  pt <- SpatialPoints(data.frame(x = x$Lon, y = x$Lat))
  Mapping <- over(pt, Washington) # what index number does pt fall inside?
  Mapping <- data.frame(
  "GEOID" = as.character(Mapping$GEOID),
  "State" = as.character(Mapping$STATEFP) , 
  "County" = as.character(Mapping$COUNTYFP), 
  "Tract" = as.character(Mapping$TRACTCE), 
  "Tract_Name" = as.character(Mapping$NAME), 
  "INTPTLAT" = as.character(Mapping$INTPTLAT),
  "INTPTLON" = as.character(Mapping$INTPTLON),
  stringsAsFactors = FALSE)
  Mapping[is.na(Mapping)] <- "NULL"   
return(Mapping)
}

tractLookup(data.frame("Lon" = -122, "Lat" = 47.5))
# GEOID State County  Tract Tract_Name    INTPTLAT     INTPTLON
# 1 53033032102    53    033 032102     321.02 +47.4851507 -121.9657839

查看纽约消防部门形状文件,您应该能够更改映射语句,以在我的示例中查找并返回适当的字段来代替标准美国人口普查形状文件中的 GEOID 和区域信息。

【讨论】:

    【解决方案2】:

    试试这个方法。

    # Geocoding a csv column of "addresses" in R
    
    
    #load ggmap
    library(ggmap)
    
    
    # Select the file from the file chooser
    fileToLoad <- file.choose(new = TRUE)
    
    
    # Read in the CSV data and store it in a variable 
    origAddress <- read.csv(fileToLoad, stringsAsFactors = FALSE)
    
    
    # Initialize the data frame
    geocoded <- data.frame(stringsAsFactors = FALSE)
    
    
    # Loop through the addresses to get the latitude and longitude of each address and add it to the
    # origAddress data frame in new columns lat and lon
    for(i in 1:nrow(origAddress))
    
    {
    # Print("Working...")
    result <- geocode(origAddress$addresses[i], output = "latlona", source = "google")
    origAddress$lon[i] <- as.numeric(result[1])
    origAddress$lat[i] <- as.numeric(result[2])
    origAddress$geoAddress[i] <- as.character(result[3])
    }
    
    
    # Write a CSV file containing origAddress to the working directory
    write.csv(origAddress, "geocoded.csv", row.names=FALSE)
    

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多