Sunday 26 March 2017

How do I retain all attribute data when clipping two polygons in R?



I would first like to say that my question is very much similar to this answered one, but the proposed solutions don't seem to be working for me. I think that my question is slightly different, as I'm performing a clip/intersection rather than a difference. Onto it!



My input can be found here at the FEWSNET website, and an example country outline can be downloaded here at the GADM site. I've been using the Ethiopia ESRI shapefile.


What I'm trying to do is quite simple. I first clip the country shapefile to the FEWSNET shapefile. Once the clip is performed, I need to symbolize the ML1 variables from the latter shapefile. In QGIS, I perform the vector clip, then with the newly clipped shapefile, I go to Properties > Style, change it from single to categorized, then choose my symbology for the ML1 category.


The code that I have so far has also been quite simple:


library(rgdal)
library(rgeos)

#read in ML and Ethiopia shapefiles
ea.ml1 <- readOGR(dsn = "D:/Mapping-R/Ethiopia", layer = "EA201507_ML1")
eth <- readOGR(dsn = "D:/Mapping-R/Ethiopia", layer = "ETH_adm0")


#perform clip
eth.clip <- gIntersection(ea.ml1, eth, byid = TRUE)

The problem is that the data that I need to symbolize (ML1) exists under ea.ml1@data$ML1. When I perform the clip, it appears that ea.ml1@data is lost altogether. How do I retain that data with my clip so that I can properly symbolize it?



Answer



Use intersect:


library(raster)
eth.clip <- intersect(ea.ml1, eth)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...