Domanda

That seems easy, but I couldn't find it. How do I test if at least part of a shapefile is inside a rectangle in R? I know I could use gIntersects from rgeos package, but for this I would need a sp object instead of a simple rectangle. Since I'm doing it lots of times, it would be more time consuming. Thanks in advance!

È stato utile?

Soluzione

Here's your rectangle points, note the first and last point match up:

rect.1 <- data.frame(long=c(-117,-117,-114,-114,-117),lat=c(35,40,40,35,35))

Now create spatial polygons object. You only need five levels of nested parens:

rectS = SpatialPolygons(list(
                         Polygons(list(
                           Polygon(coords=rect.1)
                         ),ID=1)),
                         proj4string=proj4string(cali))

where cali is my SpatialPolygonsDataFrame of California, it has a lat-long projection and you have to set the rectangle to be the same.

Now you can:

gIntersects(rectS,cali)
## [1] TRUE

Repeat over rectangles.

Oh, and of course four points in lat-long coordinates don't make a "rectangle" in the sense we learned at school because the earth isn't flat...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top