【问题标题】:Mapping points and polygons映射点和多边形
【发布时间】:2017-06-27 12:17:21
【问题描述】:

我是第一次处理 R 和空间数据的 R 初学者。所以我希望我说清楚。

我有一个意大利地区的 shapefile,分为几个人口普查部门。 另一方面,我有一个 csv 文件,其中包含每个案例的案例列表和地址。 我想绘制点和 shapefile 并计算每个人口普查部门中有多少点。

从现在开始我做了什么:

#get cases file
cases <- read.csv("cases.csv", sep =';', header = TRUE)
names(cases)
[1] "name"    "address"

#geocode addresses from Google Maps
library(GISTools)
library(rgeos)
library(ggmap)
geolocalize <- geocode(as.character(cases$address))

# bind latitude and longitude to the previous cases data frame
cases <- data.frame(cases, geolocalize)
names(cases)
[1] "name"    "address" "lon"     "lat"

#make cases a SpatialPointDataFrame
#since addresses were retrieved using GoogleMaps, I set proj4string as follows
cases.points <- SpatialPointsDataFrame(cases[,3:4], cases, proj4string = CRS("+init=EPSG:3857"))

#get the shapefile
region <- readOGR("R02_11_WGS84.shp")

现在,我可以分别绘制 case.points 和 shapefile,但不能将它们添加到同一个图中。除此之外,正如我所说,我想计算每个多边形中有多少点(即“区域”的人口普查划分)。

我必须承认我对地理不是很感兴趣。我怀疑不同的坐标和/或投影参考系统可能是问题,因此我检查了。

head(coordinates(region))
[,1]    [,2]
0 364509.0 5065900
1 363916.3 5056629
2 372585.0 5068078
3 360692.3 5048321
4 356029.7 5062399
5 360012.1 5065663


coordinates(cases)
lon      lat
[1,] 7.323667 45.73664

proj4string(region)
[1] "+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"

proj4string(cases.points)
[1] "+init=EPSG:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"

形状文件坐标是否可能以度为单位,而案例坐标为十进制?如果是,如何转换?

谢谢, 萨罗

【问题讨论】:

  • shapefile 中列的名称是什么?还是他们没有名字?
  • 暂时不能试,所以不回答,看sp::spTransform吧。

标签: r geospatial spatial


【解决方案1】:

您有两个不同的投影 - 您的“区域”shapefile 投影在 UTM 区域 32 中,并且您告诉 R 使用 Web Mercator 来处理“案例”。但是,如果您只是从 Google 下载了 lat/long 的案例数据,那么您不想告诉 R 他们的投影是 Web Mercator,因为它不是 - 它是未投影的 WGS 84,所以您需要 EPSG 4326。 Web Mercator 是 Google 用来显示地图的工具,但如果您下载纬度/经度,那只是未投影的坐标。要正确读取您的纬度/经度数据,请使用:

library(sp)
cases.points <- SpatialPointsDataFrame(cases[,3:4], cases, 
                                   proj4string = CRS("+init=EPSG:4326"))

那么对于投影,你想要 spTransform - 试试:

cases.points.utm32 <- spTransform(cases.points, CRS(proj4string(region)))

使用不适当或不匹配的投影会产生很多问题,而且它们并不总是能立即引起注意。

编辑: 要在多边形内选择点,您需要 over() 函数,也来自 sp 包(这是一个单独的问题)。请阅读 sp 包的基本功能以及 sp 类的工作原理 - 下面基本上是从 over() 的帮助部分复制而来的。

region$pointCount <- sapply(over(region, geometry(cases.points), 
                             returnList = TRUE), length)

【讨论】:

  • 太棒了!如果它回答了您的问题,请考虑“接受”答案。
猜你喜欢
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
相关资源
最近更新 更多