def write_hex(self, geom): "Returns the HEXEWKB representation of the given geometry." from django.contrib.gis.geos.polygon import Polygon geom = self._handle_empty_point(geom) wkb = wkb_writer_write_hex(self.ptr, geom.ptr, byref(c_size_t())) if geos_version_info()['version'] < '3.6.1' and isinstance(geom, Polygon) and geom.empty: wkb = wkb[:-16] + b'0' * 8 return wkb
def write(self, geom): "Returns the WKB representation of the given geometry." from django.contrib.gis.geos import Polygon geom = self._handle_empty_point(geom) wkb = wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t())) if geos_version_info()['version'] < '3.6.1' and isinstance(geom, Polygon) and geom.empty: # Fix GEOS output for empty polygon. # See https://trac.osgeo.org/geos/ticket/680. wkb = wkb[:-8] + b'\0' * 4 return six.memoryview(wkb)
from django.core.exceptions import ImproperlyConfigured try: import psycopg2 HAS_PSYCOPG2 = True except ImportError: psycopg2 = None HAS_PSYCOPG2 = False try: from django.contrib.gis.geos.libgeos import geos_version_info HAS_GEOS = geos_version_info()["version"] >= "3.3.0" except (ImportError, OSError, ImproperlyConfigured): HAS_GEOS = False
class WKBWriter(IOBase): _constructor = wkb_writer_create ptr_type = WKB_WRITE_PTR destructor = wkb_writer_destroy geos_version = geos_version_info() def __init__(self, dim=2): super(WKBWriter, self).__init__() self.outdim = dim def _handle_empty_point(self, geom): from django.contrib.gis.geos import Point if isinstance(geom, Point) and geom.empty: if self.srid: # PostGIS uses POINT(NaN NaN) for WKB representation of empty # points. Use it for EWKB as it's a PostGIS specific format. # https://trac.osgeo.org/postgis/ticket/3181 geom = Point(float('NaN'), float('NaN'), srid=geom.srid) else: raise ValueError('Empty point is not representable in WKB.') return geom def write(self, geom): "Returns the WKB representation of the given geometry." from django.contrib.gis.geos import Polygon geom = self._handle_empty_point(geom) wkb = wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t())) if self.geos_version['version'] < '3.6.1' and isinstance( geom, Polygon) and geom.empty: # Fix GEOS output for empty polygon. # See https://trac.osgeo.org/geos/ticket/680. wkb = wkb[:-8] + b'\0' * 4 return six.memoryview(wkb) def write_hex(self, geom): "Returns the HEXEWKB representation of the given geometry." from django.contrib.gis.geos.polygon import Polygon geom = self._handle_empty_point(geom) wkb = wkb_writer_write_hex(self.ptr, geom.ptr, byref(c_size_t())) if self.geos_version['version'] < '3.6.1' and isinstance( geom, Polygon) and geom.empty: wkb = wkb[:-16] + b'0' * 8 return wkb # ### WKBWriter Properties ### # Property for getting/setting the byteorder. def _get_byteorder(self): return wkb_writer_get_byteorder(self.ptr) def _set_byteorder(self, order): if order not in (0, 1): raise ValueError( 'Byte order parameter must be 0 (Big Endian) or 1 (Little Endian).' ) wkb_writer_set_byteorder(self.ptr, order) byteorder = property(_get_byteorder, _set_byteorder) # Property for getting/setting the output dimension. @property def outdim(self): return wkb_writer_get_outdim(self.ptr) @outdim.setter def outdim(self, new_dim): if new_dim not in (2, 3): raise ValueError('WKB output dimension must be 2 or 3') wkb_writer_set_outdim(self.ptr, new_dim) # Property for getting/setting the include srid flag. @property def srid(self): return bool(ord(wkb_writer_get_include_srid(self.ptr))) @srid.setter def srid(self, include): if include: flag = b'\x01' else: flag = b'\x00' wkb_writer_set_include_srid(self.ptr, flag)
geos_preservesimplify = topology(GEOSFunc('GEOSTopologyPreserveSimplify'), c_double) geos_simplify = topology(GEOSFunc('GEOSSimplify'), c_double) geos_symdifference = topology(GEOSFunc('GEOSSymDifference'), GEOM_PTR) geos_union = topology(GEOSFunc('GEOSUnion'), GEOM_PTR) # GEOSRelate returns a string, not a geometry. geos_relate = GEOSFunc('GEOSRelate') geos_relate.argtypes = [GEOM_PTR, GEOM_PTR] geos_relate.restype = geos_char_p geos_relate.errcheck = check_string # Routines only in GEOS 3.1+ if GEOS_PREPARE: geos_cascaded_union = GEOSFunc('GEOSUnionCascaded') geos_cascaded_union.argtypes = [GEOM_PTR] geos_cascaded_union.restype = GEOM_PTR __all__.append('geos_cascaded_union') # Linear referencing routines info = geos_version_info() if info['version'] >= '3.2.0': geos_project = topology(GEOSFunc('GEOSProject'), GEOM_PTR, restype=c_double, errcheck=check_minus_one) geos_interpolate = topology(GEOSFunc('GEOSInterpolate'), c_double) geos_project_normalized = topology(GEOSFunc('GEOSProjectNormalized'), GEOM_PTR, restype=c_double, errcheck=check_minus_one) geos_interpolate_normalized = topology(GEOSFunc('GEOSInterpolateNormalized'), c_double) __all__.extend(['geos_project', 'geos_interpolate', 'geos_project_normalized', 'geos_interpolate_normalized'])
def closed(self): if geos_version_info()['version'] < '3.5': raise GEOSException("MultiLineString.closed requires GEOS >= 3.5.0.") return super(MultiLineString, self).closed
def closed(self): if geos_version_info()['version'] < '3.5': raise GEOSException( "MultiLineString.closed requires GEOS >= 3.5.0.") return super(MultiLineString, self).closed
# Prepared geometry constructor and destructors. geos_prepare = GEOSFunc('GEOSPrepare') geos_prepare.argtypes = [GEOM_PTR] geos_prepare.restype = PREPGEOM_PTR prepared_destroy = GEOSFunc('GEOSPreparedGeom_destroy') prepared_destroy.argtpes = [PREPGEOM_PTR] prepared_destroy.restype = None # Prepared geometry binary predicate support. def prepared_predicate(func): func.argtypes = [PREPGEOM_PTR, GEOM_PTR] func.restype = c_char func.errcheck = check_predicate return func prepared_contains = prepared_predicate(GEOSFunc('GEOSPreparedContains')) prepared_contains_properly = prepared_predicate( GEOSFunc('GEOSPreparedContainsProperly')) prepared_covers = prepared_predicate(GEOSFunc('GEOSPreparedCovers')) prepared_intersects = prepared_predicate(GEOSFunc('GEOSPreparedIntersects')) if geos_version_info()['version'] > '3.3.0': prepared_crosses = prepared_predicate(GEOSFunc('GEOSPreparedCrosses')) prepared_disjoint = prepared_predicate(GEOSFunc('GEOSPreparedDisjoint')) prepared_overlaps = prepared_predicate(GEOSFunc('GEOSPreparedOverlaps')) prepared_touches = prepared_predicate(GEOSFunc('GEOSPreparedTouches')) prepared_within = prepared_predicate(GEOSFunc('GEOSPreparedWithin'))
from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc # Prepared geometry constructor and destructors. geos_prepare = GEOSFunc('GEOSPrepare') geos_prepare.argtypes = [GEOM_PTR] geos_prepare.restype = PREPGEOM_PTR prepared_destroy = GEOSFunc('GEOSPreparedGeom_destroy') prepared_destroy.argtpes = [PREPGEOM_PTR] prepared_destroy.restype = None # Prepared geometry binary predicate support. def prepared_predicate(func): func.argtypes = [PREPGEOM_PTR, GEOM_PTR] func.restype = c_char func.errcheck = check_predicate return func prepared_contains = prepared_predicate(GEOSFunc('GEOSPreparedContains')) prepared_contains_properly = prepared_predicate(GEOSFunc('GEOSPreparedContainsProperly')) prepared_covers = prepared_predicate(GEOSFunc('GEOSPreparedCovers')) prepared_intersects = prepared_predicate(GEOSFunc('GEOSPreparedIntersects')) if geos_version_info()['version'] > '3.3.0': prepared_crosses = prepared_predicate(GEOSFunc('GEOSPreparedCrosses')) prepared_disjoint = prepared_predicate(GEOSFunc('GEOSPreparedDisjoint')) prepared_overlaps = prepared_predicate(GEOSFunc('GEOSPreparedOverlaps')) prepared_touches = prepared_predicate(GEOSFunc('GEOSPreparedTouches')) prepared_within = prepared_predicate(GEOSFunc('GEOSPreparedWithin'))
# MultiPoint, MultiLineString, and MultiPolygon class definitions. class MultiPoint(GeometryCollection): _allowed = Point _typeid = 4 class MultiLineString(LinearGeometryMixin, GeometryCollection): _allowed = (LineString, LinearRing) _typeid = 5 @property def closed(self): <<<<<<< HEAD if geos_version_info()['version'] < '3.5': raise GEOSException("MultiLineString.closed requires GEOS >= 3.5.0.") return super(MultiLineString, self).closed ======= if geos_version_tuple() < (3, 5): raise GEOSException("MultiLineString.closed requires GEOS >= 3.5.0.") return super().closed >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 class MultiPolygon(GeometryCollection): _allowed = Polygon _typeid = 6 <<<<<<< HEAD @property
#!/usr/bin/env python import os, sys, warnings warnings.simplefilter('error', RuntimeWarning) if hasattr(sys, 'pypy_version_info'): from psycopg2cffi import compat compat.register() from django.conf import settings try: from django.contrib.gis.geos.libgeos import geos_version_info HAS_GEOS = geos_version_info()['version'] >= '3.3.0' except (ImportError, OSError): HAS_GEOS = False DISABLE_SERVER_SIDE_CURSORS = False if os.environ.get('TRAVIS_PYTHON_VERSION') == 'pypy': DISABLE_SERVER_SIDE_CURSORS = True SETTINGS = { 'DATABASES': { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis' if HAS_GEOS else 'django.db.backends.postgresql_psycopg2', 'NAME': 'travis_postgis', 'USER': '******', 'DISABLE_SERVER_SIDE_CURSORS': DISABLE_SERVER_SIDE_CURSORS, } },
# Prepared geometry constructor and destructors. geos_prepare = GEOSFunc("GEOSPrepare") geos_prepare.argtypes = [GEOM_PTR] geos_prepare.restype = PREPGEOM_PTR prepared_destroy = GEOSFunc("GEOSPreparedGeom_destroy") prepared_destroy.argtpes = [PREPGEOM_PTR] prepared_destroy.restype = None # Prepared geometry binary predicate support. def prepared_predicate(func): func.argtypes = [PREPGEOM_PTR, GEOM_PTR] func.restype = c_char func.errcheck = check_predicate return func prepared_contains = prepared_predicate(GEOSFunc("GEOSPreparedContains")) prepared_contains_properly = prepared_predicate(GEOSFunc("GEOSPreparedContainsProperly")) prepared_covers = prepared_predicate(GEOSFunc("GEOSPreparedCovers")) prepared_intersects = prepared_predicate(GEOSFunc("GEOSPreparedIntersects")) if geos_version_info()["version"] > "3.3.0": prepared_crosses = prepared_predicate(GEOSFunc("GEOSPreparedCrosses")) prepared_disjoint = prepared_predicate(GEOSFunc("GEOSPreparedDisjoint")) prepared_overlaps = prepared_predicate(GEOSFunc("GEOSPreparedOverlaps")) prepared_touches = prepared_predicate(GEOSFunc("GEOSPreparedTouches")) prepared_within = prepared_predicate(GEOSFunc("GEOSPreparedWithin"))
from six import string_types, iteritems from django.db.models.base import Model from django.db.models.query import QuerySet, ValuesQuerySet from django.forms.models import model_to_dict from django.core.serializers.python import (_get_model, Serializer as PythonSerializer, Deserializer as PythonDeserializer) from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.base import SerializationError, DeserializationError from django.utils.encoding import smart_text try: from django.contrib.gis.geos.libgeos import geos_version_info geos_version_info() from django.contrib.gis.geos import WKBWriter from django.contrib.gis.geos import GEOSGeometry from django.contrib.gis.db.models.fields import GeometryField except ImportError: from .nogeos import WKBWriter from .nogeos import GEOSGeometry from .fields import GeometryField from . import GEOJSON_DEFAULT_SRID from .fields import GeoJSONField logger = logging.getLogger(__name__)
#!/usr/bin/env python from django.contrib.gis.geos.libgeos import lib_path, geos_version_info print u'----------------------------------------------------------------------' print u' Checking GEOS' print u' - lib_path: %s' % lib_path print u' - geos_version_info: %s' % geos_version_info() print u'----------------------------------------------------------------------'