comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/JTSMultiLineStringProducer.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 4080b57dcb52
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
6 import org.apache.log4j.Logger;
7
8 import com.vividsolutions.jts.geom.GeometryFactory;
9 import com.vividsolutions.jts.geom.MultiLineString;
10 import com.vividsolutions.jts.geom.LineString;
11 import com.vividsolutions.jts.geom.Coordinate;
12
13 import gnu.trove.TIntObjectHashMap;
14
15 import de.intevation.gnv.raster.Vectorizer.Edge;
16
17 import de.intevation.gnv.utils.Pair;
18
19 import de.intevation.gnv.math.IJKey;
20
21 /**
22 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
23 */
24 public class JTSMultiLineStringProducer
25 extends IsoProducer
26 {
27 private static Logger log = Logger.getLogger(
28 JTSMultiLineStringProducer.class);
29
30 protected GeometryFactory geometryFactory;
31
32 public JTSMultiLineStringProducer(
33 double minX, double minY,
34 double maxX, double maxY
35 ) {
36 this(
37 JTSMultiPolygonProducer.createDefaultGeometryFactory(),
38 minX, minY,
39 maxX, maxY);
40 }
41
42 public JTSMultiLineStringProducer(
43 GeometryFactory geometryFactory,
44 double minX, double minY,
45 double maxX, double maxY
46 ) {
47 super(minX, minY, maxX, maxY);
48 this.geometryFactory = geometryFactory;
49 }
50
51 public List<Pair<Object, MultiLineString>> getMultiLineStrings(
52 AttributeGenerator attributeGenerator
53 ) {
54 ArrayList<Pair<Object, MultiLineString>> multiLineStrings =
55 new ArrayList<Pair<Object, MultiLineString>>();
56
57 double b1 = minX;
58 double m1 = width != 1
59 ? (maxX - minX)/(width-1)
60 : 0d;
61
62 double b2 = minY;
63 double m2 = height != 1
64 ? (maxY - minY)/(height-1)
65 : 0d;
66
67 ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
68
69 for (IJKey key: joinPairs()) {
70 ArrayList<LineString> lineStrings = new ArrayList<LineString>();
71
72 // process complete
73 ArrayList<Edge> completeList = complete.get(key);
74 if (completeList != null) {
75 for (Edge head: completeList) {
76 Edge current = head;
77 do {
78 coordinates.add(new Coordinate(
79 m1*(current.a % width) + b1,
80 m2*(current.a / width) + b2));
81 }
82 while ((current = current.next) != head);
83 // add head again to close shape
84 coordinates.add(new Coordinate(
85 m1*(head.a % width) + b1,
86 m2*(head.a / width) + b2));
87 lineStrings.add(
88 geometryFactory.createLineString(
89 coordinates.toArray(
90 new Coordinate[coordinates.size()])));
91 coordinates.clear();
92 }
93 }
94
95 // process open
96 TIntObjectHashMap map = commonOpen.get(key);
97
98 if (map != null) {
99 for (Edge head: headList(map)) {
100
101 head = Vectorizer.simplify(head, width);
102 Edge current = head, last = head;
103 do {
104 coordinates.add(new Coordinate(
105 m1*(current.a % width) + b1,
106 m2*(current.a / width) + b2));
107 last = current;
108 }
109 while ((current = current.next) != null);
110 // add b from tail
111 coordinates.add(new Coordinate(
112 m1*(last.b % width) + b1,
113 m2*(last.b / width) + b2));
114 lineStrings.add(
115 geometryFactory.createLineString(
116 coordinates.toArray(
117 new Coordinate[coordinates.size()])));
118 coordinates.clear();
119 } // for all in common open
120 } // if map defined for key
121
122 if (!lineStrings.isEmpty()) {
123 MultiLineString multiLineString =
124 geometryFactory.createMultiLineString(
125 lineStrings.toArray(
126 new LineString[lineStrings.size()]));
127 lineStrings.clear();
128 Object attribute = attributeGenerator != null
129 ? attributeGenerator.generateAttribute(key.i, key.j)
130 : key;
131 multiLineStrings.add(new Pair<Object, MultiLineString>(
132 attribute,
133 multiLineString));
134 }
135 } // for all pairs
136
137 return multiLineStrings;
138 }
139 }
140 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org