Skip to content

Commit b2668ca

Browse files
authored
Merge pull request #1828 from lucadelu/ortho_extent
added capability to generate vector polygon in DXF format to be used in AutoCAD LT
2 parents ce839d4 + 131b29c commit b2668ca

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

opendm/orthophoto.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from opendm.tiles.tiler import generate_orthophoto_tiles
1515
from opendm.cogeo import convert_to_cogeo
1616
from osgeo import gdal
17+
from osgeo import ogr
1718

1819

1920
def get_orthophoto_vars(args):
@@ -100,7 +101,62 @@ def generate_kmz(orthophoto_file, output_file=None, outsize=None):
100101

101102
system.run('gdal_translate -of KMLSUPEROVERLAY -co FORMAT=PNG "%s" "%s" %s '
102103
'--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+
104160
def post_orthophoto_steps(args, bounds_file_path, orthophoto_file, orthophoto_tiles_dir, resolution):
105161
if args.crop > 0 or args.boundary:
106162
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
120176
if args.cog:
121177
convert_to_cogeo(orthophoto_file, max_workers=args.max_concurrency, compression=args.orthophoto_compression)
122178

179+
generate_extent_polygon(orthophoto_file)
180+
123181
def compute_mask_raster(input_raster, vector_mask, output_raster, blend_distance=20, only_max_coords_feature=False):
124182
if not os.path.exists(input_raster):
125183
log.ODM_WARNING("Cannot mask raster, %s does not exist" % input_raster)

0 commit comments

Comments
 (0)