@@ -171,13 +171,14 @@ def bbox(bounds):
171
171
labels_4326_gdf = labels_gdf .to_crs (epsg = 4326 ).drop_duplicates (subset = ['geometry' , 'year' ])
172
172
else :
173
173
labels_4326_gdf = labels_gdf .to_crs (epsg = 4326 ).drop_duplicates (subset = ['geometry' ])
174
+ nb_labels = len (labels_gdf )
175
+ logger .info (f'There are { nb_labels } polygons in { SHPFILE } ' )
176
+
174
177
labels_4326_gdf ['CATEGORY' ] = 'quarry'
175
178
labels_4326_gdf ['SUPERCATEGORY' ] = 'land usage'
179
+
176
180
gt_labels_4326_gdf = labels_4326_gdf .copy ()
177
181
178
- nb_labels = len (labels_gdf )
179
- logger .info (f'There are { nb_labels } polygons in { SHPFILE } ' )
180
-
181
182
label_filename = 'labels.geojson'
182
183
label_filepath = os .path .join (OUTPUT_DIR , label_filename )
183
184
labels_4326_gdf .to_file (label_filepath , driver = 'GeoJSON' )
@@ -206,14 +207,14 @@ def bbox(bounds):
206
207
logger .success (f"{ DONE_MSG } A file was written: { filepath } " )
207
208
labels_4326_gdf = pd .concat ([labels_4326_gdf , fp_labels_4326_gdf ], ignore_index = True )
208
209
209
- # Get the label boundaries (minx, miny, maxx, maxy)
210
+ # Tiling of the AoI
210
211
logger .info ("- Get the label boundaries" )
211
212
boundaries_df = labels_4326_gdf .bounds
213
+ logger .info ("- Tiling of the AoI" )
214
+ tiles_4326_aoi_gdf = aoi_tiling (boundaries_df )
215
+ tiles_4326_labels_gdf = gpd .sjoin (tiles_4326_aoi_gdf , labels_4326_gdf , how = 'inner' , predicate = 'intersects' )
212
216
213
- # Get the global boundaries for all the labels (minx, miny, maxx, maxy)
214
- labels_bbox = bbox (labels_4326_gdf .iloc [0 ].geometry .bounds )
215
-
216
- # Get tiles for a given AoI from which empty tiles will be selected
217
+ # Tiling of the AoI from which empty tiles will be selected
217
218
if EPT_SHPFILE :
218
219
EPT_aoi_gdf = gpd .read_file (EPT_SHPFILE )
219
220
EPT_aoi_4326_gdf = EPT_aoi_gdf .to_crs (epsg = 4326 )
@@ -223,80 +224,55 @@ def bbox(bounds):
223
224
logger .info ("- Get AoI boundaries" )
224
225
EPT_aoi_boundaries_df = EPT_aoi_4326_gdf .bounds
225
226
226
- # Get the boundaries for all the AoI (minx, miny, maxx, maxy)
227
- aoi_bbox = bbox (EPT_aoi_4326_gdf .iloc [0 ].geometry .bounds )
228
- aoi_bbox_contains = aoi_bbox .contains (labels_bbox )
229
-
230
- if aoi_bbox_contains :
231
- logger .info ("- The surface area occupied by the bbox of the AoI used to find empty tiles is bigger than the label's one. The AoI boundaries will be used for tiling" )
232
- boundaries_df = EPT_aoi_boundaries_df .copy ()
233
- else :
234
- logger .info ("- The surface area occupied by the bbox of the AoI used to find empty tiles is smaller than the label's one. Both the AoI and labels area will be used for tiling" )
235
- # Get tiles coordinates and shapes
236
- empty_tiles_4326_all_gdf = aoi_tiling (EPT_aoi_boundaries_df )
237
- # Delete tiles outside of the AoI limits
238
- empty_tiles_4326_aoi_gdf = gpd .sjoin (empty_tiles_4326_all_gdf , EPT_aoi_4326_gdf , how = 'inner' , lsuffix = 'ept_tiles' , rsuffix = 'ept_aoi' )
239
- # Attribute a year to empty tiles if necessary
240
- if 'year' in labels_4326_gdf .keys ():
241
- if isinstance (EPT_YEAR , int ):
242
- empty_tiles_4326_aoi_gdf ['year' ] = int (EPT_YEAR )
243
- else :
244
- empty_tiles_4326_aoi_gdf ['year' ] = np .random .randint (low = EPT_YEAR [0 ], high = EPT_YEAR [1 ], size = (len (empty_tiles_4326_aoi_gdf )))
245
- elif EPT_SHPFILE and EPT_YEAR :
246
- logger .warning ("No year column in the label shapefile. The provided empty tile year will be ignored." )
227
+ # Get tile coordinates and shapes
228
+ logger .info ("- Tiling of the empty tiles AoI" )
229
+ empty_tiles_4326_all_gdf = aoi_tiling (EPT_aoi_boundaries_df )
230
+ # Delete tiles outside of the AoI limits
231
+ empty_tiles_4326_aoi_gdf = gpd .sjoin (empty_tiles_4326_all_gdf , EPT_aoi_4326_gdf , how = 'inner' , lsuffix = 'ept_tiles' , rsuffix = 'ept_aoi' )
232
+ # Attribute a year to empty tiles if necessary
233
+ if 'year' in labels_4326_gdf .keys ():
234
+ if isinstance (EPT_YEAR , int ):
235
+ empty_tiles_4326_aoi_gdf ['year' ] = int (EPT_YEAR )
236
+ else :
237
+ empty_tiles_4326_aoi_gdf ['year' ] = np .random .randint (low = EPT_YEAR [0 ], high = EPT_YEAR [1 ], size = (len (empty_tiles_4326_aoi_gdf )))
238
+ elif EPT_SHPFILE and EPT_YEAR :
239
+ logger .warning ("No year column in the label shapefile. The provided empty tile year will be ignored." )
247
240
elif EPT == 'shp' :
248
241
if EPT_YEAR :
249
242
logger .warning ("A shapefile of selected empty tiles are provided. The year set for the empty tiles in the configuration file will be ignored" )
250
243
EPT_YEAR = None
251
244
empty_tiles_4326_aoi_gdf = EPT_aoi_4326_gdf .copy ()
252
- aoi_bbox = None
253
- aoi_bbox_contains = False
254
-
255
- logger .info ("Creating tiles for the Area of Interest (AoI)..." )
256
-
257
- # Get tiles coordinates and shapes
258
- tiles_4326_aoi_gdf = aoi_tiling (boundaries_df )
259
-
260
- # Compute labels intersecting tiles
261
- tiles_4326_gt_gdf = gpd .sjoin (tiles_4326_aoi_gdf , gt_labels_4326_gdf , how = 'inner' , predicate = 'intersects' )
262
- tiles_4326_gt_gdf .drop_duplicates ('title' , inplace = True )
263
- logger .info (f"- Number of tiles intersecting GT labels = { len (tiles_4326_gt_gdf )} " )
264
-
265
- if FP_SHPFILE :
266
- tiles_4326_fp_gdf = gpd .sjoin (tiles_4326_aoi_gdf , fp_labels_4326_gdf , how = 'inner' , predicate = 'intersects' )
267
- tiles_4326_fp_gdf .drop_duplicates ('title' , inplace = True )
268
- logger .info (f"- Number of tiles intersecting FP labels = { len (tiles_4326_fp_gdf )} " )
269
-
270
- if not EPT_SHPFILE or EPT_SHPFILE and aoi_bbox_contains == False :
271
- # Keep only tiles intersecting labels
272
- if FP_SHPFILE :
273
- tiles_4326_aoi_gdf = pd .concat ([tiles_4326_gt_gdf , tiles_4326_fp_gdf ])
274
- else :
275
- tiles_4326_aoi_gdf = tiles_4326_gt_gdf .copy ()
276
245
277
246
# Get all the tiles in one gdf
278
- if EPT_SHPFILE and aoi_bbox_contains == False :
247
+ if EPT_SHPFILE :
279
248
logger .info ("- Concatenate label tiles and empty AoI tiles" )
280
- tiles_4326_all_gdf = pd .concat ([tiles_4326_aoi_gdf , empty_tiles_4326_aoi_gdf ])
249
+ tiles_4326_all_gdf = pd .concat ([tiles_4326_labels_gdf , empty_tiles_4326_aoi_gdf ])
281
250
else :
282
- tiles_4326_all_gdf = tiles_4326_aoi_gdf .copy ()
283
-
284
- # - Remove duplicated tiles
285
- if nb_labels > 1 :
286
- if 'year' in tiles_4326_all_gdf .keys ():
287
- tiles_4326_all_gdf ['year' ] = tiles_4326_all_gdf .year .astype (int )
288
- tiles_4326_all_gdf .drop_duplicates (['title' , 'year' ], inplace = True )
289
- else :
290
- tiles_4326_all_gdf .drop_duplicates (['title' ], inplace = True )
251
+ tiles_4326_all_gdf = tiles_4326_labels_gdf .copy ()
291
252
292
253
# - Remove useless columns, reset feature id and redefine it according to xyz format
293
- logger .info ('- Add tile IDs and reorganise data set' )
254
+ logger .info ('- Add tile IDs and reorganise the data set' )
294
255
tiles_4326_all_gdf = tiles_4326_all_gdf [['geometry' , 'title' , 'year' ] if 'year' in tiles_4326_all_gdf .keys () else ['geometry' , 'title' ]].copy ()
295
256
tiles_4326_all_gdf .reset_index (drop = True , inplace = True )
296
257
tiles_4326_all_gdf = tiles_4326_all_gdf .apply (add_tile_id , axis = 1 )
258
+
259
+ # - Remove duplicated tiles
260
+ if nb_labels > 1 :
261
+ tiles_4326_all_gdf .drop_duplicates (['id' ], inplace = True )
262
+
297
263
nb_tiles = len (tiles_4326_all_gdf )
298
264
logger .info (f"There were { nb_tiles } tiles created" )
299
265
266
+ # Get the number of tiles intersecting labels
267
+ tiles_4326_gt_gdf = gpd .sjoin (tiles_4326_all_gdf , gt_labels_4326_gdf , how = 'inner' , predicate = 'intersects' )
268
+ tiles_4326_gt_gdf .drop_duplicates (['id' ], inplace = True )
269
+ logger .info (f"- Number of tiles intersecting GT labels = { len (tiles_4326_gt_gdf )} " )
270
+
271
+ if FP_SHPFILE :
272
+ tiles_4326_fp_gdf = gpd .sjoin (tiles_4326_all_gdf , fp_labels_4326_gdf , how = 'inner' , predicate = 'intersects' )
273
+ tiles_4326_fp_gdf .drop_duplicates (['id' ], inplace = True )
274
+ logger .info (f"- Number of tiles intersecting FP labels = { len (tiles_4326_fp_gdf )} " )
275
+
300
276
# Save tile shapefile
301
277
logger .info ("Export tiles to GeoJSON (EPSG:4326)..." )
302
278
tile_filename = 'tiles.geojson'
0 commit comments