예제 #1
0
    def verify_geom(self, geom, model_field):
        """
        Verifies the geometry -- will 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
예제 #2
0
 def test03_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)
예제 #3
0
    def test14_add(self):
        "Testing GeometryCollection.add()."
        # Can't insert a Point into a MultiPolygon.
        mp = OGRGeometry("MultiPolygon")
        pnt = OGRGeometry("POINT(5 23)")
        self.assertRaises(OGRException, mp.add, pnt)

        # GeometryCollection.add may take an OGRGeometry (if another collection
        # of the same type all child geoms will be added individually) or WKT.
        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)  # Adding a geometry at a time
                mp2.add(poly.wkt)  # Adding WKT
            mp3.add(mpoly)  # Adding a MultiPolygon's entire contents at once.
            for tmp in (mp1, mp2, mp3):
                self.assertEqual(mpoly, tmp)