Skip to content

Commit 3057bfb

Browse files
authored
ENH: add GeoSeries.z and to_wkt, to_wkb on GeoSeries and GeoDataFrame (#162)
1 parent 805fafc commit 3057bfb

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

dask_geopandas/core.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,16 @@ def geohash(self, as_string=True, precision=12):
465465
def clip(self, mask, keep_geom_type=False):
466466
return dask_geopandas.clip(self, mask=mask, keep_geom_type=keep_geom_type)
467467

468+
@derived_from(geopandas.GeoDataFrame)
469+
def to_wkt(self, **kwargs):
470+
meta = self._meta.to_wkt(**kwargs)
471+
return self.map_partitions(M.to_wkt, **kwargs, meta=meta)
472+
473+
@derived_from(geopandas.GeoDataFrame)
474+
def to_wkb(self, hex=False, **kwargs):
475+
meta = self._meta.to_wkb(hex=hex, **kwargs)
476+
return self.map_partitions(M.to_wkb, hex=hex, **kwargs, meta=meta)
477+
468478

469479
class GeoSeries(_Frame, dd.core.Series):
470480
"""Parallel GeoPandas GeoSeries
@@ -795,6 +805,7 @@ def func(data, x, y, z):
795805
"geometry",
796806
"x",
797807
"y",
808+
"z",
798809
]:
799810
GeoSeries._bind_property(name)
800811

doc/source/docs/reference/geodataframe.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Serialization / IO / conversion
2020
:toctree: api/
2121

2222
GeoDataFrame.to_parquet
23+
GeoDataFrame.to_wkb
24+
GeoDataFrame.to_wkt
2325

2426
Projection handling
2527
-------------------

doc/source/docs/reference/geoseries.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ General methods and attributes
2828
GeoSeries.interiors
2929
GeoSeries.x
3030
GeoSeries.y
31+
GeoSeries.z
3132

3233
Unary predicates
3334
----------------
@@ -107,6 +108,15 @@ Aggregating and exploding
107108
GeoSeries.unary_union
108109
GeoSeries.explode
109110

111+
Serialization / IO / conversion
112+
-------------------------------
113+
114+
.. autosummary::
115+
:toctree: api/
116+
117+
GeoSeries.to_wkb
118+
GeoSeries.to_wkt
119+
110120
Projection handling
111121
-------------------
112122

tests/test_core.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from dask.dataframe.core import Scalar
1010
import dask_geopandas
1111

12+
from pandas.testing import assert_frame_equal, assert_series_equal
1213
from geopandas.testing import assert_geodataframe_equal, assert_geoseries_equal
1314
from dask_geopandas.hilbert_distance import _hilbert_distance
1415
from dask_geopandas.morton_distance import _morton_distance
@@ -751,3 +752,56 @@ def test_geohash(self, calculate_partitions, npartitions):
751752
assert ddf.spatial_partitions is None
752753

753754
assert_geodataframe_equal(ddf.compute(), expected)
755+
756+
757+
def test_to_wkt(geodf_points_crs):
758+
df = geodf_points_crs
759+
df["polygons"] = df.buffer(1)
760+
ddf = dask_geopandas.from_geopandas(df, npartitions=4)
761+
expected = df.to_wkt()
762+
result = ddf.to_wkt().compute()
763+
764+
assert_frame_equal(expected, result)
765+
766+
767+
def test_to_wkt_series(geoseries_points):
768+
s = geoseries_points
769+
dask_obj = dask_geopandas.from_geopandas(s, npartitions=4)
770+
expected = s.to_wkt()
771+
result = dask_obj.to_wkt().compute()
772+
773+
assert_series_equal(expected, result)
774+
775+
776+
@pytest.mark.parametrize("hex", [True, False])
777+
def test_to_wkb(geodf_points_crs, hex):
778+
df = geodf_points_crs
779+
df["polygons"] = df.buffer(1)
780+
ddf = dask_geopandas.from_geopandas(df, npartitions=4)
781+
expected = df.to_wkb(hex=hex)
782+
result = ddf.to_wkb(hex=hex).compute()
783+
784+
assert_frame_equal(expected, result)
785+
786+
787+
@pytest.mark.parametrize("hex", [True, False])
788+
def test_to_wkb_series(geoseries_points, hex):
789+
s = geoseries_points
790+
dask_obj = dask_geopandas.from_geopandas(s, npartitions=4)
791+
expected = s.to_wkb(hex=hex)
792+
result = dask_obj.to_wkb(hex=hex).compute()
793+
794+
assert_series_equal(expected, result)
795+
796+
797+
@pytest.mark.parametrize("coord", ["x", "y", "z"])
798+
def test_get_coord(coord):
799+
p1 = Point(1, 2, 3)
800+
p2 = Point(2, 3, 4)
801+
p3 = Point(3, 4, 5)
802+
p4 = Point(4, 1, 7)
803+
s = geopandas.GeoSeries([p1, p2, p3, p4])
804+
dask_obj = dask_geopandas.from_geopandas(s, npartitions=2)
805+
expected = getattr(s, coord)
806+
result = getattr(dask_obj, coord).compute()
807+
assert_series_equal(expected, result)

0 commit comments

Comments
 (0)