Skip to content

Commit

Permalink
feat: moved help message for SVG to JSON converter and improve code f…
Browse files Browse the repository at this point in the history
…ormatting
  • Loading branch information
ll7 committed Dec 10, 2024
1 parent 67c6fe4 commit 4da95da
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions svg_conv/svg_conv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
HELP_MSG = """This tool converts SVG maps from OpenStreetMap to JSON maps that can be imported
into the RobotSF simulator.
USAGE
python3 svg_conv.py <osm_input_file.svg> <output_file.json>
The converter extracts all polygons of given colors, which is currently
only the borders of houses (brown). Feel free to modify this script to support
your own use case."""

import os
import sys
import json
Expand All @@ -16,10 +26,11 @@ def paths_of_svg(svg: SVG) -> List[Path]:

def filter_paths_by_color(paths: List[Path], color: ColorRGB) -> List[Path]:
red, green, blue = color
paths = [e for e in paths
if e.fill.red == red
and e.fill.green == green
and e.fill.blue == blue]
paths = [
e
for e in paths
if e.fill.red == red and e.fill.green == green and e.fill.blue == blue
]
return paths


Expand All @@ -37,11 +48,7 @@ def serialize_mapjson(poly_points: List[List[Vec2D]]) -> str:
x_margin = [min([x for x, _ in all_points]), max([x for x, _ in all_points])]
y_margin = [min([y for _, y in all_points]), max([y for _, y in all_points])]

map_obj = {
'obstacles': obstacles,
'x_margin': x_margin,
'y_margin': y_margin
}
map_obj = {"obstacles": obstacles, "x_margin": x_margin, "y_margin": y_margin}

return json.dumps(map_obj)

Expand All @@ -58,30 +65,27 @@ def convert_map(input_svg_file: str, output_json_file: str):
poly_points = points_of_paths(paths)
poly_points = [[scale(p, 1.62) for p in poly] for poly in poly_points]
map_json = serialize_mapjson(poly_points)
with open(output_json_file, 'w') as file:
with open(output_json_file, "w") as file:
file.write(map_json)


HELP_MSG = """This tool converts SVG maps from OpenStreetMap to JSON maps that can be imported
into the RobotSF simulator.
USAGE
python3 svg_conv.py <osm_input_file.svg> <output_file.json>
The converter extracts all polygons of given colors, which is currently
only the borders of houses (brown). Feel free to modify this script to support
your own use case."""


def main():
def file_exists(path): return os.path.exists(path) and os.path.isfile(path)
def has_fileext(path, ext): return "." + path.split(".")[-1] == ext
if len(sys.argv) == 3 and file_exists(sys.argv[1]) \
and has_fileext(sys.argv[1], ".svg") and has_fileext(sys.argv[2], ".json"):
def file_exists(path):
return os.path.exists(path) and os.path.isfile(path)

def has_fileext(path, ext):
return "." + path.split(".")[-1] == ext

if (
len(sys.argv) == 3
and file_exists(sys.argv[1])
and has_fileext(sys.argv[1], ".svg")
and has_fileext(sys.argv[2], ".json")
):
convert_map(input_svg_file=sys.argv[1], output_json_file=sys.argv[2])
else:
print(HELP_MSG)


if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit 4da95da

Please sign in to comment.