Skip to content

Instantly share code, notes, and snippets.

@cpcloud
Last active November 15, 2023 16:20
Show Gist options
  • Save cpcloud/82d98fb1bd1a4919c6a4c41643ad3141 to your computer and use it in GitHub Desktop.
Save cpcloud/82d98fb1bd1a4919c6a4c41643ad3141 to your computer and use it in GitHub Desktop.
duckdb spatial wkb issue
In [53]: import duckdb
In [54]: con = duckdb.connect()
In [55]: con.load_extension("spatial")
In [56]: t = con.sql(
...: "select geom from st_read('./ci/ibis-testing-data/geojson/zones.geojson') limit 1"
...: )
In [57]: t
Out[57]:
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ geom │
│ geometry │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ POLYGON ((933100.9183527103 192536.08569720192, 933091.0114800561 192572.17526147654, 933088.5848363293 192604.9701013019, 933121.560402… │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
In [58]: value = t.arrow()[0][0].as_py()
In [59]: type(value)
Out[59]: bytes
In [60]: import shapely as shp
In [61]: shp.from_wkb(value)
---------------------------------------------------------------------------
GEOSException Traceback (most recent call last)
Cell In[61], line 1
----> 1 shp.from_wkb(value)
File /nix/store/phz49vhzv7nqzn3x9f1gbs53wwnvnf6j-python3-3.10.13-env/lib/python3.10/site-packages/shapely/io.py:320, in from_wkb(geometry, on_invalid, **kwargs)
316 # ensure the input has object dtype, to avoid numpy inferring it as a
317 # fixed-length string dtype (which removes trailing null bytes upon access
318 # of array elements)
319 geometry = np.asarray(geometry, dtype=object)
--> 320 return lib.from_wkb(geometry, invalid_handler, **kwargs)
GEOSException: ParseException: Unknown WKB type 452
In [62]: t = con.sql(
...: "select st_aswkb(geom) as geom from st_read('./ci/ibis-testing-data/geojson/zones.geojson') limit 1"
...: )
In [63]: value = t.arrow()[0][0].as_py()
In [64]: shp.from_wkb(value)
Out[64]: <POLYGON ((933100.918 192536.086, 933091.011 192572.175, 933088.585 192604.9...>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment