image_slice.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import os
  2. import shutil
  3. from app.defines import TILE_GRID_TYPE
  4. from app.utils.create_geojsonl import create_default_geojsonl
  5. from config import Config
  6. from starearth import FileSystem, Grid
  7. from starearth.storage.osm_zxy import StorageOSMZXY
  8. from slice import slice
  9. from starearth.utils.general_utils import print_log
  10. config = Config()
  11. def slice_zxy(
  12. input_file,
  13. output_path,
  14. tile_size,
  15. tile_grid,
  16. tile_format,
  17. min_zoom,
  18. max_zoom,
  19. epsg='EPSG:4326',
  20. nodata=0,
  21. enable_msmt=0,
  22. render_type="RGB",
  23. channels=[1, 2, 3],
  24. merging=2,
  25. ):
  26. print_log('开始切片...')
  27. # epsg = verify_geotiff(input_file, None)
  28. filesystem_cache = FileSystem(config.common.CACHE_PATH)
  29. tmp_tiles = filesystem_cache.path('tiles')
  30. sliceTiler_type_dict = {TILE_GRID_TYPE.WebMercatorQuad: Grid.WebMercatorQuad,
  31. TILE_GRID_TYPE.WGS1984Quad: Grid.WGS1984Quad,
  32. TILE_GRID_TYPE.GoogleEarthQuad: Grid.GoogleEarthQuad}
  33. sliceTiler_type = sliceTiler_type_dict[tile_grid]
  34. geojsonl = os.path.splitext(input_file)[0] + '.geojsonl'
  35. if not os.path.exists(str(geojsonl)):
  36. create_default_geojsonl(input_file, geojsonl, {"date": os.path.splitext(os.path.basename(input_file))[0]})
  37. slice \
  38. (
  39. input_file=input_file,
  40. tile_grid=sliceTiler_type,
  41. tile_size=tile_size,
  42. tmp_tiles=tmp_tiles,
  43. epsg=epsg,
  44. nodata=nodata,
  45. tile_format=tile_format,
  46. min_zoom=min_zoom,
  47. max_zoom=max_zoom,
  48. enable_msmt=enable_msmt,
  49. channels=channels,
  50. render_type=render_type,
  51. lut=None,
  52. )
  53. print_log('执行切片完成.')
  54. base_name = os.path.splitext(os.path.basename(input_file))[0]
  55. print_log("开始OSM_ZXY存储..")
  56. storage_osmzxy_obj = StorageOSMZXY(
  57. tiles_path=os.path.join(str(tmp_tiles), base_name),
  58. min_zoom=min_zoom,
  59. max_zoom=max_zoom,
  60. merging=merging,
  61. output_path=output_path,
  62. tile_format=tile_format
  63. )
  64. storage_osmzxy_obj.storage()
  65. # 删除临时目录
  66. if os.path.exists(os.path.join(str(tmp_tiles), base_name)):
  67. shutil.rmtree(os.path.join(str(tmp_tiles), base_name))
  68. print_log("OSM_ZXY存储完毕..")