annotate gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java @ 468:7ba4c7222265

Added ij-Index determination for horizontal-cross-sections. gnv-artifacts/trunk@531 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 12 Jan 2010 11:34:25 +0000
parents c7ca2fce041f
children c8089cd7d777
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
447
92b7ccbf6163 Improved generation of iso lines in vertical cross section.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 438
diff changeset
11 import org.apache.log4j.Logger;
92b7ccbf6163 Improved generation of iso lines in vertical cross section.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 438
diff changeset
12
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 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
14
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 * @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
17 */
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 public class Palette
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 implements ValueToIndex
447
92b7ccbf6163 Improved generation of iso lines in vertical cross section.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 438
diff changeset
20 {
92b7ccbf6163 Improved generation of iso lines in vertical cross section.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 438
diff changeset
21 private static Logger log = Logger.getLogger(Palette.class);
92b7ccbf6163 Improved generation of iso lines in vertical cross section.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 438
diff changeset
22
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 public static final class Entry
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 implements Comparable
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 private Entry left;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 private Entry right;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 private int index;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 private double from;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 private double to;
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 private String description;
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 private Color color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 public Entry() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
41 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
42 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
43 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
44 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
45 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
46 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
47 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
48
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 public Entry(
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 int index,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 double from,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 double to,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 Color color,
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 String description
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 ) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 this.index = index;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 this.from = from;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 this.to = to;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 this.color = color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 this.description = description;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 public int compareTo(Object other) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 Entry e = (Entry)other;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 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
66 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
67 return 0;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
448
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
70 public double getFrom() {
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
71 return from;
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
72 }
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
73
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
74 public double getTo() {
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
75 return to;
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
76 }
3cb2bea50456 Generate iso line classes according gnv-issues/issue108
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 447
diff changeset
77
449
c7ca2fce041f Add only color to scale of vertical cross section which are in the chart.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
78 public Color getColor() {
c7ca2fce041f Add only color to scale of vertical cross section which are in the chart.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
79 return color;
c7ca2fce041f Add only color to scale of vertical cross section which are in the chart.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
80 }
c7ca2fce041f Add only color to scale of vertical cross section which are in the chart.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 448
diff changeset
81
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 public Entry locateEntry(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 Entry current = this;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 while (current != null) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 boolean b = value >= current.from;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 if (b && value <= current.to) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 return current;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 current = b
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 ? current.right
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 : current.left;
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 return null;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 public int locate(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 Entry entry = locateEntry(value);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98 return entry != null
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 ? entry.index
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 : -1;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 public Entry findByIndex(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 Entry current = this;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 while (current != null) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 if (current.index == index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 break;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 current = index < current.index
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 ? current.left
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 : current.right;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 return current;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 public String getDescription() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 return description;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 }
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
119
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
120 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
121 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
122 || 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
123 }
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 } // class Entry
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 protected Entry [] entries;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 protected Entry lookup;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 protected Color [] rgbs;
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 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
131 if (lo > hi) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 return null;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 int mid = (lo + hi)/2;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135 Entry entry = entries[mid];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 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
137 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
138 return entry;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 public Palette() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 public Palette(Document document) {
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 NodeList rangeNodes = document.getElementsByTagName("range");
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 entries = new Entry[rangeNodes.getLength()];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 rgbs = new Color[entries.length];
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 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
152 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
153 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
154 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
155 Color color = color(rangeElement.getAttribute("rgb"));
426
3a0c0ad113d9 Load palettes from configuration.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 424
diff changeset
156 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
157 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
158 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
159 rgbs[i] = color;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 buildLookup();
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
165 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
166 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
167 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
168 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
169
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
170 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
171
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
172 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
173 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
174 // 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
175 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
176 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
177
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
178 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
179 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
180
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
181 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
182 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
183 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
184 // 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
185 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
186 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
187 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
188 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
189 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
190 else {
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
191 // 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
192 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
193 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
194 double delta = (to - from)/N;
447
92b7ccbf6163 Improved generation of iso lines in vertical cross section.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 438
diff changeset
195 for (int k = 0; k < N; ++k) {
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
196 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
197 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
198 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
199 from += delta;
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
200 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
201 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
202 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
203 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
204 } // limited interval
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
205 } // 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
206
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
207 buildLookup();
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
208 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
209
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210 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
211 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
212 return 0d;
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
213 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 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
215 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
216 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
218 if (s.startsWith("inf")) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 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
220 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
221
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222 return Double.parseDouble(s);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
224
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
225 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
226 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
227 return null;
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 return Color.decode(s);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
230 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
231
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 protected void buildLookup() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 Arrays.sort(entries);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
235 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
236 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237
435
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
238 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
239 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
240 }
67091b17462d Added code to split palette interval into equal sized parts.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 426
diff changeset
241
424
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
242 public int getSize() {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
243 return rgbs.length;
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 Color getColor(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
247 return rgbs[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 public int indexToRGB(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
251 return rgbs[index].getRGB();
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
252 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
253
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
254 public int toIndex(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
255 return lookup.locate(value);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
256 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
257
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
258 public Entry getEntry(double value) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
259 return lookup.locateEntry(value);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
260 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
261
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
262 public Entry getEntryByIndex(int index) {
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
263 return lookup.findByIndex(index);
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
264 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
265 }
21fbd254db71 Added support for converting 2D rasters into polygons.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
266 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org