def get_quadrant_tiles(tile): """Return indicies of tiles at one higher zoom (in google tiling scheme)""" ul = (tile.google[0] * 2, tile.google[1] * 2) return [ Tile.from_google(ul[0], ul[1], tile.zoom + 1), # UL Tile.from_google(ul[0], ul[1] + 1, tile.zoom + 1), # LL Tile.from_google(ul[0] + 1, ul[1], tile.zoom + 1), # UR Tile.from_google(ul[0] + 1, ul[1] + 1, tile.zoom + 1) ] # LR
def test_get_quadrant_tiles(self): """Test ability to get tile children on level of increased zoom.""" tile = Tile.from_google(0, 0, 0) child_tiles = _get_quadrant_tiles(tile) self.assertEqual(child_tiles, [ Tile.from_google(0, 0, 1), Tile.from_google(0, 1, 1), Tile.from_google(1, 0, 1), Tile.from_google(1, 1, 1) ])
def georeference(self): """ :return: """ print('Georeferencing ...') # tile_start = Tile.for_latitude_longitude(self.lat_start, self.lon_start, self.zoom) # tile_stop = Tile.for_latitude_longitude(self.lat_stop, self.lon_stop, self.zoom) Xs, Ys = self.tiles_in_dir() start_x, start_y = min(Xs), min(Ys) stop_x, stop_y = max(Xs), max(Ys) tile_start = Tile.from_google(start_x, start_y, self.zoom) tile_stop = Tile.from_google(stop_x, stop_y, self.zoom) point_start = Point.from_latitude_longitude( max(tile_start.bounds[0][0], tile_start.bounds[1][0]), min(tile_start.bounds[0][1], tile_start.bounds[1][1])) point_stop = Point.from_latitude_longitude( min(tile_stop.bounds[0][0], tile_stop.bounds[1][0]), max(tile_stop.bounds[0][1], tile_stop.bounds[1][1])) # print(point_start.meters, point_stop.meters) if 'bing' in self.map or 'google' in self.map: a_srs = 'EPSG:3857' else: a_srs = 'EPSG:4326' # Georeferencing os.system( "gdal_translate -of GTiff -co BIGTIFF=YES -co NUM_THREADS=8 -a_ullr " + str(point_start.meters[0]) + " " + str(point_start.meters[1]) + " " + str(point_stop.meters[0]) + " " + str(point_stop.meters[1]) + " " + "-a_srs " + a_srs + " " + self.map + "_" + self.mode + "_stitched.tif result.tif") # warping with conversion to RGBA # --config GDAL_CACHEMAX 32000 - wm 1500 os.system( "gdalwarp -dstalpha -srcnodata 0 -dstnodata 0 -overwrite -wo NUM_THREADS=8 " + "-co COMPRESS=PACKBITS -co BIGTIFF=YES " + "-s_srs " + a_srs + " -t_srs EPSG:4326 result.tif " + self.map + "_" + self.mode + "_gcps.tif") os.remove('result.tif') print('Georeferencing Complete!')
def test_assert_google_y(google_y, zoom): google_x = 0 with pytest.raises(AssertionError) as assertion_info: _ = Tile.from_google(google_x=google_x, google_y=google_y, zoom=zoom) assert 'Google Y needs to be a value between 0 and (2^zoom) -1.' in str( assertion_info.value)
def _build_tile_url( url_format: UrlFormat, url_template: str, z: int, x: int, y: int ) -> str: if url_format == UrlFormat.QUADKEY: return url_template.format( q=Tile.from_google(google_x=x, google_y=y, zoom=z).quad_tree ) if url_format == UrlFormat.XYZ: return url_template.format(z=z, x=x, y=y)
def tile_cover(geometry, z): import geopandas as gpd from supermercado import burntiles, super_utils from pygeotile.tile import Tile geo = gpd.GeoSeries([geometry]).__geo_interface__['features'][0] geo = [f for f in super_utils.filter_polygons([geo])] return [ Tile.from_google(*geo).quad_tree for geo in [f for f in burntiles.burn(geo, z)] ]
def png2GeoTif(src_filename, dst_filename, google_x, google_y, zoom, pixels, epsg): """ This function make image georeference adn convert to .tif format to be able to load it in qgis :param src_filename: '/path/to/source.tif' :param dst_filename: '/path/to/destination.tif' :param google_x: :param google_y: :param zoom: 15 :param pixels: :param epsg: 4326 #Es el eps de los puntos del IDEAM y el que retorna pygeotile :return: """ # Opens source dataset src_ds = gdal.Open(src_filename) format = "GTiff" driver = gdal.GetDriverByName(format) # Open destination dataset dst_ds = driver.CreateCopy(dst_filename, src_ds, 0) #Optener los puntos de referencia tile = Tile.from_google(google_x, google_y, zoom) bounds = tile.bounds #Retorna el punto sur occidental y el punto nororiental del tile. p1 = bounds[ 0] #Point(latitude=2.6357885741666065, longitude=-72.388916015625) p2 = bounds[1] uperleftx = min([p1[1], p2[1]]) uperlefty = max([p1[0], p2[0]]) scalex = abs(p1[1] - p2[1]) / pixels scaley = abs(p1[0] - p2[0]) / pixels # Specify raster location through geotransform array # (uperleftx, scalex, skewx, uperlefty, skewy, scaley) # Scale = size of one pixel in units of raster projection # this example below assumes 100x100 gt = [uperleftx, scalex, 0, uperlefty, 0, -scaley] #print(gt) # Set location dst_ds.SetGeoTransform(gt) # Get raster projection srs = osr.SpatialReference() srs.ImportFromEPSG(epsg) dest_wkt = srs.ExportToWkt() # Set projection dst_ds.SetProjection(dest_wkt) # Close files dst_ds = None src_ds = None
def test_string_output(self): """Test that tile details are correctly printed.""" tile = Tile.from_google(3, 1, 2) self.assertEqual(format_tile(tile, 'google'), '3 1 2') self.assertEqual(format_tile(tile, 'tms'), '3 2 2') self.assertEqual(format_tile(tile, 'quad_tree'), '13') self.assertEqual(format_tile(tile, 'google', '{z}-{x}-{y}'), '2-3-1') self.assertEqual(format_tile(tile, 'tms', '{z}-{x}-{y}'), '2-3-2')
def process_xyztile(self): x, y, z = self.xyztile tile = Tile.from_google(x, y, zoom=z) point_min, point_max = tile.bounds min_lat, min_lon = point_min.latitude_longitude max_lat, max_lon = point_max.latitude_longitude center_lat = (min_lat + max_lat) / 2.0 center_lon = (min_lon + max_lon) / 2.0 self.center = (center_lon, center_lat) self.area = ddd.rect([min_lon, min_lat, max_lon, max_lat]).geom
def populate(self): zoom, i, j = self.tile_coords tile = PyGeoTile.from_google(i, j, zoom) sw, ne = tile.bounds west, south = sw.meters east, north = ne.meters rx = (east - west) / 256 ry = (north - south) / 256 self.south = south + ry/2 self.north = north - ry/2 self.west = west + rx/2 self.east = east - rx/2
def open_tile(filename): """ Open a tile image and assign projection and geotransform """ geoimg = gippy.GeoImage(filename, True) z, x, y = map(int, geoimg.basename().split('-')[0:4]) tile = Tile.from_google(google_x=x, google_y=y, zoom=z) geoimg.set_srs('EPSG:3857') minpt = tile.bounds[0].meters maxpt = tile.bounds[1].meters affine = np.array([ minpt[0], (maxpt[0] - minpt[0]) / geoimg.xsize(), 0.0, maxpt[1], 0.0, -(maxpt[1] - minpt[1]) / geoimg.ysize() ]) geoimg.set_affine(affine) geoimg.set_nodata(-1) return geoimg
def getCorners(fileNameA): zoom = 15 pixels = 256 epsg = 4326 xA = str(fileNameA).split("_") google_xA, google_yA = int(xA[6]), int(xA[7].split(".")[0]) tile = Tile.from_google(google_xA, google_yA, zoom) bounds = tile.bounds # Retorna el punto sur occidental y el punto nororiental del tile. p1 = bounds[0] # Point(latitude=2.6357885741666065, longitude=-72.388916015625) p3 = bounds[1] p2 = (p3[0], p1[1]) p4 = (p1[0], p3[1]) corners = [QgsPointXY(p1[1], p1[0]), QgsPointXY(p2[1], p2[0]), QgsPointXY(p3[1], p3[0]), QgsPointXY(p4[1], p4[0])] # Tile Carners return corners
def getTile(self): """ download tile from planet :param self: :return: """ if not os.path.exists(self.outRaster): os.mkdir(self.outRaster) features = self.inVector.getFeatures() self.inVector.startEditing() if self.inVector.dataProvider().fieldNameIndex("bounds") == -1: self.inVector.dataProvider().addAttributes( [QgsField("bounds", QVariant.String)]) self.inVector.updateFields() id_new_col_bounds = self.inVector.dataProvider().fieldNameIndex("bounds") for feature in features: x = int(feature['tile_x']) y = int(feature['tile_y']) google_x, google_y, zoom = x, y, 15 tile = Tile.from_google(google_x, google_y, zoom) self.inVector.changeAttributeValue(feature.id(), id_new_col_bounds, str(tile.bounds)) fileNameB = feature['tile_B'] + ".png" fileNameA = feature['tile_A'] + ".png" if not os.path.exists(self.outRaster + '/' + fileNameB): # Si no existe la imagen se descarga result = requests.get( 'https://tiles.planet.com/basemaps/v1/planet-tiles/%s/gmap/%s/%s/%s.png' % (self.mosaicNameBefore, str(15), x, y), auth=HTTPBasicAuth(os.environ['PL_API_KEY_IDEAM'], '')) with open(self.outRaster + '/' + fileNameB, 'wb') as f: f.write(result.content) # Guardar imagen en archivo if not os.path.exists(self.outRaster + '/' + fileNameA): # Si no existe la imagen se descarga result = requests.get( 'https://tiles.planet.com/basemaps/v1/planet-tiles/%s/gmap/%s/%s/%s.png' % (self.mosaicNameAfter, str(15), x, y), auth=HTTPBasicAuth(os.environ['PL_API_KEY_IDEAM'], '')) with open(self.outRaster + '/' + fileNameA, 'wb') as f: f.write(result.content) # Guardar imagen en archivo self.inVector.commitChanges()
def osm_init_params_xyztile(root, pipeline, logger): if not pipeline.data.get('ddd:osm:area:xyztile'): return x, y, z = pipeline.data['ddd:osm:area:xyztile'] tile = Tile.from_google(x, y, zoom=z) point_min, point_max = tile.bounds min_lat, min_lon = point_min.latitude_longitude max_lat, max_lon = point_max.latitude_longitude center_lat = (min_lat + max_lat) / 2.0 center_lon = (min_lon + max_lon) / 2.0 pipeline.data['ddd:osm:area:center'] = (center_lon, center_lat) pipeline.data['ddd:osm:area:shape'] = ddd.rect( [min_lon, min_lat, max_lon, max_lat]).geom
def getTileUrl(self): """ https://github.com/emdete/tabulae/blob/7751d7cd4d366f2ef4ba46dbc5b8f5fb2114ac58/provider.py """ tile = Tile.from_google(self.x, self.y, self.zoom) if 'bing' in self.map: if self.mode == 'road': self.url = 'http://ecn.dynamic.t{}.tiles.virtualearth.net/comp/CompositionHandler/r{}.jpeg?mkt=ru-ru&' \ 'it=G,VE,BX,L,LA&shading=hill&g=94'.format(randint(0, 3), tile.quad_tree) elif self.mode == 'satellite': self.url = 'http://a{}.ortho.tiles.virtualearth.net/tiles/a{}.jpeg?g=94'.format(randint(0, 3), tile.quad_tree) elif 'yandex' in self.map: if self.mode == 'road': self.url = 'https://vec02.maps.yandex.net/tiles?l=map&v=17.08.08-0&x=' + str(self.x) + \ '&y=' + str(self.y) + '&z=' + str(self.zoom) + '&scale=1&lang=ru_RU' elif self.mode == 'satellite': self.url = 'https://sat01.maps.yandex.net/tiles?l=sat&v=3.339.0&x=' + str(self.x) + \ '&y=' + str(self.y) + '&z=' + str(self.zoom) + '&lang=ru_RU'
def pipeline_start(pipeline, root, logger): """ Walk a tile directory and report about available tiles. Called from local data dir as: ddd ~/ddd/pipelines/osm/osm_tile_info.py """ source_dir = settings.DDD_WORKDIR + "/ddd_http/" #source_regex = r"output/ddd_http/([0-9]+)/([0-9]+)/([0-9]+)(.*)" source_regex = r".*(17)/([0-9]+)/([0-9]+)(.*)" logger.info("Finding output results from: %s (%s)" % (source_dir, source_regex)) #use_file = "/tmp/tiles.txt" use_file = None if use_file: listing = open(use_file, "r").read().split("\n") else: listing = glob.glob(source_dir + "**/*.glb", recursive=True) listing = [f[len(source_dir):] for f in listing] features = [] feature_idx = {} for filename in listing: #dirname = os.path.dirname(filename) #basename = os.path.basename(filename) if not filename.endswith(".glb"): continue if filename.endswith(".uncompressed.glb"): continue #print(filename) #logger.debug(filename) matches = re.match(source_regex, filename) if matches: x, y, z = int(matches.group(2)), int(matches.group(3)), matches.group(1) if z == '.': z = 17 else: z = int(z) data = {"z": z, "x": x, "y": y, "remainder": matches.group(4)} #logger.debug(data) tile = Tile.from_google(x, y, zoom=z) point_min, point_max = tile.bounds min_lat, min_lon = point_min.latitude_longitude max_lat, max_lon = point_max.latitude_longitude center_lat = (min_lat + max_lat) / 2.0 center_lon = (min_lon + max_lon) / 2.0 center = (center_lon, center_lat) area = ddd.rect([min_lon, min_lat, max_lon, max_lat]).geom file_path = os.path.join(source_dir, filename) mtime = datetime.datetime.fromtimestamp(os.path.getmtime(file_path)) age = datetime.datetime.now() - mtime feature = geojson.Feature(geometry=area, properties={"available": True, #exists > 0, "name": filename, "z": z, "x": x, "y": y, "mtime": str(mtime), "size": os.stat(file_path).st_size if os.path.exists(file_path) else None} ) if (z, x ,y) not in feature_idx: feature_idx[(z, x, y)] = feature features.append(feature) else: pass feature_collection = geojson.FeatureCollection(features) dump = geojson.dumps(feature_collection, sort_keys=True, indent=4) print(dump + "\n") logger.info("Found %d files." % len(features))
def make_geo_feature(tile_dict, pred, thresh_vals, thresh_cols, coord_prec=5, cmap=None): """Create a GeoJSON feature Parameters ---------- tile_dict: dict Dict with `x`, `y`, `z` defined pred: float Model's predicted probability for having a feature of interest. thresh_vals: list Thresholds for indicated that tile should be mapped thresh_cols: list Hex color codes for each threshold coord_prec: int Number of decimals to keep for prediction score cmap: function Maps value to color hex key """ # Convert tile to lat/lon bounds tile = Tile.from_google(google_x=tile_dict['x'], google_y=tile_dict['y'], zoom=tile_dict['z']) # Get lower-left (most SW) point and upper-right (most NE) point ll = list(tile.bounds[0]) ur = list(tile.bounds[1]) # Round pred score to save bytes for di in range(len(ll)): ll[di] = np.around(ll[di], decimals=coord_prec) ur[di] = np.around(ur[di], decimals=coord_prec) # GeoJSON uses long/lat bbox = [[[ll[1], ll[0]], [ur[1], ll[0]], [ur[1], ur[0]], [ll[1], ur[0]], [ll[1], ll[0]]]] # From pred, get fill color if cmap is None: for ii, upper_thresh in enumerate(thresh_vals): if pred <= upper_thresh: fill = thresh_cols[ii] break else: fill = mpl.colors.to_hex(cmap.to_rgba(pred)) # Create properties properties = { 'X': tile_dict['x'], 'Y': tile_dict['y'], 'Z': tile_dict['z'], 'pred': pred, 'fill': fill } #'fill-opacity': 0.5, #'stroke-width': 1, #'stroke-opacity': 0.5} # Generate feature dict feature = dict(geometry={ 'coordinates': bbox, 'type': 'Polygon' }, type='Feature', properties=properties) return feature
def tile_search(boundary, max_zoom, tile_format='google', output_format='{x} {y} {z}', print_to=None): """Print (or save) tiles belonging to a shape object. Parameters: ---------- boundary: str JSON corresponding to a single polygon/multipolygon max_zoom: int Zoom at which to terminate file search tile_format: str Desired tile format. `google` (default), `tms`, or `quad_tree` output_format: str Format string for printing the tile indices. Only used for tile_format `google` and `tms`. Must contain `{x}`, `{y}`, and `{z}`. print_to: object with `write` functionality `None` (default) prints to sys.stdout. Pass a (non-binary) file object to print directly to an open file. Returns: ------- total_tiles: int Number of tiles found Note: this function is useful if you want to directly incorporate tile searching into a script (i.e., not on the command line). """ ######################################### # Initialize boundary and queue ######################################### bound_shape = shape(boundary) # Convert JSON to shape object stack = LifoQueue() total_tiles = 0 # Put whole-world tile on stack start_tile = Tile.from_google(0, 0, 0) stack.put([start_tile, False]) ######################################### # Depth-first search on tile indices ######################################### while not stack.empty(): # Pop the top tile in the stack top_tile, comp_contained = stack.get() # Check if desired zoom has been reached if top_tile.zoom >= max_zoom: # If at max zoom, print tile inds or save to file (if specified) print(format_tile(top_tile, tile_format, output_format), file=print_to) total_tiles += 1 # Otherwise, zoom in one increment, find children tiles, add to stack else: ret_tiles = get_overlap_child_tiles(top_tile, bound_shape, comp_contained) for rt in ret_tiles: stack.put(rt) return total_tiles
def test_no_assert_google_y(google_y, zoom): google_x = 0 _ = Tile.from_google(google_x=google_x, google_y=google_y, zoom=zoom) assert "No assertion raise :)"
for fi, feature in enumerate(geojson['features']): if feature['properties']['SOVEREIGNT'] == tile_p['country']: bound = feature['geometry'] break print('Computing tiles at zoom {} for {}'.format(tile_p['max_zoom'], tile_p['country'])) ######################################### # Setup boundary, initialize stack ######################################### st_dt = dt.now() # Start time boundary_shape = shape(bound) stack = LifoQueue() start_tile = Tile.from_google(0, 0, 0) max_stack_size = 0 total_tiles = 0 stack.put([start_tile, False]) # Add biggest tile to stack ######################################### # Depth-first search on tile indices ######################################### with open(tile_output_fpath, 'w') as tile_list_f: while not stack.empty(): # Track maximum stack size if stack.qsize() > max_stack_size: max_stack_size = stack.qsize() # Pop the top tile in the stack top_tile, comp_contained = stack.get()
def test_from_google(tms, google, zoom): google_x, google_y = google tile = Tile.from_google(google_x=google_x, google_y=google_y, zoom=zoom) assert tile.tms == tms
def test_from_google_tasmania(): google_x, google_y = 1853, 1289 tms_tasmania = 1853, 758 tile = Tile.from_google(google_x=google_x, google_y=google_y, zoom=11) assert tile.tms == tms_tasmania