Skip to content

ENH: add GeoSeries.z and to_wkt, to_wkb on GeoSeries and GeoDataFrame #162

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

Merged
merged 3 commits into from
Feb 12, 2022
Merged
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
11 changes: 11 additions & 0 deletions dask_geopandas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,16 @@ def geohash(self, as_string=True, precision=12):
def clip(self, mask, keep_geom_type=False):
return dask_geopandas.clip(self, mask=mask, keep_geom_type=keep_geom_type)

@derived_from(geopandas.GeoDataFrame)
def to_wkt(self, **kwargs):
meta = self._meta.to_wkt(**kwargs)
return self.map_partitions(M.to_wkt, **kwargs, meta=meta)

@derived_from(geopandas.GeoDataFrame)
def to_wkb(self, hex=False, **kwargs):
meta = self._meta.to_wkb(hex=hex, **kwargs)
return self.map_partitions(M.to_wkb, hex=hex, **kwargs, meta=meta)


class GeoSeries(_Frame, dd.core.Series):
"""Parallel GeoPandas GeoSeries
Expand Down Expand Up @@ -795,6 +805,7 @@ def func(data, x, y, z):
"geometry",
"x",
"y",
"z",
]:
GeoSeries._bind_property(name)

Expand Down
2 changes: 2 additions & 0 deletions doc/source/docs/reference/geodataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Serialization / IO / conversion
:toctree: api/

GeoDataFrame.to_parquet
GeoDataFrame.to_wkb
GeoDataFrame.to_wkt

Projection handling
-------------------
Expand Down
10 changes: 10 additions & 0 deletions doc/source/docs/reference/geoseries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ General methods and attributes
GeoSeries.interiors
GeoSeries.x
GeoSeries.y
GeoSeries.z

Unary predicates
----------------
Expand Down Expand Up @@ -107,6 +108,15 @@ Aggregating and exploding
GeoSeries.unary_union
GeoSeries.explode

Serialization / IO / conversion
-------------------------------

.. autosummary::
:toctree: api/

GeoSeries.to_wkb
GeoSeries.to_wkt

Projection handling
-------------------

Expand Down
54 changes: 54 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dask.dataframe.core import Scalar
import dask_geopandas

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

assert_geodataframe_equal(ddf.compute(), expected)


def test_to_wkt(geodf_points_crs):
df = geodf_points_crs
df["polygons"] = df.buffer(1)
ddf = dask_geopandas.from_geopandas(df, npartitions=4)
expected = df.to_wkt()
result = ddf.to_wkt().compute()

assert_frame_equal(expected, result)


def test_to_wkt_series(geoseries_points):
s = geoseries_points
dask_obj = dask_geopandas.from_geopandas(s, npartitions=4)
expected = s.to_wkt()
result = dask_obj.to_wkt().compute()

assert_series_equal(expected, result)


@pytest.mark.parametrize("hex", [True, False])
def test_to_wkb(geodf_points_crs, hex):
df = geodf_points_crs
df["polygons"] = df.buffer(1)
ddf = dask_geopandas.from_geopandas(df, npartitions=4)
expected = df.to_wkb(hex=hex)
result = ddf.to_wkb(hex=hex).compute()

assert_frame_equal(expected, result)


@pytest.mark.parametrize("hex", [True, False])
def test_to_wkb_series(geoseries_points, hex):
s = geoseries_points
dask_obj = dask_geopandas.from_geopandas(s, npartitions=4)
expected = s.to_wkb(hex=hex)
result = dask_obj.to_wkb(hex=hex).compute()

assert_series_equal(expected, result)


@pytest.mark.parametrize("coord", ["x", "y", "z"])
def test_get_coord(coord):
p1 = Point(1, 2, 3)
p2 = Point(2, 3, 4)
p3 = Point(3, 4, 5)
p4 = Point(4, 1, 7)
s = geopandas.GeoSeries([p1, p2, p3, p4])
dask_obj = dask_geopandas.from_geopandas(s, npartitions=2)
expected = getattr(s, coord)
result = getattr(dask_obj, coord).compute()
assert_series_equal(expected, result)