desc
stringlengths
3
26.7k
decl
stringlengths
11
7.89k
bodies
stringlengths
8
553k
'Testing HEX input/output.'
def test01c_hex(self):
for g in self.geometries.hex_wkt: geom1 = OGRGeometry(g.wkt) self.assertEqual(g.hex.encode(), geom1.hex) geom2 = OGRGeometry(g.hex) self.assertEqual(geom1, geom2)
'Testing WKB input/output.'
def test01d_wkb(self):
for g in self.geometries.hex_wkt: geom1 = OGRGeometry(g.wkt) wkb = geom1.wkb self.assertEqual(b2a_hex(wkb).upper(), g.hex.encode()) geom2 = OGRGeometry(wkb) self.assertEqual(geom1, geom2)
'Testing GeoJSON input/output.'
def test01e_json(self):
for g in self.geometries.json_geoms: geom = OGRGeometry(g.wkt) if (not hasattr(g, 'not_equal')): self.assertEqual(json.loads(g.json), json.loads(geom.json)) self.assertEqual(json.loads(g.json), json.loads(geom.geojson)) self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
'Testing Point objects.'
def test02_points(self):
prev = OGRGeometry('POINT(0 0)') for p in self.geometries.points: if (not hasattr(p, 'z')): pnt = OGRGeometry(p.wkt) self.assertEqual(1, pnt.geom_type) self.assertEqual('POINT', pnt.geom_name) self.assertEqual(p.x, pnt.x) self.assertEqual(p.y, pnt.y) self.assertEqual((p.x, p.y), pnt.tuple)
'Testing MultiPoint objects.'
def test03_multipoints(self):
for mp in self.geometries.multipoints: mgeom1 = OGRGeometry(mp.wkt) self.assertEqual(4, mgeom1.geom_type) self.assertEqual('MULTIPOINT', mgeom1.geom_name) mgeom2 = OGRGeometry('MULTIPOINT') mgeom3 = OGRGeometry('MULTIPOINT') for g in mgeom1: mgeom2.add(g) mgeom3.add(g.wkt) self.assertEqual(mgeom1, mgeom2) self.assertEqual(mgeom1, mgeom3) self.assertEqual(mp.coords, mgeom2.coords) self.assertEqual(mp.n_p, mgeom2.point_count)
'Testing LineString objects.'
def test04_linestring(self):
prev = OGRGeometry('POINT(0 0)') for ls in self.geometries.linestrings: linestr = OGRGeometry(ls.wkt) self.assertEqual(2, linestr.geom_type) self.assertEqual('LINESTRING', linestr.geom_name) self.assertEqual(ls.n_p, linestr.point_count) self.assertEqual(ls.coords, linestr.tuple) self.assertEqual(True, (linestr == OGRGeometry(ls.wkt))) self.assertEqual(True, (linestr != prev)) self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr)) prev = linestr x = [tmpx for (tmpx, tmpy) in ls.coords] y = [tmpy for (tmpx, tmpy) in ls.coords] self.assertEqual(x, linestr.x) self.assertEqual(y, linestr.y)
'Testing MultiLineString objects.'
def test05_multilinestring(self):
prev = OGRGeometry('POINT(0 0)') for mls in self.geometries.multilinestrings: mlinestr = OGRGeometry(mls.wkt) self.assertEqual(5, mlinestr.geom_type) self.assertEqual('MULTILINESTRING', mlinestr.geom_name) self.assertEqual(mls.n_p, mlinestr.point_count) self.assertEqual(mls.coords, mlinestr.tuple) self.assertEqual(True, (mlinestr == OGRGeometry(mls.wkt))) self.assertEqual(True, (mlinestr != prev)) prev = mlinestr for ls in mlinestr: self.assertEqual(2, ls.geom_type) self.assertEqual('LINESTRING', ls.geom_name) self.assertRaises(OGRIndexError, mlinestr.__getitem__, len(mlinestr))
'Testing LinearRing objects.'
def test06_linearring(self):
prev = OGRGeometry('POINT(0 0)') for rr in self.geometries.linearrings: lr = OGRGeometry(rr.wkt) self.assertEqual('LINEARRING', lr.geom_name) self.assertEqual(rr.n_p, len(lr)) self.assertEqual(True, (lr == OGRGeometry(rr.wkt))) self.assertEqual(True, (lr != prev)) prev = lr
'Testing Polygon objects.'
def test07a_polygons(self):
bbox = ((-180), (-90), 180, 90) p = OGRGeometry.from_bbox(bbox) self.assertEqual(bbox, p.extent) prev = OGRGeometry('POINT(0 0)') for p in self.geometries.polygons: poly = OGRGeometry(p.wkt) self.assertEqual(3, poly.geom_type) self.assertEqual('POLYGON', poly.geom_name) self.assertEqual(p.n_p, poly.point_count) self.assertEqual((p.n_i + 1), len(poly)) self.assertAlmostEqual(p.area, poly.area, 9) (x, y) = poly.centroid.tuple self.assertAlmostEqual(p.centroid[0], x, 9) self.assertAlmostEqual(p.centroid[1], y, 9) self.assertEqual(True, (poly == OGRGeometry(p.wkt))) self.assertEqual(True, (poly != prev)) if p.ext_ring_cs: ring = poly[0] self.assertEqual(p.ext_ring_cs, ring.tuple) self.assertEqual(p.ext_ring_cs, poly[0].tuple) self.assertEqual(len(p.ext_ring_cs), ring.point_count) for r in poly: self.assertEqual('LINEARRING', r.geom_name)
'Testing closing Polygon objects.'
def test07b_closepolygons(self):
poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))') self.assertEqual(8, poly.point_count) with self.assertRaises(OGRException): _ = poly.centroid poly.close_rings() self.assertEqual(10, poly.point_count) self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
'Testing MultiPolygon objects.'
def test08_multipolygons(self):
prev = OGRGeometry('POINT(0 0)') for mp in self.geometries.multipolygons: mpoly = OGRGeometry(mp.wkt) self.assertEqual(6, mpoly.geom_type) self.assertEqual('MULTIPOLYGON', mpoly.geom_name) if mp.valid: self.assertEqual(mp.n_p, mpoly.point_count) self.assertEqual(mp.num_geom, len(mpoly)) self.assertRaises(OGRIndexError, mpoly.__getitem__, len(mpoly)) for p in mpoly: self.assertEqual('POLYGON', p.geom_name) self.assertEqual(3, p.geom_type) self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt)
'Testing OGR Geometries with Spatial Reference objects.'
def test09a_srs(self):
for mp in self.geometries.multipolygons: sr = SpatialReference('WGS84') mpoly = OGRGeometry(mp.wkt, sr) self.assertEqual(sr.wkt, mpoly.srs.wkt) klone = mpoly.clone() self.assertEqual(sr.wkt, klone.srs.wkt) for poly in mpoly: self.assertEqual(sr.wkt, poly.srs.wkt) for ring in poly: self.assertEqual(sr.wkt, ring.srs.wkt) a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr) b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr) diff = a.difference(b) union = a.union(b) self.assertEqual(sr.wkt, diff.srs.wkt) self.assertEqual(sr.srid, union.srs.srid) mpoly = OGRGeometry(mp.wkt, 4326) self.assertEqual(4326, mpoly.srid) mpoly.srs = SpatialReference(4269) self.assertEqual(4269, mpoly.srid) self.assertEqual('NAD83', mpoly.srs.name) for poly in mpoly: self.assertEqual(mpoly.srs.wkt, poly.srs.wkt) poly.srs = 32140 for ring in poly: self.assertEqual(32140, ring.srs.srid) self.assertEqual('NAD83 / Texas South Central', ring.srs.name) ring.srs = str(SpatialReference(4326)) self.assertEqual(4326, ring.srs.srid) ring.srid = 4322 self.assertEqual('WGS 72', ring.srs.name) self.assertEqual(4322, ring.srid)
'Testing transform().'
def test09b_srs_transform(self):
orig = OGRGeometry('POINT (-104.609 38.255)', 4326) trans = OGRGeometry('POINT (992385.4472045 481455.4944650)', 2774) (t1, t2, t3) = (orig.clone(), orig.clone(), orig.clone()) t1.transform(trans.srid) t2.transform(SpatialReference('EPSG:2774')) ct = CoordTransform(SpatialReference('WGS84'), 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 coordinate dimension is the same on transformed geometries.'
def test09c_transform_dim(self):
ls_orig = OGRGeometry('LINESTRING(-104.609 38.255)', 4326) ls_trans = OGRGeometry('LINESTRING(992385.4472045 481455.4944650)', 2774) prec = 3 ls_orig.transform(ls_trans.srs) self.assertEqual(2, ls_orig.coord_dim) self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec) self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec)
'Testing difference().'
def test10_difference(self):
for i in xrange(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) d1 = OGRGeometry(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 intersects() and intersection().'
def test11_intersection(self):
for i in xrange(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) i1 = OGRGeometry(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 sym_difference().'
def test12_symdifference(self):
for i in xrange(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) d1 = OGRGeometry(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 union().'
def test13_union(self):
for i in xrange(len(self.geometries.topology_geoms)): a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a) b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b) u1 = OGRGeometry(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 GeometryCollection.add().'
def test14_add(self):
mp = OGRGeometry('MultiPolygon') pnt = OGRGeometry('POINT(5 23)') self.assertRaises(OGRException, mp.add, pnt) for mp in self.geometries.multipolygons: mpoly = OGRGeometry(mp.wkt) mp1 = OGRGeometry('MultiPolygon') mp2 = OGRGeometry('MultiPolygon') mp3 = OGRGeometry('MultiPolygon') for poly in mpoly: mp1.add(poly) mp2.add(poly.wkt) mp3.add(mpoly) for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp)
'Testing `extent` property.'
def test15_extent(self):
mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)') self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent) poly = OGRGeometry(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 2.5D geometries.'
def test16_25D(self):
pnt_25d = OGRGeometry('POINT(1 2 3)') self.assertEqual('Point25D', pnt_25d.geom_type.name) self.assertEqual(3.0, pnt_25d.z) self.assertEqual(3, pnt_25d.coord_dim) ls_25d = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)') self.assertEqual('LineString25D', ls_25d.geom_type.name) self.assertEqual([1.0, 2.0, 3.0], ls_25d.z) self.assertEqual(3, ls_25d.coord_dim)
'Testing pickle support.'
def test17_pickle(self):
g1 = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)', 'WGS84') g2 = pickle.loads(pickle.dumps(g1)) self.assertEqual(g1, g2) self.assertEqual(4326, g2.srs.srid) self.assertEqual(g1.srs.wkt, g2.srs.wkt)
'Testing coordinate dimensions on geometries after transformation.'
def test18_ogrgeometry_transform_workaround(self):
wkt_2d = 'MULTILINESTRING ((0 0,1 1,2 2))' wkt_3d = 'MULTILINESTRING ((0 0 0,1 1 1,2 2 2))' srid = 4326 geom = OGRGeometry(wkt_2d, srid) geom.transform(srid) self.assertEqual(2, geom.coord_dim) self.assertEqual(2, geom[0].coord_dim) self.assertEqual(wkt_2d, geom.wkt) geom = OGRGeometry(wkt_3d, srid) geom.transform(srid) self.assertEqual(3, geom.coord_dim) self.assertEqual(3, geom[0].coord_dim) self.assertEqual(wkt_3d, geom.wkt)
'Testing equivalence methods with non-OGRGeometry instances.'
def test19_equivalence_regression(self):
self.assertNotEqual(None, OGRGeometry('POINT(0 0)')) self.assertEqual(False, (OGRGeometry('LINESTRING(0 0, 1 1)') == 3))
'Testing initialization on valid OGC WKT.'
def test01_wkt(self):
for s in srlist: srs = SpatialReference(s.wkt)
'Testing initialization on invalid WKT.'
def test02_bad_wkt(self):
for bad in bad_srlist: try: srs = SpatialReference(bad) srs.validate() except (SRSException, OGRException): pass else: self.fail('Should not have initialized on bad WKT "%s"!')
'Testing getting the WKT.'
def test03_get_wkt(self):
for s in srlist: srs = SpatialReference(s.wkt) self.assertEqual(s.wkt, srs.wkt)
'Test PROJ.4 import and export.'
def test04_proj(self):
for s in srlist: if s.proj: srs1 = SpatialReference(s.wkt) srs2 = SpatialReference(s.proj) self.assertEqual(srs1.proj, srs2.proj)
'Test EPSG import.'
def test05_epsg(self):
for s in srlist: if s.epsg: srs1 = SpatialReference(s.wkt) srs2 = SpatialReference(s.epsg) srs3 = SpatialReference(str(s.epsg)) srs4 = SpatialReference(('EPSG:%d' % s.epsg)) for srs in (srs1, srs2, srs3, srs4): for (attr, expected) in s.attr: self.assertEqual(expected, srs[attr])
'Testing the boolean properties.'
def test07_boolean_props(self):
for s in srlist: srs = SpatialReference(s.wkt) self.assertEqual(s.projected, srs.projected) self.assertEqual(s.geographic, srs.geographic)
'Testing the linear and angular units routines.'
def test08_angular_linear(self):
for s in srlist: srs = SpatialReference(s.wkt) self.assertEqual(s.ang_name, srs.angular_name) self.assertEqual(s.lin_name, srs.linear_name) self.assertAlmostEqual(s.ang_units, srs.angular_units, 9) self.assertAlmostEqual(s.lin_units, srs.linear_units, 9)
'Testing the authority name & code routines.'
def test09_authority(self):
for s in srlist: if hasattr(s, 'auth'): srs = SpatialReference(s.wkt) for (target, tup) in s.auth.items(): self.assertEqual(tup[0], srs.auth_name(target)) self.assertEqual(tup[1], srs.auth_code(target))
'Testing the attribute retrieval routines.'
def test10_attributes(self):
for s in srlist: srs = SpatialReference(s.wkt) for tup in s.attr: att = tup[0] exp = tup[1] self.assertEqual(exp, srs[att])
'Testing Well Known Names of Spatial References.'
def test11_wellknown(self):
for s in well_known: srs = SpatialReference(s.wk) self.assertEqual(s.name, srs.name) for tup in s.attrs: if (len(tup) == 2): key = tup[0] exp = tup[1] elif (len(tup) == 3): key = tup[:2] exp = tup[2] self.assertEqual(srs[key], exp)
'Testing initialization of a CoordTransform.'
def test12_coordtransform(self):
target = SpatialReference('WGS84') for s in srlist: if s.proj: ct = CoordTransform(SpatialReference(s.wkt), target)
'Testing the attr_value() method.'
def test13_attr_value(self):
s1 = SpatialReference('WGS84') self.assertRaises(TypeError, s1.__getitem__, 0) self.assertRaises(TypeError, s1.__getitem__, ('GEOGCS', 'foo')) self.assertEqual('WGS 84', s1['GEOGCS']) self.assertEqual('WGS_1984', s1['DATUM']) self.assertEqual('EPSG', s1['AUTHORITY']) self.assertEqual(4326, int(s1[('AUTHORITY', 1)])) self.assertEqual(None, s1['FOOBAR'])
'Testing valid SHP Data Source files.'
def test01_valid_shp(self):
for source in ds_list: ds = DataSource(source.ds) self.assertEqual(1, len(ds)) self.assertEqual(source.ds, ds.name) self.assertEqual(source.driver, str(ds.driver)) try: ds[len(ds)] except OGRIndexError: pass else: self.fail('Expected an IndexError!')
'Testing invalid SHP files for the Data Source.'
def test02_invalid_shp(self):
for source in bad_ds: self.assertRaises(OGRException, DataSource, source.ds)
'Testing Data Source Layers.'
def test03a_layers(self):
for source in ds_list: ds = DataSource(source.ds) for layer in ds: self.assertEqual(len(layer), source.nfeat) self.assertEqual(source.nfld, layer.num_fields) self.assertEqual(source.nfld, len(layer.fields)) if ((source.driver == 'VRT') and ((GDAL_VERSION >= (1, 7, 0)) and (GDAL_VERSION < (1, 7, 3)))): pass else: self.assertEqual(True, isinstance(layer.extent, Envelope)) self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5) self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5) self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5) self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5) flds = layer.fields for f in flds: self.assertEqual(True, (f in source.fields)) self.assertRaises(OGRIndexError, layer.__getitem__, (-1)) self.assertRaises(OGRIndexError, layer.__getitem__, 50000) if hasattr(source, 'field_values'): fld_names = source.field_values.keys() for fld_name in fld_names: self.assertEqual(source.field_values[fld_name], layer.get_fields(fld_name)) for (i, fid) in enumerate(source.fids): feat = layer[fid] self.assertEqual(fid, feat.fid) for fld_name in fld_names: self.assertEqual(source.field_values[fld_name][i], feat.get(fld_name))
'Test indexing and slicing on Layers.'
def test03b_layer_slice(self):
source = ds_list[0] ds = DataSource(source.ds) sl = slice(1, 3) feats = ds[0][sl] for fld_name in ds[0].fields: test_vals = [feat.get(fld_name) for feat in feats] control_vals = source.field_values[fld_name][sl] self.assertEqual(control_vals, test_vals)
'Ensure OGR objects keep references to the objects they belong to.'
def test03c_layer_references(self):
source = ds_list[0] def get_layer(): ds = DataSource(source.ds) return ds[0] lyr = get_layer() self.assertEqual(source.nfeat, len(lyr)) self.assertEqual(source.gtype, lyr.geom_type.num) self.assertEqual(str(lyr[0]['str']), '1')
'Testing Data Source Features.'
def test04_features(self):
for source in ds_list: ds = DataSource(source.ds) for layer in ds: for feat in layer: self.assertEqual(source.nfld, len(list(feat))) self.assertEqual(source.gtype, feat.geom_type) for (k, v) in source.fields.items(): self.assertEqual(True, isinstance(feat[k], v)) for fld in feat: self.assertEqual(True, (fld.name in source.fields.keys()))
'Testing Geometries from Data Source Features.'
def test05_geometries(self):
for source in ds_list: ds = DataSource(source.ds) for layer in ds: for feat in layer: g = feat.geom self.assertEqual(source.geom, g.geom_name) self.assertEqual(source.gtype, g.geom_type) if hasattr(source, 'srs_wkt'): self.assertEqual(source.srs_wkt, g.srs.wkt.replace('SPHEROID["WGS_84"', 'SPHEROID["WGS_1984"'))
'Testing the Layer.spatial_filter property.'
def test06_spatial_filter(self):
ds = DataSource(get_ds_file('cities', 'shp')) lyr = ds[0] self.assertEqual(None, lyr.spatial_filter) self.assertRaises(TypeError, lyr._set_spatial_filter, 'foo') self.assertRaises(ValueError, lyr._set_spatial_filter, list(range(5))) filter_extent = ((-105.609252), 37.255001, (-103.609252), 39.255001) lyr.spatial_filter = ((-105.609252), 37.255001, (-103.609252), 39.255001) self.assertEqual(OGRGeometry.from_bbox(filter_extent), lyr.spatial_filter) feats = [feat for feat in lyr] self.assertEqual(1, len(feats)) self.assertEqual('Pueblo', feats[0].get('Name')) filter_geom = OGRGeometry('POLYGON((-96.363151 28.763374,-94.363151 28.763374,-94.363151 30.763374,-96.363151 30.763374,-96.363151 28.763374))') lyr.spatial_filter = filter_geom self.assertEqual(filter_geom, lyr.spatial_filter) feats = [feat for feat in lyr] self.assertEqual(1, len(feats)) self.assertEqual('Houston', feats[0].get('Name')) lyr.spatial_filter = None self.assertEqual(3, len(lyr))
'Testing that OFTReal fields, treated as OFTInteger, do not overflow.'
def test07_integer_overflow(self):
ds = DataSource(os.path.join(TEST_DATA, 'texas.dbf')) feat = ds[0][0] self.assertEqual(676586997978, feat.get('ALAND10'))
'Testing valid OGR Data Source Drivers.'
def test01_valid_driver(self):
for d in valid_drivers: dr = Driver(d) self.assertEqual(d, str(dr))
'Testing invalid OGR Data Source Drivers.'
def test02_invalid_driver(self):
for i in invalid_drivers: self.assertRaises(OGRException, Driver, i)
'Testing driver aliases.'
def test03_aliases(self):
for (alias, full_name) in aliases.items(): dr = Driver(alias) self.assertEqual(full_name, str(dr))
'Testing Envelope initilization.'
def test01_init(self):
e1 = Envelope((0, 0, 5, 5)) e2 = Envelope(0, 0, 5, 5) e3 = Envelope(0, '0', '5', 5) e4 = Envelope(e1._envelope) self.assertRaises(OGRException, Envelope, (5, 5, 0, 0)) self.assertRaises(OGRException, Envelope, 5, 5, 0, 0) self.assertRaises(OGRException, Envelope, (0, 0, 5, 5, 3)) self.assertRaises(OGRException, Envelope, ()) self.assertRaises(ValueError, Envelope, 0, 'a', 5, 5) self.assertRaises(TypeError, Envelope, 'foo') self.assertRaises(OGRException, Envelope, (1, 1, 0, 0)) try: Envelope(0, 0, 0, 0) except OGRException: self.fail("shouldn't raise an exception for min_x == max_x or min_y == max_y")
'Testing Envelope properties.'
def test02_properties(self):
e = Envelope(0, 0, 2, 3) self.assertEqual(0, e.min_x) self.assertEqual(0, e.min_y) self.assertEqual(2, e.max_x) self.assertEqual(3, e.max_y) self.assertEqual((0, 0), e.ll) self.assertEqual((2, 3), e.ur) self.assertEqual((0, 0, 2, 3), e.tuple) self.assertEqual('POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))', e.wkt) self.assertEqual('(0.0, 0.0, 2.0, 3.0)', str(e))
'Testing Envelope equivalence.'
def test03_equivalence(self):
e1 = Envelope(0.523, 0.217, 253.23, 523.69) e2 = Envelope((0.523, 0.217, 253.23, 523.69)) self.assertEqual(e1, e2) self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
'Testing Envelope expand_to_include -- point as two parameters.'
def test04_expand_to_include_pt_2_params(self):
self.e.expand_to_include(2, 6) self.assertEqual((0, 0, 5, 6), self.e) self.e.expand_to_include((-1), (-1)) self.assertEqual(((-1), (-1), 5, 6), self.e)
'Testing Envelope expand_to_include -- point as a single 2-tuple parameter.'
def test05_expand_to_include_pt_2_tuple(self):
self.e.expand_to_include((10, 10)) self.assertEqual((0, 0, 10, 10), self.e) self.e.expand_to_include(((-10), (-10))) self.assertEqual(((-10), (-10), 10, 10), self.e)
'Testing Envelope expand_to_include -- extent as 4 parameters.'
def test06_expand_to_include_extent_4_params(self):
self.e.expand_to_include((-1), 1, 3, 7) self.assertEqual(((-1), 0, 5, 7), self.e)
'Testing Envelope expand_to_include -- extent as a single 4-tuple parameter.'
def test06_expand_to_include_extent_4_tuple(self):
self.e.expand_to_include(((-1), 1, 3, 7)) self.assertEqual(((-1), 0, 5, 7), self.e)
'Testing Envelope expand_to_include with Envelope as parameter.'
def test07_expand_to_include_envelope(self):
self.e.expand_to_include(Envelope((-1), 1, 3, 7)) self.assertEqual(((-1), 0, 5, 7), self.e)
'Testing Envelope expand_to_include with Point as parameter.'
def test08_expand_to_include_point(self):
self.e.expand_to_include(TestPoint((-1), 1)) self.assertEqual(((-1), 0, 5, 5), self.e) self.e.expand_to_include(TestPoint(10, 10)) self.assertEqual(((-1), 0, 10, 10), self.e)
'Initializes Geometry on either WKT or an OGR pointer as input.'
def __init__(self, geom_input, srs=None):
str_instance = isinstance(geom_input, six.string_types) if (str_instance and hex_regex.match(geom_input)): geom_input = memoryview(a2b_hex(geom_input.upper().encode())) str_instance = False if str_instance: wkt_m = wkt_regex.match(geom_input) json_m = json_regex.match(geom_input) if wkt_m: if wkt_m.group('srid'): srs = int(wkt_m.group('srid')) if (wkt_m.group('type').upper() == 'LINEARRING'): g = capi.create_geom(OGRGeomType(wkt_m.group('type')).num) capi.import_wkt(g, byref(c_char_p(wkt_m.group('wkt').encode()))) else: g = capi.from_wkt(byref(c_char_p(wkt_m.group('wkt').encode())), None, byref(c_void_p())) elif json_m: g = capi.from_json(geom_input.encode()) else: ogr_t = OGRGeomType(geom_input) g = capi.create_geom(OGRGeomType(geom_input).num) elif isinstance(geom_input, memoryview): g = capi.from_wkb(bytes(geom_input), None, byref(c_void_p()), len(geom_input)) elif isinstance(geom_input, OGRGeomType): g = capi.create_geom(geom_input.num) elif isinstance(geom_input, self.ptr_type): g = geom_input else: raise OGRException(('Invalid input type for OGR Geometry construction: %s' % type(geom_input))) if (not g): raise OGRException(('Cannot create OGR Geometry from input: %s' % str(geom_input))) self.ptr = g if bool(srs): self.srs = srs self.__class__ = GEO_CLASSES[self.geom_type.num]
'Deletes this Geometry.'
def __del__(self):
if self._ptr: capi.destroy_geom(self._ptr)
'Constructs a Polygon from a bounding box (4-tuple).'
@classmethod def from_bbox(cls, bbox):
(x0, y0, x1, y1) = bbox return OGRGeometry(('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))' % (x0, y0, x0, y1, x1, y1, x1, y0, x0, y0)))
'Returns the union of the two geometries.'
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)
'Is this Geometry equal to the other?'
def __eq__(self, other):
if isinstance(other, OGRGeometry): return self.equals(other) else: return False
'Tests for inequality.'
def __ne__(self, other):
return (not (self == other))
'WKT is used for the string representation.'
def __str__(self):
return self.wkt
'Returns 0 for points, 1 for lines, and 2 for surfaces.'
@property def dimension(self):
return capi.get_dims(self.ptr)
'Returns the coordinate dimension of the Geometry.'
def _get_coord_dim(self):
if (isinstance(self, GeometryCollection) and (GDAL_VERSION < (1, 5, 2))): if len(self): return capi.get_coord_dim(capi.get_geom_ref(self.ptr, 0)) return capi.get_coord_dim(self.ptr)
'Sets the coordinate dimension of this Geometry.'
def _set_coord_dim(self, dim):
if (not (dim in (2, 3))): raise ValueError('Geometry dimension must be either 2 or 3') capi.set_coord_dim(self.ptr, dim)
'The number of elements in this Geometry.'
@property def geom_count(self):
return capi.get_geom_count(self.ptr)
'Returns the number of Points in this Geometry.'
@property def point_count(self):
return capi.get_point_count(self.ptr)
'Alias for `point_count` (same name method in GEOS API.)'
@property def num_points(self):
return self.point_count
'Alais for `point_count`.'
@property def num_coords(self):
return self.point_count
'Returns the Type for this Geometry.'
@property def geom_type(self):
return OGRGeomType(capi.get_geom_type(self.ptr))
'Returns the Name of this Geometry.'
@property def geom_name(self):
return capi.get_geom_name(self.ptr)
'Returns the area for a LinearRing, Polygon, or MultiPolygon; 0 otherwise.'
@property def area(self):
return capi.get_area(self.ptr)
'Returns the envelope for this Geometry.'
@property def envelope(self):
return Envelope(capi.get_envelope(self.ptr, byref(OGREnvelope())))
'Returns the envelope as a 4-tuple, instead of as an Envelope object.'
@property def extent(self):
return self.envelope.tuple
'Returns the Spatial Reference for this Geometry.'
def _get_srs(self):
try: srs_ptr = capi.get_geom_srs(self.ptr) return SpatialReference(srs_api.clone_srs(srs_ptr)) except SRSException: return None
'Sets the SpatialReference for this geometry.'
def _set_srs(self, srs):
if isinstance(srs, SpatialReference): srs_ptr = srs.ptr elif isinstance(srs, (six.integer_types + six.string_types)): sr = SpatialReference(srs) srs_ptr = sr.ptr else: raise TypeError(('Cannot assign spatial reference with object of type: %s' % type(srs))) capi.assign_srs(self.ptr, srs_ptr)
'Returns a GEOSGeometry object from this OGRGeometry.'
@property def geos(self):
from django.contrib.gis.geos import GEOSGeometry return GEOSGeometry(self.wkb, self.srid)
'Returns the GML representation of the Geometry.'
@property def gml(self):
return capi.to_gml(self.ptr)
'Returns the hexadecimal representation of the WKB (a string).'
@property def hex(self):
return b2a_hex(self.wkb).upper()
'Returns the GeoJSON representation of this Geometry.'
@property def json(self):
return capi.to_json(self.ptr)
'Returns the KML representation of the Geometry.'
@property def kml(self):
return capi.to_kml(self.ptr, None)
'Returns the size of the WKB buffer.'
@property def wkb_size(self):
return capi.get_wkbsize(self.ptr)
'Returns the WKB representation of the Geometry.'
@property def wkb(self):
if (sys.byteorder == 'little'): byteorder = 1 else: byteorder = 0 sz = self.wkb_size buf = (c_ubyte * sz)() wkb = capi.to_wkb(self.ptr, byteorder, byref(buf)) return memoryview(string_at(buf, sz))
'Returns the WKT representation of the Geometry.'
@property def wkt(self):
return capi.to_wkt(self.ptr, byref(c_char_p()))
'Returns the EWKT representation of the Geometry.'
@property def ewkt(self):
srs = self.srs if (srs and srs.srid): return ('SRID=%s;%s' % (srs.srid, self.wkt)) else: return self.wkt
'Clones this OGR Geometry.'
def clone(self):
return OGRGeometry(capi.clone_geom(self.ptr), self.srs)
'If there are any rings within this geometry that have not been closed, this routine will do so by adding the starting point at the end.'
def close_rings(self):
capi.geom_close_rings(self.ptr)
'Transforms this geometry to a different spatial reference system. May take a CoordTransform object, a SpatialReference object, string WKT or PROJ.4, and/or an integer SRID. By default nothing is returned and the geometry is transformed in-place. However, if the `clone` keyword is set, then a transformed clone of this geometry will be returned.'
def transform(self, coord_trans, clone=False):
if clone: klone = self.clone() klone.transform(coord_trans) return klone if (GDAL_VERSION < (1, 7)): orig_dim = self.coord_dim if isinstance(coord_trans, CoordTransform): capi.geom_transform(self.ptr, coord_trans.ptr) elif isinstance(coord_trans, SpatialReference): capi.geom_transform_to(self.ptr, coord_trans.ptr) elif isinstance(coord_trans, (six.integer_types + six.string_types)): sr = SpatialReference(coord_trans) capi.geom_transform_to(self.ptr, sr.ptr) else: raise TypeError('Transform only accepts CoordTransform, SpatialReference, string, and integer objects.') if (GDAL_VERSION < (1, 7)): if isinstance(self, GeometryCollection): for i in xrange(len(self)): internal_ptr = capi.get_geom_ref(self.ptr, i) if (orig_dim != capi.get_coord_dim(internal_ptr)): capi.set_coord_dim(internal_ptr, orig_dim) elif (self.coord_dim != orig_dim): self.coord_dim = orig_dim
'For backwards-compatibility.'
def transform_to(self, srs):
self.transform(srs)
'A generalized function for topology operations, takes a GDAL function and the other geometry to perform the operation on.'
def _topology(self, func, other):
if (not isinstance(other, OGRGeometry)): raise TypeError('Must use another OGRGeometry object for topology operations!') return func(self.ptr, other.ptr)
'Returns True if this geometry intersects with the other.'
def intersects(self, other):
return self._topology(capi.ogr_intersects, other)
'Returns True if this geometry is equivalent to the other.'
def equals(self, other):
return self._topology(capi.ogr_equals, other)
'Returns True if this geometry and the other are spatially disjoint.'
def disjoint(self, other):
return self._topology(capi.ogr_disjoint, other)
'Returns True if this geometry touches the other.'
def touches(self, other):
return self._topology(capi.ogr_touches, other)
'Returns True if this geometry crosses the other.'
def crosses(self, other):
return self._topology(capi.ogr_crosses, other)