desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Testing Coordinate Sequence objects.'
def test_coord_seq(self):
for p in self.geometries.polygons: if p.ext_ring_cs: poly = fromstr(p.wkt) cs = poly.exterior_ring.coord_seq self.assertEqual(p.ext_ring_cs, cs.tuple) self.assertEqual(len(p.ext_ring_cs), len(cs)) for i in xrange(len(p.ext_ring_cs)): c1 = p.ext_ring_cs[i] c2 = cs[i] self.assertEqual(c1, c2) if (len(c1) == 2): tset = (5, 23) else: tset = (5, 23, 8) cs[i] = tset for j in range(len(tset)): cs[i] = tset self.assertEqual(tset[j], cs[i][j])
'Testing relate() and relate_pattern().'
def test_relate_pattern(self):
g = fromstr(u'POINT (0 0)') self.assertRaises(GEOSException, g.relate_pattern, 0, u'invalid pattern, yo') for rg in self.geometries.relate_geoms: a = fromstr(rg.wkt_a) b = fromstr(rg.wkt_b) self.assertEqual(rg.result, a.relate_pattern(b, rg.pattern)) self.assertEqual(rg.pattern, a.relate(b))
'Testing intersects() and intersection().'
def test_intersection(self):
for i in xrange(len(self.geometries.topology_geoms)): a = fromstr(self.geometries.topology_geoms[i].wkt_a) b = fromstr(self.geometries.topology_geoms[i].wkt_b) i1 = fromstr(self.geometries.intersect_geoms[i].wkt) self.assertEqual(True, a.intersects(b)) i2 = a.intersection(b) self.assertEqual(i1, i2) self.assertEqual(i1, (a & b)) a &= b self.assertEqual(i1, a)
'Testing union().'
def test_union(self):
for i in xrange(len(self.geometries.topology_geoms)): a = fromstr(self.geometries.topology_geoms[i].wkt_a) b = fromstr(self.geometries.topology_geoms[i].wkt_b) u1 = fromstr(self.geometries.union_geoms[i].wkt) u2 = a.union(b) self.assertEqual(u1, u2) self.assertEqual(u1, (a | b)) a |= b self.assertEqual(u1, a)
'Testing difference().'
def test_difference(self):
for i in xrange(len(self.geometries.topology_geoms)): a = fromstr(self.geometries.topology_geoms[i].wkt_a) b = fromstr(self.geometries.topology_geoms[i].wkt_b) d1 = fromstr(self.geometries.diff_geoms[i].wkt) d2 = a.difference(b) self.assertEqual(d1, d2) self.assertEqual(d1, (a - b)) a -= b self.assertEqual(d1, a)
'Testing sym_difference().'
def test_symdifference(self):
for i in xrange(len(self.geometries.topology_geoms)): a = fromstr(self.geometries.topology_geoms[i].wkt_a) b = fromstr(self.geometries.topology_geoms[i].wkt_b) d1 = fromstr(self.geometries.sdiff_geoms[i].wkt) d2 = a.sym_difference(b) self.assertEqual(d1, d2) self.assertEqual(d1, (a ^ b)) a ^= b self.assertEqual(d1, a)
'Testing buffer().'
def test_buffer(self):
for bg in self.geometries.buffer_geoms: g = fromstr(bg.wkt) exp_buf = fromstr(bg.buffer_wkt) quadsegs = bg.quadsegs width = bg.width self.assertRaises(ctypes.ArgumentError, g.buffer, width, float(quadsegs)) buf = g.buffer(width, quadsegs) self.assertEqual(exp_buf.num_coords, buf.num_coords) self.assertEqual(len(exp_buf), len(buf)) for j in xrange(len(exp_buf)): exp_ring = exp_buf[j] buf_ring = buf[j] self.assertEqual(len(exp_ring), len(buf_ring)) for k in xrange(len(exp_ring)): self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9) self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9)
'Testing the SRID property and keyword.'
def test_srid(self):
pnt = Point(5, 23, srid=4326) self.assertEqual(4326, pnt.srid) pnt.srid = 3084 self.assertEqual(3084, pnt.srid) self.assertRaises(ctypes.ArgumentError, pnt.set_srid, u'4326') poly = fromstr(self.geometries.polygons[1].wkt, srid=4269) self.assertEqual(4269, poly.srid) for ring in poly: self.assertEqual(4269, ring.srid) poly.srid = 4326 self.assertEqual(4326, poly.shell.srid) gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021) self.assertEqual(32021, gc.srid) for i in range(len(gc)): self.assertEqual(32021, gc[i].srid) hex = u'0101000020E610000000000000000014400000000000003740' p1 = fromstr(hex) self.assertEqual(4326, p1.srid) exp_srid = self.null_srid p2 = fromstr(p1.hex) self.assertEqual(exp_srid, p2.srid) p3 = fromstr(p1.hex, srid=(-1)) self.assertEqual((-1), p3.srid)
'Testing the mutability of Polygons and Geometry Collections.'
def test_mutable_geometries(self):
for p in self.geometries.polygons: poly = fromstr(p.wkt) self.assertRaises(TypeError, poly.__setitem__, 0, LineString((1, 1), (2, 2))) shell_tup = poly.shell.tuple new_coords = [] for point in shell_tup: new_coords.append(((point[0] + 500.0), (point[1] + 500.0))) new_shell = LinearRing(*tuple(new_coords)) poly.exterior_ring = new_shell s = str(new_shell) self.assertEqual(poly.exterior_ring, new_shell) self.assertEqual(poly[0], new_shell) for tg in self.geometries.multipoints: mp = fromstr(tg.wkt) for i in range(len(mp)): pnt = mp[i] new = Point(random.randint(21, 100), random.randint(21, 100)) mp[i] = new s = str(new) self.assertEqual(mp[i], new) self.assertEqual(mp[i].wkt, new.wkt) self.assertNotEqual(pnt, mp[i]) for tg in self.geometries.multipolygons: mpoly = fromstr(tg.wkt) for i in xrange(len(mpoly)): poly = mpoly[i] old_poly = mpoly[i] for j in xrange(len(poly)): r = poly[j] for k in xrange(len(r)): r[k] = ((r[k][0] + 500.0), (r[k][1] + 500.0)) poly[j] = r self.assertNotEqual(mpoly[i], poly) mpoly[i] = poly s = str(poly) self.assertEqual(mpoly[i], poly) self.assertNotEqual(mpoly[i], old_poly)
'Testing three-dimensional geometries.'
def test_threed(self):
pnt = Point(2, 3, 8) self.assertEqual((2.0, 3.0, 8.0), pnt.coords) self.assertRaises(TypeError, pnt.set_coords, (1.0, 2.0)) pnt.coords = (1.0, 2.0, 3.0) self.assertEqual((1.0, 2.0, 3.0), pnt.coords) ls = LineString((2.0, 3.0, 8.0), (50.0, 250.0, (-117.0))) self.assertEqual(((2.0, 3.0, 8.0), (50.0, 250.0, (-117.0))), ls.tuple) self.assertRaises(TypeError, ls.__setitem__, 0, (1.0, 2.0)) ls[0] = (1.0, 2.0, 3.0) self.assertEqual((1.0, 2.0, 3.0), ls[0])
'Testing the distance() function.'
def test_distance(self):
pnt = Point(0, 0) self.assertEqual(0.0, pnt.distance(Point(0, 0))) self.assertEqual(1.0, pnt.distance(Point(0, 1))) self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11) ls1 = LineString((0, 0), (1, 1), (2, 2)) ls2 = LineString((5, 2), (6, 1), (7, 0)) self.assertEqual(3, ls1.distance(ls2))
'Testing the length property.'
def test_length(self):
pnt = Point(0, 0) self.assertEqual(0.0, pnt.length) ls = LineString((0, 0), (1, 1)) self.assertAlmostEqual(1.41421356237, ls.length, 11) poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0))) self.assertEqual(4.0, poly.length) mpoly = MultiPolygon(poly.clone(), poly) self.assertEqual(8.0, mpoly.length)
'Testing empty geometries and collections.'
def test_emptyCollections(self):
gc1 = GeometryCollection([]) gc2 = fromstr(u'GEOMETRYCOLLECTION EMPTY') pnt = fromstr(u'POINT EMPTY') ls = fromstr(u'LINESTRING EMPTY') poly = fromstr(u'POLYGON EMPTY') mls = fromstr(u'MULTILINESTRING EMPTY') mpoly1 = fromstr(u'MULTIPOLYGON EMPTY') mpoly2 = MultiPolygon(()) for g in [gc1, gc2, pnt, ls, poly, mls, mpoly1, mpoly2]: self.assertEqual(True, g.empty) if isinstance(g, Polygon): self.assertEqual(1, len(g)) self.assertEqual(1, g.num_geom) self.assertEqual(0, len(g[0])) elif isinstance(g, (Point, LineString)): self.assertEqual(1, g.num_geom) self.assertEqual(0, len(g)) else: self.assertEqual(0, g.num_geom) self.assertEqual(0, len(g)) if isinstance(g, Point): self.assertRaises(GEOSIndexError, g.get_x) elif isinstance(g, Polygon): lr = g.shell self.assertEqual(u'LINEARRING EMPTY', lr.wkt) self.assertEqual(0, len(lr)) self.assertEqual(True, lr.empty) self.assertRaises(GEOSIndexError, lr.__getitem__, 0) else: self.assertRaises(GEOSIndexError, g.__getitem__, 0)
'Testing GeometryCollection handling of other collections.'
def test_collections_of_collections(self):
coll = [mp.wkt for mp in self.geometries.multipolygons if mp.valid] coll.extend([mls.wkt for mls in self.geometries.multilinestrings]) coll.extend([p.wkt for p in self.geometries.polygons]) coll.extend([mp.wkt for mp in self.geometries.multipoints]) gc_wkt = (u'GEOMETRYCOLLECTION(%s)' % u','.join(coll)) gc1 = GEOSGeometry(gc_wkt) gc2 = GeometryCollection(*tuple((g for g in gc1))) self.assertEqual(gc1, gc2)
'Testing `ogr` and `srs` properties.'
@unittest.skipUnless(gdal.HAS_GDAL, u'gdal is required') def test_gdal(self):
g1 = fromstr(u'POINT(5 23)') self.assertIsInstance(g1.ogr, gdal.OGRGeometry) self.assertIsNone(g1.srs) if GEOS_PREPARE: g1_3d = fromstr(u'POINT(5 23 8)') self.assertIsInstance(g1_3d.ogr, gdal.OGRGeometry) self.assertEqual(g1_3d.ogr.z, 8) g2 = fromstr(u'LINESTRING(0 0, 5 5, 23 23)', srid=4326) self.assertIsInstance(g2.ogr, gdal.OGRGeometry) self.assertIsInstance(g2.srs, gdal.SpatialReference) self.assertEqual(g2.hex, g2.ogr.hex) self.assertEqual(u'WGS 84', g2.srs.name)
'Testing use with the Python `copy` module.'
def test_copy(self):
import copy poly = GEOSGeometry(u'POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))') cpy1 = copy.copy(poly) cpy2 = copy.deepcopy(poly) self.assertNotEqual(poly._ptr, cpy1._ptr) self.assertNotEqual(poly._ptr, cpy2._ptr)
'Testing `transform` method.'
@unittest.skipUnless(gdal.HAS_GDAL, u'gdal is required to transform geometries') def test_transform(self):
orig = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) trans = GEOSGeometry(u'POINT (992385.4472045 481455.4944650)', 2774) (t1, t2, t3) = (orig.clone(), orig.clone(), orig.clone()) t1.transform(trans.srid) t2.transform(gdal.SpatialReference(u'EPSG:2774')) ct = gdal.CoordTransform(gdal.SpatialReference(u'WGS84'), gdal.SpatialReference(2774)) t3.transform(ct) k1 = orig.clone() k2 = k1.transform(trans.srid, clone=True) self.assertEqual(k1, orig) self.assertNotEqual(k1, k2) prec = 3 for p in (t1, t2, t3, k2): self.assertAlmostEqual(trans.x, p.x, prec) self.assertAlmostEqual(trans.y, p.y, prec)
'Testing `transform` method (SRID match)'
def test_transform_noop(self):
if gdal.HAS_GDAL: g = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) gt = g.tuple g.transform(4326) self.assertEqual(g.tuple, gt) self.assertEqual(g.srid, 4326) g = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) g1 = g.transform(4326, clone=True) self.assertEqual(g1.tuple, g.tuple) self.assertEqual(g1.srid, 4326) self.assertTrue((g1 is not g), u"Clone didn't happen") old_has_gdal = gdal.HAS_GDAL try: gdal.HAS_GDAL = False g = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) gt = g.tuple g.transform(4326) self.assertEqual(g.tuple, gt) self.assertEqual(g.srid, 4326) g = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) g1 = g.transform(4326, clone=True) self.assertEqual(g1.tuple, g.tuple) self.assertEqual(g1.srid, 4326) self.assertTrue((g1 is not g), u"Clone didn't happen") finally: gdal.HAS_GDAL = old_has_gdal
'Testing `transform` method (no SRID or negative SRID)'
def test_transform_nosrid(self):
g = GEOSGeometry(u'POINT (-104.609 38.255)', srid=None) self.assertRaises(GEOSException, g.transform, 2774) g = GEOSGeometry(u'POINT (-104.609 38.255)', srid=None) self.assertRaises(GEOSException, g.transform, 2774, clone=True) g = GEOSGeometry(u'POINT (-104.609 38.255)', srid=(-1)) self.assertRaises(GEOSException, g.transform, 2774) g = GEOSGeometry(u'POINT (-104.609 38.255)', srid=(-1)) self.assertRaises(GEOSException, g.transform, 2774, clone=True)
'Testing `transform` method (GDAL not available)'
def test_transform_nogdal(self):
old_has_gdal = gdal.HAS_GDAL try: gdal.HAS_GDAL = False g = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) self.assertRaises(GEOSException, g.transform, 2774) g = GEOSGeometry(u'POINT (-104.609 38.255)', 4326) self.assertRaises(GEOSException, g.transform, 2774, clone=True) finally: gdal.HAS_GDAL = old_has_gdal
'Testing `extent` method.'
def test_extent(self):
mp = MultiPoint(Point(5, 23), Point(0, 0), Point(10, 50)) self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) pnt = Point(5.23, 17.8) self.assertEqual((5.23, 17.8, 5.23, 17.8), pnt.extent) poly = fromstr(self.geometries.polygons[3].wkt) ring = poly.shell (x, y) = (ring.x, ring.y) (xmin, ymin) = (min(x), min(y)) (xmax, ymax) = (max(x), max(y)) self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
'Testing pickling and unpickling support.'
def test_pickle(self):
from django.utils.six.moves import cPickle import pickle def get_geoms(lst, srid=None): return [GEOSGeometry(tg.wkt, srid) for tg in lst] tgeoms = get_geoms(self.geometries.points) tgeoms.extend(get_geoms(self.geometries.multilinestrings, 4326)) tgeoms.extend(get_geoms(self.geometries.polygons, 3084)) tgeoms.extend(get_geoms(self.geometries.multipolygons, 900913)) no_srid = (self.null_srid == (-1)) for geom in tgeoms: (s1, s2) = (cPickle.dumps(geom), pickle.dumps(geom)) (g1, g2) = (cPickle.loads(s1), pickle.loads(s2)) for tmpg in (g1, g2): self.assertEqual(geom, tmpg) if (not no_srid): self.assertEqual(geom.srid, tmpg.srid)
'Testing PreparedGeometry support.'
@unittest.skipUnless(GEOS_PREPARE, u'geos >= 3.1.0 is required') def test_prepared(self):
mpoly = GEOSGeometry(u'MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))') prep = mpoly.prepared pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)] covers = [True, True, False] for (pnt, c) in zip(pnts, covers): self.assertEqual(mpoly.contains(pnt), prep.contains(pnt)) self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt)) self.assertEqual(c, prep.covers(pnt))
'Testing line merge support'
def test_line_merge(self):
ref_geoms = (fromstr(u'LINESTRING(1 1, 1 1, 3 3)'), fromstr(u'MULTILINESTRING((1 1, 3 3), (3 3, 4 2))')) ref_merged = (fromstr(u'LINESTRING(1 1, 3 3)'), fromstr(u'LINESTRING (1 1, 3 3, 4 2)')) for (geom, merged) in zip(ref_geoms, ref_merged): self.assertEqual(merged, geom.merged)
'Testing IsValidReason support'
@unittest.skipUnless(GEOS_PREPARE, u'geos >= 3.1.0 is required') def test_valid_reason(self):
g = GEOSGeometry(u'POINT(0 0)') self.assertTrue(g.valid) self.assertIsInstance(g.valid_reason, six.string_types) self.assertEqual(g.valid_reason, u'Valid Geometry') g = GEOSGeometry(u'LINESTRING(0 0, 0 0)') self.assertFalse(g.valid) self.assertIsInstance(g.valid_reason, six.string_types) self.assertTrue(g.valid_reason.startswith(u'Too few points in geometry component'))
'Testing linear referencing'
@unittest.skipUnless((geos_version_info()[u'version'] >= u'3.2.0'), u'geos >= 3.2.0 is required') def test_linearref(self):
ls = fromstr(u'LINESTRING(0 0, 0 10, 10 10, 10 0)') mls = fromstr(u'MULTILINESTRING((0 0, 0 10), (10 0, 10 10))') self.assertEqual(ls.project(Point(0, 20)), 10.0) self.assertEqual(ls.project(Point(7, 6)), 24) self.assertEqual(ls.project_normalized(Point(0, 20)), (1.0 / 3)) self.assertEqual(ls.interpolate(10), Point(0, 10)) self.assertEqual(ls.interpolate(24), Point(10, 6)) self.assertEqual(ls.interpolate_normalized((1.0 / 3)), Point(0, 10)) self.assertEqual(mls.project(Point(0, 20)), 10) self.assertEqual(mls.project(Point(7, 6)), 16) self.assertEqual(mls.interpolate(9), Point(0, 9)) self.assertEqual(mls.interpolate(17), Point(10, 7))
'Testing the GEOS version regular expression.'
def test_geos_version(self):
from django.contrib.gis.geos.libgeos import version_regex versions = [(u'3.0.0rc4-CAPI-1.3.3', u'3.0.0'), (u'3.0.0-CAPI-1.4.1', u'3.0.0'), (u'3.4.0dev-CAPI-1.8.0', u'3.4.0')] for (v, expected) in versions: m = version_regex.match(v) self.assertTrue(m) self.assertEqual(m.group(u'version'), expected)
'The base constructor for GEOS geometry objects, and may take the following inputs: * strings: - WKT - HEXEWKB (a PostGIS-specific canonical form) - GeoJSON (requires GDAL) * buffer: - WKB The `srid` keyword is used to specify the Source Reference Identifier (SRID) number for this Geometry. If not set, the SRID will be None.'
def __init__(self, geo_input, srid=None):
if isinstance(geo_input, bytes): geo_input = force_text(geo_input) if isinstance(geo_input, six.string_types): wkt_m = wkt_regex.match(geo_input) if wkt_m: if wkt_m.group(u'srid'): srid = int(wkt_m.group(u'srid')) g = wkt_r().read(force_bytes(wkt_m.group(u'wkt'))) elif hex_regex.match(geo_input): g = wkb_r().read(force_bytes(geo_input)) elif (gdal.HAS_GDAL and json_regex.match(geo_input)): g = wkb_r().read(gdal.OGRGeometry(geo_input).wkb) else: raise ValueError(u'String or unicode input unrecognized as WKT EWKT, and HEXEWKB.') elif isinstance(geo_input, GEOM_PTR): g = geo_input elif isinstance(geo_input, memoryview): g = wkb_r().read(geo_input) elif isinstance(geo_input, GEOSGeometry): g = capi.geom_clone(geo_input.ptr) else: raise TypeError((u'Improper geometry input type: %s' % str(type(geo_input)))) if bool(g): self.ptr = g else: raise GEOSException(u'Could not initialize GEOS Geometry with given input.') self._post_init(srid)
'Helper routine for performing post-initialization setup.'
def _post_init(self, srid):
if (srid and isinstance(srid, int)): self.srid = srid self.__class__ = GEOS_CLASSES[self.geom_typeid] self._set_cs()
'Destroys this Geometry; in other words, frees the memory used by the GEOS C++ object.'
def __del__(self):
if self._ptr: capi.destroy_geom(self._ptr)
'Returns a clone because the copy of a GEOSGeometry may contain an invalid pointer location if the original is garbage collected.'
def __copy__(self):
return self.clone()
'The `deepcopy` routine is used by the `Node` class of django.utils.tree; thus, the protocol routine needs to be implemented to return correct copies (clones) of these GEOS objects, which use C pointers.'
def __deepcopy__(self, memodict):
return self.clone()
'WKT is used for the string representation.'
def __str__(self):
return self.wkt
'Short-hand representation because WKT may be very large.'
def __repr__(self):
return (u'<%s object at %s>' % (self.geom_type, hex(addressof(self.ptr))))
'Equivalence testing, a Geometry may be compared with another Geometry or a WKT representation.'
def __eq__(self, other):
if isinstance(other, six.string_types): return (self.wkt == other) elif isinstance(other, GEOSGeometry): return self.equals_exact(other) else: return False
'The not equals operator.'
def __ne__(self, other):
return (not (self == other))
'Returns the union of this Geometry and the other.'
def __or__(self, other):
return self.union(other)
'Returns the intersection of this Geometry and the other.'
def __and__(self, other):
return self.intersection(other)
'Return the difference this Geometry and the other.'
def __sub__(self, other):
return self.difference(other)
'Return the symmetric difference of this Geometry and the other.'
def __xor__(self, other):
return self.sym_difference(other)
'Returns True if this Geometry has a coordinate sequence, False if not.'
@property def has_cs(self):
if isinstance(self, (Point, LineString, LinearRing)): return True else: return False
'Sets the coordinate sequence for this Geometry.'
def _set_cs(self):
if self.has_cs: self._cs = GEOSCoordSeq(capi.get_cs(self.ptr), self.hasz) else: self._cs = None
'Returns a clone of the coordinate sequence for this Geometry.'
@property def coord_seq(self):
if self.has_cs: return self._cs.clone()
'Returns a string representing the Geometry type, e.g. \'Polygon\''
@property def geom_type(self):
return capi.geos_type(self.ptr).decode()
'Returns an integer representing the Geometry type.'
@property def geom_typeid(self):
return capi.geos_typeid(self.ptr)
'Returns the number of geometries in the Geometry.'
@property def num_geom(self):
return capi.get_num_geoms(self.ptr)
'Returns the number of coordinates in the Geometry.'
@property def num_coords(self):
return capi.get_num_coords(self.ptr)
'Returns the number points, or coordinates, in the Geometry.'
@property def num_points(self):
return self.num_coords
'Returns the dimension of this Geometry (0=point, 1=line, 2=surface).'
@property def dims(self):
return capi.get_dims(self.ptr)
'Converts this Geometry to normal form (or canonical form).'
def normalize(self):
return capi.geos_normalize(self.ptr)
'Returns a boolean indicating whether the set of points in this Geometry are empty.'
@property def empty(self):
return capi.geos_isempty(self.ptr)
'Returns whether the geometry has a 3D dimension.'
@property def hasz(self):
return capi.geos_hasz(self.ptr)
'Returns whether or not the geometry is a ring.'
@property def ring(self):
return capi.geos_isring(self.ptr)
'Returns false if the Geometry not simple.'
@property def simple(self):
return capi.geos_issimple(self.ptr)
'This property tests the validity of this Geometry.'
@property def valid(self):
return capi.geos_isvalid(self.ptr)
'Returns a string containing the reason for any invalidity.'
@property def valid_reason(self):
if (not GEOS_PREPARE): raise GEOSException(u'Upgrade GEOS to 3.1 to get validity reason.') return capi.geos_isvalidreason(self.ptr).decode()
'Returns true if other.within(this) returns true.'
def contains(self, other):
return capi.geos_contains(self.ptr, other.ptr)
'Returns true if the DE-9IM intersection matrix for the two Geometries is T*T****** (for a point and a curve,a point and an area or a line and an area) 0******** (for two curves).'
def crosses(self, other):
return capi.geos_crosses(self.ptr, other.ptr)
'Returns true if the DE-9IM intersection matrix for the two Geometries is FF*FF****.'
def disjoint(self, other):
return capi.geos_disjoint(self.ptr, other.ptr)
'Returns true if the DE-9IM intersection matrix for the two Geometries is T*F**FFF*.'
def equals(self, other):
return capi.geos_equals(self.ptr, other.ptr)
'Returns true if the two Geometries are exactly equal, up to a specified tolerance.'
def equals_exact(self, other, tolerance=0):
return capi.geos_equalsexact(self.ptr, other.ptr, float(tolerance))
'Returns true if disjoint returns false.'
def intersects(self, other):
return capi.geos_intersects(self.ptr, other.ptr)
'Returns true if the DE-9IM intersection matrix for the two Geometries is T*T***T** (for two points or two surfaces) 1*T***T** (for two curves).'
def overlaps(self, other):
return capi.geos_overlaps(self.ptr, other.ptr)
'Returns true if the elements in the DE-9IM intersection matrix for the two Geometries match the elements in pattern.'
def relate_pattern(self, other, pattern):
if ((not isinstance(pattern, six.string_types)) or (len(pattern) > 9)): raise GEOSException(u'invalid intersection matrix pattern') return capi.geos_relatepattern(self.ptr, other.ptr, force_bytes(pattern))
'Returns true if the DE-9IM intersection matrix for the two Geometries is FT*******, F**T***** or F***T****.'
def touches(self, other):
return capi.geos_touches(self.ptr, other.ptr)
'Returns true if the DE-9IM intersection matrix for the two Geometries is T*F**F***.'
def within(self, other):
return capi.geos_within(self.ptr, other.ptr)
'Gets the SRID for the geometry, returns None if no SRID is set.'
def get_srid(self):
s = capi.geos_get_srid(self.ptr) if (s == 0): return None else: return s
'Sets the SRID for the geometry.'
def set_srid(self, srid):
capi.geos_set_srid(self.ptr, srid)
'Returns the EWKT (WKT + SRID) of the Geometry. Note that Z values are *not* included in this representation because GEOS does not yet support serializing them.'
@property def ewkt(self):
if self.get_srid(): return (u'SRID=%s;%s' % (self.srid, self.wkt)) else: return self.wkt
'Returns the WKT (Well-Known Text) representation of this Geometry.'
@property def wkt(self):
return wkt_w().write(self).decode()
'Returns the WKB of this Geometry in hexadecimal form. Please note that the SRID is not included in this representation because it is not a part of the OGC specification (use the `hexewkb` property instead).'
@property def hex(self):
return wkb_w(((self.hasz and 3) or 2)).write_hex(self)
'Returns the EWKB of this Geometry in hexadecimal form. This is an extension of the WKB specification that includes SRID value that are a part of this geometry.'
@property def hexewkb(self):
if (self.hasz and (not GEOS_PREPARE)): raise GEOSException(u'Upgrade GEOS to 3.1 to get valid 3D HEXEWKB.') return ewkb_w(((self.hasz and 3) or 2)).write_hex(self)
'Returns GeoJSON representation of this Geometry if GDAL is installed.'
@property def json(self):
if gdal.HAS_GDAL: return self.ogr.json else: raise GEOSException(u'GeoJSON output only supported when GDAL is installed.')
'Returns the WKB (Well-Known Binary) representation of this Geometry as a Python buffer. SRID and Z values are not included, use the `ewkb` property instead.'
@property def wkb(self):
return wkb_w(((self.hasz and 3) or 2)).write(self)
'Return the EWKB representation of this Geometry as a Python buffer. This is an extension of the WKB specification that includes any SRID value that are a part of this geometry.'
@property def ewkb(self):
if (self.hasz and (not GEOS_PREPARE)): raise GEOSException(u'Upgrade GEOS to 3.1 to get valid 3D EWKB.') return ewkb_w(((self.hasz and 3) or 2)).write(self)
'Returns the KML representation of this Geometry.'
@property def kml(self):
gtype = self.geom_type return (u'<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype))
'Returns a PreparedGeometry corresponding to this geometry -- it is optimized for the contains, intersects, and covers operations.'
@property def prepared(self):
if GEOS_PREPARE: return PreparedGeometry(self) else: raise GEOSException(u'GEOS 3.1+ required for prepared geometry support.')
'Returns the OGR Geometry for this Geometry.'
@property def ogr(self):
if gdal.HAS_GDAL: if self.srid: return gdal.OGRGeometry(self.wkb, self.srid) else: return gdal.OGRGeometry(self.wkb) else: raise GEOSException(u'GDAL required to convert to an OGRGeometry.')
'Returns the OSR SpatialReference for SRID of this Geometry.'
@property def srs(self):
if gdal.HAS_GDAL: if self.srid: return gdal.SpatialReference(self.srid) else: return None else: raise GEOSException(u'GDAL required to return a SpatialReference object.')
'Alias for `srs` property.'
@property def crs(self):
return self.srs
'Requires GDAL. Transforms the geometry according to the given transformation object, which may be an integer SRID, and WKT or PROJ.4 string. By default, the geometry is transformed in-place and nothing is returned. However if the `clone` keyword is set, then this geometry will not be modified and a transformed clone will be returned instead.'
def transform(self, ct, clone=False):
srid = self.srid if (ct == srid): if clone: return self.clone() else: return if ((srid is None) or (srid < 0)): raise GEOSException(u'Calling transform() with no SRID set is not supported') if (not gdal.HAS_GDAL): raise GEOSException(u'GDAL library is not available to transform() geometry.') g = self.ogr g.transform(ct) ptr = wkb_r().read(g.wkb) if clone: return GEOSGeometry(ptr, srid=g.srid) if ptr: capi.destroy_geom(self.ptr) self.ptr = ptr self._post_init(g.srid) else: raise GEOSException(u'Transformed WKB was invalid.')
'Helper routine to return Geometry from the given pointer.'
def _topology(self, gptr):
return GEOSGeometry(gptr, srid=self.srid)
'Returns the boundary as a newly allocated Geometry object.'
@property def boundary(self):
return self._topology(capi.geos_boundary(self.ptr))
'Returns a geometry that represents all points whose distance from this Geometry is less than or equal to distance. Calculations are in the Spatial Reference System of this Geometry. The optional third parameter sets the number of segment used to approximate a quarter circle (defaults to 8). (Text from PostGIS documentation at ch. 6.1.3)'
def buffer(self, width, quadsegs=8):
return self._topology(capi.geos_buffer(self.ptr, width, quadsegs))
'The centroid is equal to the centroid of the set of component Geometries of highest dimension (since the lower-dimension geometries contribute zero "weight" to the centroid).'
@property def centroid(self):
return self._topology(capi.geos_centroid(self.ptr))
'Returns the smallest convex Polygon that contains all the points in the Geometry.'
@property def convex_hull(self):
return self._topology(capi.geos_convexhull(self.ptr))
'Returns a Geometry representing the points making up this Geometry that do not make up other.'
def difference(self, other):
return self._topology(capi.geos_difference(self.ptr, other.ptr))
'Return the envelope for this geometry (a polygon).'
@property def envelope(self):
return self._topology(capi.geos_envelope(self.ptr))
'Returns a Geometry representing the points shared by this Geometry and other.'
def intersection(self, other):
return self._topology(capi.geos_intersection(self.ptr, other.ptr))
'Computes an interior point of this Geometry.'
@property def point_on_surface(self):
return self._topology(capi.geos_pointonsurface(self.ptr))
'Returns the DE-9IM intersection matrix for this Geometry and the other.'
def relate(self, other):
return capi.geos_relate(self.ptr, other.ptr).decode()
'Returns the Geometry, simplified using the Douglas-Peucker algorithm to the specified tolerance (higher tolerance => less points). If no tolerance provided, defaults to 0. By default, this function does not preserve topology - e.g. polygons can be split, collapse to lines or disappear holes can be created or disappear, and lines can cross. By specifying preserve_topology=True, the result will have the same dimension and number of components as the input. This is significantly slower.'
def simplify(self, tolerance=0.0, preserve_topology=False):
if preserve_topology: return self._topology(capi.geos_preservesimplify(self.ptr, tolerance)) else: return self._topology(capi.geos_simplify(self.ptr, tolerance))
'Returns a set combining the points in this Geometry not in other, and the points in other not in this Geometry.'
def sym_difference(self, other):
return self._topology(capi.geos_symdifference(self.ptr, other.ptr))
'Returns a Geometry representing all the points in this Geometry and other.'
def union(self, other):
return self._topology(capi.geos_union(self.ptr, other.ptr))
'Returns the area of the Geometry.'
@property def area(self):
return capi.geos_area(self.ptr, byref(c_double()))
'Returns the distance between the closest points on this Geometry and the other. Units will be in those of the coordinate system of the Geometry.'
def distance(self, other):
if (not isinstance(other, GEOSGeometry)): raise TypeError(u'distance() works only on other GEOS Geometries.') return capi.geos_distance(self.ptr, other.ptr, byref(c_double()))
'Returns the extent of this geometry as a 4-tuple, consisting of (xmin, ymin, xmax, ymax).'
@property def extent(self):
env = self.envelope if isinstance(env, Point): (xmin, ymin) = env.tuple (xmax, ymax) = (xmin, ymin) else: (xmin, ymin) = env[0][0] (xmax, ymax) = env[0][2] return (xmin, ymin, xmax, ymax)
'Returns the length of this Geometry (e.g., 0 for point, or the circumfrence of a Polygon).'
@property def length(self):
return capi.geos_length(self.ptr, byref(c_double()))
'Clones this Geometry.'
def clone(self):
return GEOSGeometry(capi.geom_clone(self.ptr), srid=self.srid)
'Returns a _pointer_ to C GEOS Geometry object from the given WKB.'
def read(self, wkb):
if isinstance(wkb, memoryview): wkb_s = bytes(wkb) return wkb_reader_read(self.ptr, wkb_s, len(wkb_s)) elif isinstance(wkb, (bytes, six.string_types)): return wkb_reader_read_hex(self.ptr, wkb, len(wkb)) else: raise TypeError