Example #1
0
    def get_shapefile_writer(clz,
                             shapefile="",
                             driver='ESRI Shapefile',
                             crs=None,
                             schema=None):

        type_builder = SimpleFeatureTypeBuilder()
        type_builder.setName("custom_feature")
        crs = gis_util.get_crs(crs)
        type_builder.setCRS(crs)
        geom_type = geom_type_mappings[schema['geometry']]
        type_builder.add('geometry', geom_type, crs)
        for prop, property_type in schema.get('properties', {}).items():
            type_builder.add(prop, property_type_mappings[property_type])
        feature_type = type_builder.buildFeatureType()

        feature_builder = SimpleFeatureBuilder(feature_type)

        class ShapefileWriter(object):
            def __init__(self):
                # Set up feature store.
                url = File(shapefile).toURI().toURL()
                self.ds = ShapefileDataStore(url)
                self.ds.createSchema(feature_type)
                type_name = self.ds.getTypeNames()[0]
                self.fs = self.ds.getFeatureSource(type_name)
                self.transaction = DefaultTransaction("create")

                # Setup feature list.
                self.features = []

            def write(self, record):
                # Process geometry.
                geom = gis_util.geojson_to_shape(record['geometry'])
                feature_builder.set('geometry', geom._jgeom)

                # Process properties.
                for prop, value in record.get('properties', {}).items():
                    feature_builder.set(prop, value)

                # Create feature.
                id_ = record.get('id')
                if id_ is not None:
                    id_ = str(id_)
                feature = feature_builder.buildFeature(id_)

                # Add to feature list.
                self.features.append(feature)

            def close(self):
                # Add features to feature store.
                fc = ListFeatureCollection(feature_type, self.features)
                self.fs.setTransaction(self.transaction)
                self.fs.addFeatures(fc)
                self.transaction.commit()
                self.transaction.close()

        return ShapefileWriter()
Example #2
0
    def get_shapefile_writer(clz, shapefile="", driver='ESRI Shapefile', crs=None, 
                             schema=None):

        type_builder = SimpleFeatureTypeBuilder()
        type_builder.setName("custom_feature")
        crs = gis_util.get_crs(crs)
        type_builder.setCRS(crs)
        geom_type = geom_type_mappings[schema['geometry']]
        type_builder.add('geometry', geom_type, crs)
        for prop, property_type in schema.get('properties', {}).items():
            type_builder.add(prop, property_type_mappings[property_type])
        feature_type = type_builder.buildFeatureType()

        feature_builder = SimpleFeatureBuilder(feature_type)

        class ShapefileWriter(object):
            def __init__(self):
                # Set up feature store.
                url = File(shapefile).toURI().toURL()
                self.ds = ShapefileDataStore(url)
                self.ds.createSchema(feature_type)
                type_name = self.ds.getTypeNames()[0]
                self.fs = self.ds.getFeatureSource(type_name)
                self.transaction = DefaultTransaction("create")

                # Setup feature list.
                self.features = []

            def write(self, record):
                # Process geometry.
                geom = gis_util.geojson_to_shape(record['geometry'])
                feature_builder.set('geometry', geom._jgeom)

                # Process properties.
                for prop, value in record.get('properties', {}).items():
                    feature_builder.set(prop, value)

                # Create feature.
                id_ = record.get('id')
                if id_ is not None:
                    id_ = str(id_)
                feature = feature_builder.buildFeature(id_)

                # Add to feature list.
                self.features.append(feature)

            def close(self):
                # Add features to feature store.
                fc = ListFeatureCollection(feature_type, self.features)
                self.fs.setTransaction(self.transaction)
                self.fs.addFeatures(fc)
                self.transaction.commit()
                self.transaction.close()

        return ShapefileWriter()