paleolimbot / geos

Open Source Geometry Engine ('GEOS') R API
https://paleolimbot.github.io/geos/
Other
61 stars 8 forks source link

Easy conversion of SpatVector points to Geos geometry #92

Open effnstfn opened 1 year ago

effnstfn commented 1 year ago

Hi, I work with a lot of raster data and was wondering if there was an easy way to convert objects from Terra into a Geos Geometry. I'm currently converting from Terra > sf > geos which is quite memory and computationally inefficient. Was there an easy way to convert it?

mdsumner commented 1 year ago

you could template the set of rct() coordinates pretty easily if every pixel-as-a-polygon is what you want, or do you mean polygonize with seas of values defining region polygons?

I can put together examples , but note that pixel as polygon is terribly redundant and heavy on storage - essentially every pixel value adding 10 more numeric values when you only needed six numbers to describe the scheme (extent and dimension)

effnstfn commented 1 year ago

I have the raster converted to points using terra::rasterToPoints() and would like to do some calculation on these points using geos

mdsumner commented 1 year ago

oh points, well that should be much easier sorry, use xy() - definitely do not go via sf, for POINT it puts everything in a list and has always been slow

happy to pursue example , and also I could contribute a fast raster-spec to geos workflow but I think xy() will be enough, I'd use xyFromCell or geom() and not even go through SpatVector

effnstfn commented 1 year ago

I see, I wasn't aware of using the wk::xy() function for geos geometries. Could you please provide an example?
once my object is in wk, do i need to convert it to geos?

mdsumner commented 1 year ago

the wk types and geos and sfc are interchangeable in the wk scheme👌

I'm sorry not to provide examples but I can in time, I think it's worth describing tho because I think it's easier than might be for you

mdsumner commented 1 year ago

sorry 'geom()' is for vector I forgot. Here's how to turn a raster centre coordinates to geos, I hope it's relevant:

library(terra)
r <- rast(volcano)

library(wk)

cell <- seq_len(ncell(r))
geos::as_geos_geometry(xy(xFromCell(r, cell), yFromCell(r, cell)))
paleolimbot commented 12 months ago

I think @fanpageste is referring to the vect()/geom() structure in terra? Maybe:

x <- terra::vect(c("POINT (0 1)", "POINT (2 3)"))
df <- as.data.frame(terra::geom(x))
geos::geos_make_point(df$x, df$y)
#> <geos_geometry[2]>
#> [1] <POINT (0 1)> <POINT (2 3)>

Created on 2023-10-16 with reprex v2.0.2

mdsumner commented 12 months ago

there was a mix in the convo above, in the end I would do this

library(terra)
r <- rast(volcano)

library(wk)

cell <- seq_len(ncell(r))
geos::geos_make_point(xFromCell(r, cell), yFromCell(r, cell))