[R-sig-Geo] Clipping a Map

Roger Bivand Roger.Bivand at nhh.no
Fri Nov 22 22:23:00 CET 2013


On Fri, 22 Nov 2013, Ravi Vaid wrote:
>
> Dude, You are going way over my head. I think I lost you in the first 
> sentence. Since this is probably a common task that alot of people have 
> to do, can you provide an example of doing this. That would really help. 
> I have a map of western US (camapzip_temp) and I am clipping the 
> California portion of that. The CA (camap_base) portion is just a single 
> polygon. The US map has zip code polygons and data attributes (zip 
> codes) are attached to this map. I know your time is valuable but when 
> ever you have some free time can you proved the modifications that are 
> necessary to the code below. I would really appreciate it. This might be 
> a silly question but why does the gIntersection function not work in 
> this case. Keep in mind I am really new at this. My original attempt is 
> below,

These things do not happen without study, it isn't just software, and 
there are no one-size-fits-all solutions (to anything). If anyone tells 
you otherwise, they are misleading you. Not only do I not have time, but 
you will learn more by understanding what is actually going on.

What we know is in ch. 5 of Bivand et al. (2013) Applied Spatial Data 
Analysis with R, Springer; read that and its references (read ch. 2 as 
well), and things should be plainer. Simplify as much as possible, yes, 
but no more, as we know, and oversimplification is a prime source of 
confusion.

Much of what we know about GEOS (and GDAL) is derived from watching the 
underlying functions in operation, often in unusual settings, and some 
aspects become clear after many years - a recent change to rgdal corrected 
a non-obvious misunderstanding about hoe GDAL worked that has been around 
since you were likely in primary school. Looking at the data and how it is 
represented in conjunction with understanding the operations you are using 
is crucial.

Hope this informs, clarification requires maybe more,

Roger


>
> {
> cz_zip <- gIntersects(camapzip_temp,camap_base, byid=TRUE)
> out <- lapply( which(cz_zip) , function(x){ gIntersection( camapzip_temp[x,] , camap_base ) } )
> keep <- sapply(out, class)
> out <- out[keep == "SpatialPolygons"]
> czout <- SpatialPolygons( lapply( 1:length( out ) , function(i) { Pol <- slot(out[[i]], "polygons")[[1]];   slot(Pol, "ID") <- as.character(i)
>                                                                   Pol}))
> }
>
>
>
>
>
>
>
>> Date: Fri, 22 Nov 2013 19:59:53 +0100
>> From: Roger.Bivand at nhh.no
>> To: rvaid at hotmail.com
>> CC: berman.jesse at gmail.com; r-sig-geo at r-project.org
>> Subject: Re: [R-sig-Geo] Clipping a Map
>>
>> On Fri, 22 Nov 2013, Ravi Vaid wrote:
>>
>>>
>>> Jesse,
>>>
>>> The clipping of the larger map by a smaller region works perfectly, but the resulting map (the clipped version) has the data component of the SP object missing. I can't merge anything if the data component is not there. I have tried directly using the gintersection function to clip the map but that seems to fail (waited more than 2 hours for the program to run).
>>> When I run the command: slotNames(czout) I should get an object of name "data" along with the other 4 components below
>>> but I only get,
>>> I get: [1] "polygons"    "plotOrder"   "bbox"        "proj4string"So, somehow the "data" object is getting lost. I am fairly new to GIS, I just found the code for clipping on the sig-geo list and tried it and it worked, but the "data" object is missing. I know how to merge data but I just can't figure out how to recover the "data" component of the SP object.
>>
>> No, you get a SpatialPolygons object. Obviously, the intersection will be
>> some subset of your clipped input object, but the function cannot lknow or
>> guess what you want. Suppose that you had a variable in the data slot that
>> was a count of some phenomenon - we don't know what proportion of the
>> count to retain if only part of the geometry is returned. This is clearly
>> up to the user to decide. Use the row.names() of the input and output
>> objects to identify which are which - use the accessor method to retrieve
>> the string values
>>
>> row.names(camapzip_temp)
>> row.names(camap_base)
>>
>> and
>>
>> row.names(cz_zip)
>>
>> which are catenated from the input object row.names.
>>
>> Hope this clarifies,
>>
>> Roger
>>
>> PS. Never, ever, assign directly to the data slot - this almost always
>> creates havoc and confusion by sidestepping checking of row.name values.
>> It's a really great way of randomising your data by accident, but
>> otherwise has no good justification.
>>
>>>
>>> Ravi
>>>
>>>
>>>> Date: Fri, 22 Nov 2013 08:36:52 -0800
>>>> From: berman.jesse at gmail.com
>>>> To: r-sig-geo at r-project.org
>>>> Subject: Re: [R-sig-Geo] Clipping a Map
>>>>
>>>> Hi Ravi,
>>>>
>>>> After performing 'gIntersects' try merging this result to your dataframe,
>>>> then select the TRUE values?
>>>>
>>>> Jesse
>>>>
>>>>  cz_zip <- gIntersects(camapzip_temp,camap_base, byid=TRUE)
>>>> camap_base at data<-cbind(camap_base at data, czip)
>>>> camap2<-camap_base[(camap_base at data[,/???/] %in% c("TRUE)),] # Insert your
>>>> column number for czip in place of ???
>>>>
>>>> plot(camap2)
>>>> head(camap2 at data)
>>>>
>>>>
>>>>
>>>> -----
>>>> --------------------------------------------------------
>>>> Jesse D Berman, PhD
>>>> Yale University
>>>> School of Forestry and Environmental Studies
>>>> Post-Doc Fellow
>>>> --
>>>> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Clipping-a-Map-tp7585156p7585162.html
>>>> Sent from the R-sig-geo mailing list archive at Nabble.com.
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo at r-project.org
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>>
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>
>> --
>> Roger Bivand
>> Department of Economics, NHH Norwegian School of Economics,
>> Helleveien 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; fax +47 55 95 95 43
>> e-mail: Roger.Bivand at nhh.no
>>
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list