Skip to main content

Pure python library to parse WKB (Well Known Binary) GIS geospatial geometry format

Project description

Pure Python WKB (Well Known Binary) Converter

  • wkb_to_geojson() converts WKB geometry into GeoJSON
  • wkb_to_wkt() converts WKB geometry into WKT
  • geojson_to_wkb() converts GeoJSON into WKB
  • wkb_to_abstract() converts WKB into an abstract representation which closely resembles the binary format (for debugging purposes)
from parse_wkb import (
    wkb_to_abstract,
    wkb_to_geojson,
    wkb_to_wkt,
    geojson_to_wkb
)

import json

WKB = b"\x01\x04\x00\x00\x00\x04\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00$@\x00\x00\x00\x00\x00\x00D@\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00D@\x00\x00\x00\x00\x00\x00>@\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x004@\x00\x00\x00\x00\x00\x004@\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00>@\x00\x00\x00\x00\x00\x00$@"

parsed_to_geojson = wkb_to_geojson(WKB)
assert json.dumps(parsed_to_geojson) == '{"type": "MultiPoint", "coordinates": [[10.0, 40.0], [40.0, 30.0], [20.0, 20.0], [30.0, 10.0]]}'

parsed_to_wkt = wkb_to_wkt(WKB)
assert parsed_to_wkt == 'MULTIPOINT (10.0 40.0, 40.0 30.0, 20.0 20.0, 30.0 10.0)'

parsed_to_abstract = wkb_to_abstract(WKB)
assert parsed_to_abstract == (
	'LEnd',
	('MultiPoint', 'XY'),
	4,
	('LEnd', ('Point', 'XY'), 10.0, 40.0),
	('LEnd', ('Point', 'XY'), 40.0, 30.0),
	('LEnd', ('Point', 'XY'), 20.0, 20.0),
	('LEnd', ('Point', 'XY'), 30.0, 10.0)
)

# the reverse operation is only implemented from GeoJSON at the moment:
encoded_to_wkb_from_geojson = geojson_to_wkb(parsed_to_geojson)
assert encoded_to_wkb_from_geojson == WKB

Supported Geometry Types

Supports POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, and GEOMETRYCOLLECTION

Supports geometry with XY, XYZ, XYM, and XYZM dimensions

Specification

This module is based on v1.2.1 of the OpenGIS Implementation Standard for Geographic information - Simple feature access - Part 1: Common architecture (which can be found here https://www.ogc.org/standards/sfa).

Alternate Approaches

This library is somewhat redundant because:

  • MySQL, POSTGIS and SQLite (SpatiaLite extension) provide the following conversion functions:

    • ST_AsGeoJSON()
    • ST_GeometryFromGeoJSON()
    • ST_AsWKT()
    • ST_GeometryFromText()
  • if you are using geopandas+shapely these libraries provide conversion to and from WKB (implemented with fast C/C++ libraries)

However this library has a limited use-case; when python is working on your system, but you can't get the confounded binaries for geopandas or shapely to compile, or if you don't care for the size of numpy and everything else that comes with a few innocent pip/conda install commands.

From making this script I learned that WKB is actually not a great spec...

  • WKB stores a lot of redundant information by repeating the byte order and geometry type for every point.
  • Only 8 byte double precision floats are permitted... seems overkill for some applications. Would be better it we could specify precision, or even use integer types.

Planned Features?

  • WKT => WKB

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

parse_wkb-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

parse_wkb-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file parse_wkb-0.1.0.tar.gz.

File metadata

  • Download URL: parse_wkb-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.8

File hashes

Hashes for parse_wkb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ac4875d4c577611d564fa4181d4747ef7674f1b4fe9ff1811701c77726fdc304
MD5 369ece34d8a186aaa67f6fb220265533
BLAKE2b-256 b5f504ff69dbb281cd1ce71ba69bec3dce8113fc448940e72a8fb9625b5c728f

See more details on using hashes here.

Provenance

File details

Details for the file parse_wkb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: parse_wkb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.8

File hashes

Hashes for parse_wkb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80eb5bd67394f6944d170315f3315fb9c8a22ed40ca24e5af0f361684a3e12c5
MD5 887d9ea7fd38c59b166a557d1849dd66
BLAKE2b-256 1c6211a09baa84f1666f185b22d13a25ba15aacb36a82b611e98b6d5bc88b2db

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page