Esempio n. 1
0
    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='SRID=4326;' + polygon.ExportToWkt()))
                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))
Esempio n. 2
0
def createTerrainBasedTileJSON(params):
    baseUrls = getBaseUrls(params)
    with open('configs/terrain/layer.json') as f:
        terrainConfig = json.loads(f.read())

    # Delete unrelevant fields
    if 'extensions' in terrainConfig:
        del terrainConfig['extensions']
    # Overwrite
    terrainConfig['minzoom'] = params.minZoom
    terrainConfig['maxzoom'] = params.maxZoom
    terrainConfig['name'] = params.name
    terrainConfig['format'] = params.format
    terrainConfig['description'] = params.description
    terrainConfig['attribution'] = params.attribution
    terrainConfig['tiles'] = baseUrls

    geodetic = GlobalGeodetic(True)
    # Fix with empty ranges if we start with a biggeer min zoom
    for i in range(0, params.maxZoom + 1):
        if i < params.minZoom:
            terrainConfig['available'][i] = []
        # Max zoom is heigher than max terrain zoom level
        # In that case we include the full range within the bounds
        if i >= len(terrainConfig['available']):
            tileMinX, tileMinY = geodetic.LonLatToTile(
                params.bounds[0], params.bounds[1], i
            )
            tileMaxX, tileMaxY = geodetic.LonLatToTile(
                params.bounds[2], params.bounds[3], i
            )
            terrainConfig['available'].append(dict(
                startX=tileMinX,
                endX=tileMaxX,
                startY=tileMinY,
                endY=tileMaxY
            ))
    return json.dumps(terrainConfig)
Esempio n. 3
0
 def _initPyramidMetadata(self):
     # It keeps track of the starting and ending tiles
     # and the missing tiles in between
     self.metadata = {}
     self.ranges = {}
     geodetic = GlobalGeodetic(True)
     bounds = self.meta['bounds']
     # Assume the whole extent is available
     for z in range(self.tileMinZoom, self.tileMaxZoom + 1):
         tileMinX, tileMinY = geodetic.LonLatToTile(bounds[0], bounds[1], z)
         tileMaxX, tileMaxY = geodetic.LonLatToTile(bounds[2], bounds[3], z)
         self.metadata[z] = dict(x=[tileMinX, tileMaxX],
                                 y=[tileMinY, tileMaxY])
         self.ranges[z] = {}
Esempio n. 4
0
MAXX = maxPoint.GetX()
MAXY = maxPoint.GetY()
print 'Extent :'
print[MINX, MINY, MAXX, MAXY]
MINZOOM = 3
MAXZOOM = 17

geodetic = GlobalGeodetic(True)
drv = ogr.GetDriverByName('ESRI Shapefile')
directory = '.tmp/'
extension = '.shp'

# Generate table with min max tile coordinates for all zoomlevels
for tz in range(MINZOOM, MAXZOOM + 1):
    filePathTarget = '%s%s%s' % (directory, tz, extension)
    tminx, tminy = geodetic.LonLatToTile(MINX, MINY, tz)
    tmaxx, tmaxy = geodetic.LonLatToTile(MAXX, MAXY, tz)

    dataSource = drv.CreateDataSource(filePathTarget)
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)
    layer = dataSource.CreateLayer('%s' % tz, srs, ogr.wkbPolygon)
    fieldKey = ogr.FieldDefn('Key', ogr.OFTString)
    fieldKey.SetWidth(24)
    layer.CreateField(fieldKey)

    for tx in range(tminx, tmaxx + 1):
        for ty in range(tminy, tmaxy + 1):
            tileKey = '%s/%s/%s' % (tz, tx, ty)
            [xmin, ymin, xmax, ymax] = geodetic.TileBounds(tx, ty, tz)
            print 'Tile key'