[R-sig-Geo] Polygon Data into Grid Structure

Michael Sumner md@umner @end|ng |rom gm@||@com
Tue May 3 11:48:48 CEST 2022


On Tue, May 3, 2022 at 2:22 AM <lucfrank using mail.uni-mannheim.de> wrote:

> Hello everyone,
>
> I have the Aqueduct 3.0 Data from the WRI Institut which is in
> Multipolygon format.
>
> To perform a regression analysis with different datasets I want to
> transform these polygons into a grid.
> Basically, I want to overlay a grid over the map and assign each cell
> the value of the underlying polygon.
>

try

library(terra)
p <- vect(<yourpolyfile>)
r <- rasterize(p, rast(<your-raster-file>), field = seq_along(p))

In a second step, I want to count how many points (different dataset)
> are inside each cell.
>
>
I'd use terra, and terra::cells(grid, vect(points))

where 'grid' is from

rast(<your-raster-file>)

and points is cbind(x, y) just your points. If grid and points are in
different projections you'll need to declare that for the points in vect( ,
crs  = ) unless you read it from a source with that info.

You get the cell id of the raster, which you can store alongside the x-y
values and summarize from there.

Best, Mike


>
> I have tried with the sp commands GridTopology, and SpatialPoints. I
> can produce the grid but fail to crop with my map.
> Also, it is not clear to me how I can assign the underlying value of
> the polygon.
>
> This is the code I have tried so far:
> (I have copied this from doing a Course on DataCamp which I can't
> completely copy exactly because my data is different)
>
> # -25,-49  my lower left corner (these are coordinates)
> grid <- GridTopology(c(-25,-49),c(1, 1), c(72, 48))
>
>
> gridpoints <- SpatialPoints(grid, proj4string = CRS(projection(aqua)))
> plot(gridpoints)
>
> cropped_gridpoints <- crop(gridpoints, aqua)
> plot(cropped_gridpoints)
>
> spgrid <- SpatialPixels(cropped_gridpoints)
> plot(spgrid)
>
>
> I hope someone has an idea to help me solve this.
> Thanks to everyone who has any insights into this
>
> Luca Frank
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>


-- 
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner using gmail.com

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list