Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

gis - From a shapefile with polygons/areas, and points (lat,lon), figure out which polygon/area each point belongs to? In R

I am trying to identify which polygon (ZCTA... aka Zip Code analogue) a given point belongs in, given a set of points and a shapefile. While there are several questions of this type out there, nearly all seem to refer me toward QGIS. While I'll go and learn another tool if needed, is there a simple way to do this in R? I'm experienced in the R environment... not so much in the GIS space.

The shapefile I am using is located here: ftp://ftp.gisdata.mn.gov/pub/gdrs/data/pub/us_mn_state_mngeo/bdry_zip_code_tabulation_areas/shp_bdry_zip_code_tabulation_areas.zip

My first attempt was to load the shapefile as a SpatialPolygonsDataFrame, the points as a SpatialPointsDataFrame, then use "over()" to get the indicies of the polygons that match:

library(maptools)
library(maps)
library(sp)

mn.zip.map <- readShapePoly("zip_code_tabulation_areas.shp")
# The shapefile is the one referenced in the link above

latlon <- data.frame(matrix(0,nrow=2,ncol=1))
latlon$lat <- c(44.730178, 44.784711)
latlon$lon <- c(-93.235381, -93.476415)
latlon[1] <- NULL
coordinates(latlon) = ~lon+lat
indices <- over(latlon, mn.zip.map)

With results:

> indices
ZCTA5CE10 GEOID10 CLASSFP10 MTFCC10 FUNCSTAT10 ALAND10 AWATER10 INTPTLAT10 INTPTLON10
1      <NA>    <NA>      <NA>    <NA>       <NA>      NA       NA       <NA>       <NA>
2      <NA>    <NA>      <NA>    <NA>       <NA>      NA       NA       <NA>       <NA>
      Shape_Leng Shape_Area
1         NA         NA
2         NA         NA

I was hoping to have the first line output ZCTA5CE10 == 55124 and the second line output ZCTA5CE10 == 55379. However, clearly this isn't happening.

It seems like the coordinate systems are not aligned... but they should both be Lat / Lon, right?

What am I missing here? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you have to set and adjust the projection:

library(rgdal)
proj4string(mn.zip.map) <- CRS("+proj=utm +zone=15 +datum=NAD83")
mn.zip.map <- spTransform(mn.zip.map, CRS("+proj=longlat"))
proj4string(latlon) <- CRS(proj4string(mn.zip.map))
over(latlon, mn.zip.map)
#   ZCTA5CE10 GEOID10 CLASSFP10 MTFCC10 FUNCSTAT10   ALAND10 AWATER10  INTPTLAT10   INTPTLON10 Shape_Leng Shape_Area
# 1     55124   55124        B5   G6350          S  43572536  1759018 +44.7394617 -093.1938424   27059.59   45295591
# 2     55379   55379        B5   G6350          S 152635134  6181840 +44.7539755 -093.5146083   86609.93  158696544

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...