Commit 0b4ff1bb authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Finished first version of "geojson2vadere.py" and imported first topography...

Finished first version of "geojson2vadere.py" and imported first topography successfully into Vadere GUI.
parent f283d793
Pipeline #86907 passed with stages
in 100 minutes and 49 seconds
......@@ -104,17 +104,46 @@ def filter_for_buildings(geojson_content):
return buildings
def find_minimal_coordinates(buildings):
all_vertices = [vertex for building in buildings for vertex in building]
tuple_with_min_x = min(all_vertices, key=lambda point: point[0])
tuple_with_min_y = min(all_vertices, key=lambda point: point[1])
return (tuple_with_min_x[0], tuple_with_min_y[1])
def find_maximum_coordinates(buildings):
all_vertices = [vertex for building in buildings for vertex in building]
tuple_with_max_x = max(all_vertices, key=lambda point: point[0])
tuple_with_max_y = max(all_vertices, key=lambda point: point[1])
rounded_max_x = math.ceil(tuple_with_max_x[0])
rounded_max_y = math.ceil(tuple_with_max_y[1])
return (rounded_max_x, rounded_max_y)
def shift_buildings(buildings, shift_in_x_direction, shift_in_y_direction):
shifted_buildings = []
for building in buildings:
shifted_building = \
[(vertex[0] + shift_in_x_direction, vertex[1] + shift_in_y_direction) for vertex in building]
shifted_buildings.append(shifted_building)
return shifted_buildings
def convert_buildings_to_vadere_obstacle_strings(buildings):
list_of_vadere_obstacles_as_strings = []
# A single building is a list of vertices.
for building in buildings:
vadere_obstacle_as_string = create_vadere_obstracle_from_vertices(building)
vadere_obstacle_as_string = create_vadere_obstacle_from_vertices(building)
list_of_vadere_obstacles_as_strings.append(vadere_obstacle_as_string)
return list_of_vadere_obstacles_as_strings
def create_vadere_obstracle_from_vertices(cartesian_points):
def create_vadere_obstacle_from_vertices(vertices):
vadere_obstacle_string = """{
"shape" : {
"type" : "POLYGON",
......@@ -123,45 +152,38 @@ def create_vadere_obstracle_from_vertices(cartesian_points):
"id" : -1
}"""
vadere_point_string = '{ "x" : $x, "y" : $y }'
obstacle_string_template = Template(vadere_obstacle_string)
point_string_template = Template(vadere_point_string)
points_as_string = [point_string_template.substitute(x=x, y=y) for x, y in cartesian_points]
points_as_string = [point_string_template.substitute(x=x, y=y) for x, y in vertices]
points_as_string_concatenated = ",\n".join(points_as_string)
vadere_obstacle_as_string = obstacle_string_template.substitute(points=points_as_string_concatenated)
return vadere_obstacle_as_string
def find_minimal_coordinates(buildings):
all_vertices = [vertex for building in buildings for vertex in building]
tuple_with_min_x = min(all_vertices, key=lambda point: point[0])
tuple_with_min_y = min(all_vertices, key=lambda point: point[1])
return (tuple_with_min_x[0], tuple_with_min_y[1])
return vadere_obstacle_as_string
def find_maximum_coordinates(buildings):
all_vertices = [vertex for building in buildings for vertex in building]
tuple_with_max_x = max(all_vertices, key=lambda point: point[0])
tuple_with_max_y = max(all_vertices, key=lambda point: point[1])
def create_vadere_topography_with_obstacles(obstacles, width, height):
with open("vadere_topography_template.txt", "r") as template_file:
vadere_topography_template = template_file.read()
rounded_max_x = math.ceil(tuple_with_max_x[0])
rounded_max_y = math.ceil(tuple_with_max_y[1])
return (rounded_max_x, rounded_max_y)
vadere_topography_string = Template(vadere_topography_template).substitute(obstacles=obstacles, width=width, height=height)
def shift_buildings(buildings, shift_in_x_direction, shift_in_y_direction):
shifted_buildings = []
return vadere_topography_string
for building in buildings:
shifted_building = \
[(vertex[0] + shift_in_x_direction, vertex[1] + shift_in_y_direction) for vertex in building]
shifted_buildings.append(shifted_building)
def write_parsing_statistics(filename, coordinate_system_as_epsg_code, buildings, shift_buildings_by):
print("Filename: {}".format(filename))
print(" Coordinate system: {}".format(coordinate_system_as_epsg_code))
print(" Found buildings: {}".format(len(buildings)))
print(" Shift buildings by (x, y): {}".format(shift_buildings_by))
print(" Base point: {}".format("Unknown"))
return shifted_buildings
def write_vadere_topography_string_to(vadere_topography_string, output_filename):
if output_filename == None:
print(vadere_topography_string)
else:
with open(output_filename, "w") as text_file:
print(vadere_topography_string, file=text_file)
if __name__ == "__main__":
args = parse_command_line_arguments()
......@@ -175,8 +197,12 @@ if __name__ == "__main__":
minimal_coordinates = find_minimal_coordinates(buildings)
shifted_buildings = shift_buildings(buildings, shift_in_x_direction=-minimal_coordinates[0], shift_in_y_direction=-minimal_coordinates[1])
topography_width, topography_height = find_maximum_coordinates(shifted_buildings)
vadere_obstacles_as_strings = convert_buildings_to_vadere_obstacle_strings(shifted_buildings)
vadere_obstacles_as_strings_concatenated = ",\n".join(vadere_obstacles_as_strings)
topography_width, topography_height = find_maximum_coordinates(shifted_buildings)
vadere_topography_string = create_vadere_topography_with_obstacles(vadere_obstacles_as_strings_concatenated, topography_width, topography_height)
# TODO: Create Vadere topology and print topology stdout/file.
write_parsing_statistics(args.filename, geojson_content["crs"], buildings, minimal_coordinates)
write_vadere_topography_string_to(vadere_topography_string, args.output)
def build_vadere_topography_input_with_obstacles(obstacles, width, height):
with open("vadere_topography_default.txt", "r") as myfile:
vadere_topography_input = myfile.read().replace('\n', '')
vadere_topography_output = Template(vadere_topography_input).substitute(width=width, height=height, obstacles=obstacles)
return vadere_topography_output
def print_output(output_filename, output):
if output_filename == None:
print(output)
else:
with open(output_filename, "w") as text_file:
print(output, file=text_file)
......@@ -9,7 +9,7 @@
"boundingBoxWidth" : 0.5,
"bounded" : true
},
"obstacles" : [ $obstacles],
"obstacles" : [ $obstacles ],
"stairs" : [ ],
"targets" : [ ],
"sources" : [ ],
......@@ -39,4 +39,4 @@
"y" : 0.0
}
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment