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
Open
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
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,8 +146,17 @@
"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):

Check failure on line 159 in scripts/r.import/r.import.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E302)

scripts/r.import/r.import.py:159:1: E302 Expected 2 blank lines, found 1
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):

"""Returns True if current location projection
matches dataset projection, otherwise False"""
try:
Expand All @@ -157,7 +167,7 @@


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 @@
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

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.

if options["resolution_value"]:
if tgtres != "value":
gs.fatal(
Expand Down Expand Up @@ -206,6 +217,25 @@
_("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..."))

raster_info = gs.raster_info(output)
if raster_info:
estres = (raster_info["ewres"] + raster_info["nsres"])/2.0

Check failure on line 225 in scripts/r.import/r.import.py

View workflow job for this annotation

GitHub Actions / Python Code Quality Checks (ubuntu-22.04)

Ruff (E226)

scripts/r.import/r.import.py:225:79: E226 Missing whitespace around arithmetic operator
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"])?

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

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)

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

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