Revision 42441 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 | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.fmap.geom.jts.util; |
24 | 24 |
|
25 |
import java.util.ArrayList; |
|
25 | 26 |
import java.util.List; |
26 | 27 |
|
27 | 28 |
import com.vividsolutions.jts.geom.Coordinate; |
... | ... | |
34 | 35 |
import com.vividsolutions.jts.geom.MultiPoint; |
35 | 36 |
import com.vividsolutions.jts.geom.MultiPolygon; |
36 | 37 |
import com.vividsolutions.jts.geom.Triangle; |
38 |
import com.vividsolutions.jts.operation.buffer.BufferParameters; |
|
39 |
import com.vividsolutions.jts.operation.buffer.OffsetCurveBuilder; |
|
37 | 40 |
import com.vividsolutions.jts.util.GeometricShapeFactory; |
38 | 41 |
|
39 | 42 |
import org.hibernate.spatial.jts.mgeom.MCoordinateSequence; |
40 |
import org.hibernate.spatial.jts.mgeom.MGeometryFactory; |
|
41 | 43 |
import org.slf4j.Logger; |
42 | 44 |
import org.slf4j.LoggerFactory; |
43 | 45 |
|
... | ... | |
156 | 158 |
return null; |
157 | 159 |
} |
158 | 160 |
|
159 |
private static com.vividsolutions.jts.geom.GeometryFactory getFactory(CoordinateSequence coordinates){
|
|
161 |
public static com.vividsolutions.jts.geom.GeometryFactory getFactory(CoordinateSequence coordinates){
|
|
160 | 162 |
com.vividsolutions.jts.geom.GeometryFactory factory = JTSUtils.factory; |
161 | 163 |
if(coordinates.size()>0 && coordinates.getCoordinate(0) instanceof org.hibernate.spatial.jts.mgeom.MCoordinate){ |
162 | 164 |
factory = mfactory; |
... | ... | |
231 | 233 |
} |
232 | 234 |
} |
233 | 235 |
|
234 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Polygon) { |
|
236 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Polygon && !jtsGeom.isEmpty()) {
|
|
235 | 237 |
Polygon polygon; |
236 | 238 |
com.vividsolutions.jts.geom.Polygon polygonJTS = (com.vividsolutions.jts.geom.Polygon)jtsGeom; |
237 | 239 |
Coordinate[] coordinates = polygonJTS.getExteriorRing().getCoordinates(); |
... | ... | |
523 | 525 |
return getFactory(coordinates).createPolygon(coordinates); |
524 | 526 |
} |
525 | 527 |
|
528 |
public static BufferParameters getBufferParameters(){ |
|
529 |
BufferParameters bufParams = new BufferParameters(); |
|
530 |
bufParams.setSingleSided(true); |
|
531 |
bufParams.setEndCapStyle(BufferParameters.CAP_FLAT); |
|
532 |
bufParams.setJoinStyle(BufferParameters.CAP_ROUND); |
|
533 |
return bufParams; |
|
534 |
} |
|
526 | 535 |
|
536 |
|
|
537 |
/** |
|
538 |
* Creates a offset from a closed line at a distance |
|
539 |
* |
|
540 |
* @param distance |
|
541 |
* @return |
|
542 |
*/ |
|
543 |
public static Geometry offsetClosedLine(ArrayListCoordinateSequence coordinates, double distance) { |
|
544 |
com.vividsolutions.jts.geom.Geometry jtsGeom = JTSUtils.createJTSPolygon(coordinates); |
|
545 |
com.vividsolutions.jts.geom.Geometry geomJTS = jtsGeom.buffer(distance); |
|
546 |
if (geomJTS instanceof com.vividsolutions.jts.geom.Polygon) { |
|
547 |
com.vividsolutions.jts.geom.Polygon polygonJTS = (com.vividsolutions.jts.geom.Polygon) geomJTS; |
|
548 |
LineString shell = polygonJTS.getExteriorRing(); |
|
549 |
int numHoles = polygonJTS.getNumInteriorRing(); |
|
550 |
if (numHoles > 0) { |
|
551 |
LineString[] lineStrings = new LineString[numHoles + 1]; |
|
552 |
lineStrings[0] = shell; |
|
553 |
for (int i = 0; i < numHoles; i++) { |
|
554 |
LineString hole = polygonJTS.getInteriorRingN(i); |
|
555 |
lineStrings[i + 1] = hole; |
|
556 |
} |
|
557 |
return JTSUtils.createGeometry(JTSUtils.createJTSMultiLineString(lineStrings)); |
|
558 |
} |
|
559 |
return JTSUtils.createGeometry(shell); |
|
560 |
} else if (geomJTS instanceof com.vividsolutions.jts.geom.MultiPolygon) { |
|
561 |
MultiPolygon multiPolygonJTS = (com.vividsolutions.jts.geom.MultiPolygon) geomJTS; |
|
562 |
List<LineString> lineStringList = new ArrayList<LineString>(); |
|
563 |
for (int i = 0; i < multiPolygonJTS.getNumGeometries(); i++) { |
|
564 |
com.vividsolutions.jts.geom.Polygon polygonJTS = |
|
565 |
(com.vividsolutions.jts.geom.Polygon) multiPolygonJTS.getGeometryN(i); |
|
566 |
lineStringList.add(polygonJTS.getExteriorRing()); |
|
567 |
int numHoles = polygonJTS.getNumInteriorRing(); |
|
568 |
if (numHoles > 0) { |
|
569 |
for (int h = 0; h < numHoles; h++) { |
|
570 |
LineString hole = polygonJTS.getInteriorRingN(i); |
|
571 |
lineStringList.add(hole); |
|
572 |
} |
|
573 |
} |
|
574 |
} |
|
575 |
return JTSUtils.createGeometry(JTSUtils.createJTSMultiLineString(lineStringList.toArray(new LineString[lineStringList.size()]))); |
|
576 |
} |
|
577 |
//No deber?a pasar por aqu? |
|
578 |
logger.warn("offsetClosedLine does not return or Polygon JTS or MultiPolygon JTS"); |
|
579 |
return null; |
|
580 |
} |
|
581 |
|
|
582 |
/** |
|
583 |
* Creates a raw offset of a open line at a distance. |
|
584 |
* |
|
585 |
* @param coordinates |
|
586 |
* @param distance |
|
587 |
* @return |
|
588 |
*/ |
|
589 |
public static Geometry offsetRawOpenLine(ArrayListCoordinateSequence coordinates, double distance) { |
|
590 |
com.vividsolutions.jts.geom.GeometryFactory factory = getFactory(coordinates); |
|
591 |
|
|
592 |
BufferParameters bufParams = JTSUtils.getBufferParameters(); |
|
593 |
LineString jtsGeom = JTSUtils.createJTSLineString(coordinates); |
|
594 |
|
|
595 |
OffsetCurveBuilder ocb = new |
|
596 |
OffsetCurveBuilder(factory.getPrecisionModel(), bufParams); |
|
597 |
Coordinate[] coords = ocb.getOffsetCurve(jtsGeom.getCoordinates(), distance); |
|
598 |
CoordinateSequence coordSequence = factory.getCoordinateSequenceFactory().create(coords); |
|
599 |
com.vividsolutions.jts.geom.Geometry offsetGeom = JTSUtils.createJTSLineString(coordSequence); |
|
600 |
|
|
601 |
return JTSUtils.createGeometry(offsetGeom); |
|
602 |
} |
|
603 |
|
|
604 |
/* |
|
605 |
* |
|
606 |
*/ |
|
607 |
public static double straightLineThroughTwoPointsEquation(double x1, double x2, double y1, double y2, double x) { |
|
608 |
if (x2 - x1 == 0.0) { |
|
609 |
return Double.POSITIVE_INFINITY; |
|
610 |
} |
|
611 |
return ((y2 - y1) * (x - x1) / (x2 - x1)) + y1; |
|
612 |
} |
|
613 |
|
|
527 | 614 |
} |
Also available in: Unified diff