Revision 19632 trunk/libraries/libTopology/src/org/gvsig/jts/JtsUtil.java

View differences:

JtsUtil.java
74 74
import com.vividsolutions.jts.geom.GeometryFactory;
75 75
import com.vividsolutions.jts.geom.LineString;
76 76
import com.vividsolutions.jts.geom.LinearRing;
77
import com.vividsolutions.jts.geom.MultiLineString;
77 78
import com.vividsolutions.jts.geom.MultiPoint;
78 79
import com.vividsolutions.jts.geom.MultiPolygon;
79 80
import com.vividsolutions.jts.geom.Point;
80 81
import com.vividsolutions.jts.geom.Polygon;
82
import com.vividsolutions.jts.geom.PrecisionModel;
81 83
import com.vividsolutions.jts.geomgraph.Edge;
82 84
import com.vividsolutions.jts.geomgraph.EdgeIntersectionList;
83 85
import com.vividsolutions.jts.geomgraph.GeometryGraph;
......
89 91
 * 
90 92
 */
91 93
public class JtsUtil {
92
	public static GeometryFactory geomFactory = new GeometryFactory();
94
	//FIXME PrecionModel's scale should be configurable by a JTS preferences
95
	//dialog
96
	public static final PrecisionModel GVSIG_PRECISION_MODEL = 
97
		new PrecisionModel(10000);
98
	
99
	public static final GeometryFactory GEOMETRY_FACTORY = 
100
		new GeometryFactory(GVSIG_PRECISION_MODEL);
93 101

  
94 102
	public static Geometry createGeometry(Coordinate[] coords,
95 103
			String geometryType) {
96 104
		if (geometryType.equalsIgnoreCase("POINT")) {
97
			return geomFactory.createPoint(coords[0]);
105
			return GEOMETRY_FACTORY.createPoint(coords[0]);
98 106
		} else if (geometryType.equalsIgnoreCase("LINESTRING")) {
99
			return geomFactory.createLineString(coords);
107
			return GEOMETRY_FACTORY.createLineString(coords);
100 108
		} else if (geometryType.equalsIgnoreCase("LINEARRING")) {
101
			return geomFactory.createLinearRing(coords);
109
			return GEOMETRY_FACTORY.createLinearRing(coords);
102 110
		}
103 111
		// else if (geometryType.equalsIgnoreCase("POLYGON")) {
104 112
		// // LinearRing exterior = geomFactory.createLinearRing(coords);
105 113
		// // return geomFactory.createPolygon(exterior, null);
106 114
		// }
107 115
		else if (geometryType.equalsIgnoreCase("MULTIPOINT")) {
108
			return geomFactory.createMultiPoint(coords);
116
			return GEOMETRY_FACTORY.createMultiPoint(coords);
109 117
		}
110 118
		return null;
111 119
	}
......
123 131
				- firstPointOfShell + 1];
124 132
		System.arraycopy(coords, firstPointOfShell, shellCoords, 0,
125 133
				shellCoords.length);
126
		LinearRing shell = geomFactory.createLinearRing(shellCoords);
134
		LinearRing shell = GEOMETRY_FACTORY.createLinearRing(shellCoords);
127 135

  
128 136
		LinearRing[] holes = null;
129 137
		if (numberOfHoles > 0) {
......
137 145
						+ 1];
138 146
				System.arraycopy(coords, firstPointOfHole, holeCoords, 0,
139 147
						holeCoords.length);
140
				holes[i - 1] = geomFactory.createLinearRing(holeCoords);
148
				holes[i - 1] = GEOMETRY_FACTORY.createLinearRing(holeCoords);
141 149
			}
142 150
			firstPointOfHole = indexOfParts[indexOfParts.length - 1];
143 151
			lastPointOfHole = coords.length - 1;
144 152
			holeCoords = new Coordinate[lastPointOfHole - firstPointOfHole + 1];
145 153
			System.arraycopy(coords, firstPointOfHole, holeCoords, 0,
146 154
					holeCoords.length);
147
			holes[holes.length - 1] = geomFactory.createLinearRing(holeCoords);
155
			holes[holes.length - 1] = GEOMETRY_FACTORY.createLinearRing(holeCoords);
148 156
		}
149
		return geomFactory.createPolygon(shell, holes);
157
		return GEOMETRY_FACTORY.createPolygon(shell, holes);
150 158
	}
151 159

  
152 160
	/**
......
174 182
		}
175 183
		return indexOfParts;
176 184
	}
185
	
186
	public static MultiLineString convertToMultiLineString(GeometryCollection geomCol){
187
		List<LineString> lines = new ArrayList<LineString>();
188
		int numGeometries = geomCol.getNumGeometries();
189
		for (int i = 0; i < numGeometries; i++) {
190
			Geometry geom = geomCol.getGeometryN(i);
191
			if (geom instanceof LineString)
192
				lines.add((LineString) geom);
193
			else if (geom instanceof MultiLineString) {
194
				MultiLineString multiLine = (MultiLineString) geom;
195
				int numLines = multiLine.getNumGeometries();
196
				for (int j = 0; j < numLines; j++) {
197
					lines.add((LineString) multiLine.getGeometryN(j));
198
				}// j
199
			}// else
200
			else if (geom instanceof GeometryCollection) {
201
				MultiLineString multiLine = convertToMultiLineString((GeometryCollection) geom);
202
				int numLines = multiLine.getNumGeometries();
203
				for (int j = 0; j < numLines; j++) {
204
					lines.add((LineString) multiLine.getGeometryN(j));
205
				}// j
206
			}// else
207
		}// for i
208
		LineString[] lineArray = new LineString[lines.size()];
209
		lines.toArray(lineArray);
210
		return GEOMETRY_FACTORY.createMultiLineString(lineArray);
211
	}
212
	
213
	
177 214

  
178
	public static MultiPolygon convertIfPossible(GeometryCollection geomCol) {
215
	public static MultiPolygon convertToMultiPolygon(GeometryCollection geomCol) {
179 216
		List<Polygon> polygons = new ArrayList<Polygon>();
180 217
		int numGeometries = geomCol.getNumGeometries();
181 218
		for (int i = 0; i < numGeometries; i++) {
......
190 227
				}// j
191 228
			}// else
192 229
			else if (geom instanceof GeometryCollection) {
193
				MultiPolygon multiPol = convertIfPossible((GeometryCollection) geom);
230
				MultiPolygon multiPol = convertToMultiPolygon((GeometryCollection) geom);
194 231
				int numPols = multiPol.getNumGeometries();
195 232
				for (int j = 0; j < numPols; j++) {
196 233
					polygons.add((Polygon) multiPol.getGeometryN(j));
......
199 236
		}// for i
200 237
		Polygon[] polyArray = new Polygon[polygons.size()];
201 238
		polygons.toArray(polyArray);
202
		return new GeometryFactory().createMultiPolygon(polyArray);
239
		return GEOMETRY_FACTORY.createMultiPolygon(polyArray);
203 240
	}
204 241

  
205 242
	public static boolean isClosed(Geometry geom) {
......
222 259

  
223 260
			Geometry[] solutionG = GeometryFactory
224 261
					.toGeometryArray(solutionGeoms);
225
			solution = geomFactory.createGeometryCollection(solutionG);
262
			solution = GEOMETRY_FACTORY.createGeometryCollection(solutionG);
226 263
			return solution;
227 264
		} else if (geom instanceof LineString) {
228 265
			LineString solution = null;
......
232 269
					.getCoordinateN(tempGeom.getNumPoints() - 1);
233 270
			if (!SnapCGAlgorithms.snapEquals2D(start, end, snapTolerance)) {
234 271
				Coordinate[] coordinates = { start, end };
235
				solution = geomFactory.createLineString(coordinates);
272
				solution = GEOMETRY_FACTORY.createLineString(coordinates);
236 273
			}
237 274
			return solution;
238 275

  
......
255 292
			Geometry[] solutionArray = GeometryFactory
256 293
					.toGeometryArray(solutionGeoms);
257 294
			if (solutionArray.length > 0)
258
				solution = geomFactory.createGeometryCollection(solutionArray);
295
				solution = GEOMETRY_FACTORY.createGeometryCollection(solutionArray);
259 296
			return solution;
260 297
		}// else
261 298

  
......
352 389
	public static LinearRing reverse(LinearRing ring) {
353 390
		CoordinateSequence seq = ring.getCoordinateSequence();
354 391
		CoordinateSequences.reverse(seq);
355
		LinearRing solution = geomFactory.createLinearRing(seq);
392
		LinearRing solution = GEOMETRY_FACTORY.createLinearRing(seq);
356 393
		return solution;
357 394
	}
358 395

  
......
368 405
			for (int i = 0; i < geometries.length; i++) {
369 406
				theGeoms[i] = ((IGeometry) geometries[i]).toJTSGeometry();
370 407
			}
371
			solution = geomFactory.createGeometryCollection(theGeoms);
408
			solution = GEOMETRY_FACTORY.createGeometryCollection(theGeoms);
372 409

  
373 410
		} else if (fmapGeometry instanceof FMultiPoint2D) {
374 411
			solution = ((FMultiPoint2D) fmapGeometry).toJTSGeometry();

Also available in: Unified diff