9
9
import os
10
10
import apls
11
11
import argparse
12
+ import osmnx_funcs
12
13
import pandas as pd
13
14
# import shapely.wkt
14
15
@@ -18,9 +19,11 @@ def gt_geojson_to_wkt(geojson_path, im_path,
18
19
weight_keys = ['length' , 'travel_time_s' ],
19
20
subgraph_filter_weight = 'length' , min_subgraph_length = 5 ,
20
21
travel_time_key = 'travel_time_s' ,
21
- speed_key = 'speed_m/s ' ,
22
+ speed_key = 'inferred_speed_mps ' ,
22
23
use_pix_coords = False ,
23
24
verbose = False ,
25
+ simplify = False ,
26
+ refine = True ,
24
27
super_verbose = False ):
25
28
'''
26
29
Create wkt list of pixel coords in ground truth graph, for use in SpaceNet
@@ -32,26 +35,51 @@ def gt_geojson_to_wkt(geojson_path, im_path,
32
35
33
36
# linestring = "LINESTRING {}"
34
37
im_name = os .path .basename (im_path )
35
- AOI_root = 'AOI' + im_name .split ('AOI' )[- 1 ]
36
- name_root = AOI_root .split ('.' )[0 ].replace ('PS-RGB_' , '' )
37
- print ("name_root:" , name_root )
38
38
39
+ # get name_root of image file
40
+ name_root = im_name .split ('.' )[0 ].replace ('PS-RGB_' , '' ).replace ('PS-MS_' , '' )
41
+ # # v0
42
+ # AOI_root = 'AOI' + im_name.split('AOI')[-1]
43
+ # name_root = AOI_root.split('.')[0].replace('PS-RGB_', '').replace('PS-MS_', '')
44
+ print ("name_root:" , name_root )
45
+ print ("im_path:" , im_path )
46
+
39
47
G_gt , _ = apls ._create_gt_graph (geojson_path , im_path ,
40
48
subgraph_filter_weight = subgraph_filter_weight ,
41
49
min_subgraph_length = min_subgraph_length ,
42
50
travel_time_key = travel_time_key ,
43
51
speed_key = speed_key ,
44
52
use_pix_coords = use_pix_coords ,
53
+ refine_graph = refine ,
54
+ simplify_graph = simplify ,
45
55
verbose = verbose ,
46
56
super_verbose = super_verbose )
47
57
58
+ # simplify and turn to undirected
59
+ if simplify :
60
+ try :
61
+ G_gt = osmnx_funcs .simplify_graph (G_gt ).to_undirected ()
62
+ except :
63
+ G_gt = G_gt .to_undirected ()
64
+ else :
65
+ G_gt = G_gt .to_undirected ()
66
+
67
+ # return [name_root, "LINESTRING EMPTY"] if no edges
68
+ if (len (G_gt .nodes ()) == 0 ) or (len (G_gt .edges ()) == 0 ):
69
+ print (" Empty graph" )
70
+ if len (weight_keys ) > 0 :
71
+ return [[name_root , "LINESTRING EMPTY" ] + [0 ] * len (weight_keys )]
72
+ else :
73
+ return [[name_root , "LINESTRING EMPTY" ]]
74
+
48
75
# extract geometry pix wkt, save to list
49
76
wkt_list = []
50
77
for i , (u , v , attr_dict ) in enumerate (G_gt .edges (data = True )):
78
+ print ("attr_dict:" , attr_dict )
51
79
geom_pix_wkt = attr_dict ['geometry_pix' ].wkt
52
80
if verbose :
53
81
print (i , "/" , len (G_gt .edges ()), "u, v:" , u , v )
54
- print (" attr_dict:" , attr_dict )
82
+ # print(" attr_dict:", attr_dict)
55
83
print (" geom_pix_wkt:" , geom_pix_wkt )
56
84
57
85
wkt_item_root = [name_root , geom_pix_wkt ]
@@ -74,32 +102,41 @@ def gt_geojson_dir_to_wkt(geojson_dir, im_dir, output_csv_path,
74
102
weight_keys = ['length' , 'travel_time_s' ],
75
103
subgraph_filter_weight = 'length' , min_subgraph_length = 5 ,
76
104
travel_time_key = 'travel_time_s' ,
77
- speed_key = 'speed_m/s ' ,
105
+ speed_key = 'inferred_speed_mps ' ,
78
106
use_pix_coords = False ,
107
+ simplify = False ,
79
108
verbose = False ,
80
109
super_verbose = False ):
81
110
82
111
# make dict of image chip id to file name
112
+ chipper = ''
83
113
im_chip_dict = {}
84
114
for im_name in [z for z in os .listdir (im_dir ) if z .endswith ('.tif' )]:
85
115
chip_id = im_name .split ('chip' )[- 1 ].split ('.' )[0 ]
116
+ if 'chip' in im_name :
117
+ chipper = 'chip'
118
+ chip_id = im_name .split (chipper )[- 1 ].split ('.' )[0 ]
119
+ elif 'img' in im_name :
120
+ chipper = 'img'
121
+ chip_id = im_name .split (chipper )[- 1 ].split ('.' )[0 ]
86
122
im_chip_dict [chip_id ] = im_name
87
123
if verbose :
88
124
print ("im_chip_dict:" , im_chip_dict )
89
125
90
126
# iterate through geojsons
91
127
wkt_list_tot = []
92
- geojson_paths = [z for z in os .listdir (geojson_dir )
93
- if z .endswith ('.geojson' )]
128
+ geojson_paths = sorted ( [z for z in os .listdir (geojson_dir )
129
+ if z .endswith ('.geojson' )])
94
130
for i , geojson_name in enumerate (geojson_paths ):
131
+
95
132
# get image name
96
- chip_id = geojson_name .split ('chip' )[- 1 ].split ('.' )[0 ]
133
+ chip_id = geojson_name .split (chipper )[- 1 ].split ('.' )[0 ]
97
134
try :
98
135
im_name = im_chip_dict [chip_id ]
99
136
except :
100
137
print ("im_name not in im_chip_dict:" , im_name )
101
138
return
102
- continue
139
+ # continue
103
140
104
141
geojson_path = os .path .join (geojson_dir , geojson_name )
105
142
im_path = os .path .join (im_dir , im_name )
@@ -115,6 +152,7 @@ def gt_geojson_dir_to_wkt(geojson_dir, im_dir, output_csv_path,
115
152
travel_time_key = travel_time_key ,
116
153
speed_key = speed_key ,
117
154
use_pix_coords = use_pix_coords ,
155
+ simplify = simplify ,
118
156
verbose = verbose ,
119
157
super_verbose = super_verbose )
120
158
@@ -126,10 +164,15 @@ def gt_geojson_dir_to_wkt(geojson_dir, im_dir, output_csv_path,
126
164
else :
127
165
cols = ['ImageId' , 'WKT_Pix' ]
128
166
129
- print ("cols:" , cols )
130
167
# use 'length_m' instead?
131
168
cols = [z .replace ('length' , 'length_m' ) for z in cols ]
132
169
170
+ # print("wkt_list_tot:", wkt_list_tot)
171
+ # print("\n")
172
+ # for i in wkt_list_tot:
173
+ # print(len(i))
174
+ print ("cols:" , cols )
175
+
133
176
df = pd .DataFrame (wkt_list_tot , columns = cols )
134
177
print ("df:" , df )
135
178
# save
@@ -165,6 +208,16 @@ def gt_geojson_dir_to_wkt(geojson_dir, im_dir, output_csv_path,
165
208
help = 'Root directory of geojson data' )
166
209
parser .add_argument ('--PSRGB_dir' , default = '' , type = str ,
167
210
help = 'PS-RGB dir, if ' ', assume in root_dir' )
211
+ parser .add_argument ('--travel_time_key' , default = 'travel_time_s' , type = str ,
212
+ help = 'key for travel time' )
213
+ parser .add_argument ('--speed_key' , default = 'inferred_speed_mps' , type = str ,
214
+ help = 'key for road speed' )
215
+ parser .add_argument ('--out_file_name' ,
216
+ default = 'geojson_roads_speed_wkt_weighted_v0.csv' ,
217
+ type = str ,
218
+ help = 'name for output file' )
219
+ parser .add_argument ('--simplify_graph' , default = True , type = bool ,
220
+ help = 'switch to simplify graph prior to saving' )
168
221
args = parser .parse_args ()
169
222
170
223
weight_keys = ['length' , 'travel_time_s' ]
@@ -178,14 +231,22 @@ def gt_geojson_dir_to_wkt(geojson_dir, im_dir, output_csv_path,
178
231
geojson_dir = os .path .join (root_dir , 'geojson_roads_speed' )
179
232
# get name
180
233
out_prefix = '_' .join (root_dir .split ('/' )[- 3 :])
181
- output_csv_path = os .path .join (
182
- root_dir , out_prefix + 'geojson_roads_speed_wkt_weighted.csv' )
183
-
234
+ # if args.simplify_graph:
235
+ # out_name = out_prefix + 'geojson_roads_speed_wkt_weighted_simp.csv'
236
+ # else:
237
+ # out_name = out_prefix + 'geojson_roads_speed_wkt_weighted.csv'
238
+ # output_csv_path = os.path.join(root_dir, out_name)
239
+ output_csv_path = os .path .join (root_dir , out_prefix + args .out_file_name )
240
+
241
+ print ("output_csv_path:" , output_csv_path )
184
242
df = gt_geojson_dir_to_wkt (geojson_dir , im_dir ,
185
243
output_csv_path = output_csv_path ,
186
- weight_keys = weight_keys , verbose = verbose )
244
+ travel_time_key = args .travel_time_key ,
245
+ speed_key = args .speed_key ,
246
+ simplify = args .simplify_graph ,
247
+ weight_keys = weight_keys ,
248
+ verbose = verbose )
187
249
188
-
189
250
'''
190
251
Execute
191
252
0 commit comments