def getmapboxaltitude(lon, lat): # through the raster API: # https://docs.mapbox.com/help/troubleshooting/access-elevation-data/ tile = mercantile.tile(lon, lat, 14) response = altitudeprovider.tile('mapbox.terrain-rgb', *tile) if response.status_code != 200: return 0 bbox = mercantile.xy_bounds(*tile) ul = mercantile.lnglat(bbox.left, bbox.top) br = mercantile.lnglat(bbox.right, bbox.bottom) x = round(255 * (lon - ul[0]) / (br[0] - ul[0])) y = round(255 * (lat - ul[1]) / (br[1] - ul[1])) image = Image.open(BytesIO(response.content)) px = image.load()[x, y] return -10000 + ((px[0] * 256 * 256 + px[1] * 256 + px[2]) * 0.1)
def on_mouse_press(self, event): # lock needed here? we're messing with the marker and some text. with self.scene_lock: self.last_event = event if event.button == 2: # right click self.longlat_text.text = '' # should be painted below anything else self.marker.visible = False return elif event.button == 3: # middle click canvas_x, canvas_y, *_ = event.pos width, height = self.size rect = self.camera._real_rect x_interp = canvas_x / width y_interp = canvas_y / height view_x = rect.left + x_interp * (rect.right - rect.left) view_y = rect.top + y_interp * (rect.bottom - rect.top) self.longlat_text.pos = (view_x, view_y) msg = '((long {:f} lat {:f}) ({:f}, {:f})' lng, lat = mercantile.lnglat(view_x, view_y) msg = msg.format(lng, lat, view_x, view_y) self.longlat_text.text = msg self.marker.set_data(np.array([[view_x, view_y]]), size=self.marker_size) self.marker.visible = True
def current_bounds(self): """Return the tile index bounds of the current view. Returns a tuple of two mercantile.Tile instances: the northwest and southeast corners. """ rect = self.camera._real_rect z = self.tile_zoom_level x0, y0 = rect.left, rect.top y0 = self._fix_y(y0) lng0, lat0 = mercantile.lnglat(x0, y0) northwest = mercantile.tile(lng0, lat0, z) x1, y1 = rect.right, rect.bottom y1 = self._fix_y(y1) lng1, lat1 = mercantile.lnglat(x1, y1) southeast = mercantile.tile(lng1, lat1, z) return northwest, southeast
def test_lnglat_xy_roundtrip(): lnglat = (-105.0844, 40.5853) roundtrip = mercantile.lnglat(*mercantile.xy(*lnglat)) for a, b in zip(roundtrip, lnglat): assert round(a - b, 4) == 0
def test_lnglat_truncate(): xy = (-28366731.739810849, -1655181.9927159143) assert mercantile.lnglat(*xy, truncate=True) == (-180, -14.704620000000013)
def test_lnglat(): xy = (-8366731.739810849, -1655181.9927159143) assert mercantile.lnglat(*xy) == (-75.15963, -14.704620000000013)
def tile(x, y, zoom, start_year, end_year, dry_run=False): xy_bounds = mercantile.xy_bounds(x, y, zoom) west, north = mercantile.lnglat(xy_bounds.left, xy_bounds.top) east, south = mercantile.lnglat(xy_bounds.right, xy_bounds.bottom) return bounds(west, south, east, north, start_year, end_year, dry_run)
def xy_to_lng_lat(xy): lat_long = mercantile.lnglat(xy[0], xy[1]) return (lat_long.lng, lat_long.lat)
def test_lnglat_xy_roundtrip(): lnglat = (-105.0844, 40.5853) roundtrip = mercantile.lnglat(*mercantile.xy(*lnglat)) for a, b in zip(roundtrip, lnglat): assert round(a - b, 7) == 0
if ds is None: print("Could not open file!") sys.exit(1) gt = ds.GetGeoTransform() # Estimate a maximum and minimum z level to fit the image, and show some # prompts with defaults that the use can override z_max_default = int(math.floor(math.log(40075016.0, gt[1]) - 8) - 1) z_max = click.prompt("Maximum Z level", default=z_max_default, type=int) z_min_default = int( z_max - math.ceil(math.log(max(ds.RasterXSize, ds.RasterYSize) / 256, 2))) z_min = click.prompt("Minimum Z level", default=z_min_default, type=int) # Get the geographic coordinates of the file's left top and right bottom lt = mercantile.lnglat(gt[0], gt[3]) rb = mercantile.lnglat(gt[0] + (ds.RasterXSize * gt[1]), gt[3] + (ds.RasterYSize * gt[5])) # Use the coordinates and z levels we create a list of tiles to generate tiles = list( mercantile.tiles(lt.lng, rb.lat, rb.lng, lt.lat, range(z_min, z_max + 1))) num_tiles = len(tiles) # Ask for confirmation before proceeding if not click.confirm("Going to create {} tiles. Continue?".format(num_tiles), default=True): sys.exit(1) # Loop through all the the tiles, and render each one using gdal.Translate() with click.progressbar(tiles) as bar:
xmerc_min = box_min.left ymerc_min = box_min.bottom xmerc_max = box_max.right ymerc_max = box_max.top dxc = (xmerc_max - xmerc_min) / len(tile_x) dyc = (ymerc_max - ymerc_min) / len(tile_y) print('Mercator x) {} {} {}'.format(xmerc_min, xmerc_max, dxc)) print('Mercator y) {} {} {}'.format(ymerc_min, ymerc_max, dyc)) # input tile stretching data = [] polygons = [] for tx, ty in tile_list: i = shift_x[tx] j = shift_y[ty] bl = mercantile.lnglat( xmerc_min + i * dxc, ymerc_min + j * dyc ) # top left tr = mercantile.lnglat( xmerc_min + (i + 1) * dxc, ymerc_min + (j + 1) * dyc ) # bottom right lat_min = bl.lat lat_max = tr.lat lon_min = bl.lng lon_max = tr.lng data.append([ tx, ty, lat_min, lat_max, lon_min, lon_max ]) poly = Polygon([[ (lon_min, lat_min),
def marker_pos(self): """Return Marker's position as a (longitude, latitude) tuple""" return mercantile.lnglat(*self.marker._data[0][0][0:2])