def quadkey_to_lonlat(qk): tilex, tiley, level = tsm.quadkey_to_tile(qk) tile = np.column_stack((tilex, tiley)) pix, piy = tsm.tile_to_pixel(tile) pixel = np.column_stack((pix, piy)) lat, lon = tsm.pixel_to_geo(pixel, level) return np.column_stack((lat, lon))
def nearby(self): tile, level = TileSystem.quadkey_to_tile(self.key) perms = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] tiles = set( map(lambda perm: (abs(tile[0] + perm[0]), abs(tile[1] + perm[1])), perms)) return [TileSystem.tile_to_quadkey(tile, level) for tile in tiles]
def lonlat_to_metre(lonlat, level): pix = tsm.geo_to_pixel(lonlat, level) pix = np.column_stack((pix[0], pix[1])) grd_res = tsm.ground_resolution(lonlat[:, 0], level) rel_metre = pix * grd_res.reshape(-1, 1) return rel_metre, grd_res
def quadkey_to_metre(qk, grd_res): tilex, tiley, level = tsm.quadkey_to_tile(qk) tile = np.column_stack((tilex, tiley)) pix, piy = tsm.tile_to_pixel(tile) pixel = np.column_stack((pix, piy)) rel_metre = pixel * grd_res.reshape(-1, 1) return rel_metre
def lonlat_to_quadkey(lonlat, level): pix = tsm.geo_to_pixel(lonlat, level) pix = np.column_stack((pix[0], pix[1])) tile = tsm.pixel_to_tile(pix) tile = np.column_stack((tile[0], tile[1])) qk = tsm.tile_to_quadkey(tile, level) qk = qk.reshape(-1, 1) qk = pd.Series(qk[:, 0]).astype(str) return qk.values
def lonlat_to_quadkey_with_shift(quadkey, loshift): quadkeys = np.array([quadkey] * len(loshift)) tilex, tiley, level = tsm.quadkey_to_tile(quadkeys) tile = np.column_stack((tilex, tiley)) shift = np.array(loshift) tile = tile + shift loquadkey = tsm.tile_to_quadkey(tile, level) loquadkey = loquadkey.reshape(-1, 1) loquadkey = pd.Series(loquadkey[:, 0]).astype(str) loquadkey = loquadkey.str[1:] return loquadkey.values.astype(str)
def from_geo(geo, level): """ Constucts a quadkey representation from geo and level geo => (lat, lon) If lat or lon are outside of bounds, they will be clipped If level is outside of bounds, an AssertionError is raised """ pixel = TileSystem.geo_to_pixel(geo, level) tile = TileSystem.pixel_to_tile(pixel) key = TileSystem.tile_to_quadkey(tile, level) return QuadKey(key)
def download_image(location_coordinates, tileSystem_level, null_image): min_latitude, max_latitude, min_longitude, max_longitude = location_coordinates x1, y1 = TileSystem.LatLong_To_PixelXY(min_latitude, min_longitude, tileSystem_level) x2, y2 = TileSystem.LatLong_To_PixelXY(max_latitude, max_longitude, tileSystem_level) X1, Y1 = TileSystem.PixelXY_To_TileXY(min(x1, x2), min(y1, y2)) X2, Y2 = TileSystem.PixelXY_To_TileXY(max(x1, x2), max(y1, y2)) success_status = True mapurl = "http://h0.ortho.tiles.virtualearth.net/tiles/h{0}.jpeg?g=131" final_image = np.zeros( ((Y2 - Y1 + 1) * TILESIZE, (X2 - X1 + 1) * TILESIZE, 3), dtype=np.uint8) start_y = 0 end_y = TILESIZE start_x = 0 end_x = TILESIZE for Y in range(Y1, Y2 + 1): for X in range(X1, X2 + 1): quadkey = TileSystem.TileXY_To_QuadKey(X, Y, tileSystem_level) resp = requests.get(mapurl.format(quadkey), stream=True).raw image = np.asarray(bytearray(resp.read()), dtype="uint8") image = cv2.imdecode(image, cv2.IMREAD_COLOR) if (np.equal(image, null_image).all()): success_status = False break final_image[start_y:end_y, start_x:end_x, :] = image start_x += TILESIZE end_x += TILESIZE #cv2.imwrite('image_iter'+str(Y)+str(X)+'.png', image) start_x = 0 end_x = TILESIZE start_y += TILESIZE end_y += TILESIZE #temp_view = cv2.resize(final_image, (2000, 2000)) #cv2.imwrite('image'+str(Y)+str(X)+'.png', temp_view) if (not success_status): break if (not success_status): final_image, tileSystem_level = download_image(location_coordinates, tileSystem_level - 1, null_image) return final_image, tileSystem_level
def area(self): size = TileSystem.map_size(self.level) LAT = 0 res = TileSystem.ground_resolution(LAT, self.level) side = (size / 2) * res return side * side
def to_geo(self, centered=False): ret = TileSystem.quadkey_to_tile(self.key) tile = ret[0] lvl = ret[1] pixel = TileSystem.tile_to_pixel(tile, centered) return TileSystem.pixel_to_geo(pixel, lvl)
def to_tile(self): return TileSystem.quadkey_to_tile(self.key)
def from_tile(tile, level): return QuadKey(TileSystem.tile_to_quadkey(tile, level))
def overlay_ways(coorindates, aerial_image_path): min_latitude, max_latitude, min_longitude, max_longitude = coorindates api = overpy.Overpass() #roadcolor = (255, 0, 0) # blue color roadthickness = 50 aerial_image = cv2.imread(aerial_image_path) colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (203, 192, 255), (0, 255, 255), (42, 42, 165), (0, 165, 255), (216, 235, 52)] # fetch all ways and nodes result = api.query(""" way(%f, %f, %f, %f) ["highway"]; (._;>;); out body; """ % (min_latitude, min_longitude, max_latitude, max_longitude)) tile_level = int(aerial_image_path[-6:-4]) x1, y1 = TileSystem.LatLong_To_PixelXY(min_latitude, min_longitude, tile_level) x2, y2 = TileSystem.LatLong_To_PixelXY(max_latitude, max_longitude, tile_level) image = np.zeros((abs(y2 - y1), abs(x2 - x1), 3)) start_x = min(x1, x2) start_y = min(y1, y2) for way in result.ways: # print("Name: %s" % way.tags.get("name", "n/a")) # print(" Highway: %s" % way.tags.get("highway", "n/a")) # print(" Nodes:") if 'highway' in way.tags.keys(): prev_nodeX = way.nodes[0].lat prev_nodeY = way.nodes[0].lon prev_nodeX, prev_nodeY = TileSystem.LatLong_To_PixelXY( float(way.nodes[0].lat), float(way.nodes[0].lon), tile_level) prev_nodeX = prev_nodeX - start_x prev_nodeY = prev_nodeY - start_y color = random.choice(colors) for node in way.nodes[1:]: current_nodeX, current_nodeY = TileSystem.LatLong_To_PixelXY( float(node.lat), float(node.lon), tile_level) current_nodeX = current_nodeX - start_x current_nodeY = current_nodeY - start_y image = cv2.line(image, (prev_nodeX, prev_nodeY), (current_nodeX, current_nodeY), color, roadthickness) aerial_image = cv2.line(aerial_image, (prev_nodeX, prev_nodeY), (current_nodeX, current_nodeY), color, roadthickness) prev_nodeX = current_nodeX prev_nodeY = current_nodeY cv2.imwrite('network_only.png', image) resized_image = cv2.resize(image, None, fx=0.2, fy=0.2) cv2.imwrite('network_only_resized.png', resized_image) cv2.imwrite('final_result.png', aerial_image) aerial_resized = cv2.resize(aerial_image, None, fx=0.2, fy=0.2) cv2.imwrite("final_result_resized.png", aerial_resized)