|
@@ -0,0 +1,73 @@
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import os
|
|
|
+import codecs
|
|
|
+import argparse
|
|
|
+
|
|
|
+import paddlers
|
|
|
+import numpy as np
|
|
|
+import cv2
|
|
|
+import geojson
|
|
|
+from tqdm import tqdm
|
|
|
+
|
|
|
+from utils import Raster, save_geotiff, translate_vector, time_it
|
|
|
+
|
|
|
+
|
|
|
+def _gt_convert(x_geo, y_geo, geotf):
|
|
|
+ a = np.array([[geotf[1], geotf[2]], [geotf[4], geotf[5]]])
|
|
|
+ b = np.array([x_geo - geotf[0], y_geo - geotf[3]])
|
|
|
+ return np.round(np.linalg.solve(a,
|
|
|
+ b)).tolist()
|
|
|
+
|
|
|
+
|
|
|
+@time_it
|
|
|
+
|
|
|
+def convert_data(image_path, geojson_path, save_path):
|
|
|
+ raster = Raster(image_path)
|
|
|
+ tmp_img = np.zeros((raster.height, raster.width), dtype=np.int32)
|
|
|
+
|
|
|
+ temp_geojson_path = translate_vector(geojson_path, raster.proj)
|
|
|
+ geo_reader = codecs.open(temp_geojson_path, "r", encoding="utf-8")
|
|
|
+ feats = geojson.loads(geo_reader.read())["features"]
|
|
|
+ geo_reader.close()
|
|
|
+ for feat in tqdm(feats):
|
|
|
+ geo = feat["geometry"]
|
|
|
+ if geo["type"] == "Polygon":
|
|
|
+ geo_points = geo["coordinates"][0]
|
|
|
+ elif geo["type"] == "MultiPolygon":
|
|
|
+ geo_points = geo["coordinates"][0][0]
|
|
|
+ else:
|
|
|
+ raise TypeError(
|
|
|
+ "Geometry type must be 'Polygon' or 'MultiPolygon', not {}.".
|
|
|
+ format(geo["type"]))
|
|
|
+ xy_points = np.array([
|
|
|
+ _gt_convert(point[0], point[1], raster.geot) for point in geo_points
|
|
|
+ ]).astype(np.int32)
|
|
|
+
|
|
|
+ cv2.fillPoly(tmp_img, [xy_points], 1)
|
|
|
+ save_geotiff(tmp_img, save_path, raster.proj, raster.geot)
|
|
|
+ os.remove(temp_geojson_path)
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ parser = argparse.ArgumentParser()
|
|
|
+ parser.add_argument("--srcimg_path", type=str, required=True, \
|
|
|
+ help="Path of the original image.")
|
|
|
+ parser.add_argument("--geojson_path", type=str, required=True, \
|
|
|
+ help="Path of the GeoJSON file (the coordinate system is WGS84).")
|
|
|
+ parser.add_argument("--save_path", type=str, required=True, \
|
|
|
+ help="Path to store the mask data.")
|
|
|
+ args = parser.parse_args()
|
|
|
+ convert_data(args.srcimg_path, args.geojson_path, args.save_path)
|