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]] +]