Created
January 13, 2014 20:17
-
-
Save dakcarto/8407286 to your computer and use it in GitHub Desktop.
Test script for Processing LWGEOM Provider issue, http://hub.qgis.org/issues/9105
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
from ctypes import * | |
from ctypes.util import find_library | |
import binascii | |
class GBOX(Structure): | |
_fields_ = [ | |
("flags", c_ubyte), | |
("xmin", c_double), | |
("xmax", c_double), | |
("ymin", c_double), | |
("ymax", c_double), | |
("zmin", c_double), | |
("zmax", c_double), | |
("mmin", c_double), | |
("mmax", c_double) | |
] | |
class LWGEOM(Structure): | |
_fields_ = [ | |
("type", c_ubyte), | |
("flags", c_ubyte), | |
("bbox", POINTER(GBOX)), | |
("srid", c_uint), | |
("data", c_void_p), | |
] | |
def combine_bytes(*args): | |
""" | |
given the bytes of a multi byte number combine into one | |
pass them in least to most significant | |
""" | |
ans = 0 | |
for i, val in enumerate(args): | |
ans += (val << i*32) | |
return ans | |
lw = None | |
lw_path = find_library('lwgeom') | |
if lw_path: | |
lw = CDLL(lw_path) | |
print repr(lw) | |
else: | |
print 'Could not find lwgeom library' | |
sys.exit() | |
invalid_wkt = create_string_buffer( | |
'POLYGON((-1.19961978542487024 0.83367217371454649,' | |
'-1.29389587970096454 0.65017049021286311,' | |
'-0.94877803458311938 0.8723927124350852,' | |
'-0.87807096387604866 0.65353749357986657,' | |
'-1.21645480225988711 0.63838597842835121,' | |
'-1.19961978542487024 0.83367217371454649))' | |
) | |
invalid_wkb = create_string_buffer(binascii.a2b_hex( | |
'01030000000100000006000000b88c2084a431f3bffe0d364471adea3facea7b2accb3f4b' | |
'fae3b095832cee43f3053b6c0635ceebf74ad251fa4eaeb3f42462d472819ecbfae543476' | |
'c7e9e43f364b8c4f9976f3bf3064726ea86de43fb88c2084a431f3bffe0d364471adea3f' | |
)) | |
invalid_wkb_size = c_size_t(sizeof(invalid_wkb)) | |
invalid_wkb_p = cast(addressof(invalid_wkb), POINTER(c_ubyte)) | |
# print invalid_wkb_size | |
# print int(valid_wkb[0:1]) # == b'\x01' | |
# # | |
# sys.exit() | |
LW_PARSER_CHECK_NONE = c_char(chr(0)) | |
WKT_ISO = c_uint8(1) | |
WKB_HEX = c_uint8(2) | |
# LWGEOM *lwgeom_from_wkt(const char *wkt, | |
# const char check) | |
lw.lwgeom_from_wkt.argtypes = [c_char_p, c_char] | |
lw.lwgeom_from_wkt.restype = POINTER(LWGEOM) | |
# LWGEOM* lwgeom_from_wkb(const uint8_t *wkb, | |
# const size_t wkb_size, | |
# const char check) | |
lw.lwgeom_from_wkb.argtypes = [POINTER(c_ubyte), c_size_t, c_char] | |
lw.lwgeom_from_wkb.restype = POINTER(LWGEOM) | |
# int lwgeom_ndims(const LWGEOM *geom) | |
lw.lwgeom_ndims.argtypes = [POINTER(LWGEOM)] | |
lw.lwgeom_ndims.restype = c_int | |
# LWGEOM* lwgeom_make_valid(LWGEOM* lwgeom_in) | |
lw.lwgeom_make_valid.argtypes = [POINTER(LWGEOM)] | |
lw.lwgeom_make_valid.restype = POINTER(LWGEOM) | |
# char* lwgeom_to_wkt(const LWGEOM *geom, | |
# uint8_t variant, | |
# int precision, | |
# size_t *size_out) | |
lw.lwgeom_to_wkt.argtypes = [POINTER(LWGEOM), c_ubyte, c_int, POINTER(c_size_t)] | |
lw.lwgeom_to_wkt.restype = c_char_p | |
# char* lwgeom_to_hexwkb(const LWGEOM *geom, | |
# uint8_t variant, | |
# size_t *size_out) | |
lw.lwgeom_to_hexwkb.argtypes = [POINTER(LWGEOM), c_ubyte, POINTER(c_size_t)] | |
lw.lwgeom_to_hexwkb.restype = c_char_p | |
# void lwgeom_free(LWGEOM *lwgeom) | |
lw.lwgeom_free.argtypes = [POINTER(LWGEOM)] | |
lwgi_wkt, lwgi_wkb, lwgv_wkt, lwgv_wkb = None, None, None, None | |
try: | |
lwgi_wkt = lw.lwgeom_from_wkt(invalid_wkt, LW_PARSER_CHECK_NONE) | |
print 'invalid_wkt: ' + repr(invalid_wkt) | |
print 'lwgi_wkt: ' + repr(lwgi_wkt) | |
print 'lwgi_wkt dimensions: {0}'.format(lw.lwgeom_ndims(lwgi_wkt)) | |
lwgi_wkb = lw.lwgeom_from_wkb(invalid_wkb_p, invalid_wkb_size, | |
LW_PARSER_CHECK_NONE) | |
print 'invalid_wkb_p: ' + repr(invalid_wkb_p) | |
print 'lwgi_wkb: ' + repr(lwgi_wkb) | |
print 'lwgi_wkb dimensions: {0}'.format(lw.lwgeom_ndims(lwgi_wkb)) | |
lwgv_wkt = lw.lwgeom_make_valid(lwgi_wkt) | |
print 'lwgv_wkt: ' + repr(lwgv_wkt) | |
lwgv_wkb = lw.lwgeom_make_valid(lwgi_wkb) | |
print 'lwgv_wkb: ' + repr(lwgv_wkb) | |
lwgv_wkt_size_out = c_size_t() | |
lwgv_wkt_out = lw.lwgeom_to_wkt(lwgv_wkt, WKT_ISO, c_int(17), | |
byref(lwgv_wkt_size_out)) | |
print 'lwgv_wkt_out: ' + lwgv_wkt_out | |
lwgv_wkb_size_out = c_size_t() | |
lwgv_wkb_out = lw.lwgeom_to_hexwkb(lwgi_wkb, WKB_HEX, | |
byref(lwgv_wkb_size_out)) | |
print 'lwgv_wkb_out: ' + lwgv_wkb_out | |
finally: | |
for lwgeom_obj in [lwgi_wkt, lwgi_wkb, lwgv_wkt, lwgv_wkb]: | |
if lwgeom_obj is not None: | |
lw.lwgeom_free(lwgeom_obj) | |
del lwgeom_obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment