forked from osrf/gazebo_osm
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gz_osm.py
executable file
·346 lines (266 loc) · 10.7 KB
/
gz_osm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
import sys
sys.path.insert(0, 'source')
import os
import numpy as np
from lxml import etree
import argparse
from dict2sdf import GetSDF
from osm2dict import Osm2Dict
from getMapImage import getMapImage
from getOsmFile import getOsmFile
from roadSmoothing import SmoothRoad
from laneBoundaries import LaneBoundaries
from catmull_rom_spline import catmull_rom
from createStageFiles import StageWorld
import matplotlib.pyplot as plt
TIMER = 1
def tic():
#Homemade version of matlab tic and toc functions
import time
global startTime_for_tictoc
startTime_for_tictoc = time.time()
def toc():
import time
if 'startTime_for_tictoc' in globals():
print ("| Elapsed time: " + str(time.time()
- startTime_for_tictoc)
+ " sec")
else:
print "Toc: start time not set"
if TIMER:
tic()
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--outFile',
help='Output file name', type=str, default='outFile.sdf')
parser.add_argument('-o', '--osmFile', help='Name of the osm file generated',
type=str,
default='map.osm')
parser.add_argument('-O', '--inputOsmFile', help='Name of the Input osm file',
type=str,
default='')
parser.add_argument('--stage',
help='Generate Stage World',
action='store_true')
parser.add_argument('--name', help='Name of stage output name',
type=str,
default='osm-map')
parser.add_argument('-i', '--imageFile',
help='Generate and name .png image of the selected areas',
type=str,
default='')
parser.add_argument('-d', '--directory',
help='Output directory',
type=str,
default='./')
parser.add_argument('-l', '--lanes',
help='export image with lanes',
action='store_true')
parser.add_argument('-B', '--boundingbox',
help=('Give the bounding box for the area\n' +
'Format: MinLon MinLat MaxLon MaxLat'),
nargs='*',
type=float,
default=[-122.0129, 37.3596, -122.0102, 37.3614])
parser.add_argument('-r', '--roads',
help='Display Roads',
action='store_true')
parser.add_argument('-dbg', '--debug',
help='Debug Mode. Gazebo may take a while to load with this.',
action='store_true')
parser.add_argument('-m', '--models',
help='Display models',
action='store_true')
parser.add_argument('-b', '--buildings',
help='Display buildings',
action='store_true')
parser.add_argument('-a', '--displayAll',
help='Display roads and models',
action='store_true')
parser.add_argument('--interactive',
help='Starts the interactive version of the program',
action='store_true')
args = parser.parse_args()
flags = []
if args.buildings:
flags.append('b')
if args.models:
flags.append('m')
if args.roads:
flags.append('r')
if args.lanes:
flags.append('l')
if not(args.roads or args.models or args.buildings) or args.displayAll:
flags.append('a')
if not os.path.exists(args.directory):
os.makedirs(args.directory)
args.osmFile = args.directory + args.osmFile
args.outFile = args.directory + args.outFile
osmDictionary = {}
if args.interactive:
print("\nPlease enter the latitudnal and logitudnal" +
" coordinates of the area or select from" +
" default by hitting return twice \n")
startCoords = raw_input("Enter starting coordinates: " +
"[lon lat] :").split(' ')
endCoords = raw_input("Enter ending coordnates: [lon lat]: ").split(' ')
if (startCoords and endCoords and
len(startCoords) == 2 and len(endCoords) == 2):
for incoords in range(2):
startCoords[incoords] = float(startCoords[incoords])
endCoords[incoords] = float(endCoords[incoords])
else:
choice = raw_input("Default Coordinate options: West El " +
"Camino Real Highway, CA (2), Bethlehem," +
" PA (default=1): ")
# if choice != '2':
# startCoords = [37.3566, -122.0091]
# endCoords = [37.3574, -122.0081]
# else:
startCoords = [37.3596, -122.0129]
endCoords = [37.3614, -122.0102]
option = raw_input("Do you want to view the area specified? [Y/N]" +
" (default: Y): ").upper()
osmFile = 'map.osm'
args.boundingbox = [min(startCoords[1], endCoords[1]),
min(startCoords[0], endCoords[0]),
max(startCoords[1], endCoords[1]),
max(startCoords[0], endCoords[0])]
if option != 'N':
args.imageFile = 'map.png'
if args.inputOsmFile:
f = open(args.inputOsmFile, 'r')
root = etree.fromstring(f.read())
f.close()
args.boundingbox = [float(root[0].get('minlon')),
float(root[0].get('minlat')),
float(root[0].get('maxlon')),
float(root[0].get('maxlat'))]
print (' _______________________________')
print ('|')
print ('| Downloading the osm data ... ')
osmDictionary = getOsmFile(args.boundingbox,
args.osmFile, args.inputOsmFile)
# if args.imageFile:
# if TIMER:
# tic()
# print "Building the image file ..."
# args.imageFile = args.directory + args.imageFile
# getMapImage(args.osmFile, args.imageFile)
# if TIMER:
# toc()
#Initialize the class
osmRoads = Osm2Dict(args.boundingbox[0], args.boundingbox[1],
args.boundingbox[2], args.boundingbox[3],
osmDictionary, flags)
print ('| Extracting the map data for gazebo ...')
#get Road and model details
#roadPointWidthMap, modelPoseMap, buildingLocationMap = osmRoads.getMapDetails()
roadPointWidthMap = osmRoads.getRoadDetails()
print ('| Building sdf file ...')
#Initialize the getSdf class
sdfFile = GetSDF()
#Set up the spherical coordinates
sdfFile.addSphericalCoords(osmRoads.getLat(), osmRoads.getLon())
#add Required models
sdfFile.includeModel("sun")
# for model in modelPoseMap.keys():
# points = modelPoseMap[model]['points']
# sdfFile.addModel(modelPoseMap[model]['mainModel'],
# model,
# [points[0, 0], points[1, 0], points[2, 0]])
# for building in buildingLocationMap.keys():
# sdfFile.addBuilding(buildingLocationMap[building]['mean'],
# buildingLocationMap[building]['points'],
# building,
# buildingLocationMap[building]['color'])
print ('|')
print ('|-----------------------------------')
print ('| Number of Roads: ' + str(len(roadPointWidthMap.keys())))
print ('|-----------------------------------')
#print ('|')
#fig = plt.figure()
lanes = 0
roadLaneSegments = []
centerLaneSegments = []
laneSegmentWidths = []
#Include the roads in the map in sdf file
for idx, road in enumerate(roadPointWidthMap.keys()):
sdfFile.addRoad(road, roadPointWidthMap[road]['texture'])
sdfFile.setRoadWidth(roadPointWidthMap[road]['width'], road)
points = roadPointWidthMap[road]['points']
print ('| Road' + str(idx+1) + ': ' + road)
laneSegmentWidths.append(roadPointWidthMap[road]['width'])
print "| -- Width: ", str(roadPointWidthMap[road]['width'])
xData = points[0, :]
yData = points[1, :]
if len(xData) < 3:
#print ('Cannot apply spline with [' + str(len(xData)) + '] points. At least 3 needed.')
x = []
y = []
lanePoint = []
for j in np.arange(len(xData)):
sdfFile.addRoadPoint([xData[j], yData[j], 0], road)
lanePoint.append([xData[j],yData[j]])
x.append(xData[j])
y.append(yData[j])
# if len(xData) == 1:
# sdfFile.addRoadPoint([xData[0], yData[0], 0], road)
# #sdfFile.addRoadDebug([xData[0], yData[0], 0], road)
# if len(xData) == 2:
# sdfFile.addRoadPoint([xData[1], yData[1], 0], road)
# #sdfFile.addRoadDebug([xData[1], yData[1], 0], road)
roadLaneSegments.append([lanePoint, lanePoint])
centerLaneSegments.append([x,y])
else:
x, y = catmull_rom(xData, yData, 10)
centerLaneSegments.append([x, y])
lanes = LaneBoundaries(x,y)
# [lanePointsA, lanePointsB] = lanes.createLanes(6)
# roadLaneSegments.append([lanePointsA, lanePointsB])
# xPointsA = []
# yPointsA = []
# xPointsB = []
# yPointsB = []
# for i in range(len(lanePointsA)/2):
# xPointsA.append(lanePointsA[i*2][0])
# yPointsA.append(lanePointsA[i*2][1])
# #sdfFile.addLeftLaneDebug([lanePointsA[i*2][0], lanePointsA[i*2][1], 0], road)
# xPointsB.append(lanePointsB[i*2][0])
# yPointsB.append(lanePointsB[i*2][1])
# #sdfFile.addRightLaneDebug([lanePointsB[i*2][0], lanePointsB[i*2][1], 0], road)
#### Debug
#plt.plot(xData, yData, 'bo', x, y, 'r-', xPointsA, yPointsA, 'g-', xPointsB, yPointsB, 'g-')
#plt.plot(xPointsA, yPointsA, 'g-', xPointsB, yPointsB, 'g-')
#plt.plot(xData, yData, 'ro-', x, y, 'b+')
#plt.legend(['data', 'catmull'], loc='best')
##plt.plot(x, y, 'b+')
#plt.show()
#### Debug
# lanes.saveImage(size, lanePointsA, lanePointsB)
# if idx == len(roadPointWidthMap.keys())-1:
# lanes.showImage()
for point in range(len(x)):
sdfFile.addRoadPoint([x[point], y[point], 0], road)
#sdfFile.addRoadDebug([x[point], y[point], 0], road)
print ('|')
print ('|-----------------------------------')
print ('| Generating the SDF world file...')
sdfFile.writeToFile(args.outFile)
# if args.imageFile:
print ('| Generating Image File...')
print ('|-----------------------------------')
print ('|')
size = osmRoads.getMapSize()
# args.imageFile = args.directory + args.imageFile
lanes.makeImage(size, 5, roadLaneSegments, centerLaneSegments, laneSegmentWidths, args.name + ".png")
print ('| Lat Center = '+ str(osmRoads.getLat()))
print ('| Lon Center = '+ str(osmRoads.getLon()))
if args.stage:
stage = StageWorld([443, 700], [1300, 4800], [-45.12, 14.4334], [4.2,444.355])
stage.createStageSetup(args.name)
#plt.show()
if TIMER:
toc()
print ('|______________________________')
print ('')