diff --git a/python/cuspatial/cuspatial/geometry/geoarrowbuffers.py b/python/cuspatial/cuspatial/geometry/geoarrowbuffers.py index 7336a3c09..0d2bdb6d5 100644 --- a/python/cuspatial/cuspatial/geometry/geoarrowbuffers.py +++ b/python/cuspatial/cuspatial/geometry/geoarrowbuffers.py @@ -603,19 +603,4 @@ def copy(self, deep=True): return result def __len__(self): - if len(self._mpolys) > 0: - mlength = ( - self._mpolys.values[ - np.arange( - 1, len(self._mpolys), 2, like=self._mpolys.values - ) - ] - - self._mpolys.values[ - np.arange( - 0, len(self._mpolys), 2, like=self._mpolys.values - ) - ] - ).sum() - (len(self._mpolys) // 2) - else: - mlength = 0 - return (len(self.polys) - 1) - int(mlength) + return len(self._mpolys) - 1 diff --git a/python/cuspatial/cuspatial/geometry/geocolumn.py b/python/cuspatial/cuspatial/geometry/geocolumn.py index d6d6d2094..26d707b42 100644 --- a/python/cuspatial/cuspatial/geometry/geocolumn.py +++ b/python/cuspatial/cuspatial/geometry/geocolumn.py @@ -4,6 +4,7 @@ from typing import TypeVar, Union import numpy as np +import pyarrow as pa from shapely.geometry import ( LineString, MultiLineString, @@ -18,6 +19,7 @@ from cuspatial.geometry.geoarrowbuffers import GeoArrowBuffers + T = TypeVar("T", bound="GeoColumn") @@ -233,7 +235,7 @@ def _getitem_int(self, index): "mp": MultiPointShapelySerializer, "l": LineStringShapelySerializer, "ml": MultiLineStringShapelySerializer, - "poly": PolygonShapelySerializer, + "poly": MultiPolygonShapelySerializer, "mpoly": MultiPolygonShapelySerializer, } return type_map[self._sr._meta.input_types[index]](self._sr, index) @@ -406,7 +408,6 @@ def to_shapely(self): ], ) - class MultiPolygonShapelySerializer(ShapelySerializer): def to_shapely(self): """ @@ -414,15 +415,26 @@ def to_shapely(self): subsequent interior rings of all polygons that around bound by the mpolygon specified by self._index. """ - item_type = self._source._meta.input_types[self._index] - index = 0 - for i in range(self._index): - if self._source._meta.input_types[i] == item_type: - index = index + 1 - poly_indices = slice( - self._source.polygons.mpolys[index * 2], - self._source.polygons.mpolys[index * 2 + 1], - ) + multipolygon_type = pa.list_( + pa.field("polygons", pa.list_( + pa.field("parts", pa.list_( + pa.field("rings", pa.list_( + pa.field("vertices", pa.list_( + pa.field("xy", pa.float64(), nullable=False), + 2), nullable=False)), nullable=False)))))) + coords_type = pa.list_( + pa.field("vertices", pa.list_( + pa.field("xy", pa.float64(), nullable=False), + 2), nullable=False)) + breakpoint() + x = pa.array([self._source.polygons.offsets, self._source.polygons.xy], coords_type) + mpolygons = pa.array(self._source.polygons.xy, multipolygon_type) + index = np.array(self._source._meta.input_types[ + 0:(self._index + 1)]).apply({lambda x: x in [ + "poly", "mpoly"]}).sum()[0] + start_index = self._source.polygons.mpolys[index - 1] + end_index = self._source.polygons.mpolys[index] + poly_indices = slice(start_index, end_index) polys = [] for i in range(poly_indices.start, poly_indices.stop): ring_start = self._source.polygons.polys[i] @@ -454,4 +466,9 @@ def to_shapely(self): ], ) ) - return MultiPolygon(polys) + print(poly_indices) + print(polys) + if (end_index - start_index) == 1: + return Polygon(polys[0]) + else: + return MultiPolygon(polys) diff --git a/python/cuspatial/cuspatial/io/geopandas_adapter.py b/python/cuspatial/cuspatial/io/geopandas_adapter.py index dc518bf44..7469a4c83 100644 --- a/python/cuspatial/cuspatial/io/geopandas_adapter.py +++ b/python/cuspatial/cuspatial/io/geopandas_adapter.py @@ -39,7 +39,7 @@ def _load_geometry_offsets(self, geoseries: gpGeoSeries) -> dict: "multipoints": [0], "lines": [0], "mlines": [], - "polygons": {"polygons": [0], "rings": [0], "mpolys": []}, + "polygons": {"polygons": [0], "rings": [0], "mpolys": [0]}, } for geometry in geoseries: if isinstance(geometry, Point): @@ -90,11 +90,10 @@ def _load_geometry_offsets(self, geoseries: gpGeoSeries) -> dict: num_rings = num_rings + 1 current = offsets["polygons"]["polygons"][-1] offsets["polygons"]["polygons"].append(num_rings + current) + offsets["polygons"]["mpolys"].append( + offsets["polygons"]["mpolys"][-1] + 1) elif isinstance(geometry, MultiPolygon): current = offsets["polygons"]["polygons"][-1] - offsets["polygons"]["mpolys"].append( - len(offsets["polygons"]["polygons"]) - 1 - ) for poly in geometry: current = offsets["polygons"]["polygons"][-1] num_rings = 1 @@ -110,8 +109,9 @@ def _load_geometry_offsets(self, geoseries: gpGeoSeries) -> dict: num_rings = num_rings + 1 offsets["polygons"]["polygons"].append(num_rings + current) offsets["polygons"]["mpolys"].append( - len(offsets["polygons"]["polygons"]) - 1 - ) + np.array( + offsets["polygons"]["mpolys"] + ).sum() + len(geometry) - 1) return offsets def _read_geometries(