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
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'
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))
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'
def list_tiles_for_bounds(image, s2p, paper_width_pt, paper_height_pt, north, west, south, east): """ Return a list of coordinates and scan-to-coord functions for a full set of zoom levels. Internal work is done by list_tiles_for_bounds_zoom(). """ osm = OpenStreetMapProvider() coords = [] for zoom in range(19): # # Coordinates of three print corners # ul = osm.locationCoordinate(Location(north, west)).zoomTo(zoom) ur = osm.locationCoordinate(Location(north, east)).zoomTo(zoom) lr = osm.locationCoordinate(Location(south, east)).zoomTo(zoom) # # Matching points in print and coordinate spaces # ul_pt = Point(1 * ptpin - paper_width_pt, 1.5 * ptpin - paper_height_pt) ul_co = Point(ul.column, ul.row) ur_pt = Point(0, 1.5 * ptpin - paper_height_pt) ur_co = Point(ur.column, ur.row) lr_pt = Point(0, 0) lr_co = Point(lr.column, lr.row) scan_dim = hypot(image.size[0], image.size[1]) zoom_dim = hypot((lr_co.x - ul_co.x) * 256, (lr_co.y - ul_co.y) * 256) if zoom_dim / scan_dim < .05: # too zoomed-out continue if zoom_dim / scan_dim > 3.: # too zoomed-in break # # scan2coord by way of scan2print and print2coord # p2c = triangle2triangle(ul_pt, ul_co, ur_pt, ur_co, lr_pt, lr_co) s2c = s2p.multiply(p2c) coords += list_tiles_for_bounds_zoom(image, s2c, zoom) return coords
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
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
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))
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))
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 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 list_tiles_for_bounds(image, s2p, paper_width_pt, paper_height_pt, north, west, south, east): """ Return a list of coordinates and scan-to-coord functions for a full set of zoom levels. Internal work is done by list_tiles_for_bounds_zoom(). """ osm = OpenStreetMapProvider() coords = [] for zoom in range(19): # # Coordinates of three print corners # ul = osm.locationCoordinate(Location(north, west)).zoomTo(zoom) ur = osm.locationCoordinate(Location(north, east)).zoomTo(zoom) lr = osm.locationCoordinate(Location(south, east)).zoomTo(zoom) # # Matching points in print and coordinate spaces # ul_pt = Point(1 * ptpin - paper_width_pt, 1.5 * ptpin - paper_height_pt) ul_co = Point(ul.column, ul.row) ur_pt = Point(0, 1.5 * ptpin - paper_height_pt) ur_co = Point(ur.column, ur.row) lr_pt = Point(0, 0) lr_co = Point(lr.column, lr.row) scan_dim = hypot(image.size[0], image.size[1]) zoom_dim = hypot((lr_co.x - ul_co.x) * 256, (lr_co.y - ul_co.y) * 256) if zoom_dim/scan_dim < .05: # too zoomed-out continue if zoom_dim/scan_dim > 3.: # too zoomed-in break # # scan2coord by way of scan2print and print2coord # p2c = triangle2triangle(ul_pt, ul_co, ur_pt, ur_co, lr_pt, lr_co) s2c = s2p.multiply(p2c) coords += list_tiles_for_bounds_zoom(image, s2c, zoom) return coords
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
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
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
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
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)
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 = ""
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
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:
print len(places._moveable), 'moveable places vs.', len(places._places), 'others' print '-' * 80 def state_energy(places): return places.energy() def state_move(places): places.move() places, e = Annealer(state_energy, state_move).auto(places, minutes, 50) print '-' * 80 osm = Provider() point_features, label_features = [], [] visible_places = [] for place in sorted(places): is_visible = True for other in visible_places: if place.overlaps(other): print 'skip', place.name, 'because of', other.name is_visible = False break if not is_visible: continue
from TileStache import requestHandler from ModestMaps.OpenStreetMap import Provider from ModestMaps.Geo import Location from json import loads osm = Provider() loc = Location(37.795545, -122.393422) stuff = [ ('highroad', 10), ('highroad', 11), ('highroad', 12), ('highroad', 13), ('highroad', 14), ('highroad', 15), ('highroad-2x', 9), ('highroad-2x', 10), ('highroad-2x', 11), ('highroad-2x', 12), ('highroad-2x', 13), ('highroad-2x', 14), ('skeletron', 12), ('skeletron', 13), ('skeletron', 14), ('skeletron', 15), ('skeletron', 16), ('skeletron-2x', 11), ('skeletron-2x', 12), ('skeletron-2x', 13), ('skeletron-2x', 14), ('skeletron-2x', 15),
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
# content['crs'] = {'type': 'link', 'properties': {'href': '0.wkt', 'type': 'ogcwkt'}} #else: # del content['crs'] elif format in ('PNG'): content = self.content else: raise KnownUnknown('FourSquare response only saves .png, .json, and .geojson tiles, not "%s"' % format) # # Encode # if format in ('GeoJSON'): #indent = self.verbose and 2 or None indent = 2 encoded = JSONEncoder(indent=indent).iterencode(content) out.write(encoded) elif format in ('JSON'): out.write(content) elif format in ('PNG'): out.write(content) if __name__ == '__main__': p = Provider(None) #This is done in an odd order where the Zoom is last p.renderTile(256, 256, '', Coordinate(3, 2, 3)).save('out.png')
from ModestMaps.Geo import Location from ModestMaps.OpenStreetMap import Provider from ModestMaps.Core import Point, Coordinate try: import pyproj except ImportError: # don't worry about it until GeometryCustom is actually instantiated. pass from .places import Place __version__ = 'N.N.N' _osm = Provider() key_pat = compile(r'\W') int_pat = compile(r'^-?\d{1,9}$') # up to nine so we don't cross 2^32 float_pat = compile(r'^-?\d+(\.\d+)?$') class GeometryWebmercator: """ """ def __init__(self, zoom): """ """ self.zoom = zoom self.radius = 2**(zoom + 7)