def __getitem__(self, key): self._update() dx = c_double() dy = c_double() dz = c_double() m = self.__len__() has_z = self._ndim == 3 if isinstance(key, int): if key + m < 0 or key >= m: raise IndexError("index out of range") if key < 0: i = m + key else: i = key lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(dx)) lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(dy)) if has_z: lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(dz)) return (dx.value, dy.value, dz.value) else: return (dx.value, dy.value) elif isinstance(key, slice): res = [] start, stop, stride = key.indices(m) for i in range(start, stop, stride): lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(dx)) lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(dy)) if has_z: lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(dz)) res.append((dx.value, dy.value, dz.value)) else: res.append((dx.value, dy.value)) return res else: raise TypeError("key must be an index or slice")
def nearest_points(g1, g2): """Returns the calculated nearest points in the input geometries The points are returned in the same order as the input geometries. """ seq = lgeos.methods['nearest_points'](g1._geom, g2._geom) if seq is None: if g1.is_empty: raise ValueError('The first input geometry is empty') else: raise ValueError('The second input geometry is empty') x1 = c_double() y1 = c_double() x2 = c_double() y2 = c_double() lgeos.GEOSCoordSeq_getX(seq, 0, byref(x1)) lgeos.GEOSCoordSeq_getY(seq, 0, byref(y1)) lgeos.GEOSCoordSeq_getX(seq, 1, byref(x2)) lgeos.GEOSCoordSeq_getY(seq, 1, byref(y2)) p1 = Point(x1.value, y1.value) p2 = Point(x2.value, y2.value) return (p1, p2)
def xy(self): """X and Y arrays""" self._update() m = self.__len__() x = array('d') y = array('d') temp = c_double() for i in range(m): lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp)) x.append(temp.value) lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp)) y.append(temp.value) return x, y
def __iter__(self): self._update() dx = c_double() dy = c_double() dz = c_double() for i in range(self.__len__()): lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(dx)) lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(dy)) if self._ndim == 3: # TODO: use hasz lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(dz)) yield (dx.value, dy.value, dz.value) else: yield (dx.value, dy.value)
def ctypes(self): self._update() n = self._ndim m = self.__len__() array_type = c_double * (m * n) data = array_type() temp = c_double() for i in xrange(m): lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp)) data[n * i] = temp.value lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp)) data[n * i + 1] = temp.value if n == 3: # TODO: use hasz lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(temp)) data[n * i + 2] = temp.value return data
def _ctypes(self): self._update() has_z = self._ndim == 3 n = self._ndim m = self.__len__() array_type = c_double * (m * n) data = array_type() temp = c_double() for i in range(m): lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp)) data[n * i] = temp.value lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp)) data[n * i + 1] = temp.value if has_z: lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(temp)) data[n * i + 2] = temp.value return data
def _ctypes(self): if not self._ctypes_data: temp = c_double() n = self._ndim m = len(self.geoms) array_type = c_double * (m * n) data = array_type() for i in range(m): g = self.geoms[i]._geom cs = lgeos.GEOSGeom_getCoordSeq(g) lgeos.GEOSCoordSeq_getX(cs, 0, byref(temp)) data[n*i] = temp.value lgeos.GEOSCoordSeq_getY(cs, 0, byref(temp)) data[n*i+1] = temp.value if n == 3: # TODO: use hasz lgeos.GEOSCoordSeq_getZ(cs, 0, byref(temp)) data[n*i+2] = temp.value self._ctypes_data = data return self._ctypes_data
def __getitem__(self, i): self._update() M = self.__len__() if i + M < 0 or i >= M: raise IndexError("index out of range") if i < 0: ii = M + i else: ii = i dx = c_double() dy = c_double() dz = c_double() lgeos.GEOSCoordSeq_getX(self._cseq, ii, byref(dx)) lgeos.GEOSCoordSeq_getY(self._cseq, ii, byref(dy)) if self._ndim == 3: # TODO: use hasz lgeos.GEOSCoordSeq_getZ(self._cseq, ii, byref(dz)) return (dx.value, dy.value, dz.value) else: return (dx.value, dy.value)
def _ctypes(self): self._update() has_z = self._ndim == 3 n = self._ndim or 0 if n == 0: # ignore with NumPy 1.21 __array_interface__ raise AttributeError("empty geometry sequence") m = self.__len__() array_type = c_double * (m * n) data = array_type() temp = c_double() for i in range(m): lgeos.GEOSCoordSeq_getX(self._cseq, i, byref(temp)) data[n * i] = temp.value lgeos.GEOSCoordSeq_getY(self._cseq, i, byref(temp)) data[n * i + 1] = temp.value if has_z: lgeos.GEOSCoordSeq_getZ(self._cseq, i, byref(temp)) data[n * i + 2] = temp.value return data
def nearest_points(self, other): """Returns list of tuples like (x, y) of nearest points""" _ndim = 2 # TODO: remove hardcoding _cseq = self.impl['nearest_points'](self, other) cs_len = c_uint(0) lgeos.GEOSCoordSeq_getSize(_cseq, byref(cs_len)) llen = cs_len.value dx = c_double() dy = c_double() dz = c_double() has_z = _ndim == 3 res = [] for i in xrange(llen): lgeos.GEOSCoordSeq_getX(_cseq, i, byref(dx)) lgeos.GEOSCoordSeq_getY(_cseq, i, byref(dy)) if has_z: lgeos.GEOSCoordSeq_getZ(_cseq, i, byref(dz)) res.append((dx.value, dy.value, dz.value)) else: res.append((dx.value, dy.value)) return res
def __call__(self, this): self._validate(this) env = this.envelope if env.geom_type == 'Point': return env.bounds cs = lgeos.GEOSGeom_getCoordSeq(env.exterior._geom) cs_len = c_uint(0) lgeos.GEOSCoordSeq_getSize(cs, byref(cs_len)) minx = 1.e+20 maxx = -1e+20 miny = 1.e+20 maxy = -1e+20 temp = c_double() for i in range(cs_len.value): lgeos.GEOSCoordSeq_getX(cs, i, byref(temp)) x = temp.value if x < minx: minx = x if x > maxx: maxx = x lgeos.GEOSCoordSeq_getY(cs, i, byref(temp)) y = temp.value if y < miny: miny = y if y > maxy: maxy = y return (minx, miny, maxx, maxy)