From 9adc57e7a0ff9ca8a8f5242ec98e1464c4c51b8d Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 1 Feb 2026 21:04:32 -0800 Subject: [PATCH] rotate and output dir code --- topologytool.py | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/topologytool.py b/topologytool.py index 162cbc0..cf61abe 100644 --- a/topologytool.py +++ b/topologytool.py @@ -53,7 +53,7 @@ class Topology: self.subregion = self.country else: self.target_region_name=self.subregion - self.elevation_filename = self.target_region_name+".png" + self.elevation_filename = self.storage_directory + "/" + self.target_region_name+".png" print("Elevation file name is [{}]".format(self.elevation_filename)) @@ -111,13 +111,18 @@ class Topology: shutil.copyfileobj(response.raw, out_file) del response - def convert_png_to_data(self): + def convert_png_to_data(self,rotation=None): elevation_data = Image.open(self.elevation_filename).convert('L') + frame_image = Image.new(mode='L', size=(1224,1224), color=0) frame_image.paste(elevation_data, ((1224 - elevation_data.size[0]) // 2, (1224 - elevation_data.size[1]) // 2)) - elevation_data = frame_image + + if rotation is None: + elevation_data = frame_image + else: + elevation_data = frame_image.rotate(rotation, expand=False) width, height = elevation_data.size grid_w, grid_h = self.linemap_rows, self.linemap_cols @@ -137,7 +142,7 @@ class Topology: values[r, c] = np.mean(np.array(cell)) return values - def create_svg_file(self, rotation=0,file_index=None): + def create_svg_file(self, rotation=None,file_index=None): # if no refetch, check if file exists if os.path.exists(self.elevation_filename): print("Elevation file exists") @@ -148,10 +153,25 @@ class Topology: print("Elevation file does not exist, fetching") self.create_elevation_file() # get png data to cols x rows grid - values = self.convert_png_to_data() + values = self.convert_png_to_data(rotation) + + svg_filename = self.storage_directory + "/" + self.target_region_name+".svg" + if rotation is not None: + rotation_string = f"{rotation:05.2f}" + svg_filename = self.storage_directory + "/" + self.target_region_name + ".r" + rotation_string + ".svg" + if file_index is not None: + # assume this is an integer, convert to a string with %05d + file_index_string = f"{file_index:05d}" + svg_filename = (self.storage_directory + + "/" + self.target_region_name + + ".rot_" + + rotation_string + + "_degrees." + + file_index_string + + ".svg") # now make a line drawing of it - surface = cairo.SVGSurface(self.target_region_name+".svg", + surface = cairo.SVGSurface(svg_filename, self.linemap_size[0], self.linemap_size[1]) cr = cairo.Context(surface) @@ -162,6 +182,7 @@ class Topology: # Simple Orthographic Projection Matrix (pseudo-3D) # Projects (x,y,z) -> (x+z/2, y+z/2) + # max height should be no more than 3 times the distance between rows, this could be a setting def project(x, y, z, xscale=20, yscale=10, offset=(0, 0)): shelf = 0 if z == 0 else z + 2 shelf = z @@ -195,14 +216,20 @@ class Topology: cr.set_source_rgb(0, 0, 0) cr.set_line_width(1.0) cr.stroke() - surface.finish() + surface.write_to_png(svg_filename.replace(".svg", ".png")) + surface.finish() def main(): - topology = Topology(country="Switzerland", - subregion_mode=False) + topology = Topology(country="Spain", + storage_directory="/Users/he/PycharmProjects/toposhirt/outputfiles", + subregion_mode=False, + linemap_cols=170, + linemap_rows=270) topology.list_subunits() print(topology.get_boundingbox()) - topology.create_svg_file(rotation=0) + for r in range(4*360): + print("Rotating by [{}] degrees".format(r/4.0)) + topology.create_svg_file(rotation=(r / 4.0), file_index=r) sys.exit(0)