geojson2mask.py 2.8 KB

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