Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Pull legends correctly and fix missing legends on discrete types

Taylor Gunnoed663a887ef48
diff --git a/bin/wv-options-colormap b/bin/wv-options-colormap
index 419425305..ffe000a76 100755
--- a/bin/wv-options-colormap
+++ b/bin/wv-options-colormap
@@ -43,35 +43,42 @@ skips = config.get("skipPalettes", [])
def to_list(v):
return v if isinstance(v, list) else [v]
-def get_map_type(entries):
- for entry in entries:
- if entry.get("@transparent", "false") == "true":
- pass
- elif "@value" in entry:
- return "scale"
- else:
- return "class"
- return "special"
-
-def process_entries(entries):
- entries = to_list(entries)
- map_type = get_map_type(entries)
-
+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"])
+ #TODO: Legend checking is all over the place
+ if "Legend" in colormap:
+ map_type = colormap["Legend"]["@type"]
+ legends = to_list(colormap["Legend"]["LegendEntry"])
+ else:
+ map_type = "special"
colors = []
labels = []
values = []
color_format = "{0:02x}{1:02x}{2:02x}{3:02x}"
-
for entry in entries:
r,g,b = entry["@rgb"].split(",")
a = 0 if entry.get("@transparent", "false") == "true" else 255
if a == 0:
continue
colors += [color_format.format(int(r), int(g), int(b), a)]
- if "@label" in entry:
+ if "Legend" in colormap:
+ legend = match_legend(entry, legends)
+ if "@label" in legend:
+ labels += [legend["@label"]]
+ elif "@label" in entry:
+ labels += [entry["@label"]]
+ elif "@label" in entry:
labels += [entry["@label"]]
- if map_type == "scale":
+ if (map_type == "continuous") or (map_type == "discrete"):
items = re.sub(r"[\(\)\[\]]", "", entry["@value"]).split(",")
try:
new_items = []
@@ -88,11 +95,20 @@ def process_entries(entries):
result = {
"type": map_type,
- "colors": colors,
- "labels": labels
+ "entries": {
+ "type": map_type,
+ "colors": colors,
+ "labels": labels
+ },
+ "legend": {
+ "labels": labels,
+ "colors": colors,
+ "type": map_type
+ }
}
- if map_type == "scale":
- result["values"] = values
+ if (map_type == "continuous") or (map_type == "discrete"):
+ result["entries"]["values"] = values
+
return result
@@ -110,28 +126,24 @@ def process_file(file):
colormaps = to_list(document["ColorMaps"]["ColorMap"])
maps = []
for colormap in colormaps:
- result = {}
- entries = process_entries(colormap["Entries"]["ColorMapEntry"])
- if entries["type"] == "special":
+ result = process_entries(colormap)
+ if result["entries"]["type"] == "special":
continue
- result["entries"] = entries
- if "@title" in colormap:
- result["entries"]["title"] = colormap["@title"]
- if "@minLabel" in colormap["Entries"]:
- result["entries"]["minLabel"] = colormap["Entries"]["@minLabel"]
- if "@maxLabel" in colormap["Entries"]:
- result["entries"]["maxLabel"] = colormap["Entries"]["@maxLabel"]
- if "Legend" in colormap:
- legend = process_entries(colormap["Legend"]["LegendEntry"])
- result["legend"] = legend
- result["legend"]["type"] = colormap["Legend"]["@type"]
- result["entries"]["type"] = result["legend"]["type"]
- if "@title" in colormap:
- result["legend"]["title"] = colormap["@title"]
- if "@minLabel" in colormap["Legend"]:
- result["legend"]["minLabel"] = colormap["Legend"]["@minLabel"]
- if "@maxLabel" in colormap["Legend"]:
- result["legend"]["maxLabel"] = colormap["Legend"]["@maxLabel"]
+ result["title"] = colormap["@title"]
+ result["entries"]["title"] = colormap["@title"]
+ result["legend"]["title"] = colormap["@title"]
+ if ("Legend" in colormap) and ("@minLabel" in colormap["Legend"]):
+ result["legend"]["minLabel"] = colormap["Legend"]["@minLabel"]
+ result["entries"]["minLabel"] = colormap["Legend"]["@minLabel"]
+ elif "@minLabel" in colormap["Entries"]:
+ result["legend"]["minLabel"] = colormap["Entries"]["@minLabel"]
+ if ("Legend" in colormap) and ("@maxLabel" in colormap["Legend"]):
+ result["legend"]["maxLabel"] = colormap["Legend"]["@maxLabel"]
+ result["entries"]["maxLabel"] = colormap["Legend"]["@maxLabel"]
+ elif "@maxLabel" in colormap["Entries"]:
+ result["legend"]["maxLabel"] = colormap["Entries"]["@maxLabel"]
+ if "@units" in colormap:
+ result["legend"]["units"] = colormap["@units"]
maps += [result]
data = {
diff --git a/web/js/layers/wv.layers.options.js b/web/js/layers/wv.layers.options.js
index ed1b53a6b..8c0a08313 100644
--- a/web/js/layers/wv.layers.options.js
+++ b/web/js/layers/wv.layers.options.js
@@ -43,12 +43,15 @@ wv.layers.options = wv.layers.options || function(config, models, layer) {
renderOpacity($dialog);
if ( config.features.customPalettes ) {
- if ( models.palettes.allowed(layer.id) && models.palettes.getLegends(layer.id).length < 2 ) {
+ if ( models.palettes.allowed(layer.id) &&
+ (models.palettes.getLegends(layer.id).length < 2) &&
+ (models.palettes.get(layer.id).legend.type !== "classification") ) {
if ( models.palettes.getLegends(layer.id).length > 1 ) {
renderLegendButtons($dialog);
}
var legend = models.palettes.getLegend(layer.id, index);
- if ( legend.type === "continuous" ) {
+ if (( legend.type === "continuous" ) ||
+ (legend.type === "discrete") ){
renderRange($dialog);
}
renderPaletteSelector($dialog);
@@ -398,7 +401,7 @@ wv.layers.options = wv.layers.options || function(config, models, layer) {
var defaultLegend = function() {
var legend = models.palettes.getDefaultLegend(layer.id, index);
- if ( legend.type === "continuous" ) {
+ if ( ( legend.type === "continuous" ) || (legend.type === "discrete") ){
return selectorItemScale(legend.colors, "__default", "Default");
} else {
return selectorItemSingle(legend, "__default", "Default");
@@ -410,12 +413,14 @@ wv.layers.options = wv.layers.options || function(config, models, layer) {
var target = models.palettes.getCustom(id);
var targetType = ( target.colors.length === 1 ) ? "single": "continuous";
- if ( source.type === "continuous" && targetType === "continuous" ) {
+ if ( ( source.type === "continuous" && targetType === "continuous" ) ||
+ ( source.type === "discrete" && targetType === "discrete" ) ){
var translated = wv.palettes.translate(source.colors,
target.colors);
return selectorItemScale(translated, id, target.name);
}
- if ( source.type === "continuous" && targetType === "continuous" ) {
+ if ( ( source.type === "continuous" && targetType === "continuous" ) ||
+ ( source.type === "discrete" && targetType === "discrete" ) ){
return selectorItemSingle(target, id, target.name);
}
};
diff --git a/web/js/palettes/wv.palettes.legend.js b/web/js/palettes/wv.palettes.legend.js
index e13b1154e..605f6e85f 100644
--- a/web/js/palettes/wv.palettes.legend.js
+++ b/web/js/palettes/wv.palettes.legend.js
@@ -56,7 +56,8 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {
$parent.append($legendPanel);
var legends = model.getLegends(layer.id);
_.each(legends, function(legend, index) {
- if ( legend.type === "continuous" ) {
+ if ( (legend.type === "continuous") ||
+ (legend.type === "discrete") ) {
renderScale($legendPanel, legend, index);
}
if ( legend.type === "classification" ) {
@@ -160,7 +161,8 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {
}
var legends = model.getLegends(layer.id);
_.each(legends, function(legend, index) {
- if ( legend.type === "continuous" ) {
+ if ( (legend.type === "continuous") ||
+ (legend.type === "discrete") ) {
wv.palettes.colorbar(selector + " " +
"[data-index='" + index + "'] canvas", legend.colors);
showUnitRange(index);
@@ -177,8 +179,8 @@ wv.palettes.legend = wv.palettes.legend || function(spec) {
var legends = model.getLegends(layer.id, index);
var entries = model.get(layer.id, index).entries;
_.each(legends, function(legend, index) {
- var min = entries.minLabel || _.first(entries.labels);
- var max = entries.maxLabel || _.last(entries.labels);
+ var min = legend.minLabel || _.first(entries.labels);
+ var max = legend.maxLabel || _.last(entries.labels);
$(selector + " [data-index='" + index + "'] .wv-palettes-min").html(min);
$(selector + " [data-index='" + index + "'] .wv-palettes-max").html(max);
var title = legend.title || "&nbsp;";