Skip to content

Commit

Permalink
i.sentinel.import: allow importing for lat/lon locations (#769)
Browse files Browse the repository at this point in the history
* importing for latlong locations

* apply black
  • Loading branch information
griembauer committed Aug 3, 2022
1 parent 5a2934e commit ce383db
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/imagery/i.sentinel/i.sentinel.import/i.sentinel.import.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def __init__(self, input_dir, unzip_dir):
if not os.path.exists(unzip_dir):
gs.fatal(_("Directory <{}> does not exist").format(unzip_dir))

self._projection_units_meters = self._check_location_projection_meters()

def __del__(self):
# remove temporary maps
for map in self._map_list:
Expand All @@ -200,6 +202,13 @@ def __del__(self):
except OSError:
pass

def _check_location_projection_meters(self):
units = gs.parse_command("g.proj", flags="g")["units"]
if units != "meters":
return False
else:
return True

def filter(self, pattern=None):
if pattern:
filter_p = r".*{}.*.jp2$".format(pattern)
Expand Down Expand Up @@ -287,8 +296,9 @@ def import_products(self, reproject=False, link=False, override=False):
if reproject:
module = "r.import"
self._args["resample"] = "bilinear"
self._args["resolution"] = "value"
self._args["extent"] = options["extent"]
if self._projection_units_meters is True:
self._args["resolution"] = "value"
else:
module = "r.in.gdal"
self._args["flags"] = "o" if override else None
Expand Down Expand Up @@ -372,7 +382,7 @@ def _extract_semantic_label(self, map_name):
def _import_file(self, filename, module, args):
mapname = self._map_name(filename)
gs.message(_("Processing <{}>...").format(mapname))
if module == "r.import":
if module == "r.import" and self._projection_units_meters is True:
self._args["resolution_value"] = self._raster_resolution(filename)
try:
gs.run_command(module, input=filename, output=mapname, **self._args)
Expand Down Expand Up @@ -451,8 +461,9 @@ def import_cloud_masks(
)
)
if reproject:
self._args["resolution_value"] = self._raster_resolution(f)
self._args["resample"] = "bilinear"
if self._projection_units_meters is True:
self._args["resolution_value"] = self._raster_resolution(f)
gs.run_command(
"r.import", input=f, output=clouds_imported, **self._args
)
Expand All @@ -474,10 +485,11 @@ def import_cloud_masks(
"_".join([items[5], items[2], "SCL_20m.jp2"])
)
if reproject:
self._args["resolution_value"] = self._raster_resolution(
shadow_file[0]
)
self._args["resample"] = "nearest"
if self._projection_units_meters is True:
self._args[
"resolution_value"
] = self._raster_resolution(shadow_file[0])
gs.run_command(
"r.import",
input=shadow_file,
Expand Down Expand Up @@ -516,17 +528,27 @@ def import_cloud_masks(
gs.use_temp_region()
gs.run_command("g.region", raster=mask_selected)

# Cleaning small patches
try:
gs.run_command(
"r.reclass.area",
input=mask_selected,
output=mask_cleaned,
value=area_threshold,
mode="greater",
# Cleaning small patches, only works for non latlong locations
if self._projection_units_meters is True:
try:
gs.run_command(
"r.reclass.area",
input=mask_selected,
output=mask_cleaned,
value=area_threshold,
mode="greater",
)
except Exception as e:
pass # error already printed
else:
gs.warning(
_(
"Location projection units is not meters. "
"Removing small cloud/shadow areas using "
"the cloud_area_threshold parameter is skipped."
)
)
except Exception as e:
pass # error already printed
mask_cleaned = mask_selected

# Extract & Label clouds (and shadows)
gs.run_command("r.null", map=mask_cleaned, setnull="0")
Expand Down

0 comments on commit ce383db

Please sign in to comment.