Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

KandelN
Copy link

@KandelN KandelN commented Jan 14, 2025

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:

  • The estimated resolution is not calculated or displayed if no reprojection is required.
  • A temporary raster map (rimport_tmp) remains in the mapset, which should be cleaned up.

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.

@github-actions github-actions bot added raster Related to raster data processing Python Related code is in Python module labels Jan 14, 2025
):
gs.run_command(
"g.remove", type="raster", name=TMP_EST_FILE, flags="f", quiet=True
)

def is_projection_matching(GDALdatasource):
Copy link
Contributor

Choose a reason for hiding this comment

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

[ruff] reported by reviewdog 🐶

Suggested change
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
Copy link
Contributor

Choose a reason for hiding this comment

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

[ruff] reported by reviewdog 🐶

Suggested change
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0
estres = (raster_info["ewres"] + raster_info["nsres"]) / 2.0

Copy link
Member

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"])?

@HuidaeCho HuidaeCho self-assigned this Jan 14, 2025
@HuidaeCho HuidaeCho changed the title Fix for Issue #3252 - r.import with -e Flag Behaving Unexpectedly When No Reprojection is Required r.import: Fix the -e flag for estimating the resolution without importing Jan 14, 2025
@petrasovaa
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Member

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)

Copy link
Member

@HuidaeCho HuidaeCho Jan 17, 2025

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..."))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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
Copy link
Member

Choose a reason for hiding this comment

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

As @petrasovaa suggested,

Suggested change
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
Copy link
Member

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,

Suggested change
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0
estres = math.sqrt(raster_info["nsres"] * raster_info["ewres"])

@HuidaeCho
Copy link
Member

HuidaeCho commented Jan 17, 2025

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.

I think this flag needs to be fixed for consistency, but you're right about r.external. I think this PR can be improved for -e without reprojection using r.external?

@KandelN, you want to look into this possibility?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Python Related code is in Python raster Related to raster data processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] r.import with e flag behaves unexpected, when no reprojection required
3 participants