comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/IsoProducer.java @ 465:f7038820df2e

Added support to trace rasters to JTS multi polygons and multi line strings. Write them to shape files with GeoTools. gnv-artifacts/trunk@526 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 Jan 2010 00:29:45 +0000
parents
children 9a828e5a2390
comparison
equal deleted inserted replaced
464:70df44021a9f 465:f7038820df2e
1 package de.intevation.gnv.raster;
2
3 import java.util.List;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.HashSet;
7
8 import gnu.trove.TIntObjectHashMap;
9 import gnu.trove.TIntHashSet;
10 import gnu.trove.TObjectProcedure;
11
12 import de.intevation.gnv.math.IJKey;
13
14 import de.intevation.gnv.raster.Vectorizer.Edge;
15
16 /**
17 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
18 */
19 public class IsoProducer
20 extends AbstractProducer
21 {
22 public interface AttributeGenerator {
23
24 Object generateAttribute(int neighbor1, int neighbor2);
25
26 } // interface AttributeGenerator
27
28 protected HashMap<Edge, Integer> open;
29 protected HashMap<IJKey, TIntObjectHashMap> commonOpen;
30 protected HashMap<IJKey, ArrayList<Edge>> complete;
31
32 protected int width;
33 protected int height;
34
35 public IsoProducer(
36 double minX, double minY,
37 double maxX, double maxY
38 ) {
39 super(minX, minY, maxX, maxY);
40
41 open = new HashMap<Edge, Integer>();
42 commonOpen = new HashMap<IJKey, TIntObjectHashMap>();
43 complete = new HashMap<IJKey, ArrayList<Edge>>();
44 }
45
46 public void handleRings(
47 List<Edge> rings,
48 int value,
49 int width,
50 int height
51 ) {
52 if (value == -1) {
53 return;
54 }
55 this.width = width;
56 this.height = height;
57
58 Integer v = Integer.valueOf(value);
59
60 for (Edge head: rings) {
61 Edge current = head;
62 do {
63 Integer neighbor = open.remove(current);
64
65 if (neighbor != null) {
66 IJKey pair = new IJKey(value, neighbor.intValue());
67 pair.sort();
68
69 TIntObjectHashMap co = commonOpen.get(pair);
70
71 if (co == null) {
72 commonOpen.put(pair, co = new TIntObjectHashMap());
73 }
74
75 Edge edge = new Edge(current);
76
77 Edge otherA = (Edge)co.remove(edge.a);
78 if (otherA != null) {
79 otherA.chain(edge, edge.a);
80 }
81
82 Edge otherB = (Edge)co.remove(edge.b);
83 if (otherB != null) {
84 otherB.chain(edge, edge.b);
85 }
86
87 if (edge.isComplete()) {
88 ArrayList list = complete.get(pair);
89 if (list == null) {
90 complete.put(pair, list = new ArrayList());
91 }
92 list.add(Vectorizer.simplify(edge, width));
93 }
94 else {
95 if (otherA == null) {
96 co.put(edge.a, edge);
97 }
98 if (otherB == null) {
99 co.put(edge.b, edge);
100 }
101 }
102 }
103 else {
104 Edge edge = new Edge(current.b, current.a);
105 open.put(edge, v);
106 }
107
108 current = current.next;
109 }
110 while (current != head);
111 } // for all rings
112
113 } // handleRings
114
115 protected HashSet<IJKey> joinPairs() {
116 HashSet<IJKey> pairs = new HashSet<IJKey>();
117 pairs.addAll(complete .keySet());
118 pairs.addAll(commonOpen.keySet());
119 return pairs;
120 }
121
122 protected static ArrayList<Edge> headList(TIntObjectHashMap map) {
123 final ArrayList<Edge> headList = new ArrayList<Edge>();
124 map.forEachValue(new TObjectProcedure() {
125 TIntHashSet headSet = new TIntHashSet();
126 public boolean execute(Object value) {
127 Edge head = ((Edge)value).head();
128 if (headSet.add(head.a)) {
129 headList.add(head);
130 }
131 return true;
132 }
133 });
134 return headList;
135 }
136
137 public void clear() {
138 open.clear();
139 complete.clear();
140 commonOpen.clear();
141 }
142 }
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org