简体   繁体   English

从具有多边形/区域和点(纬度,经度)的shapefile中,找出每个点属于哪个多边形/区域? 在R中

[英]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. 我正在尝试在给定一组点和一个shapefile的情况下,确定给定点属于哪个多边形(ZCTA ... aka邮政编码类似物)。 While there are several questions of this type out there, nearly all seem to refer me toward QGIS. 尽管这里有几个此类问题,但几乎所有问题似乎都将我引向了QGIS。 While I'll go and learn another tool if needed, is there a simple way to do this in R? 虽然我将根据需要去学习另一个工具,但是在R中有没有简单的方法可以做到这一点? I'm experienced in the R environment... not so much in the GIS space. 我在R环境中很有经验,而在GIS领域则没有那么多。

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 我正在使用的shapefile位于此处: 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: 我的第一次尝试是将shapefile加载为SpatialPolygonsDataFrame,将点加载为SpatialPointsDataFrame,然后使用“ over()”获取匹配的多边形的标记:

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. 我希望第一行输出ZCTA5CE10 == 55124,第二行输出ZCTA5CE10 ==55379。但是,显然这没有发生。

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. 提前致谢。

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

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

相关问题 在R中使用shapefile过滤纬度点 - Filter lat-lon points using shapefile in R 对于R中的多个多边形,从Lat / Lon矩阵计算面积或在50%轮廓内计算面积 - Calculate area from Lat/Lon matrix or within a 50% contour for multiple polygons in R 当我在 shapefile 多边形上的 Points(Lat,Lon) 上运行 Over() 函数时继续获得 NA - Keep getting NAs when I run Over() function on Points(Lat,Lon) on shapefile polygons R:将多边形从Shapefile 1匹配到shapefile 2中的区号 - R: Match Polygons from Shapefile 1 to Area Codes in shapefile 2 R中的lat长点簇的多边形 - polygon from cluster of lat long points in R R:在shapefile中的多边形上绘制.gdb文件中的点 - R: Plotting points from a .gdb file over polygons in a shapefile 使用R从澳大利亚的lat / lon点网站提取高程 - Extracting elevation from website for lat/lon points in Australia, using R 在距R中已知中心点的半径内找到纬度/经度 - Find lat/lon within a radius from a known centered point in R 从 R 中的 netCDF 中提取点 (lon, lat) 的时间序列 - Extract time series of a point ( lon, lat) from netCDF in R 在LON / LAT中使用Leaflet&gt; NULL值读取readOGR点shapefile和图 - readOGR point shapefile and plot using Leaflet > NULL values in LON/LAT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM