示例#1
0
 def testPolygon(self):
     p = from_wkt(poly0)
     self.assertEqual(p, {'type': 'Polygon', 'coordinates':
         (((30.0, 10.0), (10.0, 20.0), (20.0, 40.0),
         (40.0, 40.0), (30.0, 10.0)),)
         })
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(poly1)
     self.assertEqual(p, {'type': 'Polygon', 'coordinates':
                     (((35.0, 10.0), (10.1, 20.0),
                     (15.0, 40.0), (45.0, 45.0),
                     (35.0, 10.0)),
                     ((20.0, 30.0), (35.0, 35.0),
                     (30.0, 20.0), (20.0, 30.0)))})
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(poly2)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(poly3)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(poly4)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(empty_poly0)
     self.assertEqual(p, {'type': 'Polygon', 'coordinates': None})
     p = from_wkt(empty_poly1)
     self.assertEqual(p, {'type': 'Polygon', 'coordinates': None})
示例#2
0
def convert(infilename, outfilename='doc.kml', namecolumn=0):
    sf = shapefile.Reader(infilename)
    k = kml.KML()
    doc = kml.Document(name=infilename)
    k.append(doc)
    for sr in sf.shapeRecords():
        if sr.shape.points:
            pm = kml.Placemark(name=unicode(sr.record[namecolumn]),
                    description= "test")
            if not hasattr(sr.shape, '__geo_interface__'):
                import ipdb; ipdb.set_trace()

            pm.geometry = pygeoif.as_shape(sr.shape)
            doc.append(pm)
            if  sr.shape.__geo_interface__ != pygeoif.as_shape(sr.shape).__geo_interface__:
                #import ipdb; ipdb.set_trace()
                print sr.record
                print len(sr.shape.__geo_interface__['coordinates']),  len(pygeoif.as_shape(sr.shape).__geo_interface__['coordinates'])
                print sr.shape.__geo_interface__['type'],  pygeoif.as_shape(sr.shape).__geo_interface__['type']
                #for i in range(0,len(sr.shape.__geo_interface__['coordinates'])):
                #    print sr.shape.__geo_interface__['coordinates'][i] == pygeoif.as_shape(sr.shape).__geo_interface__['coordinates'][i]



    xml = '<?xml version="1.0" encoding="UTF-8"?>' + k.to_string()
    #try:
    #    xml = xml.decode('utf-8', 'ignore')
    #except:
    #    pass
    #xml =  xml.encode('utf-8')
    out = open(outfilename, 'w')
    out.write(xml)
    out.close()
示例#3
0
 def testPoint(self):
     p = from_wkt(empty_p0)
     self.assertEqual(p, {'type': 'Point', 'coordinates': None})
     self.assertRaises(NotImplementedError, from_wkt, p_zm)
     self.assertRaises(NotImplementedError, from_wkt, point_m0)
     p = from_wkt(p0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     self.assertEqual(p, {'type': 'Point', 'coordinates': (30.0, 10.0)})
     p = from_wkt(p_z)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     self.assertEqual(p, {'type': 'Point', 'coordinates': (1.0, 1.0, 5.0)})
示例#4
0
    def _parse_shapefile_and_populate_db(file_path):
        """Given a path to a shapefile containing Community information, 
        parse the file and add each Community to the database.
        """
        if not os.path.exists(file_path):
            print(file_path + " does not exist")
        else:
            sf = shapefile.Reader(file_path)

            # Create a dict of {<field>: <index>}
            field_dict = {}
            for field in sf.fields:
                field_dict[field[0]] = sf.fields.index(field) - 1

            for shapeRecord in sf.shapeRecords():
                Community.add_comunity(
                    Community.from_dict({
                        "id":
                        shapeRecord.record[field_dict['AREA_S_CD']],
                        "name":
                        ' '.join(shapeRecord.record[
                            field_dict['AREA_NAME']].split(' ')[:-1]),
                        "boundaries":
                        WKTElement(
                            str(
                                pygeoif.MultiPolygon(
                                    pygeoif.as_shape(
                                        Community._longlat_to_latlong(
                                            shapeRecord.shape.__geo_interface__
                                        )))), 4326)
                    }))
示例#5
0
 def testMultilinestring(self):
     p = from_wkt(empty_mls0)
     self.assertEqual(p, {'type': 'MultiLineString', 'coordinates': None})
     p = from_wkt(empty_mls1)
     self.assertEqual(p, {'type': 'MultiLineString', 'coordinates': None})
     p = from_wkt(mls0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     self.assertEqual(p, {'type': 'MultiLineString', 'coordinates':
             (((10.0, 10.0), (20.0, 20.0), (10.0, 40.0)),
             ((40.0, 40.0), (30.0, 30.0), (40.0, 20.0), (30.0, 10.0)))})
     p = from_wkt(mls0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(mls0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(mls1)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(mls3)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
示例#6
0
 def testMultipoint(self):
     p = from_wkt(empty_mp0)
     self.assertEqual(p, {'type': 'MultiPoint', 'coordinates': None})
     p = from_wkt(empty_mp1)
     self.assertEqual(p, {'type': 'MultiPoint', 'coordinates': None})
     p = from_wkt(mp0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     self.assertEqual(p, {'type': 'MultiPoint', 'coordinates':
         ((10.0, 40.0), (40.0, 30.0), (20.0, 20.0), (30.0, 10.0))})
示例#7
0
 def testMultipolygon(self):
     p = from_wkt(mpoly_empty)
     self.assertEqual(p, {'type': 'MultiPolygon', 'coordinates': None})
     p = from_wkt(empty_mpoly0)
     self.assertEqual(p, {'type': 'MultiPolygon', 'coordinates': None})
     p = from_wkt(empty_mpoly1)
     self.assertEqual(p, {'type': 'MultiPolygon', 'coordinates': None})
     p = from_wkt(mpoly0)
     #self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     self.assertEqual(p, {'type': 'MultiPolygon', 'coordinates':
         ((((30.0, 20.0), (10.0, 40.0), (45.0, 40.0), (30.0, 20.0)),),
         (((15.0, 5.0), (40.0, 10.0), (10.0, 20.0),
         (5.0, 10.0), (15.0, 5.0)),))})
     p = from_wkt(mpoly0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(mpoly1)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(mpoly2)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(mpoly3)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
示例#8
0
def convert(infilename, outfilename='doc.kml', namecolumn=0):
    sf = shapefile.Reader(infilename)
    k = kml.KML()
    doc = kml.Document(name=infilename)
    k.append(doc)
    for sr in sf.shapeRecords():
        if sr.shape.points:
            pm = kml.Placemark(name=unicode(sr.record[namecolumn]),
                               description="test")
            if not hasattr(sr.shape, '__geo_interface__'):
                import ipdb
                ipdb.set_trace()

            pm.geometry = pygeoif.as_shape(sr.shape)
            doc.append(pm)
            if sr.shape.__geo_interface__ != pygeoif.as_shape(
                    sr.shape).__geo_interface__:
                #import ipdb; ipdb.set_trace()
                print sr.record
                print len(sr.shape.__geo_interface__['coordinates']), len(
                    pygeoif.as_shape(
                        sr.shape).__geo_interface__['coordinates'])
                print sr.shape.__geo_interface__['type'], pygeoif.as_shape(
                    sr.shape).__geo_interface__['type']
                #for i in range(0,len(sr.shape.__geo_interface__['coordinates'])):
                #    print sr.shape.__geo_interface__['coordinates'][i] == pygeoif.as_shape(sr.shape).__geo_interface__['coordinates'][i]

    xml = '<?xml version="1.0" encoding="UTF-8"?>' + k.to_string()
    #try:
    #    xml = xml.decode('utf-8', 'ignore')
    #except:
    #    pass
    #xml =  xml.encode('utf-8')
    out = open(outfilename, 'w')
    out.write(xml)
    out.close()
示例#9
0
 def testGeometrycollection(self):
     p = from_wkt(empty_gc0)
     self.assertEqual(p, {'type': 'GeometryCollection', 'geometries': None})
     p = from_wkt(gc0)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(gc1)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(gc2)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(gc3)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(gc4)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(gc5)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(gc6)
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
示例#10
0
def __read(
    shapefile_path: str,
    encoding: str = 'utf-8',
    debug: bool = False,
) -> Iterator[shapefile.ShapeRecord]:
    with shapefile.Reader(shapefile_path, encoding=encoding) as reader:
        if debug:
            print(f"bbox: {reader.bbox}")
            print(f"num records: {reader.numRecords}")
            print(list(field[0] for field in reader.fields))

        for shapeRecord in reader.iterShapeRecords():
            if debug:
                shape = shapeRecord.shape
                record = shapeRecord.record.as_dict()
                if not record["iso_a2"] == "JP":
                    continue
                print(record)
                print()
                print(f"shape type: {shape.shapeTypeName}")
                print(pygeoif.as_shape(shape).wkt)
            yield (shapeRecord)
示例#11
0
 def testLinestring(self):
     p = from_wkt(empty_ls0)
     self.assertEqual(p, {'type': 'LineString', 'coordinates': None})
     p = from_wkt(ls0)
     self.assertEqual(p, {'type': 'LineString', 'coordinates':
         ((30.0, 10.0), (10.0, 30.0), (40.0, 40.0))})
     self.assertEqual(p, pygeoif.as_shape(p).__geo_interface__)
     p = from_wkt(ls1)
     self.assertEqual(p, {'type': 'LineString', 'coordinates':
         ((0.0, 0.0), (0.0, 9.0), (9.0, 9.0), (9.0, 0.0), (0.0, 0.0))})
     p = from_wkt(ls2)
     self.assertEqual(p['type'], 'LineString')
     self.assertEqual(p['coordinates'][0], (3429562.6, 5799490.68))
     self.assertEqual(p['coordinates'][-1], (3431644.53, 5798012.51))
     self.assertEqual(len(p['coordinates']), 25)
     p = from_wkt(ls3)
     self.assertEqual(p, {'type': 'LineString', 'coordinates':
         ((0.0, 0.0, 2.0),
          (10.0, 0.0, 4.0),
          (10.0, 10.0, 6.0),
          (0.0, 10.0, 8.0),
          (0.0, 0.0, 10.0))})
示例#12
0
文件: osmoapi.py 项目: cleder/osmoapi
    def create_multipolygon(self, multipolygon, **kwargs):
        """
        **Create a Relation:multipolygon.**

        http://wiki.openstreetmap.org/wiki/Relation:multipolygon

        Any area that is complex (e.g., because its outline consists of several
        ways joined together, or because the area consists of multiple disjunct
        parts, or has holes) requires a multipolygon relation.

        A multipolygon relation can have any number of ways in the role outer
        (the outline) and any number of ways in the role inner (the holes),
        and these must form valid rings to build a multipolygon from.
        """
        if not isinstance(multipolygon, dict):
            multipolygon = multipolygon.__geo_interface__
        assert multipolygon['type'] == 'MultiPolygon' or multipolygon['type'] == 'Polygon'
        assert multipolygon['coordinates']
        if multipolygon['type'] == 'Polygon':
            polygons = [pygeoif.Polygon(pygeoif.as_shape(multipolygon)), ]
        else:
            polygons = []
            for coords in multipolygon['coordinates']:
                polygons.append(pygeoif.Polygon(coords[0], coords[1:]))
        ways = []
        for polygon in polygons:
            outer = self.create_way(polygon.exterior)
            ways.append(('outer', str(outer)))
            for way in polygon.interiors:
                inner = self.create_way(way)
                ways.append(('inner', str(inner)))
        self.idx -= 1
        self.multipolygons.append(dict(id=str(self.idx),
                                       ways=ways,
                                       tags=dict(**kwargs)))
        return self.idx
示例#13
0
def __apply_geometry_field(appendee: Dict, shape: shapefile.Shape):
    appendee[GEOMETRY_FIELD_NAME] = pygeoif.as_shape(shape).wkt
    return appendee