|
15 | 15 | from shapely.geometry import Polygon |
16 | 16 |
|
17 | 17 | sys.path.insert(0, '../..') |
18 | | -import helpers.misc as misc |
| 18 | +from helpers.functions_for_examples import format_all_tiles, prepare_labels |
| 19 | +from helpers.misc import format_logger |
19 | 20 | from helpers.constants import DONE_MSG |
20 | 21 |
|
21 | 22 | from loguru import logger |
@@ -176,70 +177,7 @@ def prepare_labels(shpfile, written_files, prefix=''): |
176 | 177 |
|
177 | 178 | written_files = [] |
178 | 179 |
|
179 | | - # Prepare the tiles |
180 | | - |
181 | | - ## Convert datasets shapefiles into geojson format |
182 | | - logger.info('Convert the label shapefiles into GeoJSON format (EPSG:4326)...') |
183 | | - labels_4326_gdf, written_files = prepare_labels(SHPFILE, written_files) |
184 | | - gt_labels_4326_gdf = labels_4326_gdf[['geometry', 'CATEGORY', 'SUPERCATEGORY']].copy() |
185 | | - |
186 | | - # Add FP labels if it exists |
187 | | - if FP_SHPFILE: |
188 | | - logger.info('Convert the FP label shapefiles into GeoJSON format (EPSG:4326)...') |
189 | | - fp_labels_4326_gdf, written_files = prepare_labels(FP_SHPFILE, written_files, prefix='FP_') |
190 | | - labels_4326_gdf = pd.concat([labels_4326_gdf, fp_labels_4326_gdf], ignore_index=True) |
191 | | - |
192 | | - # Tiling of the AoI |
193 | | - logger.info("- Get the label boundaries") |
194 | | - boundaries_df = labels_4326_gdf.bounds |
195 | | - logger.info("- Tiling of the AoI") |
196 | | - tiles_4326_aoi_gdf = aoi_tiling(boundaries_df) |
197 | | - tiles_4326_labels_gdf = gpd.sjoin(tiles_4326_aoi_gdf, labels_4326_gdf, how='inner', predicate='intersects') |
198 | | - |
199 | | - # Tiling of the AoI from which empty tiles will be selected |
200 | | - if EPT_SHPFILE: |
201 | | - EPT_aoi_gdf = gpd.read_file(EPT_SHPFILE) |
202 | | - EPT_aoi_4326_gdf = EPT_aoi_gdf.to_crs(epsg=4326) |
203 | | - assert_year(labels_4326_gdf, EPT_aoi_4326_gdf, 'empty_tiles', EPT_YEAR) |
204 | | - |
205 | | - if EPT_TYPE == 'aoi': |
206 | | - logger.info("- Get AoI boundaries") |
207 | | - EPT_aoi_boundaries_df = EPT_aoi_4326_gdf.bounds |
208 | | - |
209 | | - # Get tile coordinates and shapes |
210 | | - logger.info("- Tiling of the empty tiles AoI") |
211 | | - empty_tiles_4326_all_gdf = aoi_tiling(EPT_aoi_boundaries_df) |
212 | | - # Delete tiles outside of the AoI limits |
213 | | - empty_tiles_4326_aoi_gdf = gpd.sjoin(empty_tiles_4326_all_gdf, EPT_aoi_4326_gdf, how='inner', lsuffix='ept_tiles', rsuffix='ept_aoi') |
214 | | - # Attribute a year to empty tiles if necessary |
215 | | - if 'year' in labels_4326_gdf.keys(): |
216 | | - if isinstance(EPT_YEAR, int): |
217 | | - empty_tiles_4326_aoi_gdf['year'] = int(EPT_YEAR) |
218 | | - else: |
219 | | - empty_tiles_4326_aoi_gdf['year'] = np.random.randint(low=EPT_YEAR[0], high=EPT_YEAR[1], size=(len(empty_tiles_4326_aoi_gdf))) |
220 | | - elif EPT_TYPE == 'shp': |
221 | | - if EPT_YEAR: |
222 | | - logger.warning("A shapefile of selected empty tiles are provided. The year set for the empty tiles in the configuration file will be ignored") |
223 | | - EPT_YEAR = None |
224 | | - empty_tiles_4326_aoi_gdf = EPT_aoi_4326_gdf.copy() |
225 | | - |
226 | | - # Get all the tiles in one gdf |
227 | | - logger.info("- Concatenate label tiles and empty AoI tiles") |
228 | | - tiles_4326_all_gdf = pd.concat([tiles_4326_labels_gdf, empty_tiles_4326_aoi_gdf]) |
229 | | - else: |
230 | | - tiles_4326_all_gdf = tiles_4326_labels_gdf.copy() |
231 | | - |
232 | | - # - Remove useless columns, reset feature id and redefine it according to xyz format |
233 | | - logger.info('- Add tile IDs and reorganise the data set') |
234 | | - tiles_4326_all_gdf = tiles_4326_all_gdf[['geometry', 'title', 'year'] if 'year' in tiles_4326_all_gdf.keys() else ['geometry', 'title']].copy() |
235 | | - tiles_4326_all_gdf.reset_index(drop=True, inplace=True) |
236 | | - tiles_4326_all_gdf = tiles_4326_all_gdf.apply(add_tile_id, axis=1) |
237 | | - |
238 | | - # - Remove duplicated tiles |
239 | | - tiles_4326_all_gdf.drop_duplicates(['id'], inplace=True) |
240 | | - |
241 | | - nb_tiles = len(tiles_4326_all_gdf) |
242 | | - logger.info(f"There were {nb_tiles} tiles created") |
| 180 | + gt_labels_4326_gdf = prepare_labels(SHPFILE, CATEGORY, supercategory=SUPERCATEGORY) |
243 | 181 |
|
244 | 182 | # Get the number of tiles intersecting labels |
245 | 183 | tiles_4326_gt_gdf = gpd.sjoin(tiles_4326_all_gdf, gt_labels_4326_gdf, how='inner', predicate='intersects') |
|
0 commit comments