cairosvg code

This commit is contained in:
admin 2026-02-01 14:38:43 -08:00
parent 3725b6960e
commit 8efd14111f

View File

@ -147,7 +147,55 @@ class Topology:
else: else:
print("Elevation file does not exist, fetching") print("Elevation file does not exist, fetching")
self.create_elevation_file() self.create_elevation_file()
# get png data to cols x rows grid
values = self.convert_png_to_data()
# now make a line drawing of it
surface = cairo.SVGSurface(self.target_region_name+".svg",
self.linemap_size[0],
self.linemap_size[1])
cr = cairo.Context(surface)
# Background
cr.set_source_rgb(1, 1, 1)
cr.paint()
# Simple Orthographic Projection Matrix (pseudo-3D)
# Projects (x,y,z) -> (x+z/2, y+z/2)
def project(x, y, z, xscale=20, yscale=10, offset=(0, 0)):
shelf = 0 if z == 0 else z + 2
shelf = z
return (offset[0] + x * xscale, offset[1] + y * yscale + shelf)
# return (offset[0] + x * scale + z * 2, offset[1] + y * scale + z * 2)
rows, cols = values.shape
max_val = values.max()
# Normalize data for visualization height (0-100)
norm_data = (values / max_val) * 100
# Draw Lines along rows
cr.set_source_rgb(0, 0, 0) # Black lines
cr.set_line_width(1.0)
x = 0
y = 0
for r in range(rows):
for c in range(cols):
z = norm_data[r, c]
x, y = project(c, r, -z, xscale=800 / cols, yscale=800 / rows) # -z to make higher values go "up" in 2D
if c == 0:
cr.move_to(x, y + 50)
cr.line_to(x, y)
else:
cr.line_to(x, y)
cr.line_to(x, y + 50)
cr.close_path()
cr.set_source_rgb(1, 1, 1)
cr.fill_preserve()
cr.set_source_rgb(0, 0, 0)
cr.set_line_width(1.0)
cr.stroke()
surface.finish()
def main(): def main():
topology = Topology(country="Switzerland", topology = Topology(country="Switzerland",