def _stats(self, withDb=True): self.t0 = time.time() total = 0 msg = '\n' tiles = TerrainTiles(self.dbConfigFile, self.tmsConfig, self.t0) geodetic = getTileGrid(4326)(tmsCompatible=True) bounds = (tiles.minLon, tiles.minLat, tiles.maxLon, tiles.maxLat) zooms = range(tiles.tileMinZ, tiles.tileMaxZ + 1) db = DB('configs/terrain/database.cfg') try: with db.userSession() as session: for i in xrange(0, len(zooms)): zoom = zooms[i] model = modelsPyramid.getModelByZoom(zoom) nbObjects = None if withDb: nbObjects = session.query(model).filter( model.bboxIntersects(bounds)).count() # top letf corner tileMinX, tileMinY = geodetic.tileAddress( zoom, [bounds[0], bounds[3]]) # bottom right tileMaxX, tileMaxY = geodetic.tileAddress( zoom, [bounds[2], bounds[1]]) tileBounds = geodetic.tileBounds(zoom, tileMinX, tileMinY) xCount = tileMaxX - tileMinX + 1 yCount = tileMaxY - tileMinY + 1 nbTiles = xCount * yCount total += nbTiles 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 = int(round(c2d.distance(pointA, pointB))) msg += 'At zoom %s:\n' % zoom msg += 'We expect %s tiles overall\n' % nbTiles msg += 'Min X is %s, Max X is %s\n' % (tileMinX, tileMaxX) msg += '%s columns over X\n' % xCount msg += 'Min Y is %s, Max Y is %s\n' % (tileMinY, tileMaxY) msg += '%s rows over Y\n' % yCount msg += '\n' msg += 'A tile side is around %s meters' % length if nbTiles > 0 and nbObjects is not None: msg += 'We have an average of about %s triangles ' \ 'per tile\n' % int(round(nbObjects / nbTiles)) msg += '\n\n' msg += '%s tiles in total.' % total except Exception as e: logger.error('An error occured during statistics collection') logger.error('%s' % e, exc_info=True) raise Exception(e) finally: db.userEngine.dispose() return (total, msg)
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 _stats(self, withDb=True): self.t0 = time.time() total = 0 msg = "\n" tiles = Tiles(self.dbConfigFile, self.tmsConfig, self.t0) geodetic = GlobalGeodetic(True) bounds = (tiles.minLon, tiles.minLat, tiles.maxLon, tiles.maxLat) zooms = range(tiles.tileMinZ, tiles.tileMaxZ + 1) db = DB("database.cfg") self.DBSession = scoped_session(sessionmaker(bind=db.userEngine)) for i in xrange(0, len(zooms)): zoom = zooms[i] model = modelsPyramid.getModelByZoom(zoom) nbObjects = None if withDb: nbObjects = self.DBSession.query(model).filter(model.bboxIntersects(bounds)).count() tileMinX, tileMinY = geodetic.LonLatToTile(bounds[0], bounds[1], zoom) tileMaxX, tileMaxY = geodetic.LonLatToTile(bounds[2], bounds[3], zoom) # Fast approach, but might not be fully correct if tiles.fullonly == 1: tileMinX += 1 tileMinY += 1 tileMaxX -= 1 tileMaxY -= 1 tileBounds = geodetic.TileBounds(tileMinX, tileMinY, zoom) xCount = tileMaxX - tileMinX + 1 yCount = tileMaxY - tileMinY + 1 nbTiles = xCount * yCount total += nbTiles 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) if tiles.fullonly == 1: msg += "WARNING: stats are approximative because fullonly is activated!\n" msg += "At zoom %s:\n" % zoom msg += "We expect %s tiles overall\n" % nbTiles msg += "Min X is %s, Max X is %s\n" % (tileMinX, tileMaxX) msg += "%s columns over X\n" % xCount msg += "Min Y is %s, Max Y is %s\n" % (tileMinY, tileMaxY) msg += "%s rows over Y\n" % yCount msg += "\n" msg += "A tile side is around %s meters long\n" % int(round(length)) if nbTiles > 0 and nbObjects is not None: msg += "We have an average of about %s triangles per tile\n" % int(round(nbObjects / nbTiles)) msg += "\n" return (total, msg)
def _stats(self, withDb=True): self.t0 = time.time() total = 0 msg = '\n' tiles = TerrainTiles(self.dbConfigFile, self.tmsConfig, self.t0) geodetic = GlobalGeodetic(True) bounds = (tiles.minLon, tiles.minLat, tiles.maxLon, tiles.maxLat) zooms = range(tiles.tileMinZ, tiles.tileMaxZ + 1) db = DB('configs/terrain/database.cfg') try: with db.userSession() as session: for i in xrange(0, len(zooms)): zoom = zooms[i] model = modelsPyramid.getModelByZoom(zoom) nbObjects = None if withDb: nbObjects = session.query(model).filter( model.bboxIntersects(bounds) ).count() tileMinX, tileMinY = geodetic.LonLatToTile( bounds[0], bounds[1], zoom ) tileMaxX, tileMaxY = geodetic.LonLatToTile( bounds[2], bounds[3], zoom ) # Fast approach, but might not be fully correct if tiles.fullonly == 1: tileMinX += 1 tileMinY += 1 tileMaxX -= 1 tileMaxY -= 1 tileBounds = geodetic.TileBounds(tileMinX, tileMinY, zoom) xCount = tileMaxX - tileMinX + 1 yCount = tileMaxY - tileMinY + 1 nbTiles = xCount * yCount total += nbTiles 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) if tiles.fullonly == 1: msg += 'WARNING: stats are approximative because ' \ 'fullonly is activated!\n' msg += 'At zoom %s:\n' % zoom msg += 'We expect %s tiles overall\n' % nbTiles msg += 'Min X is %s, Max X is %s\n' % (tileMinX, tileMaxX) msg += '%s columns over X\n' % xCount msg += 'Min Y is %s, Max Y is %s\n' % (tileMinY, tileMaxY) msg += '%s rows over Y\n' % yCount msg += '\n' msg += 'A tile side is around %s meters' % int(round(length)) if nbTiles > 0 and nbObjects is not None: msg += 'We have an average of about %s triangles ' \ 'per tile\n' % int(round(nbObjects / nbTiles)) msg += '\n\n' msg += '%s tiles in total.' % total except Exception as e: logger.error('An error occured during statistics collection') logger.error('%s' % e, exc_info=True) raise Exception(e) finally: db.userEngine.dispose() return (total, msg)
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 _stats(self, withDb=True): self.t0 = time.time() total = 0 msg = '\n' tiles = TerrainTiles(self.dbConfigFile, self.tmsConfig, self.t0) geodetic = GlobalGeodetic(True) bounds = (tiles.minLon, tiles.minLat, tiles.maxLon, tiles.maxLat) zooms = range(tiles.tileMinZ, tiles.tileMaxZ + 1) db = DB('configs/terrain/database.cfg') try: with db.userSession() as session: for i in xrange(0, len(zooms)): zoom = zooms[i] model = modelsPyramid.getModelByZoom(zoom) nbObjects = None if withDb: nbObjects = session.query(model).filter( model.bboxIntersects(bounds)).count() tileMinX, tileMinY = geodetic.LonLatToTile( bounds[0], bounds[1], zoom) tileMaxX, tileMaxY = geodetic.LonLatToTile( bounds[2], bounds[3], zoom) # Fast approach, but might not be fully correct if tiles.fullonly == 1: tileMinX += 1 tileMinY += 1 tileMaxX -= 1 tileMaxY -= 1 tileBounds = geodetic.TileBounds(tileMinX, tileMinY, zoom) xCount = tileMaxX - tileMinX + 1 yCount = tileMaxY - tileMinY + 1 nbTiles = xCount * yCount total += nbTiles 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) if tiles.fullonly == 1: msg += 'WARNING: stats are approximative because ' \ 'fullonly is activated!\n' msg += 'At zoom %s:\n' % zoom msg += 'We expect %s tiles overall\n' % nbTiles msg += 'Min X is %s, Max X is %s\n' % (tileMinX, tileMaxX) msg += '%s columns over X\n' % xCount msg += 'Min Y is %s, Max Y is %s\n' % (tileMinY, tileMaxY) msg += '%s rows over Y\n' % yCount msg += '\n' msg += 'A tile side is around %s meters' % int( round(length)) if nbTiles > 0 and nbObjects is not None: msg += 'We have an average of about %s triangles ' \ 'per tile\n' % int(round(nbObjects / nbTiles)) msg += '\n\n' msg += '%s tiles in total.' % total except Exception as e: logger.error('An error occured during statistics collection') logger.error('%s' % e, exc_info=True) raise Exception(e) finally: db.userEngine.dispose() return (total, msg)