Nav2 Navigation Stack - kilted  kilted
ROS 2 Navigation Stack
export_shapefiles.py
1 #! /usr/bin/env python3
2 # Copyright 2021 Josh Wallace
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from datetime import datetime
17 import sys
18 
19 import geopandas as gpd
20 import pandas as pd
21 
22 try:
23  file_prefix = sys.argv[1]
24  edges = gpd.read_file(sys.argv[2])
25  nodes = gpd.read_file(sys.argv[3])
26 except Exception:
27  raise Exception('Incorrect arguments provide')
28 
29 # Rename columns to match the expected output
30 nodes = nodes.rename(columns={'start_node': 'startid'})
31 edges = edges.rename(columns={'start_node': 'startid'})
32 nodes = nodes.rename(columns={'end_node': 'endid'})
33 edges = edges.rename(columns={'end_node': 'endid'})
34 
35 # Remove edgeid and use standard id
36 edges = edges.rename(columns={'edge_id': 'id'})
37 
38 # Make sure IDs are integers
39 if 'id' in nodes.columns:
40  nodes['id'] = nodes['id'].astype(int)
41 if 'id' in edges.columns:
42  edges['id'] = edges['id'].astype(int)
43 
44 now = datetime.now()
45 
46 graph = pd.concat([nodes, edges])
47 
48 # Set start/endids to integers, for nodes, set to -1 (not used)
49 if 'startid' in graph.columns:
50  graph['startid'] = graph['startid'].fillna(-1).astype(int)
51 if 'endid' in graph.columns:
52  graph['endid'] = graph['endid'].fillna(-1).astype(int)
53 if 'startid' in graph.columns:
54  graph['startid'] = graph['startid'].astype(int)
55 if 'endid' in graph.columns:
56  graph['endid'] = graph['endid'].astype(int)
57 
58 file_name = file_prefix + '_' + now.strftime('%m_%d_%Y_%H_%M_%S') + '.geojson'
59 
60 graph.to_file(file_name, driver='GeoJSON')