Skip to content

Commit

Permalink
layers: Add Sweden topo map; add script for creating layer cutlines
Browse files Browse the repository at this point in the history
  • Loading branch information
wladich committed Sep 2, 2024
1 parent 6a6e045 commit cdf4d1f
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,33 @@ class LayerGroupWithOptions extends L.LayerGroup {
}
)
},
{
title: 'Sweden topo',
isDefault: false,
layer: L.tileLayer(
'https://minkarta.lantmateriet.se/map/topowebbcache' +
'?layer=topowebb&style=default&tilematrixset=3857&Service=WMTS&Request=GetTile&Version=1.0.0' +
'&Format=image%2Fpng&TileMatrix={z}&TileCol={x}&TileRow={y}',
{
code: 'Se',
isOverlay: true,
tms: false,
print: true,
jnx: true,
scaleDependent: true,
shortName: 'sweden_topo',
isOverlayTransparent: false,
minZoom: 0,
maxNativeZoom: 17,
bounds: [[55.13493, 10.58876], [69.07200, 24.18365]],
noCors: false,
cutline: getCutline('sweden'),
attribution: '<a href="https://minkarta.lantmateriet.se/">' +
'Lantmäteriet</a>'
}
)
},

];

const groupsDefs = [
Expand Down Expand Up @@ -1119,6 +1146,7 @@ class LayerGroupWithOptions extends L.LayerGroup {
'Topo 250m',
'Montenegro topo 250m',
'Finland Topo',
'Sweden topo',
'Great Britain Topo',
'Slovakia topo',
'Spain topo',
Expand Down Expand Up @@ -1190,6 +1218,7 @@ class LayerGroupWithOptions extends L.LayerGroup {
'Norway paper map',
'Norway topo',
'Finland Topo',
'Sweden topo',
'Slovakia topo',
'Spain topo',
'Mountains by Alexander Purikov',
Expand Down
75 changes: 75 additions & 0 deletions src/lib/layers-cutlines/scripts/cutline.py
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)

11 changes: 11 additions & 0 deletions src/lib/layers-cutlines/scripts/readme.txt
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.
1 change: 1 addition & 0 deletions src/lib/layers-cutlines/sweden.json

Large diffs are not rendered by default.

0 comments on commit cdf4d1f

Please sign in to comment.