Skip to content

Commit 7679bf9

Browse files
ENH: Add from_wkt and from_wkb tools (#293)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 94b31ea commit 7679bf9

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

dask_geopandas/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
if backends.QUERY_PLANNING_ON:
66
from .expr import (
77
points_from_xy,
8+
from_wkt,
9+
from_wkb,
810
GeoDataFrame,
911
GeoSeries,
1012
from_geopandas,
@@ -13,6 +15,8 @@
1315
else:
1416
from .core import (
1517
points_from_xy,
18+
from_wkt,
19+
from_wkb,
1620
GeoDataFrame,
1721
GeoSeries,
1822
from_geopandas,
@@ -30,6 +34,8 @@
3034

3135
__all__ = [
3236
"points_from_xy",
37+
"from_wkt",
38+
"from_wkb",
3339
"GeoDataFrame",
3440
"GeoSeries",
3541
"from_geopandas",

dask_geopandas/core.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,56 @@ def func(data, x, y, z):
882882
)
883883

884884

885+
def from_wkt(wkt, crs=None):
886+
"""
887+
Convert dask.dataframe.Series of WKT objects to a GeoSeries.
888+
889+
Parameters
890+
----------
891+
wkt: dask Series
892+
A dask Series containing WKT objects.
893+
crs: value, optional
894+
Coordinate Reference System of the geometry objects. Can be anything
895+
accepted by
896+
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
897+
such as an authority string (eg "EPSG:4326") or a WKT string.
898+
899+
Returns
900+
-------
901+
GeoSeries
902+
"""
903+
904+
def func(data):
905+
return geopandas.GeoSeries.from_wkt(data, index=data.index, crs=crs)
906+
907+
return wkt.map_partitions(func, meta=geopandas.GeoSeries(), token="from_wkt")
908+
909+
910+
def from_wkb(wkb, crs=None):
911+
"""
912+
Convert dask.dataframe.Series of WKB objects to a GeoSeries.
913+
914+
Parameters
915+
----------
916+
wkb: dask Series
917+
A dask Series containing WKB objects.
918+
crs: value, optional
919+
Coordinate Reference System of the geometry objects. Can be anything
920+
accepted by
921+
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
922+
such as an authority string (eg "EPSG:4326") or a WKT string.
923+
924+
Returns
925+
-------
926+
GeoSeries
927+
"""
928+
929+
def func(data):
930+
return geopandas.GeoSeries.from_wkb(data, index=data.index, crs=crs)
931+
932+
return wkb.map_partitions(func, meta=geopandas.GeoSeries(), token="from_wkb")
933+
934+
885935
for name in [
886936
"area",
887937
"geom_type",

dask_geopandas/expr.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,56 @@ def func(data, x, y, z):
914914
)
915915

916916

917+
def from_wkt(wkt, crs=None):
918+
"""
919+
Convert dask.dataframe.Series of WKT objects to a GeoSeries.
920+
921+
Parameters
922+
----------
923+
wkt: dask Series
924+
A dask Series containing WKT objects.
925+
crs: value, optional
926+
Coordinate Reference System of the geometry objects. Can be anything
927+
accepted by
928+
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
929+
such as an authority string (eg "EPSG:4326") or a WKT string.
930+
931+
Returns
932+
-------
933+
GeoSeries
934+
"""
935+
936+
def func(data):
937+
return geopandas.GeoSeries.from_wkt(data, index=data.index, crs=crs)
938+
939+
return wkt.map_partitions(func, meta=geopandas.GeoSeries(), token="from_wkt")
940+
941+
942+
def from_wkb(wkb, crs=None):
943+
"""
944+
Convert dask.dataframe.Series of WKB objects to a GeoSeries.
945+
946+
Parameters
947+
----------
948+
wkb: dask Series
949+
A dask Series containing WKB objects.
950+
crs: value, optional
951+
Coordinate Reference System of the geometry objects. Can be anything
952+
accepted by
953+
:meth:`pyproj.CRS.from_user_input() <pyproj.crs.CRS.from_user_input>`,
954+
such as an authority string (eg "EPSG:4326") or a WKT string.
955+
956+
Returns
957+
-------
958+
GeoSeries
959+
"""
960+
961+
def func(data):
962+
return geopandas.GeoSeries.from_wkb(data, index=data.index, crs=crs)
963+
964+
return wkb.map_partitions(func, meta=geopandas.GeoSeries(), token="from_wkb")
965+
966+
917967
for name in [
918968
"area",
919969
"geom_type",

dask_geopandas/tests/test_core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ def test_points_from_xy_with_crs():
183183
assert_geoseries_equal(actual.compute(), expected)
184184

185185

186+
def test_from_wkt():
187+
wkt = [
188+
"POLYGON ((-64.8 32.3, -65.5 18.3, -80.3 25.2, -64.8 32.3))",
189+
"POLYGON ((-81.079102 35.496456, -81.166992 31.914868, -75.541992 31.914868, -75.629883 35.675147, -81.079102 35.496456))", # noqa E501
190+
]
191+
expected = geopandas.GeoSeries.from_wkt(wkt, crs="EPSG:4326")
192+
df = pd.DataFrame({"wkt": wkt})
193+
ddf = dd.from_pandas(df, npartitions=2)
194+
actual = dask_geopandas.from_wkt(ddf["wkt"], crs="EPSG:4326")
195+
assert isinstance(actual, dask_geopandas.GeoSeries)
196+
assert_geoseries_equal(actual.compute(), expected)
197+
198+
199+
def test_from_wkb():
200+
wkb = [
201+
"0103000000010000000400000033333333333350c0666666666626404000000000006050c0cdcccccccc4c324033333333331354c0333333333333394033333333333350c06666666666264040",
202+
"0103000000010000000500000016c3d501104554c095f3c5de8bbf414064ac36ffaf4a54c02c280cca34ea3f4064ac36ffafe252c02c280cca34ea3f409c53c90050e852c00b7f86376bd6414016c3d501104554c095f3c5de8bbf4140",
203+
]
204+
expected = geopandas.GeoSeries.from_wkb(wkb, crs="EPSG:4326")
205+
df = pd.DataFrame({"wkb": wkb})
206+
ddf = dd.from_pandas(df, npartitions=2)
207+
actual = dask_geopandas.from_wkb(ddf["wkb"], crs="EPSG:4326")
208+
assert isinstance(actual, dask_geopandas.GeoSeries)
209+
assert_geoseries_equal(actual.compute(), expected)
210+
211+
186212
def test_geodataframe_crs(geodf_points_crs):
187213
df = geodf_points_crs
188214
original = df.crs

doc/source/docs/reference/tools.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ Tools
99
sjoin
1010
clip
1111
points_from_xy
12+
from_wkt
13+
from_wkb

0 commit comments

Comments
 (0)