简体   繁体   中英

Mapping points and polygons

I am a R beginner handling for the first time both with R and spatial data. So I hope I make myself clear.

I have a shapefile of an italian region, divided into several census divisions. On the other hand I have got a csv file with a cases list and addresses for each case. I would like to map points and the shapefile and to get the count of how many points are in each census division.

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. 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? 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". 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. 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:

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). 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().

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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