Revision 19705

View differences:

trunk/libraries/libTopology/src/org/gvsig/topology/topologyrules/PolygonMustNotOverlapWith.java
1
/*
2
 * Created on 10-abr-2006
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
*/
49
package org.gvsig.topology.topologyrules;
50

  
51
import java.awt.geom.Rectangle2D;
52
import java.util.ArrayList;
53

  
54
import org.gvsig.fmap.core.FGeometryUtil;
55
import org.gvsig.jts.JtsUtil;
56
import org.gvsig.topology.ITwoLyrRule;
57
import org.gvsig.topology.Messages;
58
import org.gvsig.topology.Topology;
59
import org.gvsig.topology.TopologyError;
60

  
61
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
62
import com.iver.cit.gvsig.fmap.core.IFeature;
63
import com.iver.cit.gvsig.fmap.core.IGeometry;
64
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
65
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
66
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
67
import com.vividsolutions.jts.geom.Envelope;
68
import com.vividsolutions.jts.geom.Geometry;
69
import com.vividsolutions.jts.geom.GeometryCollection;
70
import com.vividsolutions.jts.geom.MultiPolygon;
71
import com.vividsolutions.jts.geom.Polygon;
72
import com.vividsolutions.jts.precision.EnhancedPrecisionOp;
73

  
74

  
75
public class PolygonMustNotOverlapWith extends PolygonsMustNotOverlap 
76
										implements ITwoLyrRule{
77
	String ruleName = Messages.getText("must_not_overlap_with");
78
	
79
	FLyrVect destinationLyr;
80
	
81
	/**
82
	 * Constructor.
83
	 * 
84
	 * @param topology
85
	 * @param originLyr
86
	 * @param destinationLyr
87
	 * @param clusterTolerance
88
	 */
89
	public PolygonMustNotOverlapWith(Topology topology, 
90
									 FLyrVect originLyr,
91
									 FLyrVect destinationLyr,
92
									 double clusterTolerance){
93
		super(topology, originLyr, clusterTolerance);
94
		setDestinationLyr(destinationLyr);
95
	}
96
	
97
	protected void process(Geometry geometry, IFeature feature) {
98
		if (geometry instanceof GeometryCollection) {
99
			GeometryCollection geomCol = (GeometryCollection) geometry;
100
			for (int i = 0; i < geomCol.getNumGeometries(); i++) {
101
				Geometry geomI = geomCol.getGeometryN(i);
102
				process(geomI, feature);
103
			}
104
		} else if (geometry instanceof Polygon) {
105
			Polygon polygon = (Polygon) geometry;
106
			Envelope env1 = polygon.getEnvelopeInternal();
107

  
108
			// We try to extend a bit the original envelope to ensure
109
			// recovering geometries in the limit
110

  
111
			double delta = getClusterTolerance() + 10;
112
			double minX = env1.getMinX() - delta;
113
			double minY = env1.getMinY() - delta;
114
			double maxX = env1.getMaxX() + delta;
115
			double maxY = env1.getMaxY() + delta;
116

  
117
			Rectangle2D rect = new Rectangle2D.Double(minX, minY, maxX - minX,
118
					maxY - minY);
119

  
120
			try {
121
				IFeatureIterator neighbours = destinationLyr.
122
													getSource().
123
													getFeatureIterator(rect, null, null, false);
124
				while (neighbours.hasNext()) {
125
					IFeature neighbourFeature = neighbours.next();
126
					if (neighbourFeature.getID().equalsIgnoreCase(
127
							feature.getID()))
128
						continue;
129

  
130
					IGeometry geom2 = neighbourFeature.getGeometry();
131
					Rectangle2D rect2 = geom2.getBounds2D();
132
					if (rect.intersects(rect2)) {
133
						
134
						Geometry jts2 = geom2.toJTSGeometry();
135
						ArrayList<Polygon> geometriesToProcess = new ArrayList<Polygon>();
136
						
137
						if (jts2 instanceof Polygon) {
138
							geometriesToProcess.add((Polygon) jts2);
139
						} else if (jts2 instanceof MultiPolygon) {
140
							MultiPolygon multiPolygon = (MultiPolygon) jts2;
141
							int numLines = multiPolygon.getNumGeometries();
142
							for (int i = 0; i < numLines; i++) {
143
								Polygon polygon2 = (Polygon) multiPolygon
144
										.getGeometryN(i);
145
								geometriesToProcess.add(polygon2);
146
							}
147
						} else if (jts2 instanceof GeometryCollection) {
148
							MultiPolygon multiPoly = JtsUtil
149
									.convertToMultiPolygon((GeometryCollection) jts2);
150
							int numPolys = multiPoly.getNumGeometries();
151
							for (int i = 0; i < numPolys; i++) {
152
								Polygon line = (Polygon) multiPoly
153
										.getGeometryN(i);
154
								geometriesToProcess.add(line);
155
							}
156
						} else {
157
							System.out.println("Encontrado:" + jts2.toString()
158
									+ " en regla PolygonsMustNotOverlap");
159
						}
160

  
161
						int numGeometries = geometriesToProcess.size();
162
						for (int i = 0; i < numGeometries; i++) {
163
							Polygon poly2 = geometriesToProcess.get(i);
164
								if(poly2.overlaps(polygon)){
165
									ComputedTopologyError errorEntry = 
166
										new ComputedTopologyError();
167
									errorEntry.firstFeature = feature.getID();
168
									errorEntry.secondFeature = neighbourFeature.getID();
169
									
170
									if(this.errorEntries.get(errorEntry) == null){
171
										Geometry errorGeomJts = EnhancedPrecisionOp.intersection(poly2, polygon);
172
										IGeometry errorGeom = FConverter.jts_to_igeometry(errorGeomJts);
173
										TopologyError topologyError = 
174
											new TopologyError(errorGeom, 
175
														errorContainer.getErrorFid(), 
176
														this,  
177
														feature, 
178
														neighbourFeature, 
179
														topology );
180
										addTopologyError(topologyError);
181
										
182
										errorEntries.put(errorEntry, errorEntry);
183
									}//if
184
								}//if
185
						}// for
186
					}//if
187
				}// while
188

  
189
			} catch (ReadDriverException e) {
190
				e.printStackTrace();
191
				return;
192
			}
193
		} else {
194
			System.out.println("Encontrado:" + geometry.toString()
195
					+ " en regla de dangles");
196
		}
197
	}
198

  
199

  
200
	public boolean acceptsDestinationLyr(FLyrVect destLyr) {
201
		try {
202
			int shapeType = destLyr.getShapeType();
203
			return (FGeometryUtil.getDimensions(shapeType) == 2);
204
		} catch (ReadDriverException e) {
205
			e.printStackTrace();
206
			return false;
207
		}
208
	}
209

  
210

  
211
	public FLyrVect getDestinationLyr() {
212
		return destinationLyr;
213
	}
214

  
215

  
216
	public void setDestinationLyr(FLyrVect destinationLyr) {
217
		this.destinationLyr = destinationLyr;
218
	}
219
}

Also available in: Unified diff