| 1 |
import threading |
| 2 |
from ctypes import POINTER, Structure, byref, c_byte, c_char_p, c_int, c_size_t |
| 3 |
|
| 4 |
from django.contrib.gis.geos.base import GEOSBase |
| 5 |
from django.contrib.gis.geos.libgeos import ( |
| 6 |
GEOM_PTR, |
| 7 |
GEOSFuncFactory, |
| 8 |
geos_version_tuple, |
| 9 |
) |
| 10 |
from django.contrib.gis.geos.prototypes.errcheck import ( |
| 11 |
check_geom, |
| 12 |
check_sized_string, |
| 13 |
check_string, |
| 14 |
) |
| 15 |
from django.contrib.gis.geos.prototypes.geom import c_uchar_p, geos_char_p |
| 16 |
from django.utils.encoding import force_bytes |
| 17 |
from django.utils.functional import SimpleLazyObject |
| 18 |
|
| 19 |
|
| 20 |
# ### The WKB/WKT Reader/Writer structures and pointers ### |
| 21 |
class WKTReader_st(Structure): |
| 22 |
pass |
| 23 |
|
| 24 |
|
| 25 |
class WKTWriter_st(Structure): |
| 26 |
pass |
| 27 |
|
| 28 |
|
| 29 |
class WKBReader_st(Structure): |
| 30 |
pass |
| 31 |
|
| 32 |
|
| 33 |
class WKBWriter_st(Structure): |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
WKT_READ_PTR = POINTER(WKTReader_st) |
| 38 |
WKT_WRITE_PTR = POINTER(WKTWriter_st) |
| 39 |
WKB_READ_PTR = POINTER(WKBReader_st) |
| 40 |
WKB_WRITE_PTR = POINTER(WKBReader_st) |
| 41 |
|
| 42 |
# WKTReader routines |
| 43 |
wkt_reader_create = GEOSFuncFactory("GEOSWKTReader_create", restype=WKT_READ_PTR) |
| 44 |
wkt_reader_destroy = GEOSFuncFactory("GEOSWKTReader_destroy", argtypes=[WKT_READ_PTR]) |
| 45 |
|
| 46 |
wkt_reader_read = GEOSFuncFactory( |
| 47 |
"GEOSWKTReader_read", |
| 48 |
argtypes=[WKT_READ_PTR, c_char_p], |
| 49 |
restype=GEOM_PTR, |
| 50 |
errcheck=check_geom, |
| 51 |
) |
| 52 |
# WKTWriter routines |
| 53 |
wkt_writer_create = GEOSFuncFactory("GEOSWKTWriter_create", restype=WKT_WRITE_PTR) |
| 54 |
wkt_writer_destroy = GEOSFuncFactory("GEOSWKTWriter_destroy", argtypes=[WKT_WRITE_PTR]) |
| 55 |
|
| 56 |
wkt_writer_write = GEOSFuncFactory( |
| 57 |
"GEOSWKTWriter_write", |
| 58 |
argtypes=[WKT_WRITE_PTR, GEOM_PTR], |
| 59 |
restype=geos_char_p, |
| 60 |
errcheck=check_string, |
| 61 |
) |
| 62 |
|
| 63 |
wkt_writer_get_outdim = GEOSFuncFactory( |
| 64 |
"GEOSWKTWriter_getOutputDimension", argtypes=[WKT_WRITE_PTR], restype=c_int |
| 65 |
) |
| 66 |
wkt_writer_set_outdim = GEOSFuncFactory( |
| 67 |
"GEOSWKTWriter_setOutputDimension", argtypes=[WKT_WRITE_PTR, c_int] |
| 68 |
) |
| 69 |
|
| 70 |
wkt_writer_set_trim = GEOSFuncFactory( |
| 71 |
"GEOSWKTWriter_setTrim", argtypes=[WKT_WRITE_PTR, c_byte] |
| 72 |
) |
| 73 |
wkt_writer_set_precision = GEOSFuncFactory( |
| 74 |
"GEOSWKTWriter_setRoundingPrecision", argtypes=[WKT_WRITE_PTR, c_int] |
| 75 |
) |
| 76 |
|
| 77 |
# WKBReader routines |
| 78 |
wkb_reader_create = GEOSFuncFactory("GEOSWKBReader_create", restype=WKB_READ_PTR) |
| 79 |
wkb_reader_destroy = GEOSFuncFactory("GEOSWKBReader_destroy", argtypes=[WKB_READ_PTR]) |
| 80 |
|
| 81 |
|
| 82 |
class WKBReadFunc(GEOSFuncFactory): |
| 83 |
# Although the function definitions take `const unsigned char *` |
| 84 |
# as their parameter, we use c_char_p here so the function may |
| 85 |
# take Python strings directly as parameters. Inside Python there |
| 86 |
# is not a difference between signed and unsigned characters, so |
| 87 |
# it is not a problem. |
| 88 |
argtypes = [WKB_READ_PTR, c_char_p, c_size_t] |
| 89 |
restype = GEOM_PTR |
| 90 |
errcheck = staticmethod(check_geom) |
| 91 |
|
| 92 |
|
| 93 |
wkb_reader_read = WKBReadFunc("GEOSWKBReader_read") |
| 94 |
wkb_reader_read_hex = WKBReadFunc("GEOSWKBReader_readHEX") |
| 95 |
|
| 96 |
# WKBWriter routines |
| 97 |
wkb_writer_create = GEOSFuncFactory("GEOSWKBWriter_create", restype=WKB_WRITE_PTR) |
| 98 |
wkb_writer_destroy = GEOSFuncFactory("GEOSWKBWriter_destroy", argtypes=[WKB_WRITE_PTR]) |
| 99 |
|
| 100 |
|
| 101 |
# WKB Writing prototypes. |
| 102 |
class WKBWriteFunc(GEOSFuncFactory): |
| 103 |
argtypes = [WKB_WRITE_PTR, GEOM_PTR, POINTER(c_size_t)] |
| 104 |
restype = c_uchar_p |
| 105 |
errcheck = staticmethod(check_sized_string) |
| 106 |
|
| 107 |
|
| 108 |
wkb_writer_write = WKBWriteFunc("GEOSWKBWriter_write") |
| 109 |
wkb_writer_write_hex = WKBWriteFunc("GEOSWKBWriter_writeHEX") |
| 110 |
|
| 111 |
|
| 112 |
# WKBWriter property getter/setter prototypes. |
| 113 |
class WKBWriterGet(GEOSFuncFactory): |
| 114 |
argtypes = [WKB_WRITE_PTR] |
| 115 |
restype = c_int |
| 116 |
|
| 117 |
|
| 118 |
class WKBWriterSet(GEOSFuncFactory): |
| 119 |
argtypes = [WKB_WRITE_PTR, c_int] |
| 120 |
|
| 121 |
|
| 122 |
wkb_writer_get_byteorder = WKBWriterGet("GEOSWKBWriter_getByteOrder") |
| 123 |
wkb_writer_set_byteorder = WKBWriterSet("GEOSWKBWriter_setByteOrder") |
| 124 |
wkb_writer_get_outdim = WKBWriterGet("GEOSWKBWriter_getOutputDimension") |
| 125 |
wkb_writer_set_outdim = WKBWriterSet("GEOSWKBWriter_setOutputDimension") |
| 126 |
wkb_writer_get_include_srid = WKBWriterGet( |
| 127 |
"GEOSWKBWriter_getIncludeSRID", restype=c_byte |
| 128 |
) |
| 129 |
wkb_writer_set_include_srid = WKBWriterSet( |
| 130 |
"GEOSWKBWriter_setIncludeSRID", argtypes=[WKB_WRITE_PTR, c_byte] |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
# ### Base I/O Class ### |
| 135 |
class IOBase(GEOSBase): |
| 136 |
"Base class for GEOS I/O objects." |
| 137 |
|
| 138 |
def __init__(self): |
| 139 |
# Getting the pointer with the constructor. |
| 140 |
self.ptr = self._constructor() |
| 141 |
# Loading the real destructor function at this point as doing it in |
| 142 |
# __del__ is too late (import error). |
| 143 |
self.destructor.func |
| 144 |
|
| 145 |
|
| 146 |
# ### Base WKB/WKT Reading and Writing objects ### |
| 147 |
|
| 148 |
|
| 149 |
# Non-public WKB/WKT reader classes for internal use because |
| 150 |
# their `read` methods return _pointers_ instead of GEOSGeometry |
| 151 |
# objects. |
| 152 |
class _WKTReader(IOBase): |
| 153 |
_constructor = wkt_reader_create |
| 154 |
ptr_type = WKT_READ_PTR |
| 155 |
destructor = wkt_reader_destroy |
| 156 |
|
| 157 |
def read(self, wkt): |
| 158 |
if not isinstance(wkt, (bytes, str)): |
| 159 |
raise TypeError |
| 160 |
return wkt_reader_read(self.ptr, force_bytes(wkt)) |
| 161 |
|
| 162 |
|
| 163 |
class _WKBReader(IOBase): |
| 164 |
_constructor = wkb_reader_create |
| 165 |
ptr_type = WKB_READ_PTR |
| 166 |
destructor = wkb_reader_destroy |
| 167 |
|
| 168 |
def read(self, wkb): |
| 169 |
"Return a _pointer_ to C GEOS Geometry object from the given WKB." |
| 170 |
if isinstance(wkb, memoryview): |
| 171 |
wkb_s = bytes(wkb) |
| 172 |
return wkb_reader_read(self.ptr, wkb_s, len(wkb_s)) |
| 173 |
elif isinstance(wkb, bytes): |
| 174 |
return wkb_reader_read_hex(self.ptr, wkb, len(wkb)) |
| 175 |
elif isinstance(wkb, str): |
| 176 |
wkb_s = wkb.encode() |
| 177 |
return wkb_reader_read_hex(self.ptr, wkb_s, len(wkb_s)) |
| 178 |
else: |
| 179 |
raise TypeError |
| 180 |
|
| 181 |
|
| 182 |
def default_trim_value(): |
| 183 |
""" |
| 184 |
GEOS changed the default value in 3.12.0. Can be replaced by True when |
| 185 |
3.12.0 becomes the minimum supported version. |
| 186 |
""" |
| 187 |
return geos_version_tuple() >= (3, 12) |
| 188 |
|
| 189 |
|
| 190 |
DEFAULT_TRIM_VALUE = SimpleLazyObject(default_trim_value) |
| 191 |
|
| 192 |
|
| 193 |
# ### WKB/WKT Writer Classes ### |
| 194 |
class WKTWriter(IOBase): |
| 195 |
_constructor = wkt_writer_create |
| 196 |
ptr_type = WKT_WRITE_PTR |
| 197 |
destructor = wkt_writer_destroy |
| 198 |
_precision = None |
| 199 |
|
| 200 |
def __init__(self, dim=2, trim=False, precision=None): |
| 201 |
super().__init__() |
| 202 |
self._trim = DEFAULT_TRIM_VALUE |
| 203 |
self.trim = trim |
| 204 |
if precision is not None: |
| 205 |
self.precision = precision |
| 206 |
self.outdim = dim |
| 207 |
|
| 208 |
def write(self, geom): |
| 209 |
"Return the WKT representation of the given geometry." |
| 210 |
return wkt_writer_write(self.ptr, geom.ptr) |
| 211 |
|
| 212 |
@property |
| 213 |
def outdim(self): |
| 214 |
return wkt_writer_get_outdim(self.ptr) |
| 215 |
|
| 216 |
@outdim.setter |
| 217 |
def outdim(self, new_dim): |
| 218 |
if new_dim not in (2, 3): |
| 219 |
raise ValueError("WKT output dimension must be 2 or 3") |
| 220 |
wkt_writer_set_outdim(self.ptr, new_dim) |
| 221 |
|
| 222 |
@property |
| 223 |
def trim(self): |
| 224 |
return self._trim |
| 225 |
|
| 226 |
@trim.setter |
| 227 |
def trim(self, flag): |
| 228 |
if bool(flag) != self._trim: |
| 229 |
self._trim = bool(flag) |
| 230 |
wkt_writer_set_trim(self.ptr, self._trim) |
| 231 |
|
| 232 |
@property |
| 233 |
def precision(self): |
| 234 |
return self._precision |
| 235 |
|
| 236 |
@precision.setter |
| 237 |
def precision(self, precision): |
| 238 |
if (not isinstance(precision, int) or precision < 0) and precision is not None: |
| 239 |
raise AttributeError( |
| 240 |
"WKT output rounding precision must be non-negative integer or None." |
| 241 |
) |
| 242 |
if precision != self._precision: |
| 243 |
self._precision = precision |
| 244 |
wkt_writer_set_precision(self.ptr, -1 if precision is None else precision) |
| 245 |
|
| 246 |
|
| 247 |
class WKBWriter(IOBase): |
| 248 |
_constructor = wkb_writer_create |
| 249 |
ptr_type = WKB_WRITE_PTR |
| 250 |
destructor = wkb_writer_destroy |
| 251 |
geos_version = geos_version_tuple() |
| 252 |
|
| 253 |
def __init__(self, dim=2): |
| 254 |
super().__init__() |
| 255 |
self.outdim = dim |
| 256 |
|
| 257 |
def _handle_empty_point(self, geom): |
| 258 |
from django.contrib.gis.geos import Point |
| 259 |
|
| 260 |
if isinstance(geom, Point) and geom.empty: |
| 261 |
if self.srid: |
| 262 |
# PostGIS uses POINT(NaN NaN) for WKB representation of empty |
| 263 |
# points. Use it for EWKB as it's a PostGIS specific format. |
| 264 |
# https://trac.osgeo.org/postgis/ticket/3181 |
| 265 |
geom = Point(float("NaN"), float("NaN"), srid=geom.srid) |
| 266 |
else: |
| 267 |
raise ValueError("Empty point is not representable in WKB.") |
| 268 |
return geom |
| 269 |
|
| 270 |
def write(self, geom): |
| 271 |
"Return the WKB representation of the given geometry." |
| 272 |
geom = self._handle_empty_point(geom) |
| 273 |
wkb = wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t())) |
| 274 |
return memoryview(wkb) |
| 275 |
|
| 276 |
def write_hex(self, geom): |
| 277 |
"Return the HEXEWKB representation of the given geometry." |
| 278 |
geom = self._handle_empty_point(geom) |
| 279 |
wkb = wkb_writer_write_hex(self.ptr, geom.ptr, byref(c_size_t())) |
| 280 |
return wkb |
| 281 |
|
| 282 |
# ### WKBWriter Properties ### |
| 283 |
|
| 284 |
# Property for getting/setting the byteorder. |
| 285 |
def _get_byteorder(self): |
| 286 |
return wkb_writer_get_byteorder(self.ptr) |
| 287 |
|
| 288 |
def _set_byteorder(self, order): |
| 289 |
if order not in (0, 1): |
| 290 |
raise ValueError( |
| 291 |
"Byte order parameter must be 0 (Big Endian) or 1 (Little Endian)." |
| 292 |
) |
| 293 |
wkb_writer_set_byteorder(self.ptr, order) |
| 294 |
|
| 295 |
byteorder = property(_get_byteorder, _set_byteorder) |
| 296 |
|
| 297 |
# Property for getting/setting the output dimension. |
| 298 |
@property |
| 299 |
def outdim(self): |
| 300 |
return wkb_writer_get_outdim(self.ptr) |
| 301 |
|
| 302 |
@outdim.setter |
| 303 |
def outdim(self, new_dim): |
| 304 |
if new_dim not in (2, 3): |
| 305 |
raise ValueError("WKB output dimension must be 2 or 3") |
| 306 |
wkb_writer_set_outdim(self.ptr, new_dim) |
| 307 |
|
| 308 |
# Property for getting/setting the include srid flag. |
| 309 |
@property |
| 310 |
def srid(self): |
| 311 |
return bool(wkb_writer_get_include_srid(self.ptr)) |
| 312 |
|
| 313 |
@srid.setter |
| 314 |
def srid(self, include): |
| 315 |
wkb_writer_set_include_srid(self.ptr, bool(include)) |
| 316 |
|
| 317 |
|
| 318 |
# `ThreadLocalIO` object holds instances of the WKT and WKB reader/writer |
| 319 |
# objects that are local to the thread. The `GEOSGeometry` internals |
| 320 |
# access these instances by calling the module-level functions, defined |
| 321 |
# below. |
| 322 |
class ThreadLocalIO(threading.local): |
| 323 |
wkt_r = None |
| 324 |
wkt_w = None |
| 325 |
wkb_r = None |
| 326 |
wkb_w = None |
| 327 |
ewkb_w = None |
| 328 |
|
| 329 |
|
| 330 |
thread_context = ThreadLocalIO() |
| 331 |
|
| 332 |
|
| 333 |
# These module-level routines return the I/O object that is local to the |
| 334 |
# thread. If the I/O object does not exist yet it will be initialized. |
| 335 |
def wkt_r(): |
| 336 |
thread_context.wkt_r = thread_context.wkt_r or _WKTReader() |
| 337 |
return thread_context.wkt_r |
| 338 |
|
| 339 |
|
| 340 |
def wkt_w(dim=2, trim=False, precision=None): |
| 341 |
if not thread_context.wkt_w: |
| 342 |
thread_context.wkt_w = WKTWriter(dim=dim, trim=trim, precision=precision) |
| 343 |
else: |
| 344 |
thread_context.wkt_w.outdim = dim |
| 345 |
thread_context.wkt_w.trim = trim |
| 346 |
thread_context.wkt_w.precision = precision |
| 347 |
return thread_context.wkt_w |
| 348 |
|
| 349 |
|
| 350 |
def wkb_r(): |
| 351 |
thread_context.wkb_r = thread_context.wkb_r or _WKBReader() |
| 352 |
return thread_context.wkb_r |
| 353 |
|
| 354 |
|
| 355 |
def wkb_w(dim=2): |
| 356 |
if not thread_context.wkb_w: |
| 357 |
thread_context.wkb_w = WKBWriter(dim=dim) |
| 358 |
else: |
| 359 |
thread_context.wkb_w.outdim = dim |
| 360 |
return thread_context.wkb_w |
| 361 |
|
| 362 |
|
| 363 |
def ewkb_w(dim=2): |
| 364 |
if not thread_context.ewkb_w: |
| 365 |
thread_context.ewkb_w = WKBWriter(dim=dim) |
| 366 |
thread_context.ewkb_w.srid = True |
| 367 |
else: |
| 368 |
thread_context.ewkb_w.outdim = dim |
| 369 |
return thread_context.ewkb_w |