14
14
from opendm .tiles .tiler import generate_orthophoto_tiles
15
15
from opendm .cogeo import convert_to_cogeo
16
16
from osgeo import gdal
17
+ from osgeo import ogr
17
18
18
19
19
20
def get_orthophoto_vars (args ):
@@ -100,7 +101,62 @@ def generate_kmz(orthophoto_file, output_file=None, outsize=None):
100
101
101
102
system .run ('gdal_translate -of KMLSUPEROVERLAY -co FORMAT=PNG "%s" "%s" %s '
102
103
'--config GDAL_CACHEMAX %s%% ' % (orthophoto_file , output_file , bandparam , get_max_memory ()))
103
-
104
+
105
+ def generate_extent_polygon (orthophoto_file , output_file = None ):
106
+ """Function to return the orthophoto extent as a polygon into a gpkg file
107
+
108
+ Args:
109
+ orthophoto_file (str): the path to orthophoto file
110
+ output_file (str, optional): the path to the output file. Defaults to None.
111
+ """
112
+ def _create_vector (ortho_file , poly , format , output = None ):
113
+ if output is None :
114
+ base , ext = os .path .splitext (ortho_file )
115
+ output_file = base + '_extent.' + format .lower ()
116
+ # set up the shapefile driver
117
+ driver = ogr .GetDriverByName (format )
118
+
119
+ # create the data source
120
+ ds = driver .CreateDataSource (output_file )
121
+
122
+ # create one layer
123
+ layer = ds .CreateLayer ("extent" , srs , ogr .wkbPolygon )
124
+ if format != "DXF" :
125
+ layer .CreateField (ogr .FieldDefn ("id" , ogr .OFTInteger ))
126
+
127
+ # create the feature and set values
128
+ featureDefn = layer .GetLayerDefn ()
129
+ feature = ogr .Feature (featureDefn )
130
+ feature .SetGeometry (ogr .CreateGeometryFromWkt (poly ))
131
+ if format != "DXF" :
132
+ feature .SetField ("id" , 1 )
133
+ # add feature to layer
134
+ layer .CreateFeature (feature )
135
+ # save and close everything
136
+ feature = None
137
+ ds = None
138
+
139
+ try :
140
+ gtif = gdal .Open (orthophoto_file )
141
+ srs = gtif .GetSpatialRef ()
142
+ geoTransform = gtif .GetGeoTransform ()
143
+ # calculate the coordinates
144
+ minx = geoTransform [0 ]
145
+ maxy = geoTransform [3 ]
146
+ maxx = minx + geoTransform [1 ] * gtif .RasterXSize
147
+ miny = maxy + geoTransform [5 ] * gtif .RasterYSize
148
+ # create polygon in wkt format
149
+ poly_wkt = "POLYGON ((%s %s, %s %s, %s %s, %s %s, %s %s))" % (minx , miny , minx , maxy , maxx , maxy , maxx , miny , minx , miny )
150
+ # create vector file
151
+ # just the DXF to support AutoCAD users
152
+ # to load the geotiff raster correctly.
153
+ # _create_vector(orthophoto_file, poly_wkt, "GPKG", output_file)
154
+ _create_vector (orthophoto_file , poly_wkt , "DXF" , output_file )
155
+ gtif = None
156
+ except Exception as e :
157
+ log .ODM_WARNING ("Cannot create extent layer for %s: %s" % (ortho_file , str (e )))
158
+
159
+
104
160
def post_orthophoto_steps (args , bounds_file_path , orthophoto_file , orthophoto_tiles_dir , resolution ):
105
161
if args .crop > 0 or args .boundary :
106
162
Cropper .crop (bounds_file_path , orthophoto_file , get_orthophoto_vars (args ), keep_original = not args .optimize_disk_space , warp_options = ['-dstalpha' ])
@@ -120,6 +176,8 @@ def post_orthophoto_steps(args, bounds_file_path, orthophoto_file, orthophoto_ti
120
176
if args .cog :
121
177
convert_to_cogeo (orthophoto_file , max_workers = args .max_concurrency , compression = args .orthophoto_compression )
122
178
179
+ generate_extent_polygon (orthophoto_file )
180
+
123
181
def compute_mask_raster (input_raster , vector_mask , output_raster , blend_distance = 20 , only_max_coords_feature = False ):
124
182
if not os .path .exists (input_raster ):
125
183
log .ODM_WARNING ("Cannot mask raster, %s does not exist" % input_raster )
0 commit comments