Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a spatial extent (for example cbind(c(0,0),c(10,10)),I want to see whether there is a part of a SpatialPolygons falls in this extent? How can I do this? Thank you very much!

share|improve this question
How is your polygon represented? If it's in vector format, just clip the polygon to the extent and see whether the result is nonempty. Many spatial libraries implement clipping. One that is specialized to the task is at – whuber Sep 28 '12 at 22:59
I think clipping can do this, but if I have a lot of zones to process? – Seen Sep 28 '12 at 23:18
Is a "zone" a polygon or an extent? Many clipping functions will clip a collection of polygons en masse to a single rectangle: this is a fundamental operation for displaying polygons within a rectangular window, so there exist heavily optimized solutions. – whuber Sep 28 '12 at 23:36
I will consider it as an extent. So do you have some suggestions on methods? – Seen Sep 29 '12 at 2:14
up vote 1 down vote accepted

bbox(spatialpolygons) will give you the bounding box of a spatialpolygons object. You just need to test if any of the bounds are outside your extent with four greater-than or less-than tests. There's no need to do any clipping.

Never use gpclib - it has a restrictive license. When you do want to do polygon clipping and buffering and all those groovy GIS things, use rgeos.

share|improve this answer
Are you claiming that when the extent of a polygon intersects a given extent, then the polygon itself intersects that extent? That's what your first paragraph appears to say. – whuber Sep 29 '12 at 20:56
The OP asked if a polygon falls in (which I understand to mean 'is wholly contained in') an extent. For polygon A to be wholly contained in the rectangular axis-aligned box B it is sufficient to test each of the four bounds of A as being less than those of B. If any of the bounds are larger then part of the polygon must necessarily be outside of B. – Spacedman Sep 30 '12 at 7:11
Your understanding of "falls in" is an important qualification to include in your answer, so that others do not misinterpret it! – whuber Sep 30 '12 at 21:06
Agreed, and I see the ambiguity now. I would explicitly say "test if -any part- of a spatial polygons falls in an extent" to get the other possibility. An unqualified "falls in" to me means completely enclosed by. You can only "fall in" to a hole if all of you falls in :) – Spacedman Sep 30 '12 at 21:51
I think it makes sense, my concern is how to perform spatial query in R. – Seen Oct 4 '12 at 0:50

Expanding on spacedman's suggestion, here is how you can get the intersection of two extents (NULL if not intersecting). 'sp' is a Spatial* object

e <- extent(c(0,10,0,10))
intersect(e, extent(sp))

But it may indeed be, as whuber points out, that the polygon is in fact not inside extent e. If you need to know that, do

gIntersects(as(e, 'SpatialPolygons'), sp)

For large problems, it may be efficient to do

in <- intersect(e, extent(sp))
if (isTRUE(in)) { in <- gIntersects(as(e, 'SpatialPolygons'), sp) }
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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