Support for command line arguments

This commit is contained in:
Thomas Kolb 2021-05-30 22:20:40 +02:00
parent af794b0598
commit a766274d79

View file

@ -1,16 +1,18 @@
#!/usr/bin/env python3
import sys
import svgwrite
import numpy as np
import matplotlib.pyplot as pp
from matplotlib.colors import hsv_to_rgb
import json
import random
import argparse
REF_LATITUDE = 49.58666
REF_LONGITUDE = 11.01250
# REF_LATITUDE = 1
# REF_LONGITUDE = 0
# REF_LATITUDE = -30
# REF_LONGITUDE = 90
def map_azimuthal_equidistant(lat, lon, ref_lat, ref_lon, R=1):
@ -63,6 +65,7 @@ def random_country_color():
return f"#{r:02x}{g:02x}{b:02x}"
def render(ref_lat, ref_lon, output_stream):
random.seed(0)
""" Test code
@ -73,15 +76,15 @@ for lat in test_lat:
for lon in test_lon:
x, y = map_azimuthal_equidistant(np.array([lat]), np.array([lon]), np.pi/2, 0)
print(f"{lat*180/np.pi:6.3f}, {lon*180/np.pi:6.3f} => {x[0]:6.3f}, {y[0]:6.3f}")
print(f"{lat*180/np.pi:6.3f}, {lon*180/np.pi:6.3f} => {x[0]:6.3f}, {y[0]:6.3f}", file=sys.stderr)
"""
print("Loading Geodata…")
print("Loading Geodata…", file=sys.stderr)
with open('geo-countries/data/countries.geojson', 'r') as jfile:
geojson = json.load(jfile)
print("Finding boundaries…")
print("Finding boundaries…", file=sys.stderr)
# key: 3-letter country identifier
# data: {full_name, numpy.array(coordinates), numpy.array(proj_coordinates)}.
@ -101,7 +104,7 @@ for feature in features:
if key in simplegeodata.keys():
key = name
print(f"Preparing {key} ({name})…")
print(f"Preparing {key} ({name})…", file=sys.stderr)
multipoly = feature['geometry']['coordinates']
@ -121,8 +124,8 @@ for feature in features:
simplegeodata[key] = {"name": name, "coordinates": conv_polys}
ref_lat = REF_LATITUDE * np.pi / 180
ref_lon = REF_LONGITUDE * np.pi / 180
ref_lat = ref_lat * np.pi / 180
ref_lon = ref_lon * np.pi / 180
R = 500
@ -215,7 +218,7 @@ doc.add(doc.circle(center=(R, R), r=R, fill='#ddeeff',
stroke_width=1, stroke='black'))
for k, v in simplegeodata.items():
print(f"Exporting {k}")
print(f"Exporting {k}", file=sys.stderr)
color = random_country_color()
@ -302,11 +305,10 @@ for x in range(0, 26):
sectorname = chr(ord('A')+x) + chr(ord('A')+y)
"""
print(f"Writing output…", file=sys.stderr)
doc.write(output_stream, pretty=True)
print(f"Saving {doc.filename}")
doc.save(pretty=True)
exit(0)
return
# Debug Plot
@ -330,3 +332,20 @@ pp.axis('equal')
pp.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Render an azimuthal equidistant map of the world " +
"centered on the given point")
parser.add_argument(metavar='ref-lat', type=float, dest='ref_lat',
help='Reference Latitude')
parser.add_argument(metavar='ref-lon', type=float, dest='ref_lon',
help='Reference Longitude')
parser.add_argument('-o', '--output-file', type=argparse.FileType('w'),
help='The output SVG file (default: print to stdout)',
default=sys.stdout)
args = parser.parse_args()
render(args.ref_lat, args.ref_lon, args.output_file)