-
Notifications
You must be signed in to change notification settings - Fork 18
/
map_builder.py
executable file
·183 lines (151 loc) · 7.8 KB
/
map_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import matplotlib
matplotlib.use('Agg')
import os
import cv2
import math
import numpy as np
from glob import glob
from skimage import io
from os.path import join
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from collections import defaultdict
from staticmap import StaticMap, Polygon
from matplotlib.colors import ListedColormap
def _lon_to_x(lon, zoom):
if not (-180 <= lon <= 180): lon = (lon + 180) % 360 - 180
return ((lon + 180.) / 360) * pow(2, zoom)
def _lat_to_y(lat, zoom):
if not (-90 <= lat <= 90): lat = (lat + 90) % 180 - 90
return (1 - math.log(math.tan(lat * math.pi / 180) + 1 / math.cos(lat * math.pi / 180)) / math.pi) / 2 * pow(2, zoom)
def _download_map_image(min_lat=45.0, min_lon=7.6, max_lat=45.1, max_lon=7.7, size=2000):
""""Download a map of the chosen area as a numpy image"""
mean_lat = (min_lat + max_lat) / 2
mean_lon = (min_lon + max_lon) / 2
static_map = StaticMap(size, size)
static_map.add_polygon(
Polygon(((min_lon, min_lat), (min_lon, max_lat), (max_lon, max_lat), (max_lon, min_lat)), None, '#FFFFFF'))
zoom = static_map._calculate_zoom()
static_map = StaticMap(size, size)
image = static_map.render(zoom, [mean_lon, mean_lat])
print(
f"You can see the map on Google Maps at this link www.google.com/maps/place/@{mean_lat},{mean_lon},{zoom - 1}z")
min_lat_px, min_lon_px, max_lat_px, max_lon_px = \
static_map._y_to_px(_lat_to_y(min_lat, zoom)), \
static_map._x_to_px(_lon_to_x(min_lon, zoom)), \
static_map._y_to_px(_lat_to_y(max_lat, zoom)), \
static_map._x_to_px(_lon_to_x(max_lon, zoom))
assert 0 <= max_lat_px < min_lat_px < size and 0 <= min_lon_px < max_lon_px < size
return np.array(image)[max_lat_px:min_lat_px, min_lon_px:max_lon_px], static_map, zoom
def get_edges(coordinates, enlarge=0):
"""
Send the edges of the coordinates, i.e. the most south, west, north and
east coordinates.
:param coordinates: A list of numpy.arrays of shape (Nx2)
:param float enlarge: How much to increase the coordinates, to enlarge
the area included between the points
:return: a tuple with the four float
"""
min_lat, min_lon, max_lat, max_lon = (*np.concatenate(coordinates).min(0), *np.concatenate(coordinates).max(0))
diff_lat = (max_lat - min_lat) * enlarge
diff_lon = (max_lon - min_lon) * enlarge
inc_min_lat, inc_min_lon, inc_max_lat, inc_max_lon = \
min_lat - diff_lat, min_lon - diff_lon, max_lat + diff_lat, max_lon + diff_lon
return inc_min_lat, inc_min_lon, inc_max_lat, inc_max_lon
def _create_map(coordinates, colors=None, dot_sizes=None, legend_names=None, map_intensity=0.6):
dot_sizes = dot_sizes if dot_sizes is not None else [10] * len(coordinates)
colors = colors if colors is not None else ["r"] * len(coordinates)
assert len(coordinates) == len(dot_sizes) == len(colors), \
f"The number of coordinates must be equals to the number of colors and dot_sizes, but they're " \
f"{len(coordinates)}, {len(colors)}, {len(dot_sizes)}"
# Add two dummy points to slightly enlarge the map
min_lat, min_lon, max_lat, max_lon = get_edges(coordinates, enlarge=0.1)
coordinates.append(np.array([[min_lat, min_lon], [max_lat, max_lon]]))
# Download the map of the chosen area
map_img, static_map, zoom = _download_map_image(min_lat, min_lon, max_lat, max_lon)
scatters = []
fig = plt.figure(figsize=(map_img.shape[1] / 100, map_img.shape[0] / 100), dpi=1000)
for i, coord in enumerate(coordinates):
for i in range(len(coord)): # Scale latitudes because of earth's curvature
coord[i, 0] = -static_map._y_to_px(_lat_to_y(coord[i, 0], zoom))
for coord, size, color in zip(coordinates, dot_sizes, colors):
scatters.append(plt.scatter(coord[:, 1], coord[:, 0], s=size, color=color))
if legend_names != None:
plt.legend(scatters, legend_names, scatterpoints=10000, loc='lower left',
ncol=1, framealpha=0, prop={"weight": "bold", "size": 30})
min_lat, min_lon, max_lat, max_lon = get_edges(coordinates)
plt.ylim(min_lat, max_lat)
plt.xlim(min_lon, max_lon)
fig.subplots_adjust(bottom=0, top=1, left=0, right=1)
fig.canvas.draw()
plot_img = np.array(fig.canvas.renderer._renderer)
plt.close()
plot_img = cv2.resize(plot_img[:, :, :3], map_img.shape[:2][::-1], interpolation=cv2.INTER_LANCZOS4)
map_img[(map_img.sum(2) < 444)] = 188 # brighten dark pixels
map_img = (((map_img / 255) ** map_intensity) * 255).astype(np.uint8) # fade map
mask = (plot_img.sum(2) == 255 * 3)[:, :, None] # mask of plot, to find white pixels
final_map = map_img * mask + plot_img * (~mask)
return final_map
def _get_coordinates_from_dataset(dataset_folder, extension="jpg"):
"""
Takes as input the path of a dataset, such as "datasets/st_lucia/images"
and returns
[("train/database", [[45, 8.1], [45.2, 8.2]]), ("train/queries", [[45, 8.1], [45.2, 8.2]])]
"""
images_paths = glob(join(dataset_folder, "**", f"*.{extension}"), recursive=True)
if len(images_paths) != 0:
print(f"I found {len(images_paths)} images in {dataset_folder}")
else:
raise ValueError(f"I found no images in {dataset_folder} !")
grouped_gps_coords = defaultdict(list)
for image_path in images_paths:
full_path = os.path.dirname(image_path)
full_parent_path, parent_dir = os.path.split(full_path)
parent_parent_dir = os.path.split(full_parent_path)[1]
# folder_name is for example "train - database"
folder_name = " - ".join([parent_parent_dir, parent_dir])
gps_coords = image_path.split("@")[5], image_path.split("@")[6]
grouped_gps_coords[folder_name].append(gps_coords)
grouped_gps_coords = sorted([(k, np.array(v).astype(np.float64))
for k, v in grouped_gps_coords.items()])
return grouped_gps_coords
def build_map_from_dataset(dataset_folder, dot_sizes=None):
"""dataset_folder is the path that contains the 'images' folder."""
grouped_gps_coords = _get_coordinates_from_dataset(join(dataset_folder, "images"))
SORTED_FOLDERS = ["train - database", "train - queries", "val - database", "val - queries",
"test - database", "test - queries"]
try:
grouped_gps_coords = sorted(grouped_gps_coords, key=lambda x: SORTED_FOLDERS.index(x[0]))
except ValueError:
pass # This dataset has different folder names than the standard train-val-test database-queries.
coordinates = []
legend_names = []
for folder_name, coords in grouped_gps_coords:
legend_names.append(f"{folder_name} - {len(coords)}")
coordinates.append(coords)
colors = cm.rainbow(np.linspace(0, 1, len(legend_names)))
colors = ListedColormap(colors)
colors = colors.colors
if len(legend_names) == 1:
legend_names = None # If there's only one folder, don't show the legend
colors = np.array([[1, 0, 0]])
map_img = _create_map(coordinates, colors, dot_sizes, legend_names)
print(f"Map image resolution: {map_img.shape}")
dataset_name = os.path.basename(os.path.abspath(dataset_folder))
io.imsave(join(dataset_folder, f"map_{dataset_name}.png"), map_img)
if __name__ == "__main__":
coordinates = [
np.array([[41.8931, 12.4828], [45.4669, 9.1900], [40.8333, 14.2500]]),
np.array([[52.5200, 13.4050], [48.7775, 9.1800], [48.1375, 11.5750]]),
np.array([[48.8567, 2.3522], [43.2964, 5.3700], [45.7600, 4.8400]])
]
map_img = _create_map(
coordinates,
colors=["green", "black", "blue"],
dot_sizes=[1000, 1000, 1000],
legend_names=[
"Main Italian Cities",
"Main German Cities",
"Main French Cities",
])
io.imsave("cities.png", map_img)