feat: use center coordinates of faces in output filenames

This commit is contained in:
s3lph 2024-12-23 21:48:01 +01:00
parent 87c76064b7
commit 519f6b2ea9
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5

25
map.py
View file

@ -74,6 +74,8 @@ class Face:
def __init__(self, vs): def __init__(self, vs):
self.vertices = vs self.vertices = vs
self.tris = [] 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" # 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: 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] 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] 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): def convert_to_cartesian(lon, lat):
x = cos(lon*pi/180) x = cos(lon*pi/180)
y = sin(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) maxy = max(v[1] for v in face.plane_coords)
w = maxx - minx w = maxx - minx
h = maxy - miny 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) os.makedirs(os.path.dirname(outfile), exist_ok=True)
with open(outfile, 'w') as svg: with open(outfile, 'w') as svg:
svg.write(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="{minx} {miny} {w} {h}" width="{w}" height="{h}" id="face{i}">\n') svg.write(f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="{minx} {miny} {w} {h}" width="{w}" height="{h}" id="face{i}">\n')
@ -163,7 +184,7 @@ if __name__ == '__main__':
ap = argparse.ArgumentParser() ap = argparse.ArgumentParser()
ap.add_argument('--geojson', '-g', default='countries.geojson') ap.add_argument('--geojson', '-g', default='countries.geojson')
ap.add_argument('--faces', '-f', default='shapes/cube.json') 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) ap.add_argument('--scale', '-s', type=float, default=1)
ns = ap.parse_args() ns = ap.parse_args()
main(ns) main(ns)