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 = []
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()