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
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 _areaQuads(self, area): """ """ xmin, ymin, xmax, ymax = area.bounds ul = Coordinate(ymin, xmin, self.zpixel).zoomTo(self.zgroup).container() lr = Coordinate(ymax, xmax, self.zpixel).zoomTo(self.zgroup).container() quads = set() for x in range(int(1 + lr.column - ul.column)): for y in range(int(1 + lr.row - ul.row)): coord = ul.right(x).down(y) quads.add(quadkey(coord)) return quads
'http://c.tile.openstreetmap.org/6/15/28.png'] paths = [urlparse(href).path for href in hrefs] tiles = [splitext(path)[0].lstrip('/') for path in paths] values = [map(int, tile.split('/', 2)) for tile in tiles] rows, cols = zip(*[(y, x) for (z, x, y) in values]) rows_cols = [] for row in range(min(rows), max(rows) + 1): for col in range(min(cols), max(cols) + 1): rows_cols.append((row, col)) for (index, (row, col)) in enumerate(rows_cols): coord = Coordinate(row, col, zoom) filename = '%(output_dir)s/%(zoom)d-%(col)d-%(row)d.tif' % locals() print 'echo', '-' * 80, index, 'of', len(rows_cols), filename ll = mercator.coordinateProj(coord.down()) ur = mercator.coordinateProj(coord.right()) print 'gdalwarp', print '-t_srs "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"', print '-te', ll.x, ll.y, ur.x, ur.y, print '-tr', scale, scale, print '-tps -r cubicspline', print '-co COMPRESS=JPEG', print 'landcover-1km-to-merc.vrt', print filename
rows, cols = zip(*[(y, x) for (z, x, y) in values]) rows_cols = [] for row in range(min(rows), max(rows) + 1): for col in range(min(cols), max(cols) + 1): rows_cols.append((row, col)) for (index, (row, col)) in enumerate(rows_cols): coord = Coordinate(row, col, zoom) filename = '%(output_dir)s/%(zoom)d-%(col)d-%(row)d.tif' % locals() print 'echo', '-' * 80, index, 'of', len(rows_cols), filename ll = mercator.coordinateProj(coord.down()) ur = mercator.coordinateProj(coord.right()) print 'gdalwarp', print '-t_srs "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"', print '-te', ll.x, ll.y, ur.x, ur.y, print '-tr', scale, scale, print '-tps -r cubicspline', print '-co COMPRESS=JPEG', print 'landcover-1km-to-merc.vrt', print filename ########NEW FILE######## __FILENAME__ = mapping from imposm.mapping import Options, Polygons, LineStrings, PseudoArea, GeneralizedTable, meter_to_mapunit
def renderTile(self, width, height, srs, coord): """ """ img = Image.new('RGB', (width, height), colors[0]) draw = ImageDraw(img) interactivity_array = [] base_zoom = coord.zoom base_row = coord.row base_column = coord.column #We're showing detail for three zooms in from here, as fat pixels (32 pix) #256 pixels / tile = 8 pixels / tile = 32 pixels (which is 2^5) tile_pixel_width = 256 #print 'coord:', coord #print 'base_zoom:', base_zoom # 256 pixel tile == 2^8 pixel tile, so this is a constant tile_power_of_two = 8 # we want 8x8 fatbits == 2^3 pixel fatbits #TODO: use self.cell_size to find log of 2 to x? pixel_power_of_two = int(log(self.cell_size, 2)) fat_pixel_width = 2**pixel_power_of_two self.fat_pixel_count = 2**(tile_power_of_two - pixel_power_of_two) # adjust the coord to be the pixel zoom coord = coord.zoomBy(tile_power_of_two - pixel_power_of_two) #print "fat_pixel_count: ", fat_pixel_count #print "coord: ", coord #print 'over_sample_zoom_tile_width: ', over_sample_zoom_tile_width #find the fat_pixel with the maximum photo count max_count = 0 top_count = 0 top_margin = 0 #We should be seeing 64 cells (8x8) output image for row in range(self.fat_pixel_count): for col in range(self.fat_pixel_count): ul = coord.right(col).down(row) lr = ul.right().down() #Calculate key for the size dict subquad = Coordinate(ul.row, ul.column, ul.zoom) #print 'subquad:', subquad # these values should always be within (0, 256) x1 = col * fat_pixel_width x2 = (col + 1) * fat_pixel_width y1 = row * fat_pixel_width y2 = (row + 1) * fat_pixel_width #Draw fat pixel based on the returned color based on count (size) in that subquad in the dictionary #Implied that no-data is color[0], above where the img is instantiated enumeration = count_votes(self, subquad) if max_count < enumeration["photo_count_total"]: max_count = enumeration["photo_count_total"] if top_count < enumeration["photo_count0"]: top_count = enumeration["photo_count0"] if self.output_format == "utf_grid": nw = osm.coordinateLocation(subquad) se = osm.coordinateLocation(subquad.right().down()) lat = (nw.lat - se.lat) / 2 + se.lat lon = (se.lon - nw.lon) / 2 + nw.lon interactivity_array.append({ "photo_count_total": enumeration["photo_count_total"], "woe_id": enumeration["woe_id0"], #"woe_id_lau":enumeration["woe_id0_lau"], #"woe_id_adm2":enumeration["woe_id0_adm2"], #"woe_id_adm1":enumeration["woe_id0_adm1"], "woe_id_adm0": enumeration["woe_id0_adm0"], "name": enumeration["name0"], "photo_count": enumeration["photo_count0"], "margin": enumeration["margin0"], "latitude": lat, "longitude": lon, "x1": str(nw.lon), "y1": str(se.lat), "x2": str(se.lon), "y2": str(nw.lat), "row": str(base_row + row), "col": str(base_column + col), "zoom": coord.zoom }) elif self.method == "size_log": draw.rectangle( (x1, y1, x2, y2), size_color_log(int(enumeration[self.input_field]))) elif self.method == "size": draw.rectangle( (x1, y1, x2, y2), size_color(int(enumeration[self.input_field]))) elif self.method == "unique_id": draw.rectangle((x1, y1, x2, y2), size_color_unique_id( int(enumeration[self.input_field]))) if self.output_format == "utf_grid": #print "interactivity_array: ", interactivity_array #grid_utf = create_utf_grid( self, interactivity_array ) grid_utf = {'grid': ['', '.']} if max_count == 0: raise NothingToSeeHere() is_saveable = False # Are we at the last requested zoom? if coord.zoom == self.max_zoom: is_saveable = True # Are we at the minimum viable zoom but with little to no data? if (coord.zoom >= self.min_zoom) and (max_count <= self.min_size): is_saveable = True # Are we viable zoom, viable count, and no ambiguity as to the 100% within margin the winner? if (coord.zoom >= (self.min_zoom + 2)) and (max_count > self.max_size) and ( (top_count >= (max_count * self.margin_percent)) or ((max_count - top_count) < self.min_size)): is_saveable = True # Don't want to dig for needles #if coord.zoom == 17 and base_row == 50816 and base_column == 21045: # print '(coord.zoom >= (self.min_zoom + 1)) and ((max_count - top_count) < self.min_size):' # print coord.zoom,(self.min_zoom + 1),max_count, top_count, self.min_size if (coord.zoom >= (self.min_zoom + 1)) and ( (max_count - top_count) < self.min_size): #(max_count > self.min_size) and is_saveable = True # and (interactivity_array["margin"] >= self.margin_percent) if is_saveable: #print "should save to DB" #print "interactivity_array: ", interactivity_array saveTileToDatabase(self, interactivity_array) raise NothingMoreToSeeHere( SaveableResponse(json.dumps(grid_utf))) else: return SaveableResponse(json.dumps(grid_utf)) elif self.output_format == "geojson": grid_utf = create_utf_grid(self, interactivity_array) return SaveableResponse(json.dumps(grid_utf)) else: return img
def renderTile(self, width, height, srs, coord): """ """ img = Image.new('RGB', (width, height), colors[0]) draw = ImageDraw(img) interactivity_array = [] base_zoom = coord.zoom base_row = coord.row base_column = coord.column #We're showing detail for three zooms in from here, as fat pixels (32 pix) #256 pixels / tile = 8 pixels / tile = 32 pixels (which is 2^5) tile_pixel_width = 256 #print 'coord:', coord #print 'base_zoom:', base_zoom # 256 pixel tile == 2^8 pixel tile, so this is a constant tile_power_of_two = 8 # we want 8x8 fatbits == 2^3 pixel fatbits #TODO: use self.cell_size to find log of 2 to x? pixel_power_of_two = int(log( self.cell_size, 2 )) fat_pixel_width = 2**pixel_power_of_two self.fat_pixel_count = 2**(tile_power_of_two - pixel_power_of_two) # adjust the coord to be the pixel zoom coord = coord.zoomBy(tile_power_of_two - pixel_power_of_two) #print "fat_pixel_count: ", fat_pixel_count #print "coord: ", coord #print 'over_sample_zoom_tile_width: ', over_sample_zoom_tile_width #find the fat_pixel with the maximum photo count max_count = 0 top_count = 0 top_margin = 0 #We should be seeing 64 cells (8x8) output image for row in range( self.fat_pixel_count ): for col in range( self.fat_pixel_count ): ul = coord.right(col).down(row) lr = ul.right().down() #Calculate key for the size dict subquad = Coordinate(ul.row, ul.column, ul.zoom) #print 'subquad:', subquad # these values should always be within (0, 256) x1 = col * fat_pixel_width x2 = (col + 1) * fat_pixel_width y1 = row * fat_pixel_width y2 = (row + 1) * fat_pixel_width #Draw fat pixel based on the returned color based on count (size) in that subquad in the dictionary #Implied that no-data is color[0], above where the img is instantiated enumeration = count_votes( self, subquad ) if max_count < enumeration["photo_count_total"]: max_count = enumeration["photo_count_total"] if top_count < enumeration["photo_count0"]: top_count = enumeration["photo_count0"] if self.output_format == "utf_grid": nw = osm.coordinateLocation(subquad) se = osm.coordinateLocation(subquad.right().down()) lat = (nw.lat - se.lat) / 2 + se.lat lon = (se.lon - nw.lon) / 2 + nw.lon interactivity_array.append( { "photo_count_total":enumeration["photo_count_total"], "woe_id":enumeration["woe_id0"], #"woe_id_lau":enumeration["woe_id0_lau"], #"woe_id_adm2":enumeration["woe_id0_adm2"], #"woe_id_adm1":enumeration["woe_id0_adm1"], "woe_id_adm0":enumeration["woe_id0_adm0"], "name":enumeration["name0"], "photo_count":enumeration["photo_count0"], "margin":enumeration["margin0"], "latitude":lat, "longitude":lon, "x1": str(nw.lon), "y1": str(se.lat), "x2": str(se.lon), "y2": str(nw.lat), "row":str(base_row + row), "col":str(base_column + col), "zoom": coord.zoom } ) elif self.method == "size_log": draw.rectangle((x1, y1, x2, y2), size_color_log(int( enumeration[self.input_field]) )) elif self.method == "size": draw.rectangle((x1, y1, x2, y2), size_color(int( enumeration[self.input_field]) )) elif self.method == "unique_id": draw.rectangle((x1, y1, x2, y2), size_color_unique_id(int( enumeration[self.input_field]) )) if self.output_format == "utf_grid": #print "interactivity_array: ", interactivity_array #grid_utf = create_utf_grid( self, interactivity_array ) grid_utf = { 'grid':['','.'] } if max_count == 0: raise NothingToSeeHere() is_saveable = False # Are we at the last requested zoom? if coord.zoom == self.max_zoom: is_saveable = True # Are we at the minimum viable zoom but with little to no data? if (coord.zoom >= self.min_zoom) and (max_count <= self.min_size): is_saveable = True # Are we viable zoom, viable count, and no ambiguity as to the 100% within margin the winner? if (coord.zoom >= (self.min_zoom + 2)) and (max_count > self.max_size) and ((top_count >= (max_count * self.margin_percent)) or ((max_count - top_count) < self.min_size)): is_saveable = True # Don't want to dig for needles #if coord.zoom == 17 and base_row == 50816 and base_column == 21045: # print '(coord.zoom >= (self.min_zoom + 1)) and ((max_count - top_count) < self.min_size):' # print coord.zoom,(self.min_zoom + 1),max_count, top_count, self.min_size if (coord.zoom >= (self.min_zoom + 1)) and ((max_count - top_count) < self.min_size): #(max_count > self.min_size) and is_saveable = True # and (interactivity_array["margin"] >= self.margin_percent) if is_saveable: #print "should save to DB" #print "interactivity_array: ", interactivity_array saveTileToDatabase( self, interactivity_array ) raise NothingMoreToSeeHere( SaveableResponse(json.dumps(grid_utf)) ) else: return SaveableResponse(json.dumps(grid_utf)) elif self.output_format == "geojson": grid_utf = create_utf_grid( self, interactivity_array ) return SaveableResponse(json.dumps(grid_utf)) else: return img