~qeef/damn-client.py

ref: 008f97255d638613faebc379be8b52f6531aa2e5 damn-client.py/prepare_tmp.py -rwxr-xr-x 1.5 KiB
008f9725Jiri Vlasak Update requirements 4 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
#!/usr/bin/env python3
"""This script prepares temporary files for the damn project.

You need to provide damn server URI as the first argument, the area ID as the
second argument, and the GeoJSON file containing FeatureCollection with features
to be assigned to the squares as the third argument.

The output directory with the name of the area ID is generated.

Example usage:

    ./prepare_tmp.py https://server.damn-project.org 999 buildings.geojson
"""
import json
import os
import sys
from shapely.geometry import shape
from damn_client.api import DamnAPI

if __name__ == "__main__":
    if len(sys.argv) != 4:
        print(__doc__)
        sys.exit(1)
    aid = int(sys.argv[2])
    s = DamnAPI(sys.argv[1])
    s.get_squares(aid)
    tmp_out = {}
    for sq in s.areas[aid]["squares"]:
        tmp_out[sq["sid"]] = {
            "aid": sq["aid"],
            "sid": sq["sid"],
            "type": "FeatureCollection",
            "generator": "damn-client.py",
            "features": [],
        }
        sq["shape"] = shape(sq["border"])
    tmp_in = None
    with open(sys.argv[3]) as f:
        tmp_in = json.load(f)
    for f in tmp_in["features"]:
        sh = shape(f["geometry"])
        for sq in s.areas[aid]["squares"]:
            if sq["shape"].intersects(sh):
                tmp_out[sq["sid"]]["features"].append(f)
    try:
        os.mkdir("{}".format(aid))
    except FileExistsError:
        pass
    for (k, v) in tmp_out.items():
        with open("{}/{}".format(aid, k), "w") as f:
            json.dump(v, f)