class FeatureInserter(object): def __init__(self, partition, table, dest_srs=4326, source_srs=None): self.bundle = partition.bundle self.sf = TableShapefile(self.bundle, partition.database.path, table, dest_srs, source_srs) def __enter__(self): return self def __exit__(self, type_, value, traceback): self.close() if type_ is not None: self.bundle.error("Got Exception: "+str(value)) return False return self def insert(self, row, source_srs=None): from sqlalchemy.engine.result import RowProxy if isinstance(row, RowProxy): row = dict(row) return self.sf.add_feature( row, source_srs) def close(self): self.sf.close()
def create(self, dest_srs=4326, source_srs=None): from databundles.geo.sfschema import TableShapefile tsf = TableShapefile(self.bundle, self._db_class.make_path(self), self.identity.table, dest_srs = dest_srs, source_srs = source_srs ) tsf.close() self.add_tables(self.data.get('tables',None))
def test_sfschema(self): from databundles.geo.sfschema import TableShapefile from databundles.geo.analysisarea import get_analysis_area _, communities = self.bundle.library.dep('communities') csrs = communities.get_srs() gp = self.bundle.partitions.new_geo_partition(table='geot2') with gp.database.inserter(source_srs=csrs) as ins: for row in communities.query(""" SELECT *, X(Transform(Centroid(geometry), 4326)) AS lon, Y(Transform(Centroid(geometry), 4326)) as lat, AsText(geometry) as wkt, AsBinary(geometry) as wkb FROM communities"""): r = {'name':row['cpname'], 'lat': row['lat'], 'lon': row['lon'], 'wkt': row['wkt']} ins.insert(r) return aa = get_analysis_area(self.bundle.library, geoid = 'CG0666000') path1 = '/tmp/geot1.kml' if os.path.exists(path1): os.remove(path1) sfs1 = TableShapefile(self.bundle, path1, 'geot1' ) path2 = '/tmp/geot2.kml' if os.path.exists(path2): os.remove(path2) sfs2 = TableShapefile(self.bundle, path2, 'geot2', source_srs=communities.get_srs()) print sfs1.type, sfs2.type for row in communities.query(""" SELECT *, X(Transform(Centroid(geometry), 4326)) AS lon, Y(Transform(Centroid(geometry), 4326)) as lat, AsText(geometry) as wkt, AsBinary(geometry) as wkb FROM communities"""): sfs1.add_feature( {'name':row['cpname'], 'lat': row['lat'], 'lon': row['lon'], 'wkt': row['wkt']}) sfs2.add_feature( {'name':row['cpname'], 'lat': row['lat'], 'lon': row['lon'], 'wkt': row['wkt']}) sfs1.close() sfs2.close()