-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from EOxServer/wms_range_scaling_0.4
Wms range scaling 0.4
- Loading branch information
Showing
8 changed files
with
494 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
Empty file.
158 changes: 158 additions & 0 deletions
158
eoxserver/services/management/commands/wms_options_list.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#------------------------------------------------------------------------------- | ||
# | ||
# Project: EOxServer <http://eoxserver.org> | ||
# Authors: Martin Paces <martin.paces@eox.at> | ||
# | ||
#------------------------------------------------------------------------------- | ||
# Copyright (C) 2016 EOX IT Services GmbH | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies of this Software or works derived from this Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
#------------------------------------------------------------------------------- | ||
# pylint: disable=missing-docstring | ||
|
||
from sys import stdout | ||
import json | ||
from optparse import make_option | ||
try: | ||
# available in Python 2.7+ | ||
from collections import OrderedDict | ||
except ImportError: | ||
from django.utils.datastructures import SortedDict as OrderedDict | ||
from django.core.management.base import BaseCommand, CommandError | ||
from eoxserver.resources.coverages.management.commands import CommandOutputMixIn | ||
from eoxserver.services.models import WMSRenderOptions | ||
|
||
JSON_OPTIONS = { | ||
"indent": None, | ||
"separators": (', ', ': '), | ||
"sort_keys": False, | ||
} | ||
|
||
class Command(CommandOutputMixIn, BaseCommand): | ||
|
||
option_list = BaseCommand.option_list + ( | ||
make_option( | ||
'--json', dest='json_dump', action='store_true', default=False, | ||
help=( | ||
"Optional. Dump rangetype(s) in JSON format. This JSON " | ||
"dump can be loaded by another instance of EOxServer." | ||
) | ||
), | ||
make_option( | ||
'-o', '--output', dest='filename', action='store', type='string', | ||
default='-', help=( | ||
"Optional. Write output to a file rather than to the default" | ||
" standard output." | ||
) | ||
), | ||
) | ||
|
||
args = "[<eo-id> [<eo-id> ...]]" | ||
|
||
help = """ | ||
Print WMS options for listed coverages or all coverages. If the coverage | ||
does not exist or if it exists but it has WMS option record no options | ||
are printed. | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
# collect input parameters | ||
self.verbosity = int(options.get('verbosity', 1)) | ||
print_json = bool(options.get('json_dump', False)) | ||
filename = options.get('filename', '-') | ||
|
||
# get the range types | ||
wms_opts = WMSRenderOptions.objects.select_related('coverage') | ||
if args: | ||
wms_opts = wms_opts.filter(coverage__identifier__in=args) | ||
else: | ||
wms_opts = wms_opts.all() | ||
|
||
# select the right output formatter | ||
if print_json: | ||
output_formatter = output_json | ||
else: | ||
output_formatter = output_detailed | ||
|
||
# write the output | ||
try: | ||
with (stdout if filename == "-" else open(filename, "w")) as fout: | ||
for item in output_formatter(wms_opts): | ||
fout.write(item) | ||
except IOError as exc: | ||
raise CommandError( | ||
"Failed to write the output file %r! %s" % (filename, str(exc)) | ||
) | ||
|
||
|
||
def output_detailed(wms_opts): | ||
""" Detailed range-type output (includes brief bands' info). """ | ||
for wms_opt in wms_opts: | ||
yield "%s\n" % wms_opt.coverage.identifier | ||
for key, val in wms_opt_to_dict(wms_opt).items(): | ||
if hasattr(val, '__len__') and not isinstance(val, basestring): | ||
val = ",".join(str(v) for v in val) | ||
if key == "coverage_identifier": | ||
continue | ||
if key == "scale_auto" and not val: | ||
continue | ||
yield " %s: %s\n" % (key, val) | ||
yield "\n" | ||
|
||
|
||
def output_json(wms_opts): | ||
""" Full JSON range-type dump. """ | ||
yield '[' | ||
delimiter = '' | ||
for wms_opt in wms_opts: | ||
yield delimiter | ||
yield json.dumps(wms_opt_to_dict(wms_opt), **JSON_OPTIONS) | ||
delimiter = ',\n' | ||
yield ']\n' | ||
|
||
|
||
def wms_opt_to_dict(wms_opt): | ||
""" Dump objects object to a JSON serializable dictionary. """ | ||
options = OrderedDict() | ||
options['coverage_identifier'] = wms_opt.coverage.identifier | ||
if wms_opt.default_red is not None: | ||
options['red'] = wms_opt.default_red | ||
if wms_opt.default_green is not None: | ||
options['green'] = wms_opt.default_green | ||
if wms_opt.default_blue is not None: | ||
options['blue'] = wms_opt.default_blue | ||
if wms_opt.default_alpha is not None: | ||
options['alpha'] = wms_opt.default_alpha | ||
if wms_opt.resampling is not None: | ||
options['resampling'] = wms_opt.resampling | ||
if wms_opt.scale_auto: | ||
options['scale_auto'] = wms_opt.scale_auto | ||
if wms_opt.scale_min is not None: | ||
options['scale_min'] = wms_opt.scale_min | ||
if wms_opt.scale_max is not None: | ||
options['scale_max'] = wms_opt.scale_max | ||
if wms_opt.bands_scale_min is not None: | ||
options['bands_scale_min'] = [ | ||
int(v) for v in wms_opt.bands_scale_min.split(',') | ||
] | ||
if wms_opt.bands_scale_max is not None: | ||
options['bands_scale_max'] = [ | ||
int(v) for v in wms_opt.bands_scale_max.split(',') | ||
] | ||
return options |
156 changes: 156 additions & 0 deletions
156
eoxserver/services/management/commands/wms_options_load.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
#------------------------------------------------------------------------------- | ||
# | ||
# Project: EOxServer <http://eoxserver.org> | ||
# Authors: Martin Paces <martin.paces@eox.at> | ||
# | ||
#------------------------------------------------------------------------------- | ||
# Copyright (C) 2016 EOX IT Services GmbH | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies of this Software or works derived from this Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
#------------------------------------------------------------------------------- | ||
# pylint: disable=missing-docstring | ||
|
||
from sys import stdin | ||
import traceback | ||
import json | ||
from optparse import make_option | ||
from django.core.management.base import BaseCommand, CommandError | ||
from eoxserver.resources.coverages.management.commands import ( | ||
CommandOutputMixIn, nested_commit_on_success, | ||
) | ||
from eoxserver.resources.coverages.models import Coverage | ||
from eoxserver.services.models import WMSRenderOptions | ||
|
||
|
||
class Command(CommandOutputMixIn, BaseCommand): | ||
|
||
option_list = BaseCommand.option_list + ( | ||
make_option( | ||
'-i', '--input', dest='filename', action='store', type='string', | ||
default='-', help=( | ||
"Optional. Read input from a file rather than from the " | ||
"default standard input." | ||
) | ||
), | ||
) | ||
|
||
help = """ | ||
Load WMS options stored in JSON format from standard input (default) or from | ||
a file (-i option). | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
# Collect parameters | ||
self.traceback = bool(options.get("traceback", False)) | ||
self.verbosity = int(options.get('verbosity', 1)) | ||
filename = options.get('filename', '-') | ||
|
||
self.print_msg("Importing WMS options from %s ..." % ( | ||
"standard input" if filename == "-" else "file %r" % filename | ||
)) | ||
|
||
# load and parse the input data | ||
try: | ||
with (stdin if filename == "-" else open(filename, "r")) as fin: | ||
wms_opts = json.load(fin) | ||
except IOError as exc: | ||
raise CommandError( | ||
"Failed to open the input file '%s'! %s " % (filename, str(exc)) | ||
) | ||
|
||
# insert the range types to DB | ||
|
||
success_count = 0 # success counter - counts finished syncs | ||
|
||
for wms_opt in wms_opts: | ||
try: | ||
eoid = wms_opt['coverage_identifier'] | ||
insert_or_update_wms_opt(eoid, wms_opt) | ||
except Exception as exc: | ||
if self.traceback: | ||
self.print_msg(traceback.format_exc()) | ||
self.print_err("Failed to load options of %s! %s" % ( | ||
eoid, "%s: %s" % (type(exc).__name__, str(exc)) | ||
)) | ||
continue | ||
else: | ||
success_count += 1 # increment success counter | ||
|
||
# print the final summary | ||
count = len(wms_opts) | ||
error_count = count - success_count | ||
|
||
if success_count > 0: | ||
self.print_msg( | ||
"Successfully loaded %d of %s WMS option records." % | ||
(success_count, count), 1 | ||
) | ||
else: | ||
self.print_msg("No WMS option record loaded.") | ||
|
||
if error_count > 0: | ||
raise CommandError( | ||
"Failed to load %d WMS option records." % error_count | ||
) | ||
|
||
|
||
@nested_commit_on_success | ||
def insert_or_update_wms_opt(eoid, wms_opt): | ||
""" Insert or update one WMS option record. """ | ||
try: | ||
# try to get an existing object | ||
wms_opt_obj = WMSRenderOptions.objects.get(coverage__identifier=eoid) | ||
except WMSRenderOptions.DoesNotExist: | ||
try: | ||
# locate the related coverage | ||
coverage = Coverage.objects.get(identifier=eoid) | ||
except Coverage.DoesNotExist: | ||
raise CommandError("Invalid coverage identifier %s!" % eoid) | ||
# create a new object | ||
wms_opt_obj = WMSRenderOptions(coverage=coverage) | ||
|
||
# set the record | ||
set_from_dict(wms_opt_obj, wms_opt).save() | ||
|
||
|
||
def set_from_dict(wms_opt_obj, options): | ||
""" Set object from a JSON serializable dictionary. """ | ||
|
||
_int = lambda v: v if v is None else int(v) | ||
|
||
wms_opt_obj.default_red = _int(options.get('red', None)) | ||
wms_opt_obj.default_green = _int(options.get('green', None)) | ||
wms_opt_obj.default_blue = _int(options.get('blue', None)) | ||
wms_opt_obj.default_alpha = _int(options.get('alpha', None)) | ||
wms_opt_obj.resampling = options.get('resampling', None) | ||
wms_opt_obj.scale_auto = options.get('scale_auto', False) | ||
wms_opt_obj.scale_min = _int(options.get('scale_min', None)) | ||
wms_opt_obj.scale_max = _int(options.get('scale_max', None)) | ||
|
||
def _scales(scales): | ||
if scales is None: | ||
return None | ||
if isinstance(scales, basestring): | ||
scales = scales.split(',') | ||
return ",".join(str(int(round(float(v)))) for v in scales) | ||
|
||
wms_opt_obj.bands_scale_min = _scales(options.get('bands_scale_min', None)) | ||
wms_opt_obj.bands_scale_max = _scales(options.get('bands_scale_max', None)) | ||
|
||
return wms_opt_obj |
Oops, something went wrong.