Revision 13329

View differences:

trunk/libraries/libTopology/src/org/gvsig/topology/TopologyError.java
1
package org.gvsig.topology;
2

  
3
import com.hardcode.gdbms.engine.values.Value;
4
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
5
import com.iver.cit.gvsig.fmap.core.IFeature;
6
import com.iver.cit.gvsig.fmap.core.IGeometry;
7
import com.iver.cit.gvsig.fmap.core.IRow;
8

  
9

  
10
/**
11
 *Error produced when one or many features ?
12
 *violates a topology rule.
13
 */
14
public class TopologyError extends DefaultFeature {
15
 
16
	/**
17
	 *rule which has been violated
18
	 */
19
	private AbstractTopologyRule violatedRule;
20
	 
21
	/**
22
	 *features of the source layer that
23
	 *violates the rule (if the rule is a self-rule,
24
	 *the error will only have source layer features)
25
	 */
26
	private IFeature[] sourceLyrFeatures;
27
	 
28
	/**
29
	 *features of the destination layer
30
	 *that violate the rule (only in rules between
31
	 *two layers)
32
	 */
33
	private IFeature[] destinationLyrFeatures;
34
	 
35
	private boolean exception;
36
	
37
	public TopologyError(){
38
		//FIXME Change the constructor of DefaultFeature
39
		super((IGeometry)null, (Value[])null,(String) null);
40
	}
41
	 
42
	public void setViolatedRule(AbstractTopologyRule violatedRule) {
43
	}
44
	 
45
	public AbstractTopologyRule getViolatedRule() {
46
		return null;
47
	}
48
	 
49
	public void setSourceLyrFeatures(IFeature[] sourceLyrFeatures) {
50
	}
51
	 
52
	public IFeature[] getSourceLyrFeatures() {
53
		return null;
54
	}
55
	 
56
	public void setDestinationLyrFeatures(IFeature[] destinationLyrFeatures) {
57
	}
58
	 
59
	public IFeature[] getDestinationLyrFeatures() {
60
		return null;
61
	}
62
	 
63
	/**
64
	 *Ruturns the type of geometry of the error
65
	 */
66
	public int getShapeType() {
67
		return 0;
68
	}
69
	 
70
	public void setException(boolean exception) {
71
	}
72
	 
73
	public boolean isException() {
74
		return false;
75
	}
76

  
77
	public IGeometry getGeometry() {
78
		// TODO Auto-generated method stub
79
		return null;
80
	}
81

  
82
	public void setGeometry(IGeometry geom) {
83
		// TODO Auto-generated method stub
84
		
85
	}
86

  
87
	public IRow cloneRow() {
88
		// TODO Auto-generated method stub
89
		return null;
90
	}
91

  
92
	public String getID() {
93
		// TODO Auto-generated method stub
94
		return null;
95
	}
96

  
97
	public void setID(String ID) {
98
		// TODO Auto-generated method stub
99
		
100
	}
101
	 
102
}
103
 
0 104

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/LinesMustNotHavePseudonodes.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *This rule checks that lines of a line layer dont have
9
 *pseudonodes ends (a point that only touch another line)
10
 */
11
public class LinesMustNotHavePseudonodes extends AbstractTopologyRule implements IOneLyrRule {
12
 
13
}
14
 
0 15

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/LineMustNotHaveDangles.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *All end points of a line must touch at least another line.
9
 *(if only touch one line, its a pseudonode, not a dangle)
10
 *This rule is checked for the two ends of a line.
11
 *
12
 */
13
public class LineMustNotHaveDangles extends AbstractTopologyRule implements IOneLyrRule {
14
 
15
}
16
 
0 17

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/MustNotHaveRepeatedPoints.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *This rule checks that a geometry (line or polygon)
9
 *dont have consecutive repeated points)
10
 *
11
 */
12
public class MustNotHaveRepeatedPoints extends AbstractTopologyRule implements IOneLyrRule {
13
 
14
}
15
 
0 16

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/MustBeLargerThanClusterTolerance.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.IOneLyrRule;
4
import org.gvsig.topology.AbstractTopologyRule;
5
import org.gvsig.topology.Topology;
6

  
7

  
8
/**
9
 *For lines o polygons. The lenght of a line or the perimeter
10
 *of a polygon must be larger than the cluster tolerance
11
 *
12
 */
13
public class MustBeLargerThanClusterTolerance extends AbstractTopologyRule implements IOneLyrRule {
14
 
15
	private Topology topology;
16
	 
17
}
18
 
0 19

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/JtsValidRule.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *Any validated geometry must be a right JTS geometry.
9
 */
10
public class JtsValidRule extends AbstractTopologyRule implements IOneLyrRule {
11
 
12
}
13
 
0 14

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/LineMustNotSelfIntersect.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *Lines of a layer must not have self intersections.
9
 *JTS allows this. This is one of the restrictions that must
10
 *not have pseudonodos checks.
11
 *
12
 */
13
public class LineMustNotSelfIntersect extends AbstractTopologyRule implements IOneLyrRule {
14
 
15
}
16
 
0 17

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/PolygonMustNotSelfIntersect.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *The polygons of a layer must not have self intersections
9
 *
10
 */
11
public class PolygonMustNotSelfIntersect extends AbstractTopologyRule implements IOneLyrRule {
12
 
13
}
14
 
0 15

  
trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/PolygonsMustNotOverlap.java
1
package org.gvsig.topology.topologyrules;
2

  
3
import org.gvsig.topology.AbstractTopologyRule;
4
import org.gvsig.topology.IOneLyrRule;
5

  
6

  
7
/**
8
 *The polygons of a given layer must not overlaps
9
 *each other (their intersection must be a line, not an area)
10
 *
11
 */
12
public class PolygonsMustNotOverlap extends AbstractTopologyRule implements IOneLyrRule {
13
 
14
}
15
 
0 16

  
trunk/libraries/libTopology/src/org/gvsig/topology/AbstractTopologyRule.java
1
package org.gvsig.topology;
2

  
3
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
4
import com.iver.cit.gvsig.fmap.core.IFeature;
5
import java.awt.geom.Rectangle2D;
6
import java.util.Collection;
7

  
8
public abstract class AbstractTopologyRule implements ITopologyRule {
9
 
10
	private FLyrVect originLyr;
11
	 
12
	private FLyrVect destinationLyr;
13
	 
14
	private Topology topology;
15
	 
16
	public void setOriginLyr(FLyrVect originLyr) {
17
	}
18
	 
19
	public FLyrVect getOriginLyr() {
20
		return null;
21
	}
22
	 
23
	public void setDestinationLyr(FLyrVect destinationLyr) {
24
	}
25
	 
26
	public FLyrVect getDestinationLyr() {
27
		return null;
28
	}
29
	 
30
	public String getName() {
31
		return null;
32
	}
33
	 
34
	/**
35
	 *Checks if the rule's parameters 
36
	 *(sourceLyr, destinationLyr) verify 
37
	 *rule preconditions (geometry type, etc.)
38
	 */
39
	public void checkPreconditions() {
40
	}
41
	 
42
	public void checkRule() {
43
	}
44
	 
45
	void validateFeature(IFeature feature) {
46
	}
47
	 
48
	public void checkRule(Rectangle2D rect) {
49
	}
50
	 
51
	public Collection getTopologyErrors() {
52
		return null;
53
	}
54
	 
55
}
56
 
0 57

  
trunk/libraries/libTopology/src/org/gvsig/topology/IOneLyrRule.java
1
package org.gvsig.topology;
2

  
3

  
4
/**
5
 *Marker interface for all rules that applies to only one layer
6
 *
7
 */
8
public interface IOneLyrRule extends ITopologyRule {
9
 
10
}
11
 
0 12

  
trunk/libraries/libTopology/src/org/gvsig/topology/ITopologyStatus.java
1
package org.gvsig.topology;
2

  
3
public interface ITopologyStatus {
4
 
5
	public static final byte VALIDATED = 0;
6
	 
7
	public static final byte VALIDATED_WITH_ERRORS = 1;
8
	 
9
	public static final byte NON_VALIDATED = 2;
10
	 
11
}
12
 
0 13

  
trunk/libraries/libTopology/src/org/gvsig/topology/Topology.java
1
package org.gvsig.topology;
2

  
3
import com.iver.cit.gvsig.fmap.MapContext;
4
import com.iver.cit.gvsig.fmap.layers.FLayers;
5
import com.vividsolutions.jts.geom.Envelope;
6
import java.awt.geom.Rectangle2D;
7

  
8
import org.cresques.cts.IProjection;
9
import org.gvsig.topology.topologyrules.MustBeLargerThanClusterTolerance;
10
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
11

  
12
public class Topology extends FLayers implements ITopologyErrorContainer, ITopologyStatus {
13
 
14
	public Topology(MapContext fmap, FLayers parent) {
15
		super(fmap, parent);
16
		// TODO Auto-generated constructor stub
17
	}
18
	
19
	public Topology(){
20
		super(null, null);
21
	}
22

  
23
	private double clusterTolerance;
24
	 
25
	private int status;
26
	 
27
	/**
28
	 *If during validation process a topoly
29
	 *exceeds this parameters, validation will be stopped.
30
	 *
31
	 */
32
	private int maxNumberOfErrors = -1;
33
	 
34
	private AbstractTopologyRule[] abstractTopologyRule;
35
	 
36
	private Envelope[] envelope;
37
	 
38
	private Rectangle2D[] dirtyZones;
39
	 
40
	private MustBeLargerThanClusterTolerance[] clusterTolRule;
41
	 
42
	public double getClusterTolerance() {
43
		return 0;
44
	}
45
	 
46
	/**
47
	 *Adds a new topology rule to the topology.
48
	 */
49
	public void addRule(AbstractTopologyRule rule) {
50
	}
51
	 
52
	/**
53
	 *Adds a layer to the topology. If the topology has been validated, changes
54
	 *topology status to NON-VALIDATED and adds a dirty zone equals to
55
	 *the layer extent.
56
	 */
57
	public void addLayer(FLyrVect layer, int xyRank, int zRank) {
58
	}
59
	 
60
	public void setStatus(int status) {
61
	}
62
	 
63
	public int getStatus() {
64
		return 0;
65
	}
66
	 
67
	/**
68
	 *Adds a dirty zone to the topology
69
	 *(usually whe a feature of a layer
70
	 *of the topology has been edited)
71
	 */
72
	public void addDirtyZone(Rectangle2D dirtyZone) {
73
	}
74
	 
75
	public Rectangle2D getDirtyZone() {
76
		return null;
77
	}
78
	 
79
	public void validate() {
80
	}
81
	 
82
	public int getLayerCount() {
83
		return 0;
84
	}
85
	 
86
	public int getRuleCount() {
87
		return 0;
88
	}
89
	 
90
	public FLyrVect getLyr(int lyrIndex) {
91
		return null;
92
	}
93
	 
94
	public AbstractTopologyRule getRule(int ruleIndex) {
95
		return null;
96
	}
97
	 
98
	/**
99
	 *Returns if a specified rectangle touch one
100
	 *of the existing dirty zones. If not, probably
101
	 *is needed to add to the dirty zones collection.
102
	 *If true, maybe it could modify the dirty zone.
103
	 */
104
	public boolean isInDirtyZone() {
105
		return false;
106
	}
107
	 
108
	/**
109
	 *Modify the dirty zone of the specified index
110
	 */
111
	public void updateDirtyZone(int dirtyZoneIndex, Rectangle2D dirtyZone) {
112
	}
113
	 
114
	public void setMaxNumberOfErrors(int maxNumberOfErrors) {
115
	}
116
	 
117
	public int getMaxNumberOfErrors() {
118
		return 0;
119
	}
120

  
121
	public void addTopologyError(TopologyError topologyError) {
122
		// TODO Auto-generated method stub
123
		
124
	}
125

  
126
	public void markAsTopologyException(TopologyError topologyError,
127
			boolean markAsException) {
128
		// TODO Auto-generated method stub
129
		
130
	}
131

  
132
	public TopologyError[] getTopologyErrors(String ruleName, int shapeType,
133
			FLyrVect sourceLayer, IProjection desiredProjection,
134
			int includeExceptions) {
135
		// TODO Auto-generated method stub
136
		return null;
137
	}
138
	 
139
}
140
 
0 141

  
trunk/libraries/libTopology/src/org/gvsig/topology/ITopologyRule.java
1
package org.gvsig.topology;
2

  
3
public interface ITopologyRule {
4
 
5
}
6
 
0 7

  
trunk/libraries/libTopology/src/org/gvsig/topology/TopologyPersister.java
1
package org.gvsig.topology;
2

  
3
import java.util.Map;
4

  
5

  
6
/**
7
 *Class with the responsability of persist topologies
8
 *and their elements.
9
 */
10
public class TopologyPersister {
11
 
12
	public static void persist(Topology topology) {
13
	}
14
	 
15
	public static Topology load(Map storeParams) {
16
		return null;
17
	}
18
	 
19
}
20
 
0 21

  
trunk/libraries/libTopology/src/org/gvsig/topology/ITwoLyrRule.java
1
package org.gvsig.topology;
2

  
3

  
4
/**
5
 *Marker interface for all rules that applies to two layers
6
 */
7
public interface ITwoLyrRule extends ITopologyRule {
8
 
9
}
10
 
0 11

  
trunk/libraries/libTopology/src/org/gvsig/topology/ITopologyErrorContainer.java
1
package org.gvsig.topology;
2

  
3
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
4
import org.cresques.cts.IProjection;
5

  
6

  
7
/**
8
 *All the classes that contains TopologyError must 
9
 *implement this interface.
10
 */
11
public interface ITopologyErrorContainer {
12
 
13
	public static final int ONLY_ERRORS = 0;
14
	 
15
	public static final int ONLY_EXCEPTIONS = 1;
16
	 
17
	public static final int BOTH_ERROR_EXCEPTIONS = 2;
18
	 
19
	public void addTopologyError(TopologyError topologyError);
20
	/**
21
	 *Returns the errors contained. In function
22
	 *of the specified params, returned errors 
23
	 *will be reprojected, filtered by rule, geometry type,
24
	 *etc.
25
	 */
26
	public TopologyError[] getTopologyErrors(String ruleName, int shapeType, FLyrVect sourceLayer, IProjection desiredProjection, int includeExceptions);
27
	/**
28
	 *Marks the specified feature as the
29
	 *specified status (exception or not exception)
30
	 */
31
	public void markAsTopologyException(TopologyError topologyError, boolean markAsException);
32
}
33
 
0 34

  
trunk/libraries/libTopology/src/org/gvsig/jts/Snapper.java
1
package org.gvsig.jts;
2

  
3
import com.vividsolutions.jts.geom.Geometry;
4

  
5

  
6
/**
7
 *Moves coordinates (in a given tolerance) to force them to coincide.
8
 */
9
public class Snapper {
10
 
11
	public static Geometry[] snap(Geometry[] geoms2snap, int[] geomsWeights) {
12
		return null;
13
	}
14
	 
15
}
16
 
0 17

  
trunk/libraries/libTopology/src/org/gvsig/jts/Cracker.java
1
package org.gvsig.jts;
2

  
3
import com.vividsolutions.jts.geom.Geometry;
4

  
5

  
6
/**
7
 *Cracks the geometries of a topology's layers to force
8
 *the sharing of coordinates
9
 *
10
 */
11
public class Cracker {
12
 
13
	/**
14
	 *Receives many geometries, cheks if the shared
15
	 *geometry is explicit, and if not, adds vertex
16
	 *in common points to ensure the sharing of common geometries
17
	 */
18
	public static Geometry[] crack(Geometry[] geoms2crack) {
19
		return null;
20
	}
21
	 
22
}
23
 
0 24

  
trunk/libraries/libTopology/src/org/gvsig/jts/LineStringSelfIntersectionChecker.java
1
package org.gvsig.jts;
2

  
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6

  
7
import com.iver.cit.gvsig.util.SnappingCoordinateMap;
8
import com.vividsolutions.jts.algorithm.RobustLineIntersector;
9
import com.vividsolutions.jts.geom.GeometryFactory;
10
import com.vividsolutions.jts.geom.LineString;
11
import com.vividsolutions.jts.geom.Coordinate;
12
import com.vividsolutions.jts.geom.Geometry;
13
import com.vividsolutions.jts.geomgraph.Edge;
14
import com.vividsolutions.jts.geomgraph.EdgeIntersection;
15
import com.vividsolutions.jts.geomgraph.EdgeIntersectionList;
16
import com.vividsolutions.jts.geomgraph.GeometryGraph;
17
import com.vividsolutions.jts.geomgraph.index.EdgeSetIntersector;
18
import com.vividsolutions.jts.geomgraph.index.SegmentIntersector;
19
import com.vividsolutions.jts.geomgraph.index.SimpleMCSweepLineIntersector;
20

  
21
/**
22
 * 
23
 * Cheks if a given linestring has selfintersections,
24
 * and gives methods to split a line with self intersections
25
 * in many lines.
26
 * 
27
 * Doesnt  consideer snap tolerance.
28
 * 
29
 * @author azabala
30
 *
31
 */
32
public class LineStringSelfIntersectionChecker {
33
 
34
	private LineString lineString;
35
	
36
	private GeometryGraph graph;
37
	
38
	private boolean hasInitialized = false;
39
	
40
	
41
	//TODO Must we use a CoordinateMap ??
42
	private Coordinate[] selfIntersections;
43
	
44
	
45
	public LineStringSelfIntersectionChecker(LineString lineString){
46
		this.lineString = lineString;
47
		this.graph = new GeometryGraph(0, lineString);
48
	}
49
	 
50
	public boolean hasSelfIntersections() {
51
		if(! hasInitialized){
52
			initialize();
53
			hasInitialized = true;
54
		}//if
55
		if(selfIntersections != null && selfIntersections.length > 0)
56
			return true;
57
		else
58
			return false;
59
	}
60
	
61
	private void initialize(){
62
		ArrayList selfIntersectionsList = new ArrayList();
63
		SnappingCoordinateMap selfIntersectionsMap = new SnappingCoordinateMap(0d);
64
//		SegmentIntersector si = graph.computeSelfNodes(new RobustLineIntersector(), true);
65
//		Iterator i = graph.getEdgeIterator();
66
		List edges = computeSelfNodes();
67
		Iterator i = edges.iterator();
68
		while (i.hasNext()) {//for each edge graph
69
			Edge e = (Edge) i.next();
70
			EdgeIntersectionList eiList = e.getEdgeIntersectionList();
71
			for (Iterator eiIt = eiList.iterator(); eiIt.hasNext(); ) {//PROBLEMA: LAS AUTOINTERSECCIONES EN LINEAS SON PERMITIDAS
72
				EdgeIntersection ei = (EdgeIntersection) eiIt.next();
73
				if(!graph.isBoundaryNode(0, ei.coord)){
74
					if(! selfIntersectionsMap.contains(ei.coord)){
75
						selfIntersectionsList.add(ei.coord);
76
						selfIntersectionsMap.put(ei.coord, ei.coord);
77
					}	
78
				}	
79
			}//for
80
		}//while
81
		
82
		
83
		int numSelfIntersections = selfIntersectionsList.size();
84
		if(numSelfIntersections > 0){
85
			selfIntersections = new Coordinate[numSelfIntersections];
86
			selfIntersectionsList.toArray(selfIntersections);
87
		}
88
	}
89
	
90
	private List computeSelfNodes(){
91
		
92
		SegmentIntersector si = new SegmentIntersector(new RobustLineIntersector(), true, false);
93
		EdgeSetIntersector esi = new SimpleMCSweepLineIntersector();
94
		List edges = new ArrayList();
95
		Iterator edgesIt = graph.getEdgeIterator();
96
		while(edgesIt.hasNext()){
97
			edges.add(edgesIt.next());
98
		}
99
		esi.computeIntersections(edges, si, true);
100
		return edges;
101
		
102
		/*
103
		 * By design, JTS allows self intersections.
104
		 * 
105
		public SegmentIntersector computeSelfNodes(LineIntersector li, boolean computeRingSelfNodes){
106
			SegmentIntersector si = new SegmentIntersector(li, true, false);
107
			EdgeSetIntersector esi = createEdgeSetIntersector();
108
			// optimized test for Polygons and Rings
109
			 if (! computeRingSelfNodes
110
			 	&& (parentGeom instanceof LinearRing
111
			 	|| parentGeom instanceof Polygon
112
			 	|| parentGeom instanceof MultiPolygon)) {
113
			 	esi.computeIntersections(edges, si, false);
114
              }
115
              else {
116
              	esi.computeIntersections(edges, si, true);
117
              }
118
              //System.out.println("SegmentIntersector # tests = " + si.numTests);
119
               addSelfIntersectionNodes(argIndex);
120
               return si;
121
          }
122
		  */
123
	}
124
	
125
	/**
126
	 *Returns the self intersections of the linestring
127
	 */
128
	public Coordinate[] getSelfIntersections() {
129
		return selfIntersections;
130
	}
131
	 
132
	public Geometry[] clean() {
133
		/*
134
		 * The algorith to clean a polyline with self intersections is:
135
		 * 
136
		 * Create result (linestring) collection.
137
		 * 
138
		 * Create new empty geometry 
139
		 * 
140
		 * For each geometry vertex.
141
		 * 		add vertex to empty geometry
142
		 *      if vertex is self intersection
143
		 *      	save geometry in result collection
144
		 *          create new empty geometry
145
		 *          add self intersection to empty geometry 
146
		 *          
147
		 *          
148
		 *          
149
		 *  Special cases; what if a geometry only has one point 
150
		 *  (first vertex is selfintersection).
151
		 *  This wouldnt be consideered as self intersection. It is a node
152
		 *  of a closed polyline
153
		 *  
154
		 *  TODO A test case with this case       
155
		 * */
156
		
157
		Geometry[] cleanedLines = null;
158
		ArrayList solution = new ArrayList();
159
		GeometryFactory factory = new GeometryFactory();
160
		ArrayList solutionCoords = new ArrayList();
161
		
162
		Coordinate[] lineStringCoords = lineString.getCoordinates();
163
		for(int i = 0; i < lineStringCoords.length; i++){
164
			Coordinate coord = lineStringCoords[i];
165
			solutionCoords.add(coord);
166
			
167
			for(int j = 0; j < selfIntersections.length; j++){
168
				Coordinate selfIntersection = selfIntersections[j];
169
				if(selfIntersection.equals2D(coord)){
170
					//the vertex is a self intersection
171
					Coordinate[] geomCoords = new Coordinate[solutionCoords.size()];
172
					solutionCoords.toArray(geomCoords);
173
					LineString newLineString = factory.createLineString(geomCoords);
174
					solution.add(newLineString);
175
					
176
					solutionCoords.clear();
177
					solutionCoords.add(coord);
178
					
179
					break;
180
				}//if
181
			}//for j
182
		}//for i
183
		
184
		//finally, create the last geometry
185
		Coordinate[] geomCoords = new Coordinate[solutionCoords.size()];
186
		solutionCoords.toArray(geomCoords);
187
		LineString newLineString = factory.createLineString(geomCoords);
188
		solution.add(newLineString);
189
		
190
		cleanedLines = new Geometry[solution.size()];
191
		solution.toArray(cleanedLines);
192
		return cleanedLines;
193
	}
194

  
195
	public LineString getLineString() {
196
		return lineString;
197
	}
198

  
199
	public void setLineString(LineString lineString) {
200
		this.lineString = lineString;
201
	}
202
	 
203
}
204
 
0 205

  
trunk/libraries/libTopology/src-test/org/gvsig/topology/LineStringSelfIntersectionTest.java
1
/*
2
 * Created on 26-ago-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id$
47
* $Log$
48
* Revision 1.1  2007-08-27 18:05:59  azabala
49
* first version in cvs
50
*
51
*
52
*/
53
package org.gvsig.topology;
54

  
55
import org.gvsig.jts.LineStringSelfIntersectionChecker;
56

  
57
import com.vividsolutions.jts.geom.Coordinate;
58
import com.vividsolutions.jts.geom.Geometry;
59
import com.vividsolutions.jts.geom.GeometryFactory;
60
import com.vividsolutions.jts.geom.LineString;
61
import com.vividsolutions.jts.io.ParseException;
62
import com.vividsolutions.jts.io.WKTReader;
63

  
64
import junit.framework.TestCase;
65

  
66

  
67

  
68
/**
69
 * To see visually the geometries used for testing, use
70
 * JTS Test editor and paste WKT code.
71
 * 
72
 * TODO Maybe edition in gvSIG would have a wkt editor, or
73
 * a tool to select a geometry and see it in WKT.
74
 * 
75
 * 
76
 * 
77
 * */
78

  
79

  
80
public class LineStringSelfIntersectionTest extends TestCase {
81

  
82
	GeometryFactory factory = new GeometryFactory();
83
	WKTReader wktReader = new WKTReader(factory);
84
	
85
	
86
	public void testLine1() throws ParseException{
87
		
88
		//linestring with one self intersection
89
		String wkt1 = "LINESTRING (-12378 3231, -14205 1381, -8845 2486, -11080 4794, -13820 -663)";
90
		
91
		LineString lineString = (LineString) wktReader.read(wkt1);
92
		
93
		LineStringSelfIntersectionChecker checker = 
94
			new LineStringSelfIntersectionChecker(lineString);
95
		
96
		assertTrue(checker.hasSelfIntersections());
97
		
98
		Coordinate[] selfIntersections = checker.getSelfIntersections();
99
		
100
		assertTrue(selfIntersections.length == 1);
101
		
102
		Geometry[] cleanedLineString = checker.clean();
103
		
104
		assertTrue(cleanedLineString.length == 3);
105
		
106
	
107
	}
108
	
109
	public void testLine2() throws ParseException{
110
		String wkt2 = "LINESTRING (-16152 2534, -15190 5251, -10239 3808, -8316 -807, -11417 -1360, -11344 2246, -6128 5203, -600 2606, -1417 515, -3484 -1023, -5263 2486, -816 6765, 602 5948)";
111
		LineString lineString = (LineString) wktReader.read(wkt2);
112
		LineStringSelfIntersectionChecker checker = 
113
			new LineStringSelfIntersectionChecker(lineString);
114
		checker.hasSelfIntersections();
115
		checker.clean();
116
	}
117

  
118
}
119

  
0 120

  
trunk/libraries/libTopology/src-test/org/gvsig/topology/JtsSnapTest.java
1
/*
2
 * Created on 25/08/2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id$
47
* $Log$
48
* Revision 1.1  2007-08-27 18:05:59  azabala
49
* first version in cvs
50
*
51
*
52
*/
53
package org.gvsig.topology;
54

  
55
import junit.framework.TestCase;
56

  
57
import com.vividsolutions.jts.geom.Geometry;
58
import com.vividsolutions.jts.geom.GeometryFactory;
59
import com.vividsolutions.jts.geom.PrecisionModel;
60
import com.vividsolutions.jts.io.ParseException;
61
import com.vividsolutions.jts.io.WKTReader;
62
import com.vividsolutions.jts.operation.overlay.OverlayOp;
63
import com.vividsolutions.jts.operation.overlay.SnappingOverlayOperation;
64

  
65
/**
66
 * 
67
 * In these tests we try to probe the use of snapping and
68
 * precision reduction in JTS operations, to avoid undesired
69
 * effects (for example spureus lines in dissolve) and
70
 * to allow clustering tolerance in topologies.
71
 * 
72
 * Documentaci?n de tests.
73
 * Prueba de operaciones JTS con snapping.
74
 * 
75
 * 
76
 * @author azabala
77
 *
78
 */
79
public class JtsSnapTest extends TestCase {
80
	
81
	
82

  
83
	static String wktLS1 = "LINESTRING (260 120, 300 180, 420 180, 480 120)";
84
	static String wktLS2 = "LINESTRING (479.999 120, 459 114, 437 128, 426 130, 458 141)";
85
	
86
	
87
	public void testLineLineJTS18DEFAULT() throws ParseException{
88
//		GeometryFactory factory = new GeometryFactory();
89
//		WKTReader wktReader = new WKTReader(factory);
90
//		Geometry ls1 = wktReader.read(wktLS1);
91
//		Geometry ls2 = wktReader.read(wktLS2);
92
//		
93
//		Geometry intersection = SnapIfNeededOverlayOp.intersection(ls1, ls2);
94
//		System.out.println(intersection.toText());
95
//		assertTrue(intersection instanceof GeometryCollection);
96
//		GeometryCollection sol = (GeometryCollection)intersection;
97
//		assertTrue(sol.getNumGeometries() == 0);
98
		
99
		
100
	}
101
	
102
	//SnapIfNeeded solo actua si se producen errores de precision en el calculo de overlays
103
	//si no hay ningun error, pasa del snap
104
	public void testLineLineJTS18PMFIXED() throws ParseException{
105
//		GeometryFactory factory = new GeometryFactory(new PrecisionModel(1));
106
//		WKTReader wktReader = new WKTReader(factory);
107
//		Geometry ls1 = wktReader.read(wktLS1);
108
//		Geometry ls2 = wktReader.read(wktLS2);
109
//		Geometry intersection = SnapOverlayOp.intersection(ls1, ls2);
110
//		System.out.println(intersection.toText());
111
//		assertTrue(!(intersection instanceof GeometryCollection));
112
//		GeometryCollection sol = (GeometryCollection)intersection;
113
//		assertTrue(sol.getNumGeometries() != 0);
114
	}
115
	
116
	public void testLineLineJTS18PMFIXED2() throws ParseException{
117
//		GeometryFactory factory = new GeometryFactory(new PrecisionModel(1000));
118
//		WKTReader wktReader = new WKTReader(factory);
119
//		Geometry ls1 = wktReader.read(wktLS1);
120
//		Geometry ls2 = wktReader.read(wktLS2);
121
//		Geometry intersection = SnapOverlayOp.intersection(ls1, ls2);
122
//		System.out.println(intersection.toText());
123
//		assertTrue(!(intersection instanceof GeometryCollection));
124
	}
125
	
126
	/*
127
	 El resultado de estos tres primeros tests es el siguiente:
128
	 GEOMETRYCOLLECTION EMPTY
129
	 MULTIPOINT (458 141, 480 120)
130
	 LINESTRING (479.999 120, 480 120)
131
	 
132
	 El snap de JTS viene determinado por el PrecisionModel
133
	 
134
	 En el primer caso, el PrecisionModel es el por defecto (maxima precisi?n),
135
	 luego no se intersectan.
136
	 
137
	 En el segundo caso, el PrecisionModel tiene una resoluci?n de 0.1, luego considera que
138
	 el primer y ultimo punto de la segunda linea intersectan.
139
	 AHORA BIEN. Como no hay desplazamiento, considera que (480, 120) y (479.999, 120) son
140
	 el mismo punto. Deber?an desplazarse??
141
	 
142
	 
143
	 
144
	 En el tercer caso ya da resultados no deseados. Los puntos (479.999 120) y
145
	 (480, 120) si aplicamos una tolerancia de snap grande deben ser considerados el mismo punto,
146
	 pero si la tolerancia de snap es peque?a (1000 = 0.001 de resolucion) no deber?a dar
147
	 un LineString de solucion
148
	 REPORTAR A JTS-LIST
149
	 * */
150
	
151
	//TEST DE PRUEBA CON LAS CLASES DE JTS 1.7
152
	//OJO!!!!!!
153
	//EL SNAP DE GVSIG SOLO FUNCIONA CON JTS 1.7. CON JTS 1.8 NO COMPILA
154
	
155
	
156
	
157
	//TEST 1: INTERSECCI?N DE LINEAS
158
	
159
	public void testLineLineSnapGvSIG() throws ParseException{
160
//		GeometryFactory factory = new GeometryFactory();
161
//		WKTReader wktReader = new WKTReader(factory);
162
//		Geometry ls1 = wktReader.read(wktLS1);
163
//		Geometry ls2 = wktReader.read(wktLS2);
164
//		Geometry intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 0.1d);
165
//		System.out.println(intersection.toText());
166
//		
167
//		
168
//		intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 0.01d);
169
//		System.out.println(intersection.toText());
170
//		
171
//		intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 0.001d);
172
//		System.out.println(intersection.toText());
173
//		
174
//		intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 0.0001d);
175
//		System.out.println(intersection.toText());
176
//		
177
//		intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 2d);
178
//		System.out.println(intersection.toText());
179
		
180
		/*
181
		 POINT (480 120)
182
		 POINT (480 120)
183
		 POINT (480 120)
184
		 GEOMETRYCOLLECTION EMPTY
185
		 MULTIPOINT (458 141, 480 120) 
186
		 
187
		 Esto es increible!!!! El snap nuestro ofrece mejores resultados!!!!!!!
188
		 
189
		 * */
190
		
191
		//ahora vemos si afecta el precisionmodel al modo tradicional
192
//		factory = new GeometryFactory(new PrecisionModel(1));
193
//		wktReader = new WKTReader(factory);
194
//		ls1 = wktReader.read(wktLS1);
195
//		ls2 = wktReader.read(wktLS2);
196
//		
197
//		intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 0d);
198
//		System.out.println(intersection.toText());
199
//		
200
//		factory = new GeometryFactory(new PrecisionModel(10000));
201
//		wktReader = new WKTReader(factory);
202
//		ls1 = wktReader.read(wktLS1);
203
//		ls2 = wktReader.read(wktLS2);
204
//		
205
//		intersection = SnappingOverlayOperation.overlayOp(ls1, ls2, OverlayOp.INTERSECTION, 0d);
206
//		System.out.println(intersection.toText());
207
	}
208
	
209
	/*
210
	
211
	
212
	
213
	*/
214
	
215
	
216
	//MAS TESTS: INTERSECCIONES DE POLIGONOS CON POLIGONOS, LINEAS CON POLIGONOS, LINEAS
217
	//CON PUNTOS, POLIGONOS QUE SE AUTOINTERSECTEN Y LINEAS QUE SE AUTOINTERSECTEN
218
	
219
	
220
	//JTS 1.8
221
	
222
	public void testPolyPolyJTS18(){
223
		
224
	}
225
}
226

  
0 227

  

Also available in: Unified diff