diff 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
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/raster/JTSMultiLineStringProducer.java	Tue Jan 19 21:23:28 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/raster/JTSMultiLineStringProducer.java	Tue Jan 19 23:01:24 2010 +0000
@@ -1,23 +1,26 @@
 package de.intevation.gnv.raster;
 
-import java.util.List;
-import java.util.ArrayList;
-
-import org.apache.log4j.Logger;
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryCollection;
+import com.vividsolutions.jts.geom.GeometryFactory;
+import com.vividsolutions.jts.geom.Polygon;
+import com.vividsolutions.jts.geom.LineString;
+import com.vividsolutions.jts.geom.MultiLineString;
+import com.vividsolutions.jts.geom.TopologyException;
 
-import com.vividsolutions.jts.geom.GeometryFactory;
-import com.vividsolutions.jts.geom.MultiLineString;
-import com.vividsolutions.jts.geom.LineString;
-import com.vividsolutions.jts.geom.Coordinate;
-
-import gnu.trove.TIntObjectHashMap;
+import de.intevation.gnv.math.IJKey;
 
 import de.intevation.gnv.raster.Vectorizer.Edge;
 
 import de.intevation.gnv.utils.Pair;
 
-import de.intevation.gnv.math.IJKey;
+import gnu.trove.TIntObjectHashMap;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
 /**
  * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
  */
@@ -29,11 +32,15 @@
 
     protected GeometryFactory geometryFactory;
 
+    protected Polygon clip;
+
     public JTSMultiLineStringProducer(
+        Polygon clip,
         double minX, double minY,
         double maxX, double maxY
     ) {
         this(
+            clip,
             // XXX: Geotools crashes if only using a 2d packed data format!
             JTSMultiPolygonProducer.createDefaultGeometryFactory(3),
             minX, minY,
@@ -41,14 +48,57 @@
     }
 
     public JTSMultiLineStringProducer(
+        Polygon clip,
         GeometryFactory geometryFactory,
         double minX, double minY,
         double maxX, double maxY
     ) {
         super(minX, minY, maxX, maxY);
+        this.clip = clip;
         this.geometryFactory = geometryFactory;
     }
 
+    protected void clipLineString(
+        LineString            lineString,
+        ArrayList<LineString> lineStrings
+    ) {
+        if (clip == null) {
+            lineStrings.add(lineString);
+            return;
+        }
+
+        if (!lineString.intersects(clip)) {
+            return;
+        }
+
+        try {
+            Geometry result = lineString.intersection(clip);
+
+            if (result instanceof LineString) {
+                lineStrings.add((LineString)result);
+            }
+            else if (result instanceof MultiLineString) {
+                MultiLineString mls = (MultiLineString)result;
+                for (int i = 0, N = mls.getNumGeometries(); i < N; ++i) {
+                    Geometry g = mls.getGeometryN(i);
+                    if (g instanceof LineString) {
+                        lineStrings.add((LineString)g);
+                    }
+                    else {
+                        log.warn("cannot handle geometry " + g.getClass());
+                    }
+                }
+            }
+            else {
+                log.warn("cannot handle " + result.getClass());
+            }
+        }
+        catch (TopologyException te) {
+            log.error(te.getLocalizedMessage(), te);
+            lineStrings.add(lineString);
+        }
+    }
+
     public List<Pair<Object, MultiLineString>> getMultiLineStrings(
         AttributeGenerator attributeGenerator
     ) {
@@ -85,10 +135,13 @@
                     coordinates.add(new Coordinate(
                         m1*(head.a % width) + b1,
                         m2*(head.a / width) + b2));
-                    lineStrings.add(
+
+                    clipLineString(
                         geometryFactory.createLineString(
                             coordinates.toArray(
-                                new Coordinate[coordinates.size()])));
+                                new Coordinate[coordinates.size()])),
+                        lineStrings);
+
                     coordinates.clear();
                 }
             }
@@ -112,10 +165,13 @@
                     coordinates.add(new Coordinate(
                         m1*(last.b % width) + b1,
                         m2*(last.b / width) + b2));
-                    lineStrings.add(
+
+                    clipLineString(
                         geometryFactory.createLineString(
                             coordinates.toArray(
-                                new Coordinate[coordinates.size()])));
+                                new Coordinate[coordinates.size()])),
+                        lineStrings);
+
                     coordinates.clear();
                 } // for all in common open
             } // if map defined for key

http://dive4elements.wald.intevation.org