Ejemplo n.º 1
0
def retrieve_zoom_features(loc, zoom):
    ''' Retrieve all features enclosing a given point location at a zoom level.
    
        Requests TopoJSON tile from forever.codeforamerica.org spatial index,
        decodes bounding boxes and geometries if necessary, then yields a stream
        of any feature feature whose geometry covers the requested point.
    '''
    osm = Provider()

    point = Point(loc.lon, loc.lat)
    coord = osm.locationCoordinate(loc).zoomTo(zoom)
    path = '%(zoom)d/%(column)d/%(row)d' % coord.__dict__
    url = 'http://forever.codeforamerica.org/Census-API/by-tile/%s.topojson.gz' % path

    resp = get(url)
    topo = resp.json()

    print >> stderr, 'request took', resp.elapsed, 'from', url, 'in', hex(
        get_ident())

    start = time()

    assert topo['type'] == 'Topology'

    bbox_fails, shape_fails = 0, 0

    for layer in topo['objects']:
        if zoom == 8:
            assert layer in ('state', 'county', 'place', 'cbsa')
        elif zoom == 10:
            assert layer in ('zcta510', 'tract')
        else:
            raise Exception('Unknown layer %d' % zoom)

        for object in topo['objects'][layer]['geometries']:
            x_, y_, _x, _y = object['bbox']

            obj_box = Polygon([(x_, y_), (x_, _y), (_x, _y), (_x, y_),
                               (x_, y_)])

            if not point.within(obj_box):
                # object failed a simple bounding box check and can be discarded.
                bbox_fails += 1
                continue

            obj_shp = decode(object, topo)

            if not point.within(obj_shp):
                # object failed a point-in-polygon check and can be discarded.
                shape_fails += 1
                continue

            p = object['properties']

            yield p.get('NAME', None), p.get('NAMELSAD', None), p.get(
                'GEOID', None), p.get('GEOID10', None)

    print >> stderr, 'check took', (time() - start), 'seconds', 'in', hex(
        get_ident(
        )), 'with', bbox_fails, 'bbox fails and', shape_fails, 'shape fails'
Ejemplo n.º 2
0
def tilelist(data,zooms):   
        if (data["filetype"]):
            coordinates = MBTiles.list_tiles(data["filename"])
    
        else:
            lat1, lon1, lat2, lon2 = data["bbox"]
            south, west = min(lat1, lat2), min(lon1, lon2)
            north, east = max(lat1, lat2), max(lon1, lon2)
    
            northwest = Location(north, west)
            southeast = Location(south, east)
            
            osm = Provider()
    
            ul = osm.locationCoordinate(northwest)
            lr = osm.locationCoordinate(southeast)
    
            for (i, zoom) in enumerate(zooms):
                if not zoom.isdigit():
                    raise KnownUnknown('"%s" is not a valid numeric zoom level.' % zoom)
    
                zooms[i] = int(zoom)
            
            if data["padding"] < 0:
                raise KnownUnknown('A negative padding will not work.')
    
            coordinates = generateCoordinates(ul, lr, zooms, data["padding"])
            return coordinates
Ejemplo n.º 3
0
def retrieve_zoom_features(loc, zoom, include_geom, layer_names):
    ''' Retrieve all features enclosing a given point location at a zoom level.
    
        Requests TopoJSON tile from forever.codeforamerica.org spatial index,
        decodes bounding boxes and geometries if necessary, then yields a stream
        of any feature feature whose geometry covers the requested point.
    '''
    osm = Provider()

    point = Point(loc.lon, loc.lat)
    coord = osm.locationCoordinate(loc).zoomTo(zoom)
    path = '%(zoom)d/%(column)d/%(row)d' % coord.__dict__
    url = census_url + 'by-tile/%s.topojson.gz' % path
    
    resp = get(url)
    topo = resp.json()

    debug('request took %.3fs from %s in %s' % (resp.elapsed.total_seconds(), url, hex(get_ident())))
    
    start = time()
    
    assert topo['type'] == 'Topology'
    
    bbox_fails, shape_fails = 0, 0
    
    for layer in topo['objects']:
        if layer_names is not None and layer not in layer_names:
            continue
    
        if zoom in zoom_layers:
            assert layer in zoom_layers[zoom]
        else:
            raise Exception('Unknown layer %d' % zoom)
        
        for object in topo['objects'][layer]['geometries']:
            x_, y_, _x, _y = object['bbox']
            
            obj_box = Polygon([(x_, y_), (x_, _y), (_x, _y), (_x, y_), (x_, y_)])
            
            if not point.within(obj_box):
                # object failed a simple bounding box check and can be discarded.
                bbox_fails += 1
                continue
            
            obj_shp = decode(object, topo)
            
            if not point.within(obj_shp):
                # object failed a point-in-polygon check and can be discarded.
                shape_fails += 1
                continue
            
            feature = {'type': 'Feature', 'properties': object['properties']}
            
            if include_geom:
                feature['geometry'] = obj_shp.__geo_interface__
            
            yield feature
    
    debug('check took %.3fs in %s with %d bbox fails and %d shape fails' % (time() - start, hex(get_ident()), bbox_fails, shape_fails))
Ejemplo n.º 4
0
    def __init__(self, zoom):
        """ Zoom is the base zoom level we're annealing to.
        """
        self.zpixel = zoom + 8
        self.zgroup = zoom
        self.quads = {}

        self.locationCoordinate = Provider().locationCoordinate
Ejemplo n.º 5
0
def iterate_squares(ds, zoom):
    '''
    '''
    xoff, xstride, _, yoff, _, ystride = ds.GetGeoTransform()
    
    minlon, maxlat = xoff, yoff
    maxlon = xoff + ds.RasterXSize * xstride
    minlat = yoff + ds.RasterYSize * ystride
    
    if zoom > 11:
        maxlat = min(58, maxlat)
    
    osm = Provider()
    ul = osm.locationCoordinate(Location(maxlat, minlon)).zoomTo(zoom)
    lr = osm.locationCoordinate(Location(minlat, maxlon)).zoomTo(zoom)
    #lr = osm.locationCoordinate(Location(20, -60)).zoomTo(zoom)
    
    row = int(ul.row)
    while row < lr.row:
        lat = osm.coordinateLocation(Coordinate(row, 0, zoom)).lat
        print >> sys.stderr, 'lat:', round(lat, 2)
        col = int(ul.column)
        while col < lr.column:
            coord = Coordinate(row, col, zoom)
            sw = osm.coordinateLocation(coord.down())
            ne = osm.coordinateLocation(coord.right())
            
            west = max(minlon, sw.lon)
            north = min(maxlat, ne.lat)
            east = min(maxlon, ne.lon)
            south = max(minlat, sw.lat)
            
            left = round((west - xoff) / xstride)
            top = round((north - yoff) / ystride)
            width = round((east - xoff) / xstride) - left
            height = round((south - yoff) / ystride) - top
            
            yield (coord, south, north, int(left), int(top), int(width), int(height))
            
            col += 1
        row += 1
    
    return
    
    x = xmin
    while x < xmax:
        print >> sys.stderr, 'lon:', x
        y = ymin
        while y < ymax:
            left = round((x - xoff) / xstride)
            top = round((y + size - yoff) / ystride)
            width, height = round(size / xstride), round(size / -ystride)

            yield (round(x, 2), round(y, 2), int(left), int(top), int(width), int(height))
        
            y += size
        x += size
Ejemplo n.º 6
0
    def __init__(self, zoom, radius):
        """ Zoom is the base zoom level we're annealing to, radius is
            the pixel radius around each place to check for collisions.
        """
        self.zpixel = zoom + 8
        self.zgroup = zoom + 8 - ceil(log(radius * 2) / log(2))
        self.radius = radius
        self.quads = {}

        self.locationCoordinate = Provider().locationCoordinate
Ejemplo n.º 7
0
def invalidate_feature_cache(layer_key, features):
    """
    invaldiates the cached tiles that contain the features
    @:param features: array of feature objects
    :return:
    """

    logger.info("Invalidating features: {0}".format(features))
    # get bbox of features
    dj_config = Config.objects.get()
    config = dj_config.tilestache_config_object

    logger.info("Config Cache: {0}".format(config.cache))

    from TileStache.Caches import S3, Test
    if isinstance(config.cache, Test):
        return
    osm = Provider()

    try:
        ts_layer = config.layers[layer_key]
    except IndexError:
        logger.exception('Cannot invalidate %r', layer_key)
        return

    cleared = 0
    keys = []
    for f in features:

        lon1, lat1, lon2, lat2 = f.wkb_geometry.extent

        south, west = min(lat1, lat2), min(lon1, lon2)
        north, east = max(lat1, lat2), max(lon1, lon2)
        northwest = Location(north, west)
        southeast = Location(south, east)

        ul = osm.locationCoordinate(northwest)
        lr = osm.locationCoordinate(southeast)

        for coord in generate_coordinates(ul, lr, range(4, 19), padding=0):
            if isinstance(config.cache, S3.Cache):
                keys.append(
                    S3.tile_key(ts_layer, coord, 'png', config.cache.path))
            else:
                logger.info("ts_layer: {0}, coord: {1}".format(
                    ts_layer, coord))
                config.cache.remove(ts_layer, coord, 'png')
            cleared += 1

    if keys:
        config.cache.bucket.delete_keys(keys)

    logger.info("cleared {0} TILES".format(cleared))
Ejemplo n.º 8
0
def coordinates(zoom):
    '''
    '''
    osm = Provider()

    for (col, row) in product(range(2**zoom), range(2**zoom)):
        coord = Coordinate(row, col, zoom)

        sw = osm.coordinateLocation(coord.down())
        ne = osm.coordinateLocation(coord.right())

        yield coord, sw, ne
Ejemplo n.º 9
0
def location_point(lat, lon, zoom):
    """ Return a point that maps to pixels at the requested zoom level for 2^8 tile size.
    """
    try:
        osm = Provider()
    
        location = Location(float(lat), float(lon))
        coord = osm.locationCoordinate(location).zoomTo(zoom + 8)
        point = Point(coord.column, coord.row)
        
        return location, point
    except ValueError:
        raise Exception((lat, lon, zoom))
def search_tile(coord, buildings):
    ''' Search list of buildings for those within a tile coordinate.
    '''
    osm = Provider()
    
    sw = osm.coordinateLocation(coord.down())
    ne = osm.coordinateLocation(coord.right())
    
    found_buildings = [b for b in buildings
                       if sw.lon <= b['longitude'] < ne.lon
                       and sw.lat <= b['latitude'] < ne.lat]
    
    return found_buildings
def starting_tiles(buildings):
    ''' Get tile coordinates at min_zoom for a list of buildings.
    '''
    minlat = min([b['latitude'] for b in buildings])
    minlon = min([b['longitude'] for b in buildings])
    maxlat = max([b['latitude'] for b in buildings])
    maxlon = max([b['longitude'] for b in buildings])
    
    osm = Provider()
    
    ul = osm.locationCoordinate(Location(maxlat, minlon)).zoomTo(min_zoom).container()
    lr = osm.locationCoordinate(Location(minlat, maxlon)).zoomTo(min_zoom).container()
    
    rows, cols = range(int(ul.row), int(lr.row+1)), range(int(ul.column), int(lr.column+1))
    coords = [Coordinate(row, col, min_zoom) for (row, col) in product(rows, cols)]
    
    return coords
Ejemplo n.º 12
0
if __name__ == '__main__':
    options, zooms = parser.parse_args()

    if bool(options.mbtiles_input):
        coordinates = MBTiles.list_tiles(options.mbtiles_input)

    else:
        lat1, lon1, lat2, lon2 = options.bbox
        south, west = min(lat1, lat2), min(lon1, lon2)
        north, east = max(lat1, lat2), max(lon1, lon2)

        northwest = Location(north, west)
        southeast = Location(south, east)
        
        osm = Provider()

        ul = osm.locationCoordinate(northwest)
        lr = osm.locationCoordinate(southeast)

        for (i, zoom) in enumerate(zooms):
            if not zoom.isdigit():
                raise KnownUnknown('"%s" is not a valid numeric zoom level.' % zoom)

            zooms[i] = int(zoom)
        
        if options.padding < 0:
            raise KnownUnknown('A negative padding will not work.')

        coordinates = generateCoordinates(ul, lr, zooms, options.padding)
    
Ejemplo n.º 13
0
def load_places(countriesfile, inputfiles, fonts, zoom):
    """ Load a new Places instance from the named text files for a given zoom.
    """
    osm = Provider()
    places = Places()
    count = 0
    
    for row in DictReader(open(countriesfile, 'r'), dialect='excel'):
        if int(row['zoom']) > zoom:
            continue

        location, point = location_point(row['latitude'], row['longitude'], zoom)
        land_area = float(row['land area km'])
        population = int(row['population'])
        font = fonts['country']
        
        kwargs = {'name': row['name'].decode('utf-8'),
                  'abbreviation': row['abbreviation'].decode('utf-8'),
                  'land_area': land_area,
                  'population': population,
                  'font': font,
                  'zoom': int(row['zoom']),
                  
                  'location': location,
                  'position': point,
        
                  # subtract two because the biggest countries appear at z3
                  'rank': int(row['zoom']) - 2
                 }
        
        neighbors = places.add(Country(**kwargs))
        
        count += 1
        print '%5d)' % count, row['name'], location, point
        
        if neighbors:
            print '       is in range of', ', '.join([n.name for n in neighbors])
    
    for inputfile in inputfiles:
    
        input = inputfile.endswith('.gz') and GzipFile(inputfile, 'r') or open(inputfile, 'r')
    
        for row in DictReader(input, dialect='excel-tab'):
            if int(row['zoom']) > zoom:
                continue

            location, point = location_point(row['latitude'], row['longitude'], zoom)
            
            try:
                population = int(row['population'])
            except ValueError:
                population = None

            if population >= 2500000:
                font = fonts['25m']
            elif population >= 250000:
                font = fonts['250k']
            elif population >= 50000:
                font = fonts['50k']
            else:
                font = fonts['other']
            
            kwargs = {'name': row['name'].decode('utf-8'),
                      'population': population,
                      'font': font,
                      'zoom': int(row['zoom']),
                      
                      'geonameid': row['geonameid'],
                      'location': location,
                      'position': point,
            
                      # subtract three because the biggest cities appear at z4
                      'rank': int(row['zoom']) - 3
                     }
            
            if zoom >= 9:
                neighbors = places.add(HighZoomCity(**kwargs))
            else:
                neighbors = places.add(City(**kwargs))
            
            count += 1
            print '%5d)' % count, row['name'], location, point
            
            if neighbors:
                print '       is in range of', ', '.join([n.name for n in neighbors])
    
    return places
Ejemplo n.º 14
0
from re import compile
from csv import DictReader
from sys import argv, stderr
from math import cos, pi
import json

from dateutil import parser

from ModestMaps import mapByExtent
from ModestMaps.Core import Point
from ModestMaps.Geo import Location
from ModestMaps.OpenStreetMap import Provider

import lib

provider = Provider()
dimensions = Point(960, 600)

base_url = 'http://osm-extracted-metros.s3.amazonaws.com/log.txt'
extract_pat = compile(r'^((\S+)\.osm\.(bz2|pbf))\s+(\d+)$')
coastshape_pat = compile(r'^((\S+)\.coastline\.zip)\s+(\d+)$')
shp_imposm_pat = compile(r'^((\S+)\.imposm-shapefiles\.zip)\s+(\d+)$')
shp_osm2pgsql_pat = compile(r'^((\S+)\..*\bshapefiles\.zip)\s+(\d+)$')
coastline_pat = compile(r'^((\w+)-(latlon|merc)\.tar\.bz2)\s+(\d+)$')
months = '- Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split()

def nice_size(size):
    KB = 1024.
    MB = 1024. * KB
    GB = 1024. * MB
    TB = 1024. * GB
Ejemplo n.º 15
0
from PIL.ImageDraw import ImageDraw
from PIL import Image

from ModestMaps.Core import Coordinate
from ModestMaps.Tiles import toMicrosoft
from ModestMaps.Core import Coordinate
from ModestMaps.OpenStreetMap import Provider

from TileStache.Core import KnownUnknown
from TileStache.Vector import VectorResponse

from tilestacheexceptions import NothingMoreToSeeHere
from tilestacheexceptions import NothingToSeeHere

osm = Provider()

colors = [(0, 0, 0), (8, 29, 88), (37, 52, 148), (34, 94, 168), (29, 145, 192),
          (65, 182, 196), (127, 205, 187), (199, 233, 180), (237, 248, 177),
          (255, 255, 217)]

cell_size = 8
min_size = 1
max_size = 1000000
log_base = (max_size - min_size)**(1. / len(colors))
method = ""

fat_pixel_count = 32

input_field = ""
woe_field = ""
Ejemplo n.º 16
0
from bootstrap import app, db
from models import Commune
from config import API_PREFIX
from ModestMaps.OpenStreetMap import Provider
from ModestMaps.Core import Coordinate
from shapely.geometry import box
from flask import request, jsonify, abort
from flask_cors import cross_origin

OSM = Provider()


def as_bbox(se, nw, srid=4326):
    wkt = box(nw.lon, se.lat, se.lon, nw.lat).wkt
    return 'SRID=%d;%s' % (srid, wkt)


@app.route(API_PREFIX + '/communes/tiles/<int:z>/<int:x>/<int:y>.geojson',
           methods=['GET'])
@cross_origin()
def tile(x, y, z):

    # start = time()
    # TODO Add z limit -> 204 No Content
    c = Coordinate(y, x, z)
    nw = OSM.coordinateLocation(c)
    se = OSM.coordinateLocation(c.down().right())
    box = as_bbox(se, nw, 4326)
    query = Commune.query.filter(Commune.centroid.intersects(box))
    features = []
    for f in query: