geojson2mask.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python
  2. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. import codecs
  17. import argparse
  18. import paddlers
  19. import numpy as np
  20. import cv2
  21. import geojson
  22. from tqdm import tqdm
  23. from utils import Raster, save_geotiff, translate_vector, time_it
  24. def _gt_convert(x_geo, y_geo, geotf):
  25. a = np.array([[geotf[1], geotf[2]], [geotf[4], geotf[5]]])
  26. b = np.array([x_geo - geotf[0], y_geo - geotf[3]])
  27. return np.round(np.linalg.solve(a,
  28. b)).tolist() # Solve a quadratic equation
  29. @time_it
  30. # TODO: update for vector2raster
  31. def convert_data(src_img_path, geojson_path, save_path):
  32. raster = Raster(src_img_path)
  33. tmp_img = np.zeros((raster.height, raster.width), dtype=np.int32)
  34. # vector to EPSG from raster
  35. temp_geojson_path = translate_vector(geojson_path, raster.proj)
  36. geo_reader = codecs.open(temp_geojson_path, "r", encoding="utf-8")
  37. feats = geojson.loads(geo_reader.read())["features"] # All image patches
  38. geo_reader.close()
  39. for feat in tqdm(feats):
  40. geo = feat["geometry"]
  41. if geo["type"] == "Polygon":
  42. geo_points = geo["coordinates"][0]
  43. elif geo["type"] == "MultiPolygon":
  44. geo_points = geo["coordinates"][0][0]
  45. else:
  46. raise TypeError(
  47. "Geometry type must be 'Polygon' or 'MultiPolygon', not {}.".
  48. format(geo["type"]))
  49. xy_points = np.array([
  50. _gt_convert(point[0], point[1], raster.geot) for point in geo_points
  51. ]).astype(np.int32)
  52. # TODO: Label category
  53. cv2.fillPoly(tmp_img, [xy_points], 1) # Fill with polygons
  54. save_geotiff(tmp_img, save_path, raster.proj, raster.geot)
  55. os.remove(temp_geojson_path)
  56. if __name__ == "__main__":
  57. parser = argparse.ArgumentParser()
  58. parser.add_argument("--src_img_path", type=str, required=True, \
  59. help="Path of the original image.")
  60. parser.add_argument("--geojson_path", type=str, required=True, \
  61. help="Path of the GeoJSON file (the coordinate system is WGS84).")
  62. parser.add_argument("--save_path", type=str, required=True, \
  63. help="Path to store the mask data.")
  64. args = parser.parse_args()
  65. convert_data(args.src_img_path, args.geojson_path, args.save_path)