4

This question already has an answer here:

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?

marked as duplicate by Mapperz Dec 9 '15 at 22:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7

Use intersect:

library(raster)
eth.clip <- intersect(ea.ml1, eth)
  • Great, easy enough! What is the functional difference between rgeos::gIntersection and raster::intersect that makes this work? – Lauren Dec 9 '15 at 22:13
  • 1
    raster::intersect uses rgeos::gIntersection but it also takes care of the attributes, which if you use rgeos which only works on the geometries. The raster package has a set of related functions. See ?'raster-package' section XIV – Robert Hijmans Dec 9 '15 at 22:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.