Skip to content

Commit

Permalink
Support for WKB as geometry type in read_vectorized
Browse files Browse the repository at this point in the history
  • Loading branch information
snorfalorpagus committed Mar 24, 2018
1 parent 35a4308 commit dc4ab3d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion fiona/_vectorized.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .ogrext cimport Session, _deleteOgrFeature
from .ogrext import FIELD_TYPES, FIELD_TYPES_MAP, OGRERR_NONE
from ._shim cimport *
from libc.stdlib cimport malloc, free
from fiona.rfc3339 import FionaDateType, FionaDateTimeType, FionaTimeType

import logging
Expand All @@ -12,7 +13,7 @@ cimport numpy as np

log = logging.getLogger(__name__)

def read_vectorized(collection):
def read_vectorized(collection, use_wkb=False):
cdef Session session
cdef void * cogr_feature
cdef void * cogr_geometry
Expand All @@ -35,6 +36,7 @@ def read_vectorized(collection):
cdef long long [:] arr_int
cdef double [:] arr_double
cdef char * wkt
cdef char * wkb

session = collection.session
encoding = session._fileencoding
Expand Down Expand Up @@ -161,6 +163,14 @@ def read_vectorized(collection):
cogr_geometry = OGR_F_GetGeometryRef(cogr_feature)
if cogr_geometry == NULL:
data_geometry[feature_index] = None
elif use_wkb:
length = OGR_G_WkbSize(cogr_geometry)
wkb = <char*>malloc(sizeof(char)*length)
result = OGR_G_ExportToWkb(cogr_geometry, 1, wkb)
if result != OGRERR_NONE:
raise ValueError("Failed to export geometry to WKB")
data_geometry[feature_index] = wkb[:length]
free(wkb)
else:
result = OGR_G_ExportToWkt(cogr_geometry, &wkt)
if result != OGRERR_NONE:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_vectorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ def test_datetime_fields(tmpdir):
assert features["properties"]["date"][0] == np.datetime64("2018-03-24")
assert features["properties"]["datetime"][0] == np.datetime64("2018-03-24T15:06:01")
assert str(features["properties"]["nulldt"][0]) == "NaT"

def test_wkb(path_coutwildrnp_shp):
with fiona.open(path_coutwildrnp_shp, "r") as collection:
features = read_vectorized(collection, use_wkb=True)

geometry = features["geometry"][0]
assert geometry[0:1] == b"\x01" # little endian
assert geometry[1:5] == b"\x03\x00\x00\x00" # polygon
assert geometry[5:9] == b"\x01\x00\x00\x00" # 1 ring
assert len(geometry) == 1325

0 comments on commit dc4ab3d

Please sign in to comment.