~qeef/damn-client.py

ref: c8312dc6d79098d1dcb773585a1c0f77ca66a67e damn-client.py/osmsm_gen.py -rwxr-xr-x 1.8 KiB
c8312dc6Jiri Vlasak Add osm static maps generator script 3 months ago
                                                                                
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
#!/usr/bin/env python3
"""This script creates static maps for areas.

Use the damn server URI as the first and only argument.

Example usage:

    ./osmsm_gen.py https://server.damn-project.org/
"""
import json
import staticmaps
import sys
from math import cos, floor, pi
from shapely.ops import unary_union
from shapely.geometry import shape
from damn_client.api import DamnAPI

DN = "osmsm"

def save_png_for(area):
    (lonmin, latmin, lonmax, latmax) = area["shape"].bounds
    bounds = "{},{} {},{}".format(latmin, lonmin, latmax, lonmax)
    center = "{},{}".format(
        (latmax + latmin) / 2.0,
        (lonmax + lonmin) / 2.0,
    )

    zoom_if = (lonmax - lonmin) / 3.125
    scales = [
        0.001, 0.003, 0.005, 0.011, 0.022, 0.044, 0.088, 0.176, 0.352,
        0.703, 1.406, 2.813, 5.625, 11.25, 22.5, 45, 90, 180, 360,
    ]
    scales.reverse()
    scale = min(scales, key=lambda x: abs(x - zoom_if))
    zoom = scales.index(scale)
    scale /= 2.0
    zoom += 1

    c = staticmaps.Context()
    c.set_tile_provider(staticmaps.tile_provider_OSM)
    c.set_center(staticmaps.parse_latlng(center))
    c.add_bounds(staticmaps.parse_latlngs2rect(bounds))
    c.set_zoom(zoom)
    sx = 256.0 / scale
    sy = 256.0 / (scale * cos(latmax * pi / 180.0))
    image = c.render_pillow(
        floor((lonmax - lonmin) * sx),
        floor((latmax - latmin) * sy),
    )
    image.save("{}/{}.png".format(DN, area["aid"]))

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(__doc__)
        sys.exit(1)
    s = DamnAPI(sys.argv[1])
    s.get_geometries()
    for a in s.areas.values():
        a["shape"] = []
        for f in a["featurecollection"]["features"]:
            a["shape"].append(shape(f["geometry"]))
        a["shape"] = unary_union(a["shape"])
        save_png_for(a)