comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/JTSMultiLineStringProducer.java @ 499:e065a72f6b62

Clip "Horizontalschnitte" against given clipping polygon. gnv-artifacts/trunk@579 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 19 Jan 2010 23:01:24 +0000
parents 4080b57dcb52
children 70adafe2b9d5
comparison
equal deleted inserted replaced
498:4080b57dcb52 499:e065a72f6b62
1 package de.intevation.gnv.raster; 1 package de.intevation.gnv.raster;
2 2
3 import java.util.List; 3 import com.vividsolutions.jts.geom.Coordinate;
4 import java.util.ArrayList; 4 import com.vividsolutions.jts.geom.Geometry;
5 import com.vividsolutions.jts.geom.GeometryCollection;
6 import com.vividsolutions.jts.geom.GeometryFactory;
7 import com.vividsolutions.jts.geom.Polygon;
8 import com.vividsolutions.jts.geom.LineString;
9 import com.vividsolutions.jts.geom.MultiLineString;
10 import com.vividsolutions.jts.geom.TopologyException;
5 11
6 import org.apache.log4j.Logger; 12 import de.intevation.gnv.math.IJKey;
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 13
15 import de.intevation.gnv.raster.Vectorizer.Edge; 14 import de.intevation.gnv.raster.Vectorizer.Edge;
16 15
17 import de.intevation.gnv.utils.Pair; 16 import de.intevation.gnv.utils.Pair;
18 17
19 import de.intevation.gnv.math.IJKey; 18 import gnu.trove.TIntObjectHashMap;
20 19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.apache.log4j.Logger;
21 /** 24 /**
22 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) 25 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
23 */ 26 */
24 public class JTSMultiLineStringProducer 27 public class JTSMultiLineStringProducer
25 extends IsoProducer 28 extends IsoProducer
27 private static Logger log = Logger.getLogger( 30 private static Logger log = Logger.getLogger(
28 JTSMultiLineStringProducer.class); 31 JTSMultiLineStringProducer.class);
29 32
30 protected GeometryFactory geometryFactory; 33 protected GeometryFactory geometryFactory;
31 34
35 protected Polygon clip;
36
32 public JTSMultiLineStringProducer( 37 public JTSMultiLineStringProducer(
38 Polygon clip,
33 double minX, double minY, 39 double minX, double minY,
34 double maxX, double maxY 40 double maxX, double maxY
35 ) { 41 ) {
36 this( 42 this(
43 clip,
37 // XXX: Geotools crashes if only using a 2d packed data format! 44 // XXX: Geotools crashes if only using a 2d packed data format!
38 JTSMultiPolygonProducer.createDefaultGeometryFactory(3), 45 JTSMultiPolygonProducer.createDefaultGeometryFactory(3),
39 minX, minY, 46 minX, minY,
40 maxX, maxY); 47 maxX, maxY);
41 } 48 }
42 49
43 public JTSMultiLineStringProducer( 50 public JTSMultiLineStringProducer(
51 Polygon clip,
44 GeometryFactory geometryFactory, 52 GeometryFactory geometryFactory,
45 double minX, double minY, 53 double minX, double minY,
46 double maxX, double maxY 54 double maxX, double maxY
47 ) { 55 ) {
48 super(minX, minY, maxX, maxY); 56 super(minX, minY, maxX, maxY);
57 this.clip = clip;
49 this.geometryFactory = geometryFactory; 58 this.geometryFactory = geometryFactory;
59 }
60
61 protected void clipLineString(
62 LineString lineString,
63 ArrayList<LineString> lineStrings
64 ) {
65 if (clip == null) {
66 lineStrings.add(lineString);
67 return;
68 }
69
70 if (!lineString.intersects(clip)) {
71 return;
72 }
73
74 try {
75 Geometry result = lineString.intersection(clip);
76
77 if (result instanceof LineString) {
78 lineStrings.add((LineString)result);
79 }
80 else if (result instanceof MultiLineString) {
81 MultiLineString mls = (MultiLineString)result;
82 for (int i = 0, N = mls.getNumGeometries(); i < N; ++i) {
83 Geometry g = mls.getGeometryN(i);
84 if (g instanceof LineString) {
85 lineStrings.add((LineString)g);
86 }
87 else {
88 log.warn("cannot handle geometry " + g.getClass());
89 }
90 }
91 }
92 else {
93 log.warn("cannot handle " + result.getClass());
94 }
95 }
96 catch (TopologyException te) {
97 log.error(te.getLocalizedMessage(), te);
98 lineStrings.add(lineString);
99 }
50 } 100 }
51 101
52 public List<Pair<Object, MultiLineString>> getMultiLineStrings( 102 public List<Pair<Object, MultiLineString>> getMultiLineStrings(
53 AttributeGenerator attributeGenerator 103 AttributeGenerator attributeGenerator
54 ) { 104 ) {
83 while ((current = current.next) != head); 133 while ((current = current.next) != head);
84 // add head again to close shape 134 // add head again to close shape
85 coordinates.add(new Coordinate( 135 coordinates.add(new Coordinate(
86 m1*(head.a % width) + b1, 136 m1*(head.a % width) + b1,
87 m2*(head.a / width) + b2)); 137 m2*(head.a / width) + b2));
88 lineStrings.add( 138
139 clipLineString(
89 geometryFactory.createLineString( 140 geometryFactory.createLineString(
90 coordinates.toArray( 141 coordinates.toArray(
91 new Coordinate[coordinates.size()]))); 142 new Coordinate[coordinates.size()])),
143 lineStrings);
144
92 coordinates.clear(); 145 coordinates.clear();
93 } 146 }
94 } 147 }
95 148
96 // process open 149 // process open
110 while ((current = current.next) != null); 163 while ((current = current.next) != null);
111 // add b from tail 164 // add b from tail
112 coordinates.add(new Coordinate( 165 coordinates.add(new Coordinate(
113 m1*(last.b % width) + b1, 166 m1*(last.b % width) + b1,
114 m2*(last.b / width) + b2)); 167 m2*(last.b / width) + b2));
115 lineStrings.add( 168
169 clipLineString(
116 geometryFactory.createLineString( 170 geometryFactory.createLineString(
117 coordinates.toArray( 171 coordinates.toArray(
118 new Coordinate[coordinates.size()]))); 172 new Coordinate[coordinates.size()])),
173 lineStrings);
174
119 coordinates.clear(); 175 coordinates.clear();
120 } // for all in common open 176 } // for all in common open
121 } // if map defined for key 177 } // if map defined for key
122 178
123 if (!lineStrings.isEmpty()) { 179 if (!lineStrings.isEmpty()) {

http://dive4elements.wald.intevation.org