rotate and output dir code

This commit is contained in:
admin 2026-02-01 21:04:32 -08:00
parent 8efd14111f
commit 9adc57e7a0

View File

@ -53,7 +53,7 @@ class Topology:
self.subregion = self.country self.subregion = self.country
else: else:
self.target_region_name=self.subregion 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)) print("Elevation file name is [{}]".format(self.elevation_filename))
@ -111,13 +111,18 @@ class Topology:
shutil.copyfileobj(response.raw, out_file) shutil.copyfileobj(response.raw, out_file)
del response 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') elevation_data = Image.open(self.elevation_filename).convert('L')
frame_image = Image.new(mode='L', size=(1224,1224), color=0) frame_image = Image.new(mode='L', size=(1224,1224), color=0)
frame_image.paste(elevation_data, frame_image.paste(elevation_data,
((1224 - elevation_data.size[0]) // 2, ((1224 - elevation_data.size[0]) // 2,
(1224 - elevation_data.size[1]) // 2)) (1224 - elevation_data.size[1]) // 2))
if rotation is None:
elevation_data = frame_image elevation_data = frame_image
else:
elevation_data = frame_image.rotate(rotation, expand=False)
width, height = elevation_data.size width, height = elevation_data.size
grid_w, grid_h = self.linemap_rows, self.linemap_cols grid_w, grid_h = self.linemap_rows, self.linemap_cols
@ -137,7 +142,7 @@ class Topology:
values[r, c] = np.mean(np.array(cell)) values[r, c] = np.mean(np.array(cell))
return values 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 no refetch, check if file exists
if os.path.exists(self.elevation_filename): if os.path.exists(self.elevation_filename):
print("Elevation file exists") print("Elevation file exists")
@ -148,10 +153,25 @@ class Topology:
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 # 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 # 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[0],
self.linemap_size[1]) self.linemap_size[1])
cr = cairo.Context(surface) cr = cairo.Context(surface)
@ -162,6 +182,7 @@ class Topology:
# Simple Orthographic Projection Matrix (pseudo-3D) # Simple Orthographic Projection Matrix (pseudo-3D)
# Projects (x,y,z) -> (x+z/2, y+z/2) # 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)): def project(x, y, z, xscale=20, yscale=10, offset=(0, 0)):
shelf = 0 if z == 0 else z + 2 shelf = 0 if z == 0 else z + 2
shelf = z shelf = z
@ -195,14 +216,20 @@ class Topology:
cr.set_source_rgb(0, 0, 0) cr.set_source_rgb(0, 0, 0)
cr.set_line_width(1.0) cr.set_line_width(1.0)
cr.stroke() cr.stroke()
surface.write_to_png(svg_filename.replace(".svg", ".png"))
surface.finish() surface.finish()
def main(): def main():
topology = Topology(country="Switzerland", topology = Topology(country="Spain",
subregion_mode=False) storage_directory="/Users/he/PycharmProjects/toposhirt/outputfiles",
subregion_mode=False,
linemap_cols=170,
linemap_rows=270)
topology.list_subunits() topology.list_subunits()
print(topology.get_boundingbox()) 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) sys.exit(0)