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

Test and Publish PyPi PyPI - Version

  • 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-1.0.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

parse_wkb-1.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for parse_wkb-1.0.0.tar.gz
Algorithm Hash digest
SHA256 61995fbc5d6e49e08281e978754058690119ee2669ecd62dafe37b1d4e5fcca2
MD5 8b14166e4f4781094fd4b1efa969c988
BLAKE2b-256 5d8a97731d2666a9e7e4c94e7a0d4abdee3675c647cf11169c6644d55653f9fe

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: parse_wkb-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 421c8687dabd1156b2b288af274a9d574df685ee476d1cf8f4ba1fe0ac99a08c
MD5 8081a1454019a79ab50a08aaaf435a92
BLAKE2b-256 f61a0d742151996c454d99b2fa38ed5eeecf67b213b0ebaa1ae88a3f32214a9c

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