Daniel Varga commited on
Commit
f479bdf
·
1 Parent(s): 9de5f50

geotag viewer

Browse files
Files changed (2) hide show
  1. geotag/flaskapp.py +93 -0
  2. geotag/geomap.py +85 -0
geotag/flaskapp.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ flask_example.py
2
+
3
+ Required packages:
4
+ - flask
5
+ - folium
6
+
7
+ Usage:
8
+
9
+ Start the flask server by running:
10
+
11
+ $ python flask_example.py
12
+
13
+ And then head to http://127.0.0.1:5000/ in your browser to see the map displayed
14
+
15
+ """
16
+
17
+ from flask import Flask, render_template_string
18
+ import folium
19
+
20
+ import geomap
21
+
22
+
23
+ app = Flask(__name__)
24
+
25
+
26
+ @app.route("/")
27
+ def fullscreen():
28
+ m = geomap.create_map()
29
+ return m.get_root().render()
30
+
31
+
32
+ @app.route("/iframe")
33
+ def iframe():
34
+ """Embed a map as an iframe on a page."""
35
+ m = folium.Map()
36
+
37
+ # set the iframe width and height
38
+ m.get_root().width = "800px"
39
+ m.get_root().height = "600px"
40
+ iframe = m.get_root()._repr_html_()
41
+
42
+ return render_template_string(
43
+ """
44
+ <!DOCTYPE html>
45
+ <html>
46
+ <head></head>
47
+ <body>
48
+ <h1>Using an iframe</h1>
49
+ {{ iframe|safe }}
50
+ </body>
51
+ </html>
52
+ """,
53
+ iframe=iframe,
54
+ )
55
+
56
+
57
+ @app.route("/components")
58
+ def components():
59
+ """Extract map components and put those on a page."""
60
+ m = folium.Map(
61
+ width=800,
62
+ height=600,
63
+ )
64
+
65
+ m.get_root().render()
66
+ header = m.get_root().header.render()
67
+ body_html = m.get_root().html.render()
68
+ script = m.get_root().script.render()
69
+
70
+ return render_template_string(
71
+ """
72
+ <!DOCTYPE html>
73
+ <html>
74
+ <head>
75
+ {{ header|safe }}
76
+ </head>
77
+ <body>
78
+ <h1>Using components</h1>
79
+ {{ body_html|safe }}
80
+ <script>
81
+ {{ script|safe }}
82
+ </script>
83
+ </body>
84
+ </html>
85
+ """,
86
+ header=header,
87
+ body_html=body_html,
88
+ script=script,
89
+ )
90
+
91
+
92
+ if __name__ == "__main__":
93
+ app.run(debug=True)
geotag/geomap.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import folium
3
+
4
+
5
+ BASE_URL = "https://static.renyi.hu/ai-shared/daniel/sameenergy/"
6
+ CENTER = np.array([47.499541, 19.046246])
7
+ RADIUS = 1e9 # 0.01
8
+
9
+ def read_data():
10
+ data = []
11
+ for l in open("PhotoLibrary.854G.geotags", "r"):
12
+ a = l.strip().split("\t")
13
+ if len(a) == 2:
14
+ assert a[1] == "no"
15
+ elif a[1] == "nan" or a[2] == "nan":
16
+ pass
17
+ else:
18
+ data.append(a)
19
+ data = np.array(data, dtype=object)
20
+ coords = np.array(data[:, 1:], dtype=np.float32)
21
+ filenames = data[:, 0]
22
+ isclose = np.linalg.norm(coords - CENTER[None, :], axis=1) < RADIUS
23
+ filenames = filenames[isclose]
24
+ coords = coords[isclose]
25
+ print("data read")
26
+ print("patching data to use thumbnails")
27
+ # TODO HACK
28
+ for i, filename in enumerate(filenames):
29
+ assert filename.startswith("PhotoLibrary/")
30
+ filenames[i] = filename.replace("PhotoLibrary/", "PhotoLibrary.thumbs/", 1)
31
+ return filenames, coords
32
+
33
+
34
+ def create_toy_map():
35
+ """Simple example of a fullscreen map."""
36
+ m = folium.Map()
37
+
38
+ # Sample data
39
+ data = [
40
+ {"lat": 40.7128, "lon": -74.0060, "image_url": "http://localhost/images/img1.png"},
41
+ {"lat": 37.7749, "lon": -122.4194, "image_url": "https://static.renyi.hu/ai-shared/daniel/personal/diffusion/vis-1-0.png"},
42
+ # Add more data points with lat, lon, and image_url
43
+ ]
44
+
45
+ # Add markers with custom icons for each data point
46
+ for point in data:
47
+ img = folium.CustomIcon(icon_image=point["image_url"], icon_size=(100, 100))
48
+ folium.Marker(location=[point["lat"], point["lon"]], icon=img).add_to(m)
49
+
50
+ return m
51
+
52
+
53
+ def filter_directories(filenames, coords):
54
+ directories = set()
55
+ filenames2 = []
56
+ coords2 = []
57
+ collected = 0
58
+ # Add markers with custom icons for each data point
59
+ for filename, (lat, lon) in zip(filenames, coords):
60
+ directory = filename.split("/")[-2]
61
+ if directory in directories:
62
+ continue
63
+ directories.add(directory)
64
+ filenames2.append(filename)
65
+ coords2.append([lat, lon])
66
+ return filenames2, coords2
67
+
68
+
69
+ def create_map():
70
+ m = folium.Map(location=CENTER, zoom_start=13)
71
+
72
+ filenames, coords = read_data()
73
+ print("before picking one from each directory", len(filenames), "photos with geotags")
74
+ filenames, coords = filter_directories(filenames, coords)
75
+ print("after picking one from each directory", len(filenames), "photos with geotags")
76
+
77
+ N = 300
78
+ print(f"keeping {N} of them")
79
+ filenames = filenames[:N] ; coords = coords[:N]
80
+
81
+ for i, (filename, (lat, lon)) in enumerate(zip(filenames, coords)):
82
+ img = folium.CustomIcon(icon_image=BASE_URL + filename, icon_size=(100, 100))
83
+ folium.Marker(location=[lat, lon], icon=img).add_to(m)
84
+
85
+ return m