From 519f6b2ea9dd407f4c40b0a79b6f70838ef88a62 Mon Sep 17 00:00:00 2001 From: s3lph Date: Mon, 23 Dec 2024 21:48:01 +0100 Subject: [PATCH] feat: use center coordinates of faces in output filenames --- map.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/map.py b/map.py index aec330a..a6f2a4f 100644 --- a/map.py +++ b/map.py @@ -74,6 +74,8 @@ class Face: def __init__(self, vs): self.vertices = vs self.tris = [] + avg = np.average(self.vertices, axis=0) + self.center = convert_to_spherical(*avg) # Sort vertices so that so that the normal points "outward" if np.dot(self.vertices[0], np.cross(self.vertices[1] - self.vertices[0], self.vertices[2] - self.vertices[0])) < 0: self.vertices = self.vertices[::-1] @@ -82,6 +84,14 @@ class Face: self.plane_coords = [self.tris[0].plane_coords(v) for v in self.vertices] +def convert_to_spherical(x, y, z): + xy = sqrt(x**2+y**2) + lon = atan2(z, xy)*180/pi + lat = atan2(y, x)*180/pi + mag = sqrt(x**2+y**2+z**2) + return np.array([lon, lat, mag]) + + def convert_to_cartesian(lon, lat): x = cos(lon*pi/180) y = sin(lon*pi/180) @@ -139,7 +149,18 @@ def main(ns): maxy = max(v[1] for v in face.plane_coords) w = maxx - minx h = maxy - miny - outfile = ns.out.format(faces=os.path.basename(ns.faces).rsplit('.', 1)[0], geojson=ns.geojson, face=i) + formatters = dict( + faces=os.path.basename(ns.faces).rsplit('.', 1)[0], + geojson=ns.geojson, + face=i, + lon=face.center[0], + lat=face.center[1], + mag=face.center[2], + ilon=int(face.center[0]), + ilat=int(face.center[1]), + imag=int(face.center[2]), + ) + outfile = ns.out.format(**formatters) os.makedirs(os.path.dirname(outfile), exist_ok=True) with open(outfile, 'w') as svg: svg.write(f'\n') @@ -163,7 +184,7 @@ 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('--out', '-o', default='faces/{faces}/face{face}_{ilat}_{ilon}.svg') ap.add_argument('--scale', '-s', type=float, default=1) ns = ap.parse_args() main(ns)