-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmaps.py
More file actions
365 lines (299 loc) · 14.4 KB
/
Copy pathmaps.py
File metadata and controls
365 lines (299 loc) · 14.4 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import os
import json
# Everything ending in .json is in TopoJSON format, and GeoJSON otherwise
FILENAME_TO_COUNTRY = {
# exploratory.io
"states.json": "United States of America",
"canada_provinces.json": "Canada",
"aus_state.json": "Australia",
"br_regions.json": "Brazil",
# Joost Hietbrink
"provinces.json": "Netherlands",
# deldersveld
"algeria-provinces.json": "Algeria",
"argentina-provinces.json": "Argentina",
"azerbaijan-regions.json": "Azerbaijan",
"belgium-provinces.json": "Belgium",
"chile-regions.json": "Chile",
"china-provinces.json": "China",
"colombia-departments.json": "Colombia",
"czech-republic-regions.json": "Czechia",
"denmark-counties.json": "Denmark",
"finland-provinces.json": "Finland",
"fr-departments.json": "France",
"germany-regions.json": "Germany",
"india-states.json": "India",
"ireland-counties.json": "Ireland",
"italy-regions.json": "Italy",
"jp-prefectures.json": "Japan",
"liberia-counties.json": "Liberia",
"nepal-zones.json": "Nepal",
"new-zealand-regional-councils.json": "New Zealand",
"nigeria-states.json": "Nigeria",
"norway-counties.json": "Norway",
"pakistan-provinces.json": "Pakistan",
"peru-departments-no-illegal-char.json": "Peru",
"philippines-provinces.json": "Philippines",
"poland-provinces.json": "Poland",
"portugal-districts.json": "Portugal",
"romania-counties.json": "Romania",
"south-africa-provinces.json": "South Africa",
"spain-comunidad-with-canary-islands.json": "Spain",
"sweden-counties.json": "Sweden",
"turkiye.json": "Turkey",
"uk-counties.json": "United Kingdom",
"united-arab-emirates.json": "United Arab Emirates",
"venezuela-estados.json": "Venezuela",
# ponentesincausa
"mexico.json": "Mexico",
}
LINKS = [
# React Simple Maps
"https://github.com/zcreativelabs/react-simple-maps/raw/master/topojson-maps/world-110m.json",
# exploratory.io, may have to download manually
"https://download2.exploratory.io/maps/states.zip",
"https://download2.exploratory.io/maps/canada_provinces.zip",
"https://download2.exploratory.io/maps/aus_states.zip",
"https://download2.exploratory.io/maps/br_regions.zip", # not MIT licensed
# Joost Hietbrink
"https://www.webuildinternet.com/articles/2015-07-19-geojson-data-of-the-netherlands/provinces.geojson", # not MIT licensed
# deldersveld
"https://github.com/deldersveld/topojson/raw/master/countries/algeria/algeria-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/argentina/argentina-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/azerbaijan/azerbaijan-regions.json",
"https://github.com/deldersveld/topojson/raw/master/countries/belgium/belgium-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/chile/chile-regions.json",
"https://github.com/deldersveld/topojson/raw/master/countries/china/china-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/colombia/colombia-departments.json",
"https://github.com/deldersveld/topojson/raw/master/countries/czech-republic/czech-republic-regions.json",
"https://github.com/deldersveld/topojson/raw/master/countries/denmark/denmark-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/finland/finland-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/france/fr-departments.json",
"https://github.com/deldersveld/topojson/raw/master/countries/germany/germany-regions.json",
"https://github.com/deldersveld/topojson/raw/master/countries/india/india-states.json",
"https://github.com/deldersveld/topojson/raw/master/countries/ireland/ireland-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/italy/italy-regions.json",
"https://github.com/deldersveld/topojson/raw/master/countries/japan/jp-prefectures.json",
"https://github.com/deldersveld/topojson/raw/master/countries/liberia/liberia-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/nepal/nepal-zones.json",
"https://github.com/deldersveld/topojson/raw/master/countries/new-zealand/new-zealand-regional-councils.json",
"https://github.com/deldersveld/topojson/raw/master/countries/nigeria/nigeria-states.json",
"https://github.com/deldersveld/topojson/raw/master/countries/norway/norway-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/pakistan/pakistan-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/peru/peru-departments.json",
"https://github.com/deldersveld/topojson/raw/master/countries/philippines/philippines-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/poland/poland-provinces.json",
"https://github.com/deldersveld/topojson/raw/master/countries/portugal/portugal-districts.json",
"https://github.com/deldersveld/topojson/raw/master/countries/romania/romania-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/spain/spain-comunidad-with-canary-islands.json",
"https://github.com/deldersveld/topojson/raw/master/countries/sweden/sweden-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/turkey/turkiye.json",
"https://github.com/deldersveld/topojson/raw/master/countries/united-arab-emirates/united-arab-emirates.json",
"https://github.com/deldersveld/topojson/raw/master/countries/united-kingdom/uk-counties.json",
"https://github.com/deldersveld/topojson/raw/master/countries/venezuela/venezuela-estados.json",
# ponentesincausa
"https://gist.github.com/ponentesincausa/46d1d9a94ca04a56f93d/raw/a05f4e2b42cf981e31ef9f6f9ee151a060a38c25/mexico.json", # not MIT licensed
]
# Given ~/installation-map/maps.py and ~/installation-map/maps
REPO_ROOT = "installation-map/"
MAPS = "maps/used/"
BASE_MAP = "world-110m.json"
USA_MAP = "us-albers.json"
PERU_MAP = "peru-departments.json"
HOME_DIR = "%s/" % os.getenv("HOME")
YARN_DIR = "%s.yarn/bin/" % HOME_DIR
MAPS_DIR = "%s%s%s" % (HOME_DIR, REPO_ROOT, MAPS)
BASE_MAP_DIR = "%s%s" % (MAPS_DIR, BASE_MAP)
USA_MAP_DIR = "%s%s" % (MAPS_DIR, USA_MAP)
PERU_MAP_DIR = "%s%s" % (MAPS_DIR, PERU_MAP)
PUBLIC_DIR = "%s%s%s" % (HOME_DIR, REPO_ROOT, "public/")
def remove_old_files(maps_path):
os.chdir(maps_path)
print("Removing old .txt, .simple, .merged, and .geojson files")
os.system("rm *.txt")
os.system("rm *.simple")
os.system("rm *.merged")
os.system("rm *.geojson")
def get_jsons(maps_path, links):
def is_zip(filename):
return filename.endswith(".zip")
os.chdir(maps_path)
files = os.listdir(maps_path)
zips = list(filter(is_zip, files))
# Comment out if TopoJSONs already exist
# for link in links:
# os.system('curl -LJO %s' % link)
for zipfile in zips:
os.system("unzip -o %s" % zipfile)
# Convert downloaded GeoJSON to TopoJSON
os.system("mv mexico.json mexico.geojson")
files = os.listdir(maps_path)
os.system('find . -name "*.geojson" > geojsons.txt')
f = open("geojsons.txt", "r+")
geojsons = f.read().splitlines()
f.close()
for input_geojson in geojsons:
temp = input_geojson.split("/")
last = len(temp) - 1
filename = temp[last]
temp = filename.split(".")
last = len(temp) - 1
prefix = temp[last - 1]
output_topojson = "%s.json" % prefix
convert_geo2topo(maps_path, input_geojson, output_topojson)
def remove_peru_objects_illegal_char(peru_map_path):
prefix = peru_map_path.split(".")[0]
output_file = "%s-no-illegal-char.json" % prefix
peru_map = json.load(open(peru_map_path))
old_key = "peru-departments'"
new_key = "peru-departments"
peru_map["objects"][new_key] = peru_map["objects"].pop(old_key)
with open(output_file, "w") as outfile:
json.dump(peru_map, outfile)
def create_filelist(path, filename_map):
def is_json(filename):
return filename.endswith(".json")
def in_filename_map(filename):
return filename in filename_map
files = os.listdir(path)
filelist = list(filter(is_json, files))
filelist = list(filter(in_filename_map, filelist))
return filelist
def create_template_file(base_map_path, filelist, filename_map):
def get_geometries(base_map):
geometries = base_map["objects"]["ne_110m_admin_0_countries"]["geometries"]
return geometries
def update_geometries():
base_map = json.load(open(base_map_path))
geometries = get_geometries(base_map)
names = get_countries_with_states(filelist, filename_map)
base_map["objects"]["ne_110m_admin_0_countries"][
"geometries"
] = overwrite_countries_arcs(geometries, names)
return base_map
# Open base map and remove countries who will be replaced with their states
updated_base_map = update_geometries()
# Write 2 copies of template file for detailed maps with and without states
prefix = base_map_path.split(".")[0]
with open("%s.simple" % prefix, "w") as outfile:
json.dump(updated_base_map, outfile)
os.system("cp %s.simple %s.merged" % (prefix, prefix))
def get_countries_with_states(filelist, filename_map):
def country_has_states(input_file):
return input_file in filename_map
def get_real_name(input_file):
return filename_map[input_file]
filtered_filelist = list(filter(country_has_states, filelist))
countries_with_states = list(map(get_real_name, filtered_filelist))
return countries_with_states
def overwrite_countries_arcs(geometries, names):
def get_country_name(geometry):
return geometry["properties"]["NAME"]
def empty_geometry_arcs(geometry):
given_name = get_country_name(geometry)
if given_name in names:
geometry["arcs"].clear()
return geometry
updated_geometries = list(map(empty_geometry_arcs, geometries))
return updated_geometries
def simplify_topojsons(maps_path, filelist, filename_map):
os.chdir(maps_path)
toposimplify = "%stoposimplify" % YARN_DIR
for input_file in filelist:
if not input_file in filename_map.keys():
continue
else:
prefix = input_file.split(".")[0]
output_file = prefix + ".simple"
threshold = 0.27
os.system(
"%s -o %s -P %f %s" % (toposimplify, output_file, threshold, input_file)
)
def merge_topojson_states(maps_path, filelist, filename_map):
os.chdir(maps_path)
topomerge = "%stopomerge" % YARN_DIR
for input_file in filelist:
prefix = input_file.split(".")[0]
if not input_file in filename_map.keys():
continue
else:
simple = prefix + ".simple"
output_file = prefix + ".merged"
input_map = json.load(open(simple))
source_object = list(input_map["objects"].keys())[0]
os.system(
"%s %s=%s -o %s %s"
% (topomerge, source_object, source_object, output_file, simple)
)
def convert_topo2geo(maps_path, filelist, input_suffix):
os.chdir(maps_path)
topo2geo = "%stopo2geo" % YARN_DIR
filelist.append(BASE_MAP)
os.system("> objects.txt")
# Get list of all simplified topojsons with their objects
for f in filelist:
prefix = f.split(".")[0]
input_file = "%s%s" % (prefix, input_suffix)
command = "%s -l -i %s >> objects.txt" % (topo2geo, input_file)
os.system(command)
# Since files must end with a newline in UNIX, the last element is empty.
# So, we must remove it, or else we zip through lists of uneven length.
f = open("objects.txt", "r+")
objects = f.read().split("\n")[:-1]
f.close()
for object, f in zip(objects, filelist):
prefix = f.split(".")[0]
input_file = "%s%s" % (prefix, input_suffix)
output_file = "%s.geojson" % input_file
command = "%s %s=%s < %s" % (topo2geo, object, output_file, input_file)
os.system(command)
def merge_geojsons(maps_path, output_geojson, input_suffix):
os.chdir(maps_path)
input_files = "*%s.geojson" % input_suffix
geojson_merge = "%sgeojson-merge" % YARN_DIR
command = "%s %s > %s" % (geojson_merge, input_files, output_geojson)
os.system(command)
def convert_geo2topo(maps_path, input_geojson, output_topojson):
os.chdir(maps_path)
geo2topo = "%sgeo2topo" % YARN_DIR
command = "%s features=%s > %s" % (geo2topo, input_geojson, output_topojson)
os.system(command)
def deploy_to_public(maps_path, input_topojson, public_path):
os.chdir(maps_path)
os.system("cp %s %s" % (input_topojson, public_path))
def main():
# GeoJSON = intermediate output
states_geojson = "detailed-with-states.geojson"
borders_geojson = "detailed-no-states.geojson"
# TopoJSON = final output
states_topojson = "detailed-with-states.topojson"
borders_topojson = "detailed-no-states.topojson"
# Remove all files from previous run
remove_old_files(MAPS_DIR)
# Get JSONs for processing, will need to convert some GeoJSONs to TopoJSON
# get_jsons(MAPS_DIR, LINKS)
# Remove illegal single quote from Peru objects name
remove_peru_objects_illegal_char(PERU_MAP_DIR)
# Create filelist to use in creating combined map
filelist = create_filelist(MAPS_DIR, FILENAME_TO_COUNTRY)
# Replace countries by their states if states exist
create_template_file(BASE_MAP_DIR, filelist, FILENAME_TO_COUNTRY)
# Simplify TopoJSONs for merging
simplify_topojsons(MAPS_DIR, filelist, FILENAME_TO_COUNTRY)
# Merge TopoJSON states to make countries with better borders
merge_topojson_states(MAPS_DIR, filelist, FILENAME_TO_COUNTRY)
# Convert TopoJSONs to GeoJSONs
convert_topo2geo(MAPS_DIR, filelist, ".simple")
convert_topo2geo(MAPS_DIR, filelist, ".merged")
# Merge all GeoJSONs into 1
merge_geojsons(MAPS_DIR, states_geojson, ".simple")
merge_geojsons(MAPS_DIR, borders_geojson, ".merged")
# Convert GeoJSON back to TopoJSON
convert_geo2topo(MAPS_DIR, states_geojson, states_topojson)
convert_geo2topo(MAPS_DIR, borders_geojson, borders_topojson)
# Copy the final TopoJSONs into the public folder
deploy_to_public(MAPS_DIR, states_topojson, PUBLIC_DIR)
deploy_to_public(MAPS_DIR, borders_topojson, PUBLIC_DIR)
deploy_to_public(MAPS_DIR, BASE_MAP, PUBLIC_DIR)
if __name__ == "__main__":
main()