Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GeoArrow format for MultiLine and MultiPolygon root object offsets. #581

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions python/cuspatial/cuspatial/geometry/geoarrowbuffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 29 additions & 12 deletions python/cuspatial/cuspatial/geometry/geocolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TypeVar, Union

import numpy as np
import pyarrow as pa
from shapely.geometry import (
LineString,
MultiLineString,
Expand All @@ -18,6 +19,7 @@

from cuspatial.geometry.geoarrowbuffers import GeoArrowBuffers


T = TypeVar("T", bound="GeoColumn")


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -406,23 +408,33 @@ def to_shapely(self):
],
)


class MultiPolygonShapelySerializer(ShapelySerializer):
def to_shapely(self):
"""
Iteratively construct Polygons from exterior (first) rings and
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]
Expand Down Expand Up @@ -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)
12 changes: 6 additions & 6 deletions python/cuspatial/cuspatial/io/geopandas_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down