From: Philipp Spitzer Date: Wed, 22 Nov 2023 21:48:07 +0000 (+0100) Subject: Rename script austria_simplified to simplify_country. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/mediawiki_extensions/wrmap.git/commitdiff_plain/85cbe000278a8a6a3a6c5b91b61dfca8e8ac78da Rename script austria_simplified to simplify_country. --- diff --git a/tools/austria_simplified.py b/tools/austria_simplified.py deleted file mode 100644 index c903014..0000000 --- a/tools/austria_simplified.py +++ /dev/null @@ -1,64 +0,0 @@ -import argparse -from collections import OrderedDict -import fiona -import shapely.geometry -from fiona.crs import CRS -from fiona.transform import transform_geom -from shapely.wkt import dumps - -# data from https://www.naturalearthdata.com/downloads/50m-cultural-vectors/ -ZIP_FILE = '/home/philipp/daten/GeoData/naturalearth/v5.1/ne_50m_admin_0_countries.zip' -LAYER_NAME = 'ne_50m_admin_0_countries' - - -def load_country(country_name: str): - 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 = 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(country_name: str, shapefile: str | None): - country, crs = load_country(country_name) - 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 a country') - parser.add_argument('--country', default='Austria', help='Country to simplify (e.g. Switzerland)') - parser.add_argument('--shapefile', help='Write result to shape file with this name') - args = parser.parse_args() - main(args.country, args.shapefile) diff --git a/tools/simplify_country.py b/tools/simplify_country.py new file mode 100644 index 0000000..c903014 --- /dev/null +++ b/tools/simplify_country.py @@ -0,0 +1,64 @@ +import argparse +from collections import OrderedDict +import fiona +import shapely.geometry +from fiona.crs import CRS +from fiona.transform import transform_geom +from shapely.wkt import dumps + +# data from https://www.naturalearthdata.com/downloads/50m-cultural-vectors/ +ZIP_FILE = '/home/philipp/daten/GeoData/naturalearth/v5.1/ne_50m_admin_0_countries.zip' +LAYER_NAME = 'ne_50m_admin_0_countries' + + +def load_country(country_name: str): + 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 = 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(country_name: str, shapefile: str | None): + country, crs = load_country(country_name) + 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 a country') + parser.add_argument('--country', default='Austria', help='Country to simplify (e.g. Switzerland)') + parser.add_argument('--shapefile', help='Write result to shape file with this name') + args = parser.parse_args() + main(args.country, args.shapefile)