Helper script to create a simplified, inner WKT of Austria.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 6 Jan 2021 22:47:20 +0000 (23:47 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 6 Jan 2021 22:49:19 +0000 (23:49 +0100)
tools/austria_simplified.py [new file with mode: 0644]

diff --git a/tools/austria_simplified.py b/tools/austria_simplified.py
new file mode 100644 (file)
index 0000000..1dfa24d
--- /dev/null
@@ -0,0 +1,64 @@
+import argparse
+from collections import OrderedDict
+import fiona
+import shapely.geometry
+from fiona.crs import from_epsg
+from fiona.transform import transform_geom
+from shapely.wkt import dumps
+
+ZIP_FILE = '/home/philipp/daten/GeoData/naturalearth/v4.1/50m_cultural.zip'
+LAYER_NAME = 'ne_50m_admin_0_countries'
+COUNTRY_NAME = 'Austria'
+
+
+def load_austria():
+    with fiona.open(f'zip://{ZIP_FILE}', layer=LAYER_NAME) as src:
+        for feature in src:
+            properties = feature['properties']
+            if properties['NAME'] == COUNTRY_NAME:
+                return feature['geometry'], src.crs
+
+
+def simplify_inner(country, crs):
+    metric_crs = from_epsg(32632)  # Western Austria (UTM32)
+    country = transform_geom(crs, metric_crs, country)
+    country = shapely.geometry.shape(country)
+    country = country.buffer(-5000)
+    country = country.simplify(tolerance=5000)
+    country = shapely.geometry.mapping(country)
+    country = transform_geom(metric_crs, crs, country)
+    return country
+
+
+def save_country(country, crs, name, shapefile):
+    schema = {
+        'geometry': 'Polygon',
+        'properties': OrderedDict([('name', 'str')])
+    }
+    with fiona.open(shapefile, 'w', crs=crs, schema=schema, driver='Shapefile') as c:
+        feature = {
+            'geometry': country,
+            'properties': OrderedDict([('name', name)]),
+        }
+        c.write(feature)
+
+
+def print_wkt(country):
+    country = shapely.geometry.shape(country)
+    print(dumps(country, rounding_precision=3))
+
+
+def main(shapefile):
+    country, crs = load_austria()
+    simplified_country = simplify_inner(country, crs)
+    if shapefile:
+        save_country(simplified_country, crs, COUNTRY_NAME, shapefile)
+    print_wkt(simplified_country)
+
+
+if __name__ == '__main__':
+    parser = argparse.ArgumentParser(description='Creates a simplified version of Austria')
+    parser.add_argument('--shapefile', default='simplified_austria.shp',
+                        help='Write result to shape file with this name')
+    args = parser.parse_args()
+    main(args.shapefile)