def populateLakes(self): self.setupDatabase() logger.info('Action: populateLakes()') # For now we never reproject lakes with self.userSession() as session: shpFile = self.config.get('Data', 'lakes') if not os.path.exists(shpFile): logger.error('Shapefile %s does not exists' % (shpFile)) sys.exit(1) count = 1 shp = ShpToGDALFeatures(shpFile) logger.info('Processing %s' % (shpFile)) bulk = BulkInsert(Lakes, session, withAutoCommit=1000) for feature in shp.getFeatures(): polygon = feature.GetGeometryRef() # Force 2D for lakes polygon.FlattenTo2D() # add shapefile path to dict # self.shpFilePath bulk.add( dict(the_geom=WKTElement(polygon.ExportToWkt(), 4326))) count += 1 bulk.commit() logger.info('Commit %s features for %s.' % (count, shpFile)) # Once all features have been commited, start creating all # the simplified versions of the lakes logger.info('Simplifying lakes') tiles = TerrainTiles(self.dbConfigFile, tmsConfig, time.time()) geodetic = GlobalGeodetic(True) bounds = (tiles.minLon, tiles.minLat, tiles.maxLon, tiles.maxLat) zooms = range(tiles.tileMinZ, tiles.tileMaxZ + 1) for i in xrange(0, len(zooms)): zoom = zooms[i] tablename = 'lakes_%s' % zoom tileMinX, tileMinY = geodetic.LonLatToTile( bounds[0], bounds[1], zoom) tileMaxX, tileMaxY = geodetic.LonLatToTile( bounds[2], bounds[3], zoom) tileBounds = geodetic.TileBounds(tileMinX, tileMinY, zoom) pointA = transformCoordinate( 'POINT(%s %s)' % (tileBounds[0], tileBounds[1]), 4326, 21781).GetPoints()[0] pointB = transformCoordinate( 'POINT(%s %s)' % (tileBounds[2], tileBounds[3]), 4326, 21781).GetPoints()[0] length = c2d.distance(pointA, pointB) pixelArea = pow(length, 2) / pow(256.0, 2) pixelLength = math.sqrt(pixelArea) session.execute( create_simplified_geom_table(tablename, pixelLength)) session.commit() logger.info('Commit table public.%s with %s meters ' 'tolerance' % (tablename, pixelLength))
def populateFeatures(args): pid = os.getpid() session = None shpFile = args.shpFile reproject = args.reproject keepfiles = args.keepfiles if reproject: try: shpFile = reprojectShp(shpFile, args) except Exception as e: raise Exception(e) try: models = modelsPyramid.models engine = sqlalchemy.create_engine(args.engineURL) session = scoped_session(sessionmaker(bind=engine)) model = models[args.modelIndex] if not os.path.exists(shpFile): logger.error('[%s]: Shapefile %s does not exists' % (pid, shpFile)) sys.exit(1) count = 1 shp = ShpToGDALFeatures(shpFile) logger.info('[%s]: Processing %s' % (pid, shpFile)) bulk = BulkInsert(model, session, withAutoCommit=1000) for feature in shp.getFeatures(): polygon = feature.GetGeometryRef() # add shapefile path to dict # self.shpFilePath bulk.add(dict( the_geom = WKTElement(polygon.ExportToWkt(), 4326), shapefilepath=shpFile )) count += 1 bulk.commit() logger.info('[%s]: Commit %s features for %s.' % (pid, count, shpFile)) except Exception as e: logger.error(e, exc_info=True) raise Exception(e) finally: if session is not None: session.close_all() engine.dispose() if reproject: # Discard file after reprojection if specified in config if not keepfiles: logger.info('[%s] Removing %s...' % (pid, shpFile)) cleanup(shpFile) return count
def populateFeatures(args): pid = os.getpid() session = None shpFile = args.shpFile reproject = args.reproject keepfiles = args.keepfiles if reproject: try: shpFile = reprojectShp(shpFile, args) except Exception as e: raise Exception(e) try: models = modelsPyramid.models engine = sqlalchemy.create_engine(args.engineURL) session = scoped_session(sessionmaker(bind=engine)) model = models[args.modelIndex] if not os.path.exists(shpFile): logger.error('[%s]: Shapefile %s does not exists' % (pid, shpFile)) sys.exit(1) count = 1 shp = ShpToGDALFeatures(shpFile) logger.info('[%s]: Processing %s' % (pid, shpFile)) bulk = BulkInsert(model, session, withAutoCommit=1000) for feature in shp.getFeatures(): polygon = feature.GetGeometryRef() # add shapefile path to dict # self.shpFilePath bulk.add( dict(shapefilepath=shpFile, the_geom='SRID=4326;' + polygon.ExportToWkt())) count += 1 bulk.commit() logger.info('[%s]: Commit %s features for %s.' % (pid, count, shpFile)) except Exception as e: logger.error(e, exc_info=True) raise Exception(e) finally: if session is not None: session.close_all() engine.dispose() if reproject: # Discard file after reprojection if specified in config if not keepfiles: logger.info('[%s] Removing %s...' % (pid, shpFile)) cleanup(shpFile) return count
import os from forge.terrain import TerrainTile from forge.terrain.topology import TerrainTopology from forge.lib.shapefile_utils import ShpToGDALFeatures from forge.lib.global_geodetic import GlobalGeodetic basename = '7_133_98' directory = '.tmp' extension = '.terrain' curDir = os.getcwd() filePathSource = '%s/forge/data/shapefile-features/%s.shp' % (curDir, basename) filePathTarget = '%s/%s%s' % (directory, basename, extension) shapefile = ShpToGDALFeatures(shpFilePath=filePathSource) features = shapefile.__read__() terrainTopo = TerrainTopology(features=features) terrainTopo.fromGDALFeatures() terrainFormat = TerrainTile() geodetic = GlobalGeodetic(True) zxy = basename.split('_') bounds = geodetic.TileBounds(float(zxy[1]), float(zxy[2]), float(zxy[0])) terrainFormat.fromTerrainTopology(terrainTopo, bounds) terrainFormat.toFile(filePathTarget) # Display SwissCoordinates terrainFormat.computeVerticesCoordinates(epsg=21781)
def populateLakes(self): self.setupDatabase() logger.info('Action: populateLakes()') # For now we never reproject lakes with self.userSession() as session: shpFile = self.config.get('Data', 'lakes') if not os.path.exists(shpFile): logger.error('Shapefile %s does not exists' % (shpFile)) sys.exit(1) count = 1 shp = ShpToGDALFeatures(shpFile) logger.info('Processing %s' % (shpFile)) bulk = BulkInsert(Lakes, session, withAutoCommit=1000) for feature in shp.getFeatures(): polygon = feature.GetGeometryRef() # Force 2D for lakes polygon.FlattenTo2D() # add shapefile path to dict # self.shpFilePath bulk.add(dict( the_geom = WKTElement(polygon.ExportToWkt(), 4326) )) count += 1 bulk.commit() logger.info('Commit %s features for %s.' % (count, shpFile)) # Once all features have been commited, start creating all # the simplified versions of the lakes logger.info('Simplifying lakes') tiles = TerrainTiles(self.dbConfigFile, tmsConfig, time.time()) geodetic = GlobalGeodetic(True) bounds = (tiles.minLon, tiles.minLat, tiles.maxLon, tiles.maxLat) zooms = range(tiles.tileMinZ, tiles.tileMaxZ + 1) for i in xrange(0, len(zooms)): zoom = zooms[i] tablename = 'lakes_%s' % zoom tileMinX, tileMinY = geodetic.LonLatToTile( bounds[0], bounds[1], zoom ) tileMaxX, tileMaxY = geodetic.LonLatToTile( bounds[2], bounds[3], zoom ) tileBounds = geodetic.TileBounds(tileMinX, tileMinY, zoom) pointA = transformCoordinate('POINT(%s %s)' % ( tileBounds[0], tileBounds[1]), 4326, 21781 ).GetPoints()[0] pointB = transformCoordinate('POINT(%s %s)' % ( tileBounds[2], tileBounds[3]), 4326, 21781 ).GetPoints()[0] length = c2d.distance(pointA, pointB) pixelArea = pow(length, 2) / pow(256.0, 2) pixelLength = math.sqrt(pixelArea) session.execute( create_simplified_geom_table(tablename, pixelLength) ) session.commit() logger.info('Commit table public.%s with %s meters ' 'tolerance' % (tablename, pixelLength))