def generateMockGrid(clz, dir_):
     shpfile = os.path.join(dir_, "grid.shp")
     schema = {
         'geometry': 'MultiPolygon',
         'properties': {
             'ID': 'int'
         }
     }
     coord_sets = [
         [[dg.generate_polygon_coords(x0=0, x1=2, y0=-1, y1=1)]],
         [[dg.generate_polygon_coords(x0=0, x1=2, y0=0, y1=1)]]
     ]
     records = []
     i = 0
     for coord_set in coord_sets:
         i += 1
         records.append({
             'id': i,
             'geometry': {
                 'type': 'MultiPolygon',
                 'coordinates': coord_set
             },
             'properties': {
                 'ID': i
             }
         })
     return clz.generate_shapefile(shpfile=shpfile, schema=schema,
                                   records=records)
Exemple #2
0
 def test_write_multipolygon_shapefile(self):
     hndl, shapefile = tempfile.mkstemp(suffix=".shp")
     schema = {
         'geometry': 'MultiPolygon',
         'properties': {
             'INT': 'int',
             'FLOAT': 'float',
             'STRING': 'str',
         }
     }
     records = []
     for i in range(1, 4):
         coords = [[dg.generate_polygon_coords(x0=i, y0=i)]]
         records.append({
             'id': i,
             'geometry': {
                 'type': 'MultiPolygon',
                 'coordinates': coords
             },
             'properties': {
                 'INT': i,
                 'FLOAT': float(i),
                 'STRING': str(i),
             }
         })
     writer = shapefile_util.get_shapefile_writer(
         shapefile=shapefile,
         crs='EPSG:4326',
         schema=schema,
     )
     for record in records:
         writer.write(record)
     writer.close()
     print "shp: ", shapefile
 def test_write_multipolygon_shapefile(self):
     hndl, shapefile = tempfile.mkstemp(suffix=".shp")
     schema = {"geometry": "MultiPolygon", "properties": {"INT": "int", "FLOAT": "float", "STRING": "str"}}
     records = []
     for i in range(1, 4):
         coords = [[dg.generate_polygon_coords(x0=i, y0=i)]]
         records.append(
             {
                 "id": i,
                 "geometry": {"type": "MultiPolygon", "coordinates": coords},
                 "properties": {"INT": i, "FLOAT": float(i), "STRING": str(i)},
             }
         )
     writer = shapefile_util.get_shapefile_writer(shapefile=shapefile, crs="EPSG:4326", schema=schema)
     for record in records:
         writer.write(record)
     writer.close()
     print "shp: ", shapefile
 def generateMockStatAreas(clz, dir_):
     shpfile = os.path.join(dir_, "stat_areas.shp")
     schema = {
         'geometry': 'MultiPolygon',
         'properties': {
             'SAREA': 'int'
         }
     }
     coords = [[dg.generate_polygon_coords(x0=1, x1=2, y0=-1, y1=2)]]
     records = [{
         'id': 1,
         'geometry': {
             'type': 'MultiPolygon',
             'coordinates': coords
         },
         'properties': {
             'SAREA': 1
         }
     }]
     return clz.generate_shapefile(shpfile=shpfile, schema=schema,
                                   records=records)
    def test_shapefile_ingestor(self):
        Base = declarative_base()

        class TestClass(Base):
            __tablename__ = 'testclass'
            id = Column(Integer, primary_key=True)
            attr1 = Column(Integer) 
            attr2 = Column(String)
            geom = GeometryColumn(MultiPolygon(2))
        GeometryDDL(TestClass.__table__)
        schema = {
            'sources': {
                'TestClass': TestClass
            }
        }

        Base.metadata.create_all(self.connection)

        dao = ORM_DAO(session=self.session, schema=schema)

        shapedir = tempfile.mkdtemp()
        shapefile = os.path.join(shapedir, "test.shp")
        schema = {
            'geometry': 'MultiPolygon',
            'properties': {
                'S_ATTR1': 'int',
                'S_ATTR2': 'str',
            }
        }
        records = []
        for i in range(5):
            coords = [[dg.generate_polygon_coords(x=i, y=i)]]
            records.append({
                'id': i,
                'geometry': {
                    'type': 'MultiPolygon',
                    'coordinates': coords
                },
                'properties': {
                    'S_ATTR1': i,
                    'S_ATTR2': str(i),
                }
            })
        writer = shapefile_util.get_shapefile_writer(
            shapefile=shapefile,
            crs='EPSG:4326',
            schema=schema,
        )
        for record in records:
            writer.write(record)
        writer.close()

        mappings = [
            {
                'source': 'S_ATTR1', 
                'target': 'attr1',
                'processor': lambda value: int(value) * 10
            },
            {
                'source': 'S_ATTR2', 
                'target': 'attr2',
            },
            {
                'source': '__shape',
                'target': 'geom',
                'processor': gis_util.shape_to_wkt,
            }
        ]

        Ingestor(
            reader=ShapefileReader(shp_file=shapefile),
            processors=[
                ClassMapper(clazz=TestClass, mappings=mappings),
                DAOWriter(dao=dao, commit=False),
            ]
        ).ingest()
        results = dao.query({
            'SELECT': ['__TestClass']
        }).all()

        for r in results:
            print r.attr1, r.attr2, dao.session.scalar(r.geom.wkt)
Exemple #6
0
    def test_shapefile_ingestor(self):
        Base = declarative_base()

        class TestClass(Base):
            __tablename__ = 'testclass'
            id = Column(Integer, primary_key=True)
            attr1 = Column(Integer)
            attr2 = Column(String)
            geom = GeometryColumn(MultiPolygon(2))

        GeometryDDL(TestClass.__table__)
        schema = {'sources': {'TestClass': TestClass}}

        Base.metadata.create_all(self.connection)

        dao = ORM_DAO(session=self.session, schema=schema)

        shapedir = tempfile.mkdtemp()
        shapefile = os.path.join(shapedir, "test.shp")
        schema = {
            'geometry': 'MultiPolygon',
            'properties': {
                'S_ATTR1': 'int',
                'S_ATTR2': 'str',
            }
        }
        records = []
        for i in range(5):
            coords = [[dg.generate_polygon_coords(x=i, y=i)]]
            records.append({
                'id': i,
                'geometry': {
                    'type': 'MultiPolygon',
                    'coordinates': coords
                },
                'properties': {
                    'S_ATTR1': i,
                    'S_ATTR2': str(i),
                }
            })
        writer = shapefile_util.get_shapefile_writer(
            shapefile=shapefile,
            crs='EPSG:4326',
            schema=schema,
        )
        for record in records:
            writer.write(record)
        writer.close()

        mappings = [{
            'source': 'S_ATTR1',
            'target': 'attr1',
            'processor': lambda value: int(value) * 10
        }, {
            'source': 'S_ATTR2',
            'target': 'attr2',
        }, {
            'source': '__shape',
            'target': 'geom',
            'processor': gis_util.shape_to_wkt,
        }]

        Ingestor(reader=ShapefileReader(shp_file=shapefile),
                 processors=[
                     ClassMapper(clazz=TestClass, mappings=mappings),
                     DAOWriter(dao=dao, commit=False),
                 ]).ingest()
        results = dao.query({'SELECT': ['__TestClass']}).all()

        for r in results:
            print r.attr1, r.attr2, dao.session.scalar(r.geom.wkt)