Revision 42360 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.jts/src/main/java/org/gvsig/fmap/geom/jts/util/JTSUtils.java
JTSUtils.java | ||
---|---|---|
36 | 36 |
import com.vividsolutions.jts.geom.Triangle; |
37 | 37 |
import com.vividsolutions.jts.util.GeometricShapeFactory; |
38 | 38 |
|
39 |
import org.hibernate.spatial.jts.mgeom.MCoordinateSequence; |
|
40 |
import org.hibernate.spatial.jts.mgeom.MGeometryFactory; |
|
39 | 41 |
import org.slf4j.Logger; |
40 | 42 |
import org.slf4j.LoggerFactory; |
41 | 43 |
|
... | ... | |
88 | 90 |
|
89 | 91 |
public static final Logger logger = LoggerFactory.getLogger(JTSUtils.class); |
90 | 92 |
|
91 |
public static final com.vividsolutions.jts.geom.GeometryFactory factory = |
|
93 |
private static class MyMGeometryFactory extends org.hibernate.spatial.jts.mgeom.MGeometryFactory { |
|
94 |
|
|
95 |
/** |
|
96 |
* |
|
97 |
*/ |
|
98 |
private static final long serialVersionUID = -8174926092714691479L; |
|
99 |
|
|
100 |
public com.vividsolutions.jts.geom.Point createPoint(CoordinateSequence coordinates) { |
|
101 |
if(!(coordinates instanceof MCoordinateSequence)){ |
|
102 |
if (coordinates != null) { |
|
103 |
coordinates = new MCoordinateSequence(coordinates); |
|
104 |
} |
|
105 |
} |
|
106 |
return super.createPoint(coordinates); |
|
107 |
} |
|
108 |
|
|
109 |
public LinearRing createLinearRing(CoordinateSequence coordinates) { |
|
110 |
if(!(coordinates instanceof MCoordinateSequence)){ |
|
111 |
if (coordinates != null) { |
|
112 |
coordinates = new MCoordinateSequence(coordinates); |
|
113 |
} |
|
114 |
} |
|
115 |
return super.createLinearRing(coordinates); |
|
116 |
} |
|
117 |
|
|
118 |
@Override |
|
119 |
public LineString createLineString(CoordinateSequence coordinates) { |
|
120 |
if(!(coordinates instanceof MCoordinateSequence)){ |
|
121 |
if (coordinates != null) { |
|
122 |
coordinates = new MCoordinateSequence(coordinates); |
|
123 |
} |
|
124 |
} |
|
125 |
return super.createLineString(coordinates); |
|
126 |
} |
|
127 |
|
|
128 |
} |
|
129 |
|
|
130 |
|
|
131 |
private static final com.vividsolutions.jts.geom.GeometryFactory factory = |
|
92 | 132 |
new com.vividsolutions.jts.geom.GeometryFactory(); |
93 | 133 |
|
134 |
private static final com.vividsolutions.jts.geom.GeometryFactory mfactory = new MyMGeometryFactory(); |
|
135 |
|
|
94 | 136 |
public static Point createPoint(GeometryType type, Coordinate coordinate) throws CreateGeometryException { |
95 | 137 |
|
96 | 138 |
switch (type.getSubType()) { |
... | ... | |
114 | 156 |
return null; |
115 | 157 |
} |
116 | 158 |
|
159 |
private static com.vividsolutions.jts.geom.GeometryFactory getFactory(CoordinateSequence coordinates){ |
|
160 |
com.vividsolutions.jts.geom.GeometryFactory factory = JTSUtils.factory; |
|
161 |
if(coordinates.size()>0 && coordinates.getCoordinate(0) instanceof org.hibernate.spatial.jts.mgeom.MCoordinate){ |
|
162 |
factory = mfactory; |
|
163 |
} |
|
164 |
return factory; |
|
165 |
} |
|
166 |
|
|
167 |
public static com.vividsolutions.jts.geom.GeometryFactory getFactory(Coordinate coordinate) { |
|
168 |
com.vividsolutions.jts.geom.GeometryFactory factory = JTSUtils.factory; |
|
169 |
if(coordinate instanceof org.hibernate.spatial.jts.mgeom.MCoordinate){ |
|
170 |
factory = mfactory; |
|
171 |
} |
|
172 |
return factory; |
|
173 |
} |
|
174 |
|
|
175 |
public static com.vividsolutions.jts.geom.GeometryFactory getFactory(Coordinate[] coordinates) { |
|
176 |
com.vividsolutions.jts.geom.GeometryFactory factory = JTSUtils.factory; |
|
177 |
if(coordinates.length>0 && coordinates[0] instanceof org.hibernate.spatial.jts.mgeom.MCoordinate){ |
|
178 |
factory = mfactory; |
|
179 |
} |
|
180 |
return factory; |
|
181 |
} |
|
182 |
|
|
117 | 183 |
public static LineString createJTSLineString(CoordinateSequence coordinates) { |
118 |
return factory.createLineString(coordinates);
|
|
184 |
return getFactory(coordinates).createLineString(coordinates);
|
|
119 | 185 |
} |
120 | 186 |
|
121 | 187 |
public static LinearRing createJTSLinearRing(CoordinateSequence coordinates) { |
122 |
return factory.createLinearRing(coordinates);
|
|
188 |
return getFactory(coordinates).createLinearRing(coordinates);
|
|
123 | 189 |
} |
124 | 190 |
|
125 | 191 |
public static MultiPoint createJTSMultiPoint(CoordinateSequence coordinates) { |
126 |
return factory.createMultiPoint(coordinates);
|
|
192 |
return getFactory(coordinates).createMultiPoint(coordinates);
|
|
127 | 193 |
} |
128 | 194 |
|
129 | 195 |
public static Geometry createGeometry(com.vividsolutions.jts.geom.Geometry jtsGeom) { |
130 | 196 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Point) { |
131 |
com.vividsolutions.jts.geom.Point point = (com.vividsolutions.jts.geom.Point)jtsGeom; |
|
132 | 197 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
133 |
if (coordinate instanceof MCoordinate) {
|
|
198 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) {
|
|
134 | 199 |
if (Double.isNaN(coordinate.z)) { |
135 | 200 |
return new Point2DM(coordinate); |
136 | 201 |
} else { |
... | ... | |
148 | 213 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.LineString) { |
149 | 214 |
Coordinate[] coordinates = jtsGeom.getCoordinates(); |
150 | 215 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
151 |
if (coordinate instanceof MCoordinate) { |
|
152 |
if (Double.isNaN(coordinate.z)) { |
|
153 |
return new Line2DM(coordinates); |
|
216 |
com.vividsolutions.jts.geom.LineString lineString = (com.vividsolutions.jts.geom.LineString) jtsGeom; |
|
217 |
if (!lineString.isEmpty()) { |
|
218 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) { |
|
219 |
if (coordinate!=null && Double.isNaN(coordinate.z)) { |
|
220 |
return new Line2DM(coordinates); |
|
221 |
} else { |
|
222 |
return new Line3DM(coordinates); |
|
223 |
} |
|
154 | 224 |
} else { |
155 |
return new Line3DM(coordinates); |
|
225 |
if (coordinate!=null && Double.isNaN(coordinate.z)) { |
|
226 |
return new Line2D(coordinates); |
|
227 |
} else { |
|
228 |
return new Line3D(coordinates); |
|
229 |
} |
|
156 | 230 |
} |
157 |
} else { |
|
158 |
if (Double.isNaN(coordinate.z)) { |
|
159 |
return new Line2D(coordinates); |
|
160 |
} else { |
|
161 |
return new Line3D(coordinates); |
|
162 |
} |
|
163 | 231 |
} |
164 | 232 |
} |
165 | 233 |
|
... | ... | |
168 | 236 |
com.vividsolutions.jts.geom.Polygon polygonJTS = (com.vividsolutions.jts.geom.Polygon)jtsGeom; |
169 | 237 |
Coordinate[] coordinates = polygonJTS.getExteriorRing().getCoordinates(); |
170 | 238 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
171 |
if (coordinate instanceof MCoordinate) {
|
|
239 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) {
|
|
172 | 240 |
if (Double.isNaN(coordinate.z)) { |
173 | 241 |
polygon = new Polygon2DM(coordinates); |
174 | 242 |
} else { |
... | ... | |
186 | 254 |
coordinates = ringJTS.getCoordinates(); |
187 | 255 |
coordinate = ringJTS.getCoordinate(); |
188 | 256 |
Ring ring; |
189 |
if (coordinate instanceof MCoordinate) {
|
|
257 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) {
|
|
190 | 258 |
if (Double.isNaN(coordinate.z)) { |
191 | 259 |
ring = new Ring2DM(coordinates); |
192 | 260 |
} else { |
... | ... | |
209 | 277 |
Coordinate coordinate = collection.getCoordinate(); |
210 | 278 |
MultiPrimitive multiprimitive = null; |
211 | 279 |
if (jtsGeom instanceof MultiLineString) { |
212 |
if (coordinate instanceof MCoordinate) {
|
|
280 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) {
|
|
213 | 281 |
if (Double.isNaN(coordinate.z)) { |
214 | 282 |
multiprimitive = new MultiLine2DM(); |
215 | 283 |
} else { |
... | ... | |
224 | 292 |
} |
225 | 293 |
} |
226 | 294 |
if (jtsGeom instanceof MultiPolygon) { |
227 |
if (coordinate instanceof MCoordinate) {
|
|
295 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) {
|
|
228 | 296 |
if (Double.isNaN(coordinate.z)) { |
229 | 297 |
multiprimitive = new MultiPolygon2DM(); |
230 | 298 |
} else { |
... | ... | |
240 | 308 |
} |
241 | 309 |
|
242 | 310 |
if (jtsGeom instanceof MultiPoint) { |
243 |
if (coordinate instanceof MCoordinate) {
|
|
311 |
if (jtsGeom.getFactory() instanceof MyMGeometryFactory) {
|
|
244 | 312 |
if (Double.isNaN(coordinate.z)) { |
245 | 313 |
multiprimitive = new MultiPoint2DM(); |
246 | 314 |
} else { |
... | ... | |
279 | 347 |
*/ |
280 | 348 |
public static com.vividsolutions.jts.geom.Geometry convertTypes(com.vividsolutions.jts.geom.Geometry g, |
281 | 349 |
int sourceType, int destinationType) { |
350 |
|
|
351 |
com.vividsolutions.jts.geom.GeometryFactory factory = g.getFactory(); |
|
352 |
|
|
282 | 353 |
if ((sourceType == Geometry.TYPES.CURVE || sourceType == Geometry.TYPES.SPLINE |
283 | 354 |
|| sourceType == Geometry.TYPES.ARC || sourceType == Geometry.TYPES.ELLIPTICARC |
284 | 355 |
|| sourceType == Geometry.TYPES.CIRCUMFERENCE || sourceType == Geometry.TYPES.PERIELLIPSE) |
... | ... | |
309 | 380 |
|
310 | 381 |
private static com.vividsolutions.jts.geom.Polygon convertLineStringToPolygon(LineString line) { |
311 | 382 |
Coordinate[] coordinates = line.getCoordinates(); |
383 |
com.vividsolutions.jts.geom.GeometryFactory factory = line.getFactory(); |
|
312 | 384 |
LinearRing shell = factory.createLinearRing(coordinates); |
313 | 385 |
com.vividsolutions.jts.geom.Polygon pol = factory.createPolygon(shell, null); |
314 | 386 |
return pol; |
... | ... | |
344 | 416 |
} |
345 | 417 |
|
346 | 418 |
public static MultiLineString createJTSMultiLineString(LineString[] lineStrings) { |
419 |
com.vividsolutions.jts.geom.GeometryFactory factory = JTSUtils.factory; |
|
420 |
if(lineStrings.length>0){ |
|
421 |
factory = lineStrings[0].getFactory(); |
|
422 |
} |
|
347 | 423 |
return factory.createMultiLineString(lineStrings); |
348 | 424 |
} |
349 | 425 |
|
... | ... | |
355 | 431 |
*/ |
356 | 432 |
public static com.vividsolutions.jts.geom.Polygon createJTSPolygon(ArrayListCoordinateSequence coordinates, |
357 | 433 |
List<Ring> interiorRings) { |
434 |
com.vividsolutions.jts.geom.GeometryFactory factory = getFactory(coordinates); |
|
435 |
|
|
358 | 436 |
LinearRing shell = factory.createLinearRing(coordinates); |
359 | 437 |
LinearRing[] holes = new LinearRing[interiorRings.size()]; |
360 | 438 |
for(int i=0; i<interiorRings.size(); i++){ |
... | ... | |
371 | 449 |
* @throws GeometryException |
372 | 450 |
*/ |
373 | 451 |
public static com.vividsolutions.jts.geom.Polygon createJTSPolygon(ArrayListCoordinateSequence coordinates) { |
452 |
com.vividsolutions.jts.geom.GeometryFactory factory = getFactory(coordinates); |
|
453 |
|
|
374 | 454 |
LinearRing shell = factory.createLinearRing(coordinates); |
375 | 455 |
return factory.createPolygon(shell); |
376 | 456 |
} |
377 | 457 |
|
378 | 458 |
public static com.vividsolutions.jts.geom.MultiPolygon createJTSMultiPolygon(com.vividsolutions.jts.geom.Polygon[] polygons){ |
459 |
com.vividsolutions.jts.geom.GeometryFactory factory = JTSUtils.factory; |
|
460 |
if(polygons.length>0){ |
|
461 |
factory = polygons[0].getFactory(); |
|
462 |
} |
|
379 | 463 |
return factory.createMultiPolygon(polygons); |
380 | 464 |
} |
381 | 465 |
|
... | ... | |
428 | 512 |
* @return |
429 | 513 |
*/ |
430 | 514 |
public static com.vividsolutions.jts.geom.Geometry createJTSPoint(Coordinate coordinate) { |
431 |
|
|
432 |
return factory.createPoint(coordinate); |
|
515 |
return getFactory(coordinate).createPoint(coordinate); |
|
433 | 516 |
} |
434 | 517 |
|
435 | 518 |
/** |
... | ... | |
437 | 520 |
* @return |
438 | 521 |
*/ |
439 | 522 |
public static com.vividsolutions.jts.geom.Geometry createJTSPolygon(Coordinate[] coordinates) { |
440 |
|
|
441 |
return factory.createPolygon(coordinates); |
|
523 |
return getFactory(coordinates).createPolygon(coordinates); |
|
442 | 524 |
} |
443 | 525 |
|
444 | 526 |
|
Also available in: Unified diff