Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions pyfiles/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os
import subprocess

from osgeo import gdal, osr
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
import numpy as np

Expand Down Expand Up @@ -50,17 +50,25 @@ def initialize_output_raster_file(base_raster_file, output_raster_file, initial_
grid_initial_data = np.zeros((rows, cols), dtype=np.float32)
grid_initial_data[:] = float(initial_data)
else:
grid_initial_data = np.zeros((rows, cols), dtype=np.int)
grid_initial_data = np.zeros((rows, cols), dtype=np.int32)
grid_initial_data[:] = int(initial_data)

outband = outRaster.GetRasterBand(1)
outband.SetNoDataValue(NO_DATA_VALUE)
outband.WriteArray(grid_initial_data)

# set the projection of the tif file same as that of the base_raster file
outRasterSRS = osr.SpatialReference()
outRasterSRS.ImportFromWkt(base_raster.GetProjectionRef())
outRaster.SetProjection(outRasterSRS.ExportToWkt())
# Set projection from the source raster without reparsing WKT because
# some ArcGIS binary DEM projections can fail ImportFromWkt with corrupt data errors.
source_srs = base_raster.GetSpatialRef() if hasattr(base_raster, 'GetSpatialRef') else None
if source_srs:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do the else option all the time?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added comments to the this code block to clarify the branching logic.

# GetSpatialRef is the preferred modern GDAL path and returns a spatial reference object that we export to WKT
outRaster.SetProjection(source_srs.ExportToWkt())
else:
# GetProjectionRef is retained only as a fallback for datasets/drivers where no spatial reference object is exposed
# Rely on dataset's raw projection string
source_wkt = base_raster.GetProjectionRef()
if source_wkt and source_wkt.strip():
outRaster.SetProjection(source_wkt)

outRaster = None

Expand Down
Loading