I see libraries such as shape from shapely.geometry that support conversion to WKB and WKT binaries. What I am unable to find is guidance on converting to EWKB specifically.

I suspect this is because libraries that support WKB conversion also support EWKB? How do I know if I am creating an EWKB vs WKB? Are all WKB also EWKB (but not the other way around)?

If EWKB is an extension of WKB, how would I check that, say, Shapely, supports EWKB format?

I am trying to understand if EWKB is standard or something that was just built for PostGIS (where I have seen it used) and how I can make sure that data I am producing for a function that expects EWKB can be correctly formatted.

Because of @shongololo's comment, I wanted to add a snippet of what I am going presently to generate the WKB of the shape from GeoJSON.

from shapely.geometry import shape
import binascii
# convert geom to wkb format
geom_str = json.dumps(route['geometry'])
geom_geo = geojson.loads(geom_str)
geom_shp = shape(geom_geo)
geom_wkb = geom_shp.to_wkb()
geom_cln = binascii.hexlify(geom_wkb).decode('ascii')
  • With shapely use the wkb module and set the hex option to true, e.g. wkb.loads(geom, hex=true) – songololo Jan 20 '17 at 8:25
  • Thanks @shongololo - Any chance you could elaborate on what including the hex boolean as a parameter is doing? Looks like you are using a library wkb? Is that from Shapely? – kuanb Jan 20 '17 at 17:13

Shapely is based on GEOS, which natively reads/writes EWKB. The Z-dimension support of EWKB (different from ISO) is well supported by GEOS/Shapely, but getting/setting SRIDs is a hidden feature. Here's a quick demo:

from shapely import geos, wkb, wkt

# Show EWKB
p = wkt.loads('POINT Z(1 2 3)')
print(p.wkb_hex)  # This is little endian EWKB with just PointZ bit set
# 0101000080000000000000F03F00000000000000400000000000000840

# Change a the default mode to add this, if SRID is set
geos.WKBWriter.defaults['include_srid'] = True

# Get/Set SRID
print(geos.lgeos.GEOSGetSRID(p._geom))  # 0, or unset
# Set it to EPSG:4326
geos.lgeos.GEOSSetSRID(p._geom, 4326)
print(p.wkb_hex)  # This is little endian EWKB with SRID and PointZ bit set
# 01010000A0E6100000000000000000F03F00000000000000400000000000000840

# And for fun, change the output to big endian
geos.WKBWriter.defaults['big_endian'] = True
# 00A0000001000010E63FF000000000000040000000000000004008000000000000

Note that if SRID is not used and the geometries are simple 2D, then ISO WKB and EWKB are the same.

| improve this answer | |
  • 1
    Thanks, this is a terrific answer. I hope it wasn't "bad form" to switch this to the best answer, but it most directly addresses the question and provides examples that are immediately applicable. – kuanb Feb 3 '17 at 21:02

To respond to your question

Any chance you could elaborate on what including the hex boolean as a parameter is doing?

It is returning a hex encoded string. The module he referred to (wkb) is a shapely module.

Here is the documentation page for shapely. You can search the page (ctrl+f) for shapely.wkb.loads.

For posterity, the question quoted above was in response to an answer by shongololo.

With shapely use the wkb module and set the hex option to true, e.g. wkb.loads(geom, hex=true) – shongololo

| improve this answer | |
  • Thanks. And when hex is true, the product is a EWKB file? Does that mean that all EWKB is is WKB hex encoded? I had a "hunch" that SRID inclusion was also a component of EWKB and is not included in WKB by default. Any chance this is true or what I am saying even makes sense in that regard? – kuanb Jan 20 '17 at 18:18
  • 1
    I believe that hex-encoded WKB is EWKB. I think you are also correct in thinking that EWKB has an SRID. Here is a link to another question. The first answer explains a lot about EWKB. I would also suggest looking at the linked information from the answer. – Dylan Hamilton Jan 20 '17 at 18:34
  • No, hex-encoded WKB is just a non-binary representation of WKB. EWKB can include a SRID. postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT – inc42 Jan 3 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.