def _create_polygon(self, length, items): # Instantiate LinearRing objects if necessary, but don't clone them yet # _construct_ring will throw a TypeError if a parameter isn't a valid ring # If we cloned the pointers here, we wouldn't be able to clean up # in case of error. if not length: return capi.create_empty_polygon() rings = [] for r in items: if isinstance(r, GEOM_PTR): rings.append(r) else: rings.append(self._construct_ring(r)) shell = self._clone(rings.pop(0)) n_holes = length - 1 if n_holes: holes = get_pointer_arr(n_holes) for i, r in enumerate(rings): holes[i] = self._clone(r) holes_param = byref(holes) else: holes_param = None return capi.create_polygon(shell, holes_param, c_uint(n_holes))
def __setitem__(self, index, ring): "Sets the ring at the specified index with the given ring." # Checking the index and ring parameters. self._checkindex(index) if not isinstance(ring, LinearRing): raise TypeError('must set Polygon index with a LinearRing object') # Getting the shell if index == 0: shell = geom_clone(ring.ptr) else: shell = geom_clone(get_extring(self.ptr)) # Getting the interior rings (holes) nholes = len(self) - 1 if nholes > 0: holes = get_pointer_arr(nholes) for i in xrange(nholes): if i == (index - 1): holes[i] = geom_clone(ring.ptr) else: holes[i] = geom_clone(get_intring(self.ptr, i)) holes_param = byref(holes) else: holes_param = None # Getting the current pointer, replacing with the newly constructed # geometry, and destroying the old geometry. prev_ptr = self.ptr srid = self.srid self._ptr = create_polygon(shell, holes_param, c_uint(nholes)) if srid: self.srid = srid destroy_geom(prev_ptr)
def _create_polygon(self, length, items): # Instantiate LinearRing objects if necessary, but don't clone them yet # _construct_ring will throw a TypeError if a parameter isn't a valid ring # If we cloned the pointers here, we wouldn't be able to clean up # in case of error. if not length: return capi.create_empty_polygon() rings = [] for r in items: if isinstance(r, GEOM_PTR): rings.append(r) else: rings.append(self._construct_ring(r)) shell = self._clone(rings.pop(0)) n_holes = length - 1 if n_holes: holes = get_pointer_arr(n_holes) for i, r in enumerate(rings): holes[i] = self._clone(r) holes_param = byref(holes) else: holes_param = None return capi.create_polygon(shell, holes_param, c_uint(n_holes))
def __setitem__(self, index, ring): "Sets the ring at the specified index with the given ring." # Checking the index and ring parameters. self._checkindex(index) if not isinstance(ring, LinearRing): raise TypeError('must set Polygon index with a LinearRing object') # Getting the shell if index == 0: shell = geom_clone(ring.ptr) else: shell = geom_clone(get_extring(self.ptr)) # Getting the interior rings (holes) nholes = len(self)-1 if nholes > 0: holes = get_pointer_arr(nholes) for i in xrange(nholes): if i == (index-1): holes[i] = geom_clone(ring.ptr) else: holes[i] = geom_clone(get_intring(self.ptr, i)) holes_param = byref(holes) else: holes_param = None # Getting the current pointer, replacing with the newly constructed # geometry, and destroying the old geometry. prev_ptr = self.ptr srid = self.srid self._ptr = create_polygon(shell, holes_param, c_uint(nholes)) if srid: self.srid = srid destroy_geom(prev_ptr)
def __init__(self, *args, **kwargs): "Initializes a Geometry Collection from a sequence of Geometry objects." # Checking the arguments if not args: raise TypeError, 'Must provide at least one Geometry to initialize %s.' % self.__class__.__name__ if len(args) == 1: # If only one geometry provided or a list of geometries is provided # in the first argument. if isinstance(args[0], (TupleType, ListType)): init_geoms = args[0] else: init_geoms = args else: init_geoms = args # Ensuring that only the permitted geometries are allowed in this collection if False in [isinstance(geom, self._allowed) for geom in init_geoms]: raise TypeError('Invalid Geometry type encountered in the arguments.') # Creating the geometry pointer array. ngeoms = len(init_geoms) geoms = get_pointer_arr(ngeoms) for i in xrange(ngeoms): geoms[i] = geom_clone(init_geoms[i].ptr) super(GeometryCollection, self).__init__(create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms)), **kwargs)
def __init__(self, *args, **kwargs): "Initializes a Geometry Collection from a sequence of Geometry objects." # Checking the arguments if not args: raise TypeError, 'Must provide at least one Geometry to initialize %s.' % self.__class__.__name__ if len(args) == 1: # If only one geometry provided or a list of geometries is provided # in the first argument. if isinstance(args[0], (TupleType, ListType)): init_geoms = args[0] else: init_geoms = args else: init_geoms = args # Ensuring that only the permitted geometries are allowed in this collection if False in [isinstance(geom, self._allowed) for geom in init_geoms]: raise TypeError( 'Invalid Geometry type encountered in the arguments.') # Creating the geometry pointer array. ngeoms = len(init_geoms) geoms = get_pointer_arr(ngeoms) for i in xrange(ngeoms): geoms[i] = geom_clone(init_geoms[i].ptr) super(GeometryCollection, self).__init__( create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms)), **kwargs)
def _create_collection(self, length, items): # Creating the geometry pointer array. geoms = get_pointer_arr(length) for i, g in enumerate(items): # this is a little sloppy, but makes life easier # allow GEOSGeometry types (python wrappers) or pointer types geoms[i] = capi.geom_clone(getattr(g, 'ptr', g)) return capi.create_collection(c_int(self._typeid), byref(geoms), c_uint(length))
def _create_collection(self, length, items): # Creating the geometry pointer array. geoms = get_pointer_arr(length) for i, g in enumerate(items): # this is a little sloppy, but makes life easier # allow GEOSGeometry types (python wrappers) or pointer types geoms[i] = capi.geom_clone(getattr(g, 'ptr', g)) return capi.create_collection(c_int(self._typeid), byref(geoms), c_uint(length))
def __init__(self, *args, **kwargs): """ Initializes on an exterior ring and a sequence of holes (both instances may be either LinearRing instances, or a tuple/list that may be constructed into a LinearRing). Examples of initialization, where shell, hole1, and hole2 are valid LinearRing geometries: >>> poly = Polygon(shell, hole1, hole2) >>> poly = Polygon(shell, (hole1, hole2)) Example where a tuple parameters are used: >>> poly = Polygon(((0, 0), (0, 10), (10, 10), (0, 10), (0, 0)), ((4, 4), (4, 6), (6, 6), (6, 4), (4, 4))) """ if not args: raise TypeError( 'Must provide at list one LinearRing instance to initialize Polygon.' ) # Getting the ext_ring and init_holes parameters from the argument list ext_ring = args[0] init_holes = args[1:] n_holes = len(init_holes) # If initialized as Polygon(shell, (LinearRing, LinearRing)) [for backward-compatibility] if n_holes == 1 and isinstance(init_holes[0], (tuple, list)) and \ (len(init_holes[0]) == 0 or isinstance(init_holes[0][0], LinearRing)): init_holes = init_holes[0] n_holes = len(init_holes) # Ensuring the exterior ring and holes parameters are LinearRing objects # or may be instantiated into LinearRings. ext_ring = self._construct_ring( ext_ring, 'Exterior parameter must be a LinearRing or an object that can initialize a LinearRing.' ) holes_list = [] # Create new list, cause init_holes is a tuple. for i in xrange(n_holes): holes_list.append( self._construct_ring( init_holes[i], 'Holes parameter must be a sequence of LinearRings or objects that can initialize to LinearRings' )) # Why another loop? Because if a TypeError is raised, cloned pointers will # be around that can't be cleaned up. holes = get_pointer_arr(n_holes) for i in xrange(n_holes): holes[i] = geom_clone(holes_list[i].ptr) # Getting the shell pointer address. shell = geom_clone(ext_ring.ptr) # Calling with the GEOS createPolygon factory. super(Polygon, self).__init__( create_polygon(shell, byref(holes), c_uint(n_holes)), **kwargs)
def __init__(self, *args, **kwargs): """ Initializes on an exterior ring and a sequence of holes (both instances may be either LinearRing instances, or a tuple/list that may be constructed into a LinearRing). Examples of initialization, where shell, hole1, and hole2 are valid LinearRing geometries: >>> poly = Polygon(shell, hole1, hole2) >>> poly = Polygon(shell, (hole1, hole2)) Example where a tuple parameters are used: >>> poly = Polygon(((0, 0), (0, 10), (10, 10), (0, 10), (0, 0)), ((4, 4), (4, 6), (6, 6), (6, 4), (4, 4))) """ if not args: raise TypeError('Must provide at list one LinearRing instance to initialize Polygon.') # Getting the ext_ring and init_holes parameters from the argument list ext_ring = args[0] init_holes = args[1:] n_holes = len(init_holes) # If initialized as Polygon(shell, (LinearRing, LinearRing)) [for backward-compatibility] if n_holes == 1 and isinstance(init_holes[0], (tuple, list)) and \ (len(init_holes[0]) == 0 or isinstance(init_holes[0][0], LinearRing)): init_holes = init_holes[0] n_holes = len(init_holes) # Ensuring the exterior ring and holes parameters are LinearRing objects # or may be instantiated into LinearRings. ext_ring = self._construct_ring(ext_ring, 'Exterior parameter must be a LinearRing or an object that can initialize a LinearRing.') holes_list = [] # Create new list, cause init_holes is a tuple. for i in xrange(n_holes): holes_list.append(self._construct_ring(init_holes[i], 'Holes parameter must be a sequence of LinearRings or objects that can initialize to LinearRings')) # Why another loop? Because if a TypeError is raised, cloned pointers will # be around that can't be cleaned up. holes = get_pointer_arr(n_holes) for i in xrange(n_holes): holes[i] = geom_clone(holes_list[i].ptr) # Getting the shell pointer address. shell = geom_clone(ext_ring.ptr) # Calling with the GEOS createPolygon factory. super(Polygon, self).__init__(create_polygon(shell, byref(holes), c_uint(n_holes)), **kwargs)
def __setitem__(self, index, geom): "Sets the Geometry at the specified index." self._checkindex(index) if not isinstance(geom, self._allowed): raise TypeError('Incompatible Geometry for collection.') ngeoms = len(self) geoms = get_pointer_arr(ngeoms) for i in xrange(ngeoms): if i == index: geoms[i] = geom_clone(geom.ptr) else: geoms[i] = geom_clone(get_geomn(self.ptr, i)) # Creating a new collection, and destroying the contents of the previous poiner. prev_ptr = self.ptr srid = self.srid self._ptr = create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms)) if srid: self.srid = srid destroy_geom(prev_ptr)
def __setitem__(self, index, geom): "Sets the Geometry at the specified index." self._checkindex(index) if not isinstance(geom, self._allowed): raise TypeError('Incompatible Geometry for collection.') ngeoms = len(self) geoms = get_pointer_arr(ngeoms) for i in xrange(ngeoms): if i == index: geoms[i] = geom_clone(geom.ptr) else: geoms[i] = geom_clone(get_geomn(self.ptr, i)) # Creating a new collection, and destroying the contents of the previous poiner. prev_ptr = self.ptr srid = self.srid self._ptr = create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms)) if srid: self.srid = srid destroy_geom(prev_ptr)
from ctypes import byref, c_uint
for i in range(len(self)): yield self[i] def __len__(self): <<<<<<< HEAD "Returns the number of geometries in this Collection." ======= "Return the number of geometries in this Collection." >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 return self.num_geom # ### Methods for compatibility with ListMixin ### def _create_collection(self, length, items): # Creating the geometry pointer array. <<<<<<< HEAD geoms = get_pointer_arr(length) for i, g in enumerate(items): # this is a little sloppy, but makes life easier # allow GEOSGeometry types (python wrappers) or pointer types geoms[i] = capi.geom_clone(getattr(g, 'ptr', g)) ======= geoms = (GEOM_PTR * length)(*[ # this is a little sloppy, but makes life easier # allow GEOSGeometry types (python wrappers) or pointer types capi.geom_clone(getattr(g, 'ptr', g)) for g in items ]) >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 return capi.create_collection(c_int(self._typeid), byref(geoms), c_uint(length)) def _get_single_internal(self, index):
"""
if not length: return capi.create_empty_polygon() rings = [] for r in items: if isinstance(r, GEOM_PTR): rings.append(r) else: rings.append(self._construct_ring(r)) shell = self._clone(rings.pop(0)) n_holes = length - 1 if n_holes: <<<<<<< HEAD holes = get_pointer_arr(n_holes) for i, r in enumerate(rings): holes[i] = self._clone(r) holes_param = byref(holes) ======= holes = (GEOM_PTR * n_holes)(*[self._clone(r) for r in rings]) holes_param = byref(holes) >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 else: holes_param = None return capi.create_polygon(shell, holes_param, c_uint(n_holes)) def _clone(self, g): if isinstance(g, GEOM_PTR): return capi.geom_clone(g)