¿ add PyGEOS in geojson-pydantic ?
Closed this issue · 6 comments
It might be a nice feature to have pygeos as an optional dependencies which could then enable additional methods in geometries (https://pygeos.readthedocs.io/en/stable/measurement.html).
try:
import pygeos
except ImportError: # pragma: nocover
pygeos = None # type: ignore
class Polygon(_GeometryBase):
"""Polygon Model"""
type: str = Field("Polygon", const=True)
coordinates: PolygonCoords
@property
def as_pygeos_geometry(self):
assert pygeos is not None, "pygeos must be installed to use this method"
return pygeos.from_geojson(self.json())
@property
def area(self):
assert pygeos is not None, "pygeos must be installed to use this method"
return pygeos.area(self.as_pygeos_geometry)
IMO the benefit of pygeos is really when you have arrays of geometries, so that it can take advantage of vectorization. If you just have a single polygon, I think it would make sense to return a single shapely geometry. If you had a PolygonCollection
class, then that could make sense to cast to GEOS geometries using Pygeos.
IMO pygeos seems like a smaller requirement than shapely.
Note for the example I took Polygon, but we could use it also for feature collection 🤷♂️
IMO pygeos seems like a smaller requirement than shapely.
I just assumed they were about the same size because they both wrap GEOS.
Note for the example I took Polygon, but we could use it also for feature collection 🤷♂️
I didn't think pygeos supported creation of mixed-type geometries? The geometrycollections
function seems to take pygeos geometries as input? So if you had a FeatureCollection
you'd need to loop over each Feature
, creating a GEOS item one at a time, which then wouldn't be any faster than shapely?
This is cool but I personally think it's out of scope of what this library should do, which is simply provide data models / validation / schemas for geojson spec. I hesitate to add a GEOS dependency even if its an extra.
I tend to agree. And it's one line to convert to a shapely geometry: from shapely.geometry import shape; shape(item.dict())
it's out of scope of what this library should do, which is simply provide data models / validation / schemas for geojson spec.
💯
alright it doesn't seem a great idea.
closing for now, thanks for the feedback 🙏