Ejemplo n.º 1
0
 def test_equals(self):
     self.assertIs(
         OGRGeometry('POINT(0 0)').contains(OGRGeometry('POINT(0 0)')),
         True)
     self.assertIs(
         OGRGeometry('POINT(0 0)').contains(OGRGeometry('POINT(0 1)')),
         False)
Ejemplo n.º 2
0
 def test_touches(self):
     self.assertIs(
         OGRGeometry('POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))').touches(
             OGRGeometry('LINESTRING(0 2, 2 0)')), True)
     self.assertIs(
         OGRGeometry('POINT(0 0)').touches(OGRGeometry('POINT(0 1)')),
         False)
Ejemplo n.º 3
0
 def test_disjoint(self):
     self.assertIs(
         OGRGeometry('LINESTRING(0 0, 1 1)').disjoint(
             OGRGeometry('LINESTRING(0 1, 1 0)')), False)
     self.assertIs(
         OGRGeometry('LINESTRING(0 0, 0 1)').disjoint(
             OGRGeometry('LINESTRING(1 0, 1 1)')), True)
Ejemplo n.º 4
0
 def test_crosses(self):
     self.assertIs(
         OGRGeometry('LINESTRING(0 0, 1 1)').crosses(
             OGRGeometry('LINESTRING(0 1, 1 0)')), True)
     self.assertIs(
         OGRGeometry('LINESTRING(0 0, 0 1)').crosses(
             OGRGeometry('LINESTRING(1 0, 1 1)')), False)
Ejemplo n.º 5
0
 def test_intersects(self):
     self.assertIs(
         OGRGeometry('LINESTRING(0 0, 1 1)').intersects(
             OGRGeometry('LINESTRING(0 1, 1 0)')), True)
     self.assertIs(
         OGRGeometry('LINESTRING(0 0, 0 1)').intersects(
             OGRGeometry('LINESTRING(1 0, 1 1)')), False)
Ejemplo n.º 6
0
 def test_overlaps(self):
     self.assertIs(
         OGRGeometry('POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))').overlaps(
             OGRGeometry('POLYGON ((1 1, 1 5, 5 5, 5 1, 1 1))')), True)
     self.assertIs(
         OGRGeometry('POINT(0 0)').overlaps(OGRGeometry('POINT(0 1)')),
         False)
Ejemplo n.º 7
0
 def test_from_gml(self):
     self.assertEqual(
         OGRGeometry('POINT(0 0)'),
         OGRGeometry.from_gml(
             '<gml:Point gml:id="p21" srsName="http://www.opengis.net/def/crs/EPSG/0/4326">'
             '    <gml:pos srsDimension="2">0 0</gml:pos>'
             '</gml:Point>'),
     )
Ejemplo n.º 8
0
 def test_hex(self):
     "Testing HEX input/output."
     for g in self.geometries.hex_wkt:
         geom1 = OGRGeometry(g.wkt)
         self.assertEqual(g.hex.encode(), geom1.hex)
         # Constructing w/HEX
         geom2 = OGRGeometry(g.hex)
         self.assertEqual(geom1, geom2)
Ejemplo n.º 9
0
 def test_wkb(self):
     "Testing WKB input/output."
     for g in self.geometries.hex_wkt:
         geom1 = OGRGeometry(g.wkt)
         wkb = geom1.wkb
         self.assertEqual(wkb.hex().upper(), g.hex)
         # Constructing w/WKB.
         geom2 = OGRGeometry(wkb)
         self.assertEqual(geom1, geom2)
Ejemplo n.º 10
0
 def test_25D(self):
     "Testing 2.5D geometries."
     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)
Ejemplo n.º 11
0
 def test_ewkt(self):
     "Testing EWKT input/output."
     for ewkt_val in ('POINT (1 2 3)', 'LINEARRING (0 0,1 1,2 1,0 0)'):
         # First with ewkt output when no SRID in EWKT
         self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt)
         # No test consumption with an SRID specified.
         ewkt_val = 'SRID=4326;%s' % ewkt_val
         geom = OGRGeometry(ewkt_val)
         self.assertEqual(ewkt_val, geom.ewkt)
         self.assertEqual(4326, geom.srs.srid)
Ejemplo n.º 12
0
 def test_union(self):
     "Testing union()."
     for i in range(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)  # __or__ is union operator
         a |= b  # testing __ior__
         self.assertEqual(u1, a)
Ejemplo n.º 13
0
 def test_difference(self):
     "Testing difference()."
     for i in range(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)  # __sub__ is difference operator
         a -= b  # testing __isub__
         self.assertEqual(d1, a)
Ejemplo n.º 14
0
 def test_linearring(self):
     "Testing LinearRing objects."
     prev = OGRGeometry('POINT(0 0)')
     for rr in self.geometries.linearrings:
         lr = OGRGeometry(rr.wkt)
         # self.assertEqual(101, lr.geom_type.num)
         self.assertEqual('LINEARRING', lr.geom_name)
         self.assertEqual(rr.n_p, len(lr))
         self.assertEqual(lr, OGRGeometry(rr.wkt))
         self.assertNotEqual(lr, prev)
         prev = lr
Ejemplo n.º 15
0
    def test_transform_dim(self):
        "Testing coordinate dimension is the same on transformed geometries."
        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)
        # Making sure the coordinate dimension is still 2D.
        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)
Ejemplo n.º 16
0
    def test_points(self):
        "Testing Point objects."

        OGRGeometry('POINT(0 0)')
        for p in self.geometries.points:
            if not hasattr(p, 'z'):  # No 3D
                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)
Ejemplo n.º 17
0
 def test_extent(self):
     "Testing `extent` property."
     # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
     mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)')
     self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
     # Testing on the 'real world' Polygon.
     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)
Ejemplo n.º 18
0
 def test_symdifference(self):
     "Testing sym_difference()."
     for i in range(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)  # __xor__ is symmetric difference operator
         a ^= b  # testing __ixor__
         self.assertEqual(d1, a)
Ejemplo n.º 19
0
    def test_closepolygons(self):
        "Testing closing Polygon objects."
        # Both rings in this geometry are not closed.
        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(GDALException):
            poly.centroid

        poly.close_rings()
        self.assertEqual(
            10, poly.point_count)  # Two closing points should've been added
        self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
Ejemplo n.º 20
0
 def test_json(self):
     "Testing GeoJSON input/output."
     for g in self.geometries.json_geoms:
         geom = OGRGeometry(g.wkt)
         if not hasattr(g, 'not_equal'):
             # Loading jsons to prevent decimal differences
             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))
     # Test input with some garbage content (but valid json) (#15529)
     geom = OGRGeometry(
         '{"type": "Point", "coordinates": [ 100.0, 0.0 ], "other": "<test>"}'
     )
     self.assertIsInstance(geom, OGRGeometry)
Ejemplo n.º 21
0
 def test_pickle(self):
     "Testing pickle support."
     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)
Ejemplo n.º 22
0
 def test_polygons_templates(self):
     # Accessing Polygon attributes in templates should work.
     engine = Engine()
     template = engine.from_string('{{ polygons.0.wkt }}')
     polygons = [
         OGRGeometry(p.wkt) for p in self.geometries.multipolygons[:2]
     ]
     content = template.render(Context({'polygons': polygons}))
     self.assertIn('MULTIPOLYGON (((100', content)
Ejemplo n.º 23
0
 def test_multipoints(self):
     "Testing MultiPoint objects."
     for mp in self.geometries.multipoints:
         mgeom1 = OGRGeometry(mp.wkt)  # First one from WKT
         self.assertEqual(4, mgeom1.geom_type)
         self.assertEqual('MULTIPOINT', mgeom1.geom_name)
         mgeom2 = OGRGeometry('MULTIPOINT')  # Creating empty multipoint
         mgeom3 = OGRGeometry('MULTIPOINT')
         for g in mgeom1:
             mgeom2.add(g)  # adding each point from the multipoints
             mgeom3.add(g.wkt)  # should take WKT as well
         self.assertEqual(mgeom1, mgeom2)  # they should equal
         self.assertEqual(mgeom1, mgeom3)
         self.assertEqual(mp.coords, mgeom2.coords)
         self.assertEqual(mp.n_p, mgeom2.point_count)
Ejemplo n.º 24
0
    def verify_geom(self, geom, model_field):
        """
        Verify the geometry -- construct and return a GeometryCollection
        if necessary (for example if the model field is MultiPolygonField while
        the mapped shapefile only contains Polygons).
        """
        # Downgrade a 3D geom to a 2D one, if necessary.
        if self.coord_dim != geom.coord_dim:
            geom.coord_dim = self.coord_dim

        if self.make_multi(geom.geom_type, model_field):
            # Constructing a multi-geometry type to contain the single geometry
            multi_type = self.MULTI_TYPES[geom.geom_type.num]
            g = OGRGeometry(multi_type)
            g.add(geom)
        else:
            g = geom

        # Transforming the geometry with our Coordinate Transformation object,
        # but only if the class variable `transform` is set w/a CoordTransform
        # object.
        if self.transform:
            g.transform(self.transform)

        # Returning the WKT of the geometry.
        return g.wkt
Ejemplo n.º 25
0
    def test06_spatial_filter(self):
        "Testing the Layer.spatial_filter property."
        ds = DataSource(get_ds_file('cities', 'shp'))
        lyr = ds[0]

        # When not set, it should be None.
        self.assertIsNone(lyr.spatial_filter)

        # Must be set a/an OGRGeometry or 4-tuple.
        with self.assertRaises(TypeError):
            lyr._set_spatial_filter('foo')

        # Setting the spatial filter with a tuple/list with the extent of
        # a buffer centering around Pueblo.
        with 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'))

        # Setting the spatial filter with an OGRGeometry for buffer centering
        # around Houston.
        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'))

        # Clearing the spatial filter by setting it to None.  Now
        # should indicate that there are 3 features in the Layer.
        lyr.spatial_filter = None
        self.assertEqual(3, len(lyr))
Ejemplo n.º 26
0
 def test_intersection(self):
     "Testing intersects() and intersection()."
     for i in range(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.assertTrue(a.intersects(b))
         i2 = a.intersection(b)
         self.assertEqual(i1, i2)
         self.assertEqual(i1, a & b)  # __and__ is intersection operator
         a &= b  # testing __iand__
         self.assertEqual(i1, a)
Ejemplo n.º 27
0
 def test_multipolygons(self):
     "Testing MultiPolygon objects."
     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))
             msg = 'Index out of range when accessing geometry in a collection: %s.'
             with self.assertRaisesMessage(IndexError, msg % len(mpoly)):
                 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)
Ejemplo n.º 28
0
    def test_ogrgeometry_transform_workaround(self):
        "Testing coordinate dimensions on geometries after transformation."
        # A bug in GDAL versions prior to 1.7 changes the coordinate
        # dimension of a geometry after it has been transformed.
        # This test ensures that the bug workarounds employed within
        # `OGRGeometry.transform` indeed work.
        wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))"
        wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))"
        srid = 4326

        # For both the 2D and 3D MultiLineString, ensure _both_ the dimension
        # of the collection and the component LineString have the expected
        # coordinate dimension after transform.
        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)
Ejemplo n.º 29
0
 def test_multilinestring(self):
     "Testing MultiLineString objects."
     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(mlinestr, OGRGeometry(mls.wkt))
         self.assertNotEqual(mlinestr, prev)
         prev = mlinestr
         for ls in mlinestr:
             self.assertEqual(2, ls.geom_type)
             self.assertEqual('LINESTRING', ls.geom_name)
         msg = 'Index out of range when accessing geometry in a collection: %s.'
         with self.assertRaisesMessage(IndexError, msg % len(mlinestr)):
             mlinestr.__getitem__(len(mlinestr))
Ejemplo n.º 30
0
    def test_srs_transform(self):
        "Testing transform()."
        orig = OGRGeometry('POINT (-104.609 38.255)', 4326)
        trans = OGRGeometry('POINT (992385.4472045 481455.4944650)', 2774)

        # Using an srid, a SpatialReference object, and a CoordTransform object
        # or transformations.
        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)

        # Testing use of the `clone` keyword.
        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)