From 6398b4f987c28e4086f1cdb61eedcdf2d044e64c Mon Sep 17 00:00:00 2001 From: s3lph Date: Wed, 18 Dec 2024 06:52:39 +0100 Subject: [PATCH] feat: more sane arguments --- map.py | 45 ++++++++++++++++++++++++----------------- shapes/cube.json | 8 ++++++++ shapes/octahedron.json | 10 +++++++++ shapes/tetrahedron.json | 6 ++++++ 4 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 shapes/cube.json create mode 100644 shapes/octahedron.json create mode 100644 shapes/tetrahedron.json diff --git a/map.py b/map.py index fad2927..904bbc8 100644 --- a/map.py +++ b/map.py @@ -1,9 +1,11 @@ -from math import * -import numpy as np -import sys +import argparse import json +from math import * +import os +import sys +import numpy as np from tqdm import tqdm, trange @@ -108,22 +110,17 @@ def map_poly(tri, poly, ref=None): return mapped -if __name__ == '__main__': - fs = [Face([a, b, c, d]) for a, b, c, d in [ - (np.array([1,1,1]), np.array([-1,1,1]), np.array([-1,-1,1]), np.array([1,-1,1])), # top - (np.array([1,1,-1]), np.array([-1,1,-1]), np.array([-1,-1,-1]), np.array([1,-1,-1])), # bottom - (np.array([-1,-1,-1]), np.array([-1,1,-1]), np.array([-1,1,1]), np.array([-1,-1,1])), # left - (np.array([1,-1,-1]), np.array([1,1,-1]), np.array([1,1,1]), np.array([1,-1,1])), # right - (np.array([-1,-1,-1]), np.array([1,-1,-1]), np.array([1,-1,1]), np.array([-1,-1,1])), # front - (np.array([-1,1,-1]), np.array([1,1,-1]), np.array([1,1,1]), np.array([-1,1,1])), # back - ]] - with open('countries.geojson', 'r') as f: +def main(ns): + with open(ns.faces, 'r') as f: j = json.load(f) + faces = [Face([np.array(v)*ns.scale for v in vs]) for vs in j] + with open(ns.geojson, 'r') as f: + geojson = json.load(f) - for i, face in tqdm(list(enumerate(fs)), desc='Faces '): + for i, face in tqdm(list(enumerate(faces)), desc='Faces '): countries = {} for t in face.tris: - for f in tqdm(j['features'], desc='Features', total=len(face.tris)*len(j['features'])): + for f in tqdm(geojson['features'], desc='Features', total=len(face.tris)*len(geojson['features'])): cc = f['properties']['ADMIN'] if f['geometry']['type'] == 'MultiPolygon': for poly in f['geometry']['coordinates']: @@ -136,9 +133,11 @@ if __name__ == '__main__': maxy = max(v[1] for v in face.plane_coords) w = maxx - minx h = maxy - miny - with open(f'face{i}.svg', 'w') as svg: + outfile = ns.out.format(faces=os.path.basename(ns.faces).rsplit('.', 1)[0], geojson=ns.geojson, face=i) + os.makedirs(os.path.dirname(outfile), exist_ok=True) + with open(outfile, 'w') as svg: svg.write(f'\n') - svg.write(f' \n \n \n \n \n') @@ -146,9 +145,19 @@ if __name__ == '__main__': if l: svg.write(f' \n') for ls in l: - svg.write(f' \n') svg.write(' \n') svg.write(' \n\n') + + +if __name__ == '__main__': + ap = argparse.ArgumentParser() + ap.add_argument('--geojson', '-g', default='countries.geojson') + ap.add_argument('--faces', '-f', default='shapes/cube.json') + ap.add_argument('--out', '-o', default='faces/{faces}/face{face}.svg') + ap.add_argument('--scale', '-s', type=int, default=1) + ns = ap.parse_args() + main(ns) diff --git a/shapes/cube.json b/shapes/cube.json new file mode 100644 index 0000000..b6bce48 --- /dev/null +++ b/shapes/cube.json @@ -0,0 +1,8 @@ +[ + [[1,1,1], [-1,1,1], [-1,-1,1], [1,-1,1]], + [[1,1,-1], [-1,1,-1], [-1,-1,-1], [1,-1,-1]], + [[-1,-1,-1], [-1,1,-1], [-1,1,1], [-1,-1,1]], + [[1,-1,-1], [1,1,-1], [1,1,1], [1,-1,1]], + [[-1,-1,-1], [1,-1,-1], [1,-1,1], [-1,-1,1]], + [[-1,1,-1], [1,1,-1], [1,1,1], [-1,1,1]] +] diff --git a/shapes/octahedron.json b/shapes/octahedron.json new file mode 100644 index 0000000..3bec92b --- /dev/null +++ b/shapes/octahedron.json @@ -0,0 +1,10 @@ +[ + [[0,0,1], [-1,0,0], [0,-1,0]], + [[0,0,1], [0,-1,0], [1,0,0]], + [[0,0,1], [1,0,0], [0,1,0]], + [[0,0,1], [0,1,0], [-1,0,0]], + [[0,0,-1], [-1,0,0], [0,-1,0]], + [[0,0,-1], [0,-1,0], [1,0,0]], + [[0,0,-1], [1,0,0], [0,1,0]], + [[0,0,-1], [0,1,0], [-1,0,0]] +] diff --git a/shapes/tetrahedron.json b/shapes/tetrahedron.json new file mode 100644 index 0000000..f214b1d --- /dev/null +++ b/shapes/tetrahedron.json @@ -0,0 +1,6 @@ +[ + [[1,1,1], [1,-1,-1], [-1,1,-1]], + [[1,1,1], [1,-1,-1], [-1,-1,1]], + [[1,1,1], [-1,1,-1], [-1,-1,1]], + [[1,-1,-1], [-1,1,-1], [-1,-1,1]] +]