|
1 | 1 | import logging as log |
2 | 2 | import os |
3 | | -import subprocess |
4 | 3 | import traceback |
5 | 4 | from concurrent.futures import ThreadPoolExecutor, as_completed |
6 | 5 |
|
|
11 | 10 | from shapely import wkb |
12 | 11 | from tqdm import tqdm |
13 | 12 |
|
| 13 | +from src.classes.bucket_manager import GCSBucketManager |
| 14 | +from src.classes.file_manager import FileManager, FileType, LoadType |
| 15 | +from src.classes.loaders import generate_pmtiles |
14 | 16 | from src.config.config import ( |
15 | 17 | FORCE_RELOAD, |
16 | 18 | USE_CRS, |
17 | 19 | log_level, |
18 | | - min_tiles_file_size_in_bytes, |
19 | | - write_production_tiles_file, |
20 | 20 | ) |
21 | | -from src.classes.bucket_manager import GCSBucketManager |
22 | | -from src.classes.file_manager import FileManager, FileType, LoadType |
23 | 21 | from src.loaders import load_carto_data, load_esri_data |
24 | 22 |
|
25 | 23 | log.basicConfig(level=log_level) |
@@ -281,101 +279,4 @@ def build_and_publish(self, tiles_file_id_prefix: str) -> None: |
281 | 279 | Raises: |
282 | 280 | ValueError: Raised if the generated PMTiles file is smaller than the minimum allowed size, indicating a potential corruption or incomplete file. |
283 | 281 | """ |
284 | | - zoom_threshold: int = 13 |
285 | | - |
286 | | - # Export the GeoDataFrame to a temporary GeoJSON file |
287 | | - temp_geojson_points: str = ( |
288 | | - f"storage/temp/temp_{tiles_file_id_prefix}_points.geojson" |
289 | | - ) |
290 | | - temp_geojson_polygons: str = ( |
291 | | - f"storage/temp/temp_{tiles_file_id_prefix}_polygons.geojson" |
292 | | - ) |
293 | | - temp_pmtiles_points: str = ( |
294 | | - f"storage/temp/temp_{tiles_file_id_prefix}_points.pmtiles" |
295 | | - ) |
296 | | - temp_pmtiles_polygons: str = ( |
297 | | - f"storage/temp/temp_{tiles_file_id_prefix}_polygons.pmtiles" |
298 | | - ) |
299 | | - temp_merged_pmtiles: str = ( |
300 | | - f"storage/temp/temp_{tiles_file_id_prefix}_merged.pmtiles" |
301 | | - ) |
302 | | - |
303 | | - # Reproject |
304 | | - gdf_wm = self.gdf.to_crs(epsg=4326) |
305 | | - gdf_wm.to_file(temp_geojson_polygons, driver="GeoJSON") |
306 | | - |
307 | | - # Create points dataset |
308 | | - self.centroid_gdf = self.gdf.copy() |
309 | | - self.centroid_gdf["geometry"] = self.centroid_gdf["geometry"].centroid |
310 | | - self.centroid_gdf = self.centroid_gdf.to_crs(epsg=4326) |
311 | | - self.centroid_gdf.to_file(temp_geojson_points, driver="GeoJSON") |
312 | | - |
313 | | - # Command for generating PMTiles for points up to zoom level zoom_threshold |
314 | | - points_command: list[str] = [ |
315 | | - "tippecanoe", |
316 | | - f"--output={temp_pmtiles_points}", |
317 | | - f"--maximum-zoom={zoom_threshold}", |
318 | | - "--minimum-zoom=10", |
319 | | - "-zg", |
320 | | - "-aC", |
321 | | - "-r0", |
322 | | - temp_geojson_points, |
323 | | - "-l", |
324 | | - "vacant_properties_tiles_points", |
325 | | - "--force", |
326 | | - ] |
327 | | - |
328 | | - # Command for generating PMTiles for polygons from zoom level zoom_threshold |
329 | | - polygons_command: list[str] = [ |
330 | | - "tippecanoe", |
331 | | - f"--output={temp_pmtiles_polygons}", |
332 | | - f"--minimum-zoom={zoom_threshold}", |
333 | | - "--maximum-zoom=16", |
334 | | - "-zg", |
335 | | - "--no-tile-size-limit", |
336 | | - temp_geojson_polygons, |
337 | | - "-l", |
338 | | - "vacant_properties_tiles_polygons", |
339 | | - "--force", |
340 | | - ] |
341 | | - |
342 | | - # Command for merging the two PMTiles files into a single output file |
343 | | - merge_command: list[str] = [ |
344 | | - "tile-join", |
345 | | - f"--output={temp_merged_pmtiles}", |
346 | | - "--no-tile-size-limit", |
347 | | - temp_pmtiles_polygons, |
348 | | - temp_pmtiles_points, |
349 | | - "--force", |
350 | | - ] |
351 | | - |
352 | | - # Run the commands |
353 | | - for command in [points_command, polygons_command, merge_command]: |
354 | | - subprocess.run(command) |
355 | | - |
356 | | - write_files: list[str] = [f"{tiles_file_id_prefix}_staging.pmtiles"] |
357 | | - |
358 | | - if write_production_tiles_file: |
359 | | - write_files.append(f"{tiles_file_id_prefix}.pmtiles") |
360 | | - |
361 | | - # Check whether the temp saved tiles files is big enough. |
362 | | - file_size: int = os.stat(temp_merged_pmtiles).st_size |
363 | | - if file_size < min_tiles_file_size_in_bytes: |
364 | | - raise ValueError( |
365 | | - f"{temp_merged_pmtiles} is {file_size} bytes in size but should be at least {min_tiles_file_size_in_bytes}. Therefore, we are not uploading any files to the GCP bucket. The file may be corrupt or incomplete." |
366 | | - ) |
367 | | - |
368 | | - bucket = google_cloud_bucket(require_write_access=True) |
369 | | - if bucket is None: |
370 | | - print("Skipping PMTiles upload due to read-only bucket access.") |
371 | | - return |
372 | | - |
373 | | - # Upload PMTiles to Google Cloud Storage |
374 | | - bucket = google_cloud_bucket() |
375 | | - for file in write_files: |
376 | | - blob = bucket.blob(file) |
377 | | - try: |
378 | | - blob.upload_from_filename(temp_merged_pmtiles) |
379 | | - print(f"PMTiles upload successful for {file}!") |
380 | | - except Exception as e: |
381 | | - print(f"PMTiles upload failed for {file}: {e}") |
| 282 | + generate_pmtiles(self.gdf, tiles_file_id_prefix) |
0 commit comments