Exemplo n.º 1
0
    def test_polygon_transf(self):
        p1 = shapely.geometry.Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])

        p2 = transform_geometry(SRS(4326), SRS(900913), p1)
        assert p2.contains(shapely.geometry.Point((1000000, 1000000)))
        p3 = transform_geometry(SRS(900913), SRS(4326), p2)

        assert p3.symmetric_difference(p1).area < 0.00001
Exemplo n.º 2
0
    def test_polygon_transf(self):
        p1 = shapely.geometry.Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])

        p2 = transform_geometry(SRS(4326), SRS(900913), p1)
        assert p2.contains(shapely.geometry.Point((1000000, 1000000)))
        p3 = transform_geometry(SRS(900913), SRS(4326), p2)

        assert p3.symmetric_difference(p1).area < 0.00001
Exemplo n.º 3
0
    def test_multipolygon_transf(self):
        p1 = shapely.geometry.Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])
        p2 = shapely.geometry.Polygon([(20, 20), (30, 20), (30, 30), (20, 30)])
        mp1 = shapely.geometry.MultiPolygon([p1, p2])

        mp2 = transform_geometry(SRS(4326), SRS(900913), mp1)
        assert mp2.contains(shapely.geometry.Point((1000000, 1000000)))
        assert not mp2.contains(shapely.geometry.Point((2000000, 2000000)))
        assert mp2.contains(shapely.geometry.Point((3000000, 3000000)))

        mp3 = transform_geometry(SRS(900913), SRS(4326), mp2)

        assert mp3.symmetric_difference(mp1).area < 0.00001
Exemplo n.º 4
0
    def test_multipolygon_transf(self):
        p1 = shapely.geometry.Polygon([(0, 0), (10, 0), (10, 10), (0, 10)])
        p2 = shapely.geometry.Polygon([(20, 20), (30, 20), (30, 30), (20, 30)])
        mp1 = shapely.geometry.MultiPolygon([p1, p2])

        mp2 = transform_geometry(SRS(4326), SRS(900913), mp1)
        assert mp2.contains(shapely.geometry.Point((1000000, 1000000)))
        assert not mp2.contains(shapely.geometry.Point((2000000, 2000000)))
        assert mp2.contains(shapely.geometry.Point((3000000, 3000000)))

        mp3 = transform_geometry(SRS(900913), SRS(4326), mp2)

        assert mp3.symmetric_difference(mp1).area < 0.00001
Exemplo n.º 5
0
    def as_shape_record(self, record):
        data = self.shp_defaults.copy()
        # if record has filter values or filter is not set then continue
        if self.filter_json(record):
            if self.other_srs != self.srs:
                data['geometry'] = mapping(
                    transform_geometry(self.srs, self.other_srs,
                                       asShape(record['geometry'])))
            else:
                data['geometry'] = record['geometry']
            if data['geometry']['type'] == 'Polygon':
                # pass Polygons as Multipolygon , Fiona only supports same type for all records
                data['geometry']['type'] = 'MultiPolygon'
                data['geometry']['coordinates'] = (
                    data['geometry']['coordinates'], )
            data['properties'] = {}
            for json, shp, _type in self.fields:
                if record['properties'].get(json, False):
                    val = record['properties'].get(json)
                    if isinstance(val, basestring):
                        val = val.encode(self.shp_encoding)
                    data['properties'][shp] = val
                else:
                    data['properties'][shp] = None

            return data
Exemplo n.º 6
0
 def _geom_in_coverage_srs(self, geom, srs):
     if isinstance(geom, shapely.geometry.base.BaseGeometry):
         if srs != self.srs:
             geom = transform_geometry(srs, self.srs, geom)
     elif len(geom) == 2:
         if srs != self.srs:
             geom = srs.transform_to(self.srs, geom)
         geom = shapely.geometry.Point(geom)
     else:
         if srs != self.srs:
             geom = srs.transform_bbox_to(self.srs, geom)
         geom = bbox_polygon(geom)
     return geom
Exemplo n.º 7
0
 def _geom_in_coverage_srs(self, geom, srs):
     if isinstance(geom, shapely.geometry.base.BaseGeometry):
         if srs != self.srs:
             geom = transform_geometry(srs, self.srs, geom)
     elif len(geom) == 2:
         if srs != self.srs:
             geom = srs.transform_to(self.srs, geom)
         geom = shapely.geometry.Point(geom)
     else:
         if srs != self.srs:
             geom = srs.transform_bbox_to(self.srs, geom)
         geom = bbox_polygon(geom)
     return geom
Exemplo n.º 8
0
    def as_json_record(self, record):
        data = self.json_defaults.copy()
        for json, shp, _type in self.fields:
            if record.get('properties', False).get(shp, False):
                val = record['properties'].get(shp, None)
                if isinstance(val, str):
                    val = val.decode(self.shp_encoding)
                data[json] = val
        if self.other_srs != self.srs:
            data['geometry'] = mapping(transform_geometry(self.other_srs, self.srs, asShape(record.get('geometry', None))))
        else:
            data['geometry'] = record.get('geometry', None)

        return data
Exemplo n.º 9
0
    def as_json_record(self, record):
        data = self.json_defaults.copy()
        for json, shp, _type in self.fields:
            if record.get('properties', False).get(shp, False):
                val = record['properties'].get(shp, None)
                if isinstance(val, str):
                    val = val.decode(self.shp_encoding)
                data[json] = val
        if self.other_srs != self.srs:
            data['geometry'] = mapping(
                transform_geometry(self.other_srs, self.srs,
                                   asShape(record.get('geometry', None))))
        else:
            data['geometry'] = record.get('geometry', None)

        return data
Exemplo n.º 10
0
    def as_shape_record(self, record):
        data = self.shp_defaults.copy()
        # if record has filter values or filter is not set then continue
        if self.filter_json(record):
            if self.other_srs != self.srs:
                data['geometry'] = mapping(transform_geometry(self.srs, self.other_srs, asShape(record['geometry'])))
            else:
                data['geometry'] = record['geometry']
            if data['geometry']['type'] == 'Polygon':
                # pass Polygons as Multipolygon , Fiona only supports same type for all records
                data['geometry']['type'] = 'MultiPolygon'
                data['geometry']['coordinates'] = (data['geometry']['coordinates'], )
            data['properties'] = {}
            for json, shp, _type in self.fields:
                if record.get(json, False):
                    val = record.get(json)
                    if isinstance(val, basestring):
                        val = val.encode(self.shp_encoding)
                    data['properties'][shp] = val

            return data
Exemplo n.º 11
0
    def transform_to(self, srs):
        if srs == self.srs:
            return self

        geom = transform_geometry(self.srs, srs, self.geom)
        return GeomCoverage(geom, srs)
Exemplo n.º 12
0
 def test_invalid_transf(self):
     p = shapely.geometry.Point((0, 0))
     transform_geometry(SRS(4326), SRS(900913), p)
Exemplo n.º 13
0
    def transform_to(self, srs):
        if srs == self.srs:
            return self

        geom = transform_geometry(self.srs, srs, self.geom)
        return GeomCoverage(geom, srs, clip=self.clip)
Exemplo n.º 14
0
 def test_invalid_transf(self):
     p = shapely.geometry.Point((0, 0))
     transform_geometry(SRS(4326), SRS(900913), p)
Exemplo n.º 15
0
 def test_invalid_transf(self):
     p = shapely.geometry.Point((0, 0))
     with pytest.raises(ValueError):
         transform_geometry(SRS(4326), SRS(900913), p)