-
-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
r.import: Fix the -e flag for estimating the resolution without importing #4947
base: main
Are you sure you want to change the base?
Conversation
): | ||
gs.run_command( | ||
"g.remove", type="raster", name=TMP_EST_FILE, flags="f", quiet=True | ||
) | ||
|
||
def is_projection_matching(GDALdatasource): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ruff] reported by reviewdog 🐶
def is_projection_matching(GDALdatasource): | |
def is_projection_matching(GDALdatasource): |
gs.message(_("Calculating estimted resolution...")) | ||
raster_info = gs.raster_info(output) | ||
if raster_info: | ||
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ruff] reported by reviewdog 🐶
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0 | |
estres = (raster_info["ewres"] + raster_info["nsres"]) / 2.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with the original code: estres = math.sqrt(raster_info["nsres"] * raster_info["ewres"])
?
I am not entirely sure this is needed. The resolution needs to be estimated because of the reprojection, when you don't reproject, the resolution is not 'estimated'. But I agree, that having consistent behavior of the e flag would make sense (for automated workflows for example). But in that case, wouldn't using r.external work better, so that you avoid the import, which can take a long time. With r.external, the raster is only linked and I believe you can get correct resolution with r.info. |
@@ -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 = output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need something unique, see gs.append_node_pid
below.
gs.message(_("Calculating estimted resolution...")) | ||
raster_info = gs.raster_info(output) | ||
if raster_info: | ||
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with the original code: estres = math.sqrt(raster_info["nsres"] * raster_info["ewres"])
?
return 0 | ||
except CalledModuleError: | ||
gs.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for another try except
and move this if flags["e"]
block with return 0
below the original try except
try:
gs.run_command("r.in.gdal", **parameters)
...
return 0 # <-- delete this line
except
...
if flags["e"]:
...
return 0
@@ -206,6 +217,25 @@ def main(): | |||
_("Input <%s> successfully imported without reprojection") | |||
% GDALdatasource | |||
) | |||
if flags["e"]: | |||
try: | |||
gs.message(_("Calculating estimted resolution...")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gs.message(_("Calculating estimted resolution...")) | |
gs.message(_("Calculating estimated resolution...")) |
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As @petrasovaa suggested,
output = "rimport_tmp" # will be removed with the entire tmp location | |
output = gs.append_node_pid("rimport_tmp") # will be removed with the entire tmp location |
gs.message(_("Calculating estimted resolution...")) | ||
raster_info = gs.raster_info(output) | ||
if raster_info: | ||
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with the original code,
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0 | |
estres = math.sqrt(raster_info["nsres"] * raster_info["ewres"]) |
I think this flag needs to be fixed for consistency, but you're right about @KandelN, you want to look into this possibility? |
Problem Summary
When using r.import with the -e flag to estimate the resolution of a raster file without performing a full import, the following issues were observed:
Root Cause
The issue arises due to improper handling of cases where the raster input is already in the same projection as the current GRASS GIS session. There is no code for -e flag behavior when no reprojection is required. Additionally, the temporary raster map is not deleted.
Fixes
Resolution Estimation Logic:
Added a -e flag behavior when reprojection is not required. Applied the logic to ensure the resolution is calculated and displayed even when no reprojection is required.
Temporary Map Cleanup:
Created a new flag TMP_EST_FILE to hold the temporary file. Ensured that the temporary map (rimport_tmp) is deleted after the resolution estimation process.