简体   繁体   English

映射点和多边形

[英]Mapping points and polygons

I am a R beginner handling for the first time both with R and spatial data. 我是R初学者,第一次使用R和空间数据进行处理。 So I hope I make myself clear. 所以我希望我能说清楚。

I have a shapefile of an italian region, divided into several census divisions. 我有一个意大利地区的shapefile,分为几个人口普查部门。 On the other hand I have got a csv file with a cases list and addresses for each case. 另一方面,我有一个csv文件,其中包含一个案件列表和每个案件的地址。 I would like to map points and the shapefile and to get the count of how many points are in each census division. 我想映射点和shapefile,并获取每个普查区中有多少点的计数。

Here what I have done since now: 这是我从现在开始所做的事情:

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

Now, I am able to plot cases.points and shapefile separately, but not to add them in the same plot. 现在,我能够分别绘制case.points和shapefile,但不能将它们添加到同一图中。 Along with that, as I said, I would like to count how many points lie in each polygon (ie census division of "region"). 就像我说的那样,我还要计算每个多边形中有多少个点(即“区域”的人口普查分区)。

I must admit I'm not very keen on geography. 我必须承认我不太热衷于地理。 I had the doubt that different coordinates and/or projection reference systems could be the problem, thus I've checked. 我怀疑是否可能存在不同的坐标和/或投影参考系,因此我进行了检查。

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"

Is it likely that the shapefile coordinates are in degrees while the cases coordinates are in decimal? shapefile坐标可能以度为单位,而格坐标是十进制? If so, how to convert them? 如果是这样,如何转换它们?

Thanks, Saro 谢谢,萨罗

You have two different projections - your "region" shapefile is projected in UTM zone 32 and you told R to use Web Mercator for "cases". 您有两种不同的投影方式-您的“区域” shapefile投影在UTM区域32中,并且您告诉R将Web Mercator用于“案例”。 However, if you just downloaded the cases data as lat/long from Google, then you don't want to tell R their projection is Web Mercator, because it isn't - it's unprojected WGS 84, so you'd want EPSG 4326. Web Mercator is what Google uses to display maps, but if you download lat/long, that's just unprojected coordinates. 但是,如果您只是从Google下载经/纬度的案例数据,则您不想告诉R他们的投影是Web Mercator,因为不是-它不是投影的WGS 84,所以您需要EPSG 4326。 Web Mercator是Google用于显示地图的工具,但是如果您下载经/纬度,那只是未投影的坐标。 To get your lat/long data read in correctly, use: 要正确读取经/纬度数据,请使用:

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

Then for the projection, you want spTransform - try: 然后对于投影,您需要spTransform-尝试:

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

Using inappropriate or non-matching projections can create a lot of problems, and they're not always noticeable right away. 使用不适当或不匹配的预测会产生很多问题,而且这些问题并不总是立即可见。

EDIT: For selecting points within a polygon, you want the over() function, also from the sp package (this is kind of a separate question). 编辑:对于在多边形内选择点,您还希望从sp包获得over()函数(这是一个单独的问题)。 Do read up on the basic functions of the sp package and how the sp classes work - below is basically copied from the help section for over(). 请仔细阅读sp软件包的基本功能以及sp类的工作原理-基本上是从over()的帮助部分复制以下内容。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM