[R-sig-Geo] Intersect polygon with grid
Robert J. Hijmans
r.hijmans at gmail.com
Tue Dec 22 19:47:51 CET 2009
Here is another approximation, based on the concept that Paul
suggested (first disaggregating, then aggregating the results):
# get some data
pol = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)),"meuse")))
coordinates(meuse.grid) = c("x", "y")
gridded(meuse.grid) = TRUE
# create a RasterLayer (in your case from file)
r <- raster(meuse.grid)
rr <- disaggregate(raster(r), 10)
pr <- polygonsToRaster(pol, rr, -1, progress='text')
ra <- aggregate(pr, 10, sum, progress='text')
These should work on a raster of any size (and ample patience).
On Tue, Dec 22, 2009 at 4:02 AM, Paul Hiemstra <p.hiemstra at geo.uu.nl> wrote:
> A simple approach would be the following:
> 1. Create a point dataset that comprises of the center points of the grid
> 2. Overlay the pointset with the polygon set using the overlay() command
> 3. Convert the pointset into a grid using the gridded() command
> A problem could be that if your polygons are small in regard to the
> gridsize, only the center point of the gridcell determines the value of the
> gridcell. It does not look at how much area from several polygons is in a
> gridcell, and creating a weigted average by area. You could remedy this by
> making the pointset much denser, doing step 1-3 and then aggregating the
> dense grid into a coarser grid, calculating for example the mean. This can
> be done using the pointsToRaster() function from the raster package.
> Johannes Signer wrote:
>> Dear List,
>> I am trying to do the following:
>> I have a shapefile, that I want to overlay with a raster and then assign
>> each raster cell a value that represents the percentage covered by the
>> file. I have found a way to do that with small data sets, but I am
>> struggling with large datasets (1mio+ cells).
>> My approach so far is:
>> 1.) Create rectangular polygons (covering the whole area), that will be
>> for the grid afterwards.
>> 2.) Intersect the each polygon with the shapefile and calculate the area.
>> 3.) Create a grid from the rectangular polygons.
>> Any suggestions are very welcome!
>> Thanks in advance
>> [[alternative HTML version deleted]]
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
> Drs. Paul Hiemstra
> Department of Physical Geography
> Faculty of Geosciences
> University of Utrecht
> Heidelberglaan 2
> P.O. Box 80.115
> 3508 TC Utrecht
> Phone: +3130 274 3113 Mon-Tue
> Phone: +3130 253 5773 Wed-Fri
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
More information about the R-sig-Geo