-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding python script used to fix borders on the map
- Loading branch information
Sacha Laurent
committed
Jan 31, 2025
1 parent
c45e80d
commit 2fbd4df
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/features/worldmap/assets/merging_simplified_world_map_and_borders_tmp.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import json | ||
|
||
countries_to_be_fixed = [ | ||
"CHN", | ||
"EGY", | ||
"IND", | ||
"ISR", | ||
"KOR", | ||
"PAK", | ||
"PRK", | ||
"PSE", | ||
"RUS", | ||
"SDN", | ||
"SSD" | ||
] | ||
|
||
fixed_borders = {} | ||
|
||
for country in countries_to_be_fixed: | ||
mainland = "QGISModifiedBorders/MainLands/" + country + ".geojson" | ||
islands = "QGISModifiedBorders/Islands/" + country + ".geojson" | ||
borders = [] | ||
try: | ||
f = open(islands, "r", encoding="utf8") | ||
data = json.load(f) | ||
for feature in data["features"]: | ||
if feature["geometry"]["type"] == "LineString": | ||
borders.append(feature["geometry"]["coordinates"]) | ||
elif feature["geometry"]["type"] == "MultiLineString": | ||
for line in feature["geometry"]["coordinates"]: | ||
borders.append(line) | ||
f.close() | ||
except FileNotFoundError: | ||
pass | ||
f = open(mainland, "r", encoding="utf8") | ||
data = json.load(f) | ||
for feature in data["features"]: | ||
if feature["geometry"]["type"] == "LineString": | ||
borders.append(feature["geometry"]["coordinates"]) | ||
elif feature["geometry"]["type"] == "MultiLineString": | ||
for line in feature["geometry"]["coordinates"]: | ||
borders.append(line) | ||
print(country, len(borders)) | ||
fixed_borders[country] = { | ||
"type": "MultiLineString", | ||
"coordinates": borders | ||
} | ||
|
||
with open('QGISModifiedBorders/WHO_ADM0_simplified_by_area_tolerance_0_15.geojson', 'r', encoding="utf8") as f: | ||
data = json.load(f) | ||
|
||
for feature in data["features"]: | ||
feature["properties"] = {k: feature["properties"][k] for k in ("ISO_3_CODE", "WHO_REGION", "ADM0_SOVRN", "GUID")} | ||
if feature["properties"]["ISO_3_CODE"]=="IOT": | ||
new_polygon = [] | ||
print(feature["geometry"]["coordinates"]) | ||
for coordinate in feature["geometry"]["coordinates"]: | ||
new_shape = [] | ||
for shape in coordinate: | ||
new_shape.append(list([list(u) for u in set([tuple(v) for v in shape])])) | ||
new_polygon.append(new_shape) | ||
print(new_polygon) | ||
feature["geometry"]["coordinates"] = new_polygon | ||
if feature["properties"]["ISO_3_CODE"] in countries_to_be_fixed: | ||
polygon_copy = feature["geometry"].copy() | ||
feature["geometry"] = { | ||
"type": "GeometryCollection", | ||
"geometries":[ | ||
polygon_copy, | ||
fixed_borders[feature["properties"]["ISO_3_CODE"]] | ||
] | ||
} | ||
|
||
with open('odata.json', 'w', encoding="utf8") as outfile: | ||
json.dump(data, outfile, indent=2) | ||
|
||
|