def close(lock, path, file, min_zoom, max_zoom): connection = sqlite3.connect(file, check_same_thread=False) c = connection.cursor() c.execute( "SELECT min(tile_row), max(tile_row), min(tile_column), max(tile_column) FROM tiles WHERE zoom_level = ?", [max_zoom]) min_y, max_y, min_x, max_x = c.fetchone() min_y = (2**max_zoom) - min_y - 1 max_y = (2**max_zoom) - max_y - 1 min_lat, min_lon = num2deg(min_x, min_y, max_zoom) max_lat, max_lon = num2deg(max_x + 1, max_y + 1, max_zoom) bounds = [min_lon, min_lat, max_lon, max_lat] bounds_string = ','.join(map(str, bounds)) center = [(min_lon + max_lon) / 2, (min_lat + max_lat) / 2, max_zoom] center_string = ','.join(map(str, center)) c.execute("UPDATE metadata SET value = ? WHERE name = 'bounds'", [bounds_string]) c.execute("UPDATE metadata SET value = ? WHERE name = 'center'", [center_string]) connection.commit()
def fetch_style_images(self, city, zoom=16, hq=True): city_polygon = utils.polygon_union(city['features']) city_bounds = city_polygon.bounds all_style_imgs = [] fetcher = MapBoxFetcher() lat = city_bounds[3] lon = city_bounds[0] while lat >= city_bounds[1]: while lon <= city_bounds[2]: numx, numy = utils.deg2num(lat, lon, zoom) style_img = fetcher.style(lat, lon, zoom, hq=hq) all_style_imgs.append(style_img.bw_array()) lon = utils.num2deg( utils.deg2num(lat, lon, zoom)[0] + 1, utils.deg2num(lat, lon, zoom)[1], zoom)[1] lon = city_bounds[0] lat = utils.num2deg( utils.deg2num(lat, lon, zoom)[0], utils.deg2num(lat, lon, zoom)[1] + 1, zoom)[0] return all_style_imgs
def write_josm_file(self, filename, tilezoom=14): """ create a osm file for the editor JOSM, that only contains the download boundary information. Load the file in JOSM and update the data. Note: Please do not missuse this function to download large areas with josm """ from shapely.geometry import LineString, Polygon f_out = open(filename, 'w') f_out.write("<?xml version='1.0' encoding='UTF-8'?>\n") f_out.write("<osm version='0.6' upload='true' generator='JOSM'>\n") for i, op in enumerate(self.outer_polygons): # create coordinate list and then a polygon plist = [(node.lat, node.lon) for node in op] outer_polygon = Polygon(LineString(plist)) if not outer_polygon.is_valid: raise ValueError('outer polygon no %i is not valid' % (i + 1)) (minlat, minlon, maxlat, maxlon) = outer_polygon.bounds (x1, y2) = deg2num(minlat, minlon, tilezoom) (x2, y1) = deg2num(maxlat, maxlon, tilezoom) for ty in range(y1, y2 + 1): for tx in range(x1, x2 + 1): tile_rectangle = [ num2deg(tx, ty, tilezoom), num2deg(tx + 1, ty, tilezoom), num2deg(tx + 1, ty + 1, tilezoom), num2deg(tx, ty + 1, tilezoom), num2deg(tx, ty, tilezoom) ] tile_polygon = Polygon(tile_rectangle) if outer_polygon.contains( tile_polygon) or outer_polygon.intersects( tile_polygon): minlat = tile_rectangle[3][0] minlon = tile_rectangle[3][1] maxlat = tile_rectangle[1][0] maxlon = tile_rectangle[1][1] f_out.write(' <bounds minlat="%.7f" minlon="%.7f" maxlat="%.7f" maxlon="%.7f" />\n' \ % (minlat-0.0000001, minlon-0.0000001, maxlat+0.0000001, maxlon+0.0000001)) f_out.write("</osm>\n") f_out.close
def write_josm_file(self, filename, tilezoom=14): """ create a osm file for the editor JOSM, that only contains the download boundary information. Load the file in JOSM and update the data. Note: Please do not missuse this function to download large areas with josm """ from shapely.geometry import LineString, Polygon f_out = open(filename,'w') f_out.write("<?xml version='1.0' encoding='UTF-8'?>\n") f_out.write("<osm version='0.6' upload='true' generator='JOSM'>\n") for i, op in enumerate(self.outer_polygons): # create coordinate list and then a polygon plist = [(node.lat, node.lon) for node in op] outer_polygon = Polygon(LineString(plist)) if not outer_polygon.is_valid: raise ValueError('outer polygon no %i is not valid' % (i+1)) (minlat, minlon, maxlat, maxlon) = outer_polygon.bounds (x1, y2) = deg2num(minlat, minlon, tilezoom) (x2, y1) = deg2num(maxlat, maxlon, tilezoom) for ty in range(y1, y2 + 1): for tx in range(x1, x2 + 1): tile_rectangle = [num2deg(tx, ty, tilezoom), num2deg(tx+1, ty, tilezoom), num2deg(tx+1, ty+1, tilezoom), num2deg(tx, ty+1, tilezoom), num2deg(tx, ty, tilezoom)] tile_polygon = Polygon(tile_rectangle) if outer_polygon.contains(tile_polygon) or outer_polygon.intersects(tile_polygon): minlat = tile_rectangle[3][0] minlon = tile_rectangle[3][1] maxlat = tile_rectangle[1][0] maxlon = tile_rectangle[1][1] f_out.write(' <bounds minlat="%.7f" minlon="%.7f" maxlat="%.7f" maxlon="%.7f" />\n' \ % (minlat-0.0000001, minlon-0.0000001, maxlat+0.0000001, maxlon+0.0000001)) f_out.write("</osm>\n") f_out.close
def extract_video_tiles(tiles, year, hansen_db, forest_loss_dir): out_fl = os.path.join(forest_loss_dir, year) create_dir(out_fl) fl_template = 'fl{year}_{z}_{x}_{y}.npy' # for z,x,y in [(12, 1260, 2185)]: for z, x, y in tiles: lon, lat = num2deg(int(x), int(y), int(z)) int_year = int(year[2:]) extract_fc_and_fl_tile(hansen_db, lon, lat, int_year, os.path.join(out_fl, fl_template.format(year=year, z=z, x=x, y=y)))
def extract_forma_tiles(tiles, year, forma_db, forest_loss_dir): # TODO: CHANGE SAVE_FC SAVING MODE!!!!!!! out_fl = os.path.join(forest_loss_dir) create_dir(out_fl) fl_template = 'forma{year}_{z}_{x}_{y}.npy' for z, x, y in tiles: out_name = os.path.join(out_fl, fl_template.format(z=z, x=x, y=y)) lon, lat = num2deg(int(x), int(y), int(z)) int_year = int(year[2:]) img_arr = extract_tile(hansen_db, lon, lat, 256, crs='ESPG:4326') save_fc(img_arr, out_name, int_year)
def main(): gee_dir = '/mnt/ds3lab-scratch/lming/gee_data/images_forma_compare' # with open('/mnt/ds3lab-scratch/lming/gee_data/forma_tiles2017.pkl', 'rb') as f: # tiles = pkl.load(f) tiles = [('11', '753', '1076'), ('11', '773', '1071')] forma_dir = os.path.join(gee_dir, 'forma') forma2017db = rasterio.open( '/mnt/ds3lab-scratch/lming/gee_data/forma/forma2017.vrt') # out_dir = os.path.join(forma_dir, '2017') out_dir = gee_dir create_dir(out_dir) forma_name = 'forma{year}_{z}_{x}_{y}.npy' for tile in tiles: z, x, y = tile lon, lat = num2deg(int(x), int(y), int(z)) tile = extract_tile(forma2017db, lon, lat, 256, crs='ESPG:4326')[0] np.save( os.path.join(out_dir, forma_name.format(year='2017', z=z, x=x, y=y)), tile)