2013-08-27 60 views
2

编辑;回答如下。如何批处理从csv文件到R的地址解析街道地址?

使用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和形状文件纽约市的人口普查地图。接下来,我想用一民宅发生火灾的街道地址使用CSV文件,我下载在这里打下了地图的顶部创建一个数据帧:

FDNY Firehouse Locations

形状文件人口普查是定位在这里(这是2010年版黑色):

NYC Shape File

我的问题是,数据不单列市和状态,我不知道怎么写,可以抓住这些地址的功能并使用Google地理编码使用类似ggmap

任何建议或在正确的方向微调将不胜感激。我是R和stackoverflow的新手,对我来说很简单。

编辑:有没有人把这个标记为已经问过A)看看我的实际数据还是B)意识到你认为我重复的问题是3岁? R在过去3年里没有发生过新的事情吗?世界是平坦的,沿着人们移动。/rant

我可以使用ggmap和geocode()函数来获取lat和lon而不用创建一个函数来完成它。

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

的问题,又是如何让R读取我的CSV文件,该文件是缺少城市和州的数据,以便它可以创建200多个纬度和经度测量我需要没有我不必进行地址解析1个地址。

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

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

+0

消防站位置数据包含一个街道地址和市镇。 shapefile给消防员号码,营和师。我看到了一些问题,询问如何将数据分配给一个邮政编码,但从来没有问过如何将街道地址分配给一个多边形。您可能最终不得不手动输入每个消防站的邮政编码和/或经纬度坐标到街道地址文件。但这是一个猜测。也许有一种方法可以为GIS层分配一个街道地址。对不起,我不能给出更好的建议。 –

+0

从下面的链接RJ提供的,我发现这个网站,也许可以分配邮政地址到一个GIS层没有Lat-Long坐标:https://geoservices.tamu.edu/Services/Geocode/Default.aspx –

+0

继我发布的链接,您可以使用“FDNY Firehouse Locations”数据对地址进行地理编码。虽然它没有列出城市或州,但您可以限制位置以获得更准确的地理编码。请参阅https://developers.google.com/maps/documentation/geocoding/index。接下来,你可以在NYC shapefile的多边形测试中做一个点。 – JackeJR

回答

2

我最近解决了类似的问题。以下是两段代码。第一个函数将地址转换为纬度/经度(如果您无法遵守Google的使用条款,请将Data Science Toolkit作为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 
#2  321.02 +47.4851507 -121.9657839 

纵观纽约消防部门形状文件,你应该能够改变映射语句来寻找和到位的,从标准的美国人口普查形状文件中的大地水准面和道信息返回相应的字段我的例子。

0

试试这个方法。

# 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) 

enter image description here