Skip to content

Commit

Permalink
Add case for estimating resolution if projection matches
Browse files Browse the repository at this point in the history
  • Loading branch information
KandelN committed Jan 14, 2025
1 parent 30de3bd commit 0e1d8e0
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion scripts/r.import/r.import.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
SRCGISRC = None
GISDBASE = None
TMP_REG_NAME = None
TMP_EST_FILE = None


def cleanup():
Expand All @@ -145,6 +146,15 @@ def cleanup():
"g.remove", type="vector", name=TMP_REG_NAME, flags="f", quiet=True
)

if (
TMP_EST_FILE
and gs.find_file(
name=TMP_EST_FILE, element="cell"
)["fullname"]
):
gs.run_command(
"g.remove", type="raster", name=TMP_EST_FILE, flags="f", quiet=True
)

def is_projection_matching(GDALdatasource):
"""Returns True if current location projection
Expand All @@ -157,7 +167,7 @@ def is_projection_matching(GDALdatasource):


def main():
global TMPLOC, SRCGISRC, GISDBASE, TMP_REG_NAME
global TMPLOC, SRCGISRC, GISDBASE, TMP_REG_NAME, TMP_EST_FILE

GDALdatasource = options["input"]
output = options["output"]
Expand All @@ -168,6 +178,7 @@ def main():
title = options["title"]
if flags["e"] and not output:
output = "rimport_tmp" # will be removed with the entire tmp location
TMP_EST_FILE = gs.append_node_pid(output)
if options["resolution_value"]:
if tgtres != "value":
gs.fatal(
Expand Down Expand Up @@ -206,6 +217,25 @@ def main():
_("Input <%s> successfully imported without reprojection")
% GDALdatasource
)
if flags["e"]:
try:
gs.message(_("Calculating estimted resolution..."))
raster_info = gs.raster_info(output)
if raster_info:
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0
gs.message(
_("Estimated target resolution for input band <{out}>: {res}").format(
out=output, res=estres
)
)
else:
gs.warning(
_("Unable to estimate the resolution for the input raster")
)
return 0
except CalledModuleError:
gs.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)

return 0
except CalledModuleError:
gs.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)
Expand Down

0 comments on commit 0e1d8e0

Please sign in to comment.