@@ -86,6 +86,7 @@ def generate_png(orthophoto_file, output_file=None, outsize=None):
8686 params .append ("-outsize %s 0" % outsize )
8787
8888 system .run ('gdal_translate -of png "%s" "%s" %s '
89+ '-co WORLDFILE=YES '
8990 '--config GDAL_CACHEMAX %s%% ' % (orthophoto_file , output_file , " " .join (params ), get_max_memory ()))
9091
9192def generate_kmz (orthophoto_file , output_file = None , outsize = None ):
@@ -102,59 +103,67 @@ def generate_kmz(orthophoto_file, output_file=None, outsize=None):
102103 system .run ('gdal_translate -of KMLSUPEROVERLAY -co FORMAT=PNG "%s" "%s" %s '
103104 '--config GDAL_CACHEMAX %s%% ' % (orthophoto_file , output_file , bandparam , get_max_memory ()))
104105
105- def generate_extent_polygon (orthophoto_file , output_file = None ):
106+ def generate_extent_polygon (orthophoto_file ):
106107 """Function to return the orthophoto extent as a polygon into a gpkg file
107108
108109 Args:
109110 orthophoto_file (str): the path to orthophoto file
110- output_file (str, optional): the path to the output file. Defaults to None.
111111 """
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
112+ base , ext = os .path .splitext (orthophoto_file )
113+ output_file = base + '_extent.dxf'
138114
139115 try :
140116 gtif = gdal .Open (orthophoto_file )
141117 srs = gtif .GetSpatialRef ()
142118 geoTransform = gtif .GetGeoTransform ()
119+
143120 # calculate the coordinates
144121 minx = geoTransform [0 ]
145122 maxy = geoTransform [3 ]
146123 maxx = minx + geoTransform [1 ] * gtif .RasterXSize
147124 miny = maxy + geoTransform [5 ] * gtif .RasterYSize
125+
148126 # create polygon in wkt format
149127 poly_wkt = "POLYGON ((%s %s, %s %s, %s %s, %s %s, %s %s))" % (minx , miny , minx , maxy , maxx , maxy , maxx , miny , minx , miny )
128+
150129 # create vector file
151130 # just the DXF to support AutoCAD users
152131 # 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 )
132+ driver = ogr .GetDriverByName ("DXF" )
133+ ds = driver .CreateDataSource (output_file )
134+ layer = ds .CreateLayer ("extent" , srs , ogr .wkbPolygon )
135+
136+ # create the feature and set values
137+ featureDefn = layer .GetLayerDefn ()
138+ feature = ogr .Feature (featureDefn )
139+ feature .SetGeometry (ogr .CreateGeometryFromWkt (poly_wkt ))
140+
141+ # add feature to layer
142+ layer .CreateFeature (feature )
143+
144+ # save and close everything
145+ feature = None
146+ ds = None
155147 gtif = None
148+ log .ODM_INFO ("Wrote %s" % output_file )
149+ except Exception as e :
150+ log .ODM_WARNING ("Cannot create extent layer for %s: %s" % (orthophoto_file , str (e )))
151+
152+
153+ def generate_tfw (orthophoto_file ):
154+ base , ext = os .path .splitext (orthophoto_file )
155+ tfw_file = base + '.tfw'
156+
157+ try :
158+ with rasterio .open (orthophoto_file ) as ds :
159+ t = ds .transform
160+ with open (tfw_file , 'w' ) as f :
161+ # rasterio affine values taken by
162+ # https://mharty3.github.io/til/GIS/raster-affine-transforms/
163+ f .write ("\n " .join ([str (v ) for v in [t .a , t .d , t .b , t .e , t .c , t .f ]]) + "\n " )
164+ log .ODM_INFO ("Wrote %s" % tfw_file )
156165 except Exception as e :
157- log .ODM_WARNING ("Cannot create extent layer for %s: %s" % (ortho_file , str (e )))
166+ log .ODM_WARNING ("Cannot create .tfw for %s: %s" % (orthophoto_file , str (e )))
158167
159168
160169def post_orthophoto_steps (args , bounds_file_path , orthophoto_file , orthophoto_tiles_dir , resolution ):
@@ -177,6 +186,7 @@ def post_orthophoto_steps(args, bounds_file_path, orthophoto_file, orthophoto_ti
177186 convert_to_cogeo (orthophoto_file , max_workers = args .max_concurrency , compression = args .orthophoto_compression )
178187
179188 generate_extent_polygon (orthophoto_file )
189+ generate_tfw (orthophoto_file )
180190
181191def compute_mask_raster (input_raster , vector_mask , output_raster , blend_distance = 20 , only_max_coords_feature = False ):
182192 if not os .path .exists (input_raster ):
0 commit comments