]> ToastFreeware Gitweb - philipp/winterrodeln/mediawiki_extensions/wrmap.git/blob - tools/austria_simplified.py
Update to NaturalEarth 5.1
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / tools / austria_simplified.py
1 import argparse
2 from collections import OrderedDict
3 import fiona
4 import shapely.geometry
5 from fiona.crs import CRS
6 from fiona.transform import transform_geom
7 from shapely.wkt import dumps
8
9 # data from https://www.naturalearthdata.com/downloads/50m-cultural-vectors/
10 ZIP_FILE = '/home/philipp/daten/GeoData/naturalearth/v5.1/ne_50m_admin_0_countries.zip'
11 LAYER_NAME = 'ne_50m_admin_0_countries'
12 COUNTRY_NAME = 'Austria'
13
14
15 def load_austria():
16     with fiona.open(f'zip://{ZIP_FILE}', layer=LAYER_NAME) as src:
17         for feature in src:
18             properties = feature['properties']
19             if properties['NAME'] == COUNTRY_NAME:
20                 return feature['geometry'], src.crs
21
22
23 def simplify_inner(country, crs):
24     metric_crs = CRS.from_epsg(32632)  # Western Austria (UTM32)
25     country = transform_geom(crs, metric_crs, country)
26     country = shapely.geometry.shape(country)
27     country = country.buffer(-5000)
28     country = country.simplify(tolerance=5000)
29     country = shapely.geometry.mapping(country)
30     country = transform_geom(metric_crs, crs, country)
31     return country
32
33
34 def save_country(country, crs, name, shapefile):
35     schema = {
36         'geometry': 'Polygon',
37         'properties': OrderedDict([('name', 'str')])
38     }
39     with fiona.open(shapefile, 'w', crs=crs, schema=schema, driver='Shapefile') as c:
40         feature = {
41             'geometry': country,
42             'properties': OrderedDict([('name', name)]),
43         }
44         c.write(feature)
45
46
47 def print_wkt(country):
48     country = shapely.geometry.shape(country)
49     print(dumps(country, rounding_precision=3))
50
51
52 def main(shapefile):
53     country, crs = load_austria()
54     simplified_country = simplify_inner(country, crs)
55     if shapefile:
56         save_country(simplified_country, crs, COUNTRY_NAME, shapefile)
57     print_wkt(simplified_country)
58
59
60 if __name__ == '__main__':
61     parser = argparse.ArgumentParser(description='Creates a simplified version of Austria')
62     parser.add_argument('--shapefile', default='simplified_austria.shp',
63                         help='Write result to shape file with this name')
64     args = parser.parse_args()
65     main(args.shapefile)