2 from collections import OrderedDict
4 import shapely.geometry
5 from fiona.crs import CRS
6 from fiona.transform import transform_geom
7 from shapely.wkt import dumps
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'
16 with fiona.open(f'zip://{ZIP_FILE}', layer=LAYER_NAME) as src:
18 properties = feature['properties']
19 if properties['NAME'] == COUNTRY_NAME:
20 return feature['geometry'], src.crs
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)
34 def save_country(country, crs, name, shapefile):
36 'geometry': 'Polygon',
37 'properties': OrderedDict([('name', 'str')])
39 with fiona.open(shapefile, 'w', crs=crs, schema=schema, driver='Shapefile') as c:
42 'properties': OrderedDict([('name', name)]),
47 def print_wkt(country):
48 country = shapely.geometry.shape(country)
49 print(dumps(country, rounding_precision=3))
53 country, crs = load_austria()
54 simplified_country = simplify_inner(country, crs)
56 save_country(simplified_country, crs, COUNTRY_NAME, shapefile)
57 print_wkt(simplified_country)
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()