-
Notifications
You must be signed in to change notification settings - Fork 258
/
convert_geojson_to_legacyjson.py
executable file
·89 lines (74 loc) · 2.56 KB
/
convert_geojson_to_legacyjson.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
#!/usr/bin/env python
import json
import sys
import io
import argparse
from shapely.geometry import shape, Polygon, MultiPolygon
def convert_json_source(args, source):
converted = {}
extent_obj = {}
geometry = source.get("geometry")
if geometry:
geom = shape(geometry)
if args.gen_bbox:
minx, miny, maxx, maxy = geom.bounds
bbox_obj = {"min_lon": minx, "max_lon": maxx, "min_lat": miny, "max_lat": maxy}
extent_obj["bbox"] = bbox_obj
if not args.remove_polygons:
exterior_rings = []
if isinstance(geom, Polygon):
exterior_rings.append(list(geom.exterior.coords))
elif isinstance(geom, MultiPolygon):
for poly in geom.geoms:
exterior_rings.append(list(poly.exterior.coords))
extent_obj["polygon"] = exterior_rings
properties = source.get("properties") or {}
if args.tms_only and properties["type"] == "wms":
return {}
for f in [
"name",
"type",
"url",
"license_url",
"id",
"description",
"country_code",
"default",
"best",
"start_date",
"end_date",
"overlay",
"available_projections",
"attribution",
"icon",
"privacy_policy_url",
]:
thing = properties.get(f)
if thing is not None:
converted[f] = thing
for f in ["min_zoom", "max_zoom"]:
thing = properties.get(f)
if thing is not None:
extent_obj[f] = thing
if extent_obj:
converted["extent"] = extent_obj
return converted
parser = argparse.ArgumentParser(description="Generate legacy json output format from geojosn format sources")
parser.add_argument("files", metavar="F", nargs="+", help="file(s) to process")
parser.add_argument("-b", dest="gen_bbox", action="store_true", help="generate bounding boxes from polygons")
parser.add_argument("-t", dest="tms_only", action="store_true", help="only include tile servers")
parser.add_argument(
"-r",
dest="remove_polygons",
action="store_true",
help="remove polygons from output, typically used together with -b",
)
args = parser.parse_args()
features = []
for file in args.files:
with io.open(file, "r") as f:
features.append(convert_json_source(args, json.load(f, parse_float=lambda x: round(float(x), 5))))
output = json.dumps(features, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
if sys.version_info.major == 2:
output = output.encode("utf8")
print(output)