Skip to content

Commit

Permalink
feat: updating rasterstyles via --replace
Browse files Browse the repository at this point in the history
  • Loading branch information
lubojr committed Oct 3, 2023
1 parent a377bdd commit 03eae92
Showing 1 changed file with 51 additions and 15 deletions.
66 changes: 51 additions & 15 deletions eoxserver/resources/coverages/management/commands/rasterstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def add_arguments(self, parser):
'style_name', nargs=1, help=''
)

for parser in [create_parser, import_parser]:
parser.add_argument(
'--replace', action='store_true',
default=False,
help=('''Change raster style if already exists.''')
)

@transaction.atomic
def handle(self, subcommand, *args, **kwargs):
""" Dispatch sub-commands: create, delete, list, link.
Expand All @@ -119,22 +126,36 @@ def handle(self, subcommand, *args, **kwargs):
*args, **kwargs
)

def handle_create(self, name, type, title, abstract, color_entries,
*args, **kwargs):
def handle_create(self, name, type, title, abstract, color_entries, replace, *args, **kwargs):
""" Handle the creation of a new raster style.
"""

raster_style = models.RasterStyle.objects.create(
name=name,
type=type,
title=title,
abstract=abstract,
)
if replace:
raster_style = models.RasterStyle.objects.update_or_create(
name=name,
defaults={
'type': type,
'title': title,
'abstract': abstract,
},
)[0]
else:
raster_style = models.RasterStyle.objects.create(
name=name,
type=type,
title=title,
abstract=abstract,
)

if not color_entries:
raise CommandError("No color entries specified")

first_iteration = True
for value, color, opacity, label in color_entries:
if replace and first_iteration:
raster_style_color_entries_existing = models.RasterStyleColorEntry.objects.filter(
raster_style=raster_style,
)
raster_style_color_entries_existing.delete()
first_iteration = False
entry = models.RasterStyleColorEntry(
raster_style=raster_style,
value=float(value),
Expand All @@ -147,7 +168,7 @@ def handle_create(self, name, type, title, abstract, color_entries,

print('Successfully created raster style %r' % name)

def handle_import(self, filename, selects, renames, *args, **kwargs):
def handle_import(self, filename, selects, renames, replace, *args, **kwargs):
tree = etree.parse(filename)
nsmap = {
"sld": "http://www.opengis.net/sld",
Expand All @@ -170,14 +191,29 @@ def handle_import(self, filename, selects, renames, *args, **kwargs):
"sld:FeatureTypeStyle/sld:Rule/sld:RasterSymbolizer/sld:ColorMap)",
namespaces=nsmap,
)[0]
raster_style = models.RasterStyle.objects.create(
name=name, type=color_map.get("type", "ramp")
)
if replace:
raster_style = models.RasterStyle.objects.update_or_create(
name=name,
defaults={
'type': color_map.get("type", "ramp"),
},
)
else:
raster_style = models.RasterStyle.objects.create(
name=name, type=color_map.get("type", "ramp")
)

color_map_entries = color_map.xpath(
"sld:ColorMapEntry", namespaces=nsmap
)
first_iteration = True
for color_map_entry in color_map_entries:
if replace and first_iteration:
raster_style_color_entries_existing = models.RasterStyleColorEntry.objects.filter(
raster_style=raster_style,
)
raster_style_color_entries_existing.delete()
first_iteration = False
entry = models.RasterStyleColorEntry(
raster_style=raster_style,
value=float(color_map_entry.get("quantity")),
Expand Down Expand Up @@ -225,7 +261,7 @@ def handle_link(self, name, product_type_name, browse_type_name, stylename,
product_type__name=product_type_name,
name=browse_type_name
)
models.RasterStyleToBrowseTypeThrough.objects.create(
models.RasterStyleToBrowseTypeThrough.objects.get_or_create(
raster_style=raster_style,
browse_type=browse_type,
style_name=stylename,
Expand Down

0 comments on commit 03eae92

Please sign in to comment.