-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
layers: Add Sweden topo map; add script for creating layer cutlines
- Loading branch information
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
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
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,75 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
import json | ||
|
||
from shapely.geometry import MultiPolygon, Point, Polygon, shape | ||
|
||
OUTPUT_PRECISION = 5 | ||
|
||
|
||
def simplify_line(line, tolerance): | ||
return line.simplify(tolerance, preserve_topology=True) | ||
|
||
|
||
def round_coordinates(line, precision): | ||
return [(round(x, precision), round(y, precision)) for x, y in line.coords] | ||
|
||
|
||
def main(input_file, output_file, tolerance): | ||
with open(input_file, "r") as f: | ||
data = json.load(f) | ||
|
||
total_nodes = 0 | ||
simplified_geometries = [] | ||
for feature in data["features"]: | ||
geom = shape(feature["geometry"]) | ||
if isinstance(geom, Point): | ||
print("Skipping point geometry") | ||
continue | ||
if isinstance(geom, Polygon): | ||
polygons = [geom] | ||
elif isinstance(geom, MultiPolygon): | ||
polygons = geom.geoms | ||
else: | ||
print(f"Error: Unsupported geometry type '{geom.geom_type}' encountered.") | ||
exit(1) | ||
|
||
for polygon in polygons: | ||
if polygon.interiors: | ||
print("Error: Polygons with holes not supported") | ||
exit(1) | ||
linestring = polygon.exterior | ||
simplified_linestring = simplify_line(linestring, tolerance) | ||
simplified_geometries.append( | ||
round_coordinates(simplified_linestring, OUTPUT_PRECISION) | ||
) | ||
|
||
polygon_index = len(simplified_geometries) | ||
orig_nodes_count = len(linestring.coords) | ||
result_nodes_count = len(simplified_linestring.coords) | ||
|
||
print( | ||
f"Polygon #{polygon_index}: {orig_nodes_count} -> {result_nodes_count} nodes." | ||
) | ||
total_nodes += len(simplified_linestring.coords) | ||
|
||
with open(output_file, "w") as f: | ||
data = {"cutline": simplified_geometries} | ||
json.dump(data, f) | ||
print(f"Total nodes: {total_nodes}") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="Create boundary from a GeoJSON.") | ||
parser.add_argument("input_file", type=str, help="Input GeoJSON file") | ||
parser.add_argument("output_file", type=str, help="Output JSON file") | ||
parser.add_argument( | ||
"-t", | ||
"--tolerance", | ||
default=0.01, | ||
type=float, | ||
help="Tolerance for simplifying geometries, default=0.01", | ||
) | ||
args = parser.parse_args() | ||
main(args.input_file, args.output_file, args.tolerance) | ||
|
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,11 @@ | ||
How to prepare json cutline for country. | ||
|
||
1. On https://www.openstreetmap.org/ find relation id for country border | ||
2. On https://overpass-turbo.eu/ execute query, replacing ID with actual value: | ||
rel(ID); | ||
out body; | ||
>; | ||
out skel qt; | ||
3. Export result as geojson. | ||
4. Check result at https://geojson.io/ | ||
5. Use script cutline.py to create cutline file. |
Large diffs are not rendered by default.
Oops, something went wrong.