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
Ejemplo n.º 2
0
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)