annotate gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java @ 440:eb2ac62e853a

Cleaned up messy code a bit in "Horizontales Schnittprofil" and "Profilschnitt". gnv-artifacts/trunk@488 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 28 Dec 2009 17:13:12 +0000
parents 7399bb8f83ea
children 92b7ccbf6163
rev   line source
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.gnv.raster;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import org.w3c.dom.Document;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import org.w3c.dom.Element;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import org.w3c.dom.NodeList;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.util.Arrays;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import java.awt.Color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import de.intevation.gnv.raster.Raster.ValueToIndex;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 /**
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 */
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 public class Palette
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 implements ValueToIndex
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 public static final class Entry
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 implements Comparable
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 private Entry left;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 private Entry right;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 private int index;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 private double from;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 private double to;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 private String description;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 private Color color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 public Entry() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
37 public Entry(Entry other) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
38 index = other.index;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
39 from = other.from;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
40 to = other.to;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
41 description = other.description;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
42 color = other.color;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
43 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
44
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 public Entry(
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 int index,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 double from,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 double to,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 Color color,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 String description
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 ) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 this.index = index;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 this.from = from;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 this.to = to;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 this.color = color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 this.description = description;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 public int compareTo(Object other) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 Entry e = (Entry)other;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 if (from < e.from) return -1;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 if (from > e.from) return +1;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 return 0;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 public Entry locateEntry(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 Entry current = this;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 while (current != null) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 boolean b = value >= current.from;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 if (b && value <= current.to) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 return current;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 current = b
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 ? current.right
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 : current.left;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 return null;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 public int locate(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 Entry entry = locateEntry(value);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 return entry != null
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 ? entry.index
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 : -1;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 public Entry findByIndex(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 Entry current = this;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 while (current != null) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 if (current.index == index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 break;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 current = index < current.index
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 ? current.left
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 : current.right;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 return current;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 public String getDescription() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 return description;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102 }
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
103
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
104 public boolean isInfinity() {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
105 return from == -Double.MAX_VALUE
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
106 || to == Double.MAX_VALUE;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
107 }
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 } // class Entry
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 protected Entry [] entries;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 protected Entry lookup;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 protected Color [] rgbs;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 private Entry buildLookup(Entry [] entries, int lo, int hi) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 if (lo > hi) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 return null;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 int mid = (lo + hi)/2;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 Entry entry = entries[mid];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 entry.left = buildLookup(entries, lo, mid-1);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 entry.right = buildLookup(entries, mid+1, hi);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 return entry;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 public Palette() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 public Palette(Document document) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 NodeList rangeNodes = document.getElementsByTagName("range");
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 entries = new Entry[rangeNodes.getLength()];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 rgbs = new Color[entries.length];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 for (int i = 0; i < entries.length; ++i) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 Element rangeElement = (Element)rangeNodes.item(i);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 double from = doubleValue(rangeElement.getAttribute("from"));
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 double to = doubleValue(rangeElement.getAttribute("to"));
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 Color color = color(rangeElement.getAttribute("rgb"));
426
3a0c0ad113d9 Load palettes from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 424
diff changeset
140 String desc = rangeElement.getAttribute("description");
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 if (from > to) { double t = from; from = to; to = t; }
426
3a0c0ad113d9 Load palettes from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 424
diff changeset
142 entries[i] = new Entry(i, from, to, color, desc);
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 rgbs[i] = color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 buildLookup();
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
149 public Palette(Palette original, int N) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
150 if (N < 2) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
151 throw new IllegalArgumentException("N < 2");
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
152 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
153
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
154 Entry [] origEntries = original.entries;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
155
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
156 int newSize = 0;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
157 for (int i = 0; i < origEntries.length; ++i) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
158 // cannot split infinity intervals
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
159 newSize += origEntries[i].isInfinity() ? 1 : N;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
160 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
161
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
162 entries = new Entry[newSize];
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
163 rgbs = new Color[newSize];
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
164
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
165 for (int i = 0, j = 0; i < origEntries.length; ++i) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
166 Entry origEntry = origEntries[i];
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
167 if (origEntry.isInfinity()) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
168 // infinity intervals are just copied
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
169 Entry nEntry = new Entry(origEntry);
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
170 entries[j] = nEntry;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
171 rgbs[j] = nEntry.color;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
172 nEntry.index = j++;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
173 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
174 else {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
175 // split interval into N parts
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
176 double from = origEntry.from;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
177 double to = origEntry.to;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
178 double delta = (to - from)/N;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
179 while (from < to) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
180 Entry nEntry = new Entry(origEntry);
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
181 nEntry.from = from;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
182 nEntry.to = from + delta;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
183 from += delta;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
184 entries[j] = nEntry;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
185 rgbs[j] = nEntry.color;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
186 nEntry.index = j++;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
187 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
188 } // limited interval
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
189 } // for all original entries
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
190
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
191 buildLookup();
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
192 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
193
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 private static final double doubleValue(String s) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 if (s == null || (s = s.trim()).length() == 0) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
196 return 0d;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
197 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
198 if ((s = s.toLowerCase()).startsWith("-inf")) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
199 return -Double.MAX_VALUE; // XXX: Not using Double.NEGATIVE_INFINITY!
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
202 if (s.startsWith("inf")) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
203 return Double.MAX_VALUE; // XXX: Not using Double.NEGATIVE_INFINITY!
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 return Double.parseDouble(s);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 private static final Color color(String s) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 if (s == null || (s = s.trim()).length() == 0) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 return null;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
213 return Color.decode(s);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217 protected void buildLookup() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
218 Arrays.sort(entries);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 lookup = buildLookup(entries, 0, entries.length-1);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
222 public Palette subdivide(int N) {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
223 return new Palette(this, N);
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
224 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
225
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
226 public int getSize() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
227 return rgbs.length;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
228 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
229
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
230 public Color getColor(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
231 return rgbs[index];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
232 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 public int indexToRGB(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
235 return rgbs[index].getRGB();
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
236 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
238 public int toIndex(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
239 return lookup.locate(value);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
240 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
241
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
242 public Entry getEntry(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
243 return lookup.locateEntry(value);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
244 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
245
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
246 public Entry getEntryByIndex(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
247 return lookup.findByIndex(index);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
248 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
249 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
250 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org