Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

added id to colormap legend objs

Benjamin38bf6d116c55
diff --git a/bin/wv-options-colormap b/bin/wv-options-colormap
index 90c22ff1e..3acf8c07b 100755
--- a/bin/wv-options-colormap
+++ b/bin/wv-options-colormap
@@ -19,92 +19,125 @@ import xmltodict
prog = os.path.basename(__file__)
base_dir = os.path.join(os.path.dirname(__file__), "..")
-version = "1.0.0"
+version = "3.0.0"
help_description = """\
Converts colormaps to JSON files
"""
-parser = OptionParser(usage="Usage: %s <input_dir> <output_dir>" % prog,
+parser = OptionParser(usage="Usage: %s <config_file> <input_dir> <output_dir>" % prog,
version="%s version %s" % (prog, version),
epilog=help_description)
(options, args) = parser.parse_args()
-if len(args) != 2:
+if len(args) != 3:
parser.error("Invalid number of arguments")
-input_dir = args[0]
-output_dir = args[1]
-
-def process_file(file):
- input_file = os.path.join(root, file)
- with open(input_file) as fp:
- xml = fp.read()
- colormap = xmltodict.parse(xml)
-
- scaleColors = []
- scaleLabels = []
- scaleValues = []
- classColors = []
- classLabels = []
- specialColors = []
- specialLabels = []
- units = None
- minValue = float("inf")
- maxValue = float("-inf")
-
- entries = colormap["ColorMap"]["ColorMapEntry"]
- if not isinstance(entries, list):
- entries = [entries]
-
+config_file = args[0]
+input_dir = args[1]
+output_dir = args[2]
+
+with open(config_file) as fp:
+ config = json.load(fp)
+skips = config.get("skipPalettes", [])
+
+def to_list(v):
+ return v if isinstance(v, list) else [v]
+
+def match_legend(entry, legends):
+ try:
+ for legend in legends:
+ if legend["@id"] == entry["@ref"]:
+ return legend
+ except ValueError as e:
+ raise ValueError("Invalid reference: %s" % entry["@ref"])
+
+def process_entries(colormap):
+ entries = to_list(colormap["Entries"]["ColorMapEntry"])
+ map_type = colormap["Legend"]["@type"]
+ legends = to_list(colormap["Legend"]["LegendEntry"])
+ colors = []
+ values = []
+ tooltips = []
+ transparent_map = "true"
+ for entry in entries:
+ if entry["@transparent"] == "false":
+ transparent_map = "false"
+ if (transparent_map == "true"):
+ return "transparent"
+ color_format = "{0:02x}{1:02x}{2:02x}{3:02x}"
for entry in entries:
r,g,b = entry["@rgb"].split(",")
- a = 255
- if "@transparent" in entry and entry["@transparent"] == "true":
- a = 0
- color = "{0:02x}{1:02x}{2:02x}{3:02x}".format(int(r), int(g), int(b), a)
- label = entry["@label"]
+ a = 0 if entry.get("@transparent", "false") == "true" else 255
if a == 0:
- specialColors += [color]
- specialLabels += [label]
- elif "@value" in entry:
+ continue
+ colors += [color_format.format(int(r), int(g), int(b), a)]
+ legend = match_legend(entry, legends)
+ tooltips += [legend["@tooltip"]]
+ if (map_type == "continuous") or (map_type == "discrete"):
items = re.sub(r"[\(\)\[\]]", "", entry["@value"]).split(",")
try:
- for scaleValue in items:
- v = float(scaleValue)
+ new_items = []
+ for item in items:
+ v = float(item)
if v == float("inf"):
v = sys.float_info.max
if v == float("-inf"):
v = sys.float_info.min
- scaleValues += [v]
+ new_items += [v]
+ values += [new_items]
except ValueError as e:
raise ValueError("Invalid value: %s" % entry["@value"])
- scaleColors += [color]
- scaleLabels += [label]
- else:
- classColors += [color]
- classLabels += [label]
- id = os.path.splitext(os.path.basename(input_file))[0]
- data = {}
- if len(scaleColors) > 0:
- data["scale"] = {
- "colors": scaleColors,
- "values": scaleValues,
- "labels": scaleLabels,
- }
- if len(specialColors) > 0:
- data["special"] = {
- "colors": specialColors,
- "labels": specialLabels
- }
- if len(classColors) > 0:
- data["classes"] = {
- "colors": classColors,
- "labels": classLabels,
+ result = {
+ "type": map_type,
+ "entries": {
+ "type": map_type,
+ "colors": colors,
+ },
+ "legend": {
+ "tooltips": tooltips,
+ "colors": colors,
+ "type": map_type
}
+ }
+ if (map_type == "continuous") or (map_type == "discrete"):
+ result["entries"]["values"] = values
- data["id"] = id
+ return result
+
+def process_file(file):
+ input_file = os.path.join(root, file)
+ id = os.path.splitext(os.path.basename(input_file))[0]
+ if id in skips:
+ sys.stderr.write("%s: WARN: [%s] %s\n" % (prog, input_file,
+ "Skipping"))
+ return
+ with open(input_file) as fp:
+ xml = fp.read()
+ document = xmltodict.parse(xml)
+ colormaps = to_list(document["ColorMaps"]["ColorMap"])
+ maps = []
+ for colormap in colormaps:
+ result = process_entries(colormap)
+ if result == "transparent":
+ continue
+ result["title"] = colormap["@title"]
+ result["entries"]["title"] = colormap["@title"]
+ result["legend"]["title"] = colormap["@title"]
+ result["legend"]["id"] = id + "_" + str(len(maps)) + "_legend"
+ if "@minLabel" in colormap["Legend"]:
+ result["legend"]["minLabel"] = colormap["Legend"]["@minLabel"]
+ if "@maxLabel" in colormap["Legend"]:
+ result["legend"]["maxLabel"] = colormap["Legend"]["@maxLabel"]
+ if "@units" in colormap:
+ result["legend"]["units"] = colormap["@units"]
+ maps += [result]
+
+ data = {
+ "id": id,
+ "maps": maps
+ }
json_options = {}
json_options["indent"] = 4
json_options["separators"] = (',', ': ')
diff --git a/web/js/palettes/wv.palettes.model.js b/web/js/palettes/wv.palettes.model.js
index 1fa8f3715..afcfe97c8 100644
--- a/web/js/palettes/wv.palettes.model.js
+++ b/web/js/palettes/wv.palettes.model.js
@@ -465,7 +465,8 @@ wv.palettes.model = wv.palettes.model || function(models, config) {
tooltips: oldLegend.tooltips,
units: oldLegend.units,
type: entries.type,
- title: entries.title
+ title: entries.title,
+ id: oldLegend.id
};
var source = entries.colors;
var values = entries.values;