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 addList(self, featureList): if self.readonly: raise Exception('Layer is read-only') fc = FeatureCollections.newCollection() for o in featureList: if isinstance(o, feature.Feature): f = o if not f.schema: f.schema = self.schema elif isinstance(o, (dict,list)): f = self.schema.feature(o) fc.add(f._feature) trans = DefaultTransaction("ex") self._source.setTransaction(trans) try: self._source.addFeatures(fc) trans.commit() trans.close() except: print 'Exception', sys.exc_info()[2] print sys.exc_info() traceback.print_stack() print repr(traceback.extract_stack()) print repr(traceback.format_stack()) trans.rollback()
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()