svn-gvsig-desktop / 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 / utils / JTSUtils.java @ 42260
History | View | Annotate | Download (7.01 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2015 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.geom.jts.utils; |
24 |
|
25 |
import com.vividsolutions.jts.geom.Coordinate; |
26 |
import com.vividsolutions.jts.geom.CoordinateSequence; |
27 |
import com.vividsolutions.jts.geom.LineString; |
28 |
import com.vividsolutions.jts.geom.LinearRing; |
29 |
import com.vividsolutions.jts.geom.MultiLineString; |
30 |
import com.vividsolutions.jts.geom.Polygon; |
31 |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
33 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
34 |
import org.gvsig.fmap.geom.jts.MCoordinate; |
35 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D; |
36 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM; |
37 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3D; |
38 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3DM; |
39 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2D; |
40 |
import org.gvsig.fmap.geom.jts.primitive.point.Point2DM; |
41 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3D; |
42 |
import org.gvsig.fmap.geom.jts.primitive.point.Point3DM; |
43 |
import org.gvsig.fmap.geom.primitive.Point; |
44 |
import org.gvsig.fmap.geom.type.GeometryType; |
45 |
|
46 |
/**
|
47 |
* @author fdiaz
|
48 |
*
|
49 |
*/
|
50 |
public class JTSUtils { |
51 |
|
52 |
public static final com.vividsolutions.jts.geom.GeometryFactory factory = |
53 |
new com.vividsolutions.jts.geom.GeometryFactory();
|
54 |
|
55 |
public static Point createPoint(GeometryType type, Coordinate coordinate) throws CreateGeometryException { |
56 |
|
57 |
switch (type.getSubType()) {
|
58 |
case Geometry.SUBTYPES.GEOM2D:
|
59 |
return new Point2D(coordinate); |
60 |
case Geometry.SUBTYPES.GEOM2DM:
|
61 |
return new Point2DM(coordinate); |
62 |
case Geometry.SUBTYPES.GEOM3D:
|
63 |
return new Point3D(coordinate); |
64 |
case Geometry.SUBTYPES.GEOM3DM:
|
65 |
return new Point3DM(coordinate); |
66 |
default:
|
67 |
Point p = null; |
68 |
p = (Point) type.create();
|
69 |
for (int i = 0; i < p.getDimension(); i++) { |
70 |
p.setCoordinateAt(i, coordinate.getOrdinate(i)); |
71 |
} |
72 |
break;
|
73 |
} |
74 |
|
75 |
return null; |
76 |
} |
77 |
|
78 |
public static LineString createJTSLineString(CoordinateSequence coordinates){ |
79 |
return factory.createLineString(coordinates);
|
80 |
} |
81 |
|
82 |
public static Polygon createJTSPolygon(CoordinateSequence coordinates){ |
83 |
return factory.createPolygon(coordinates);
|
84 |
} |
85 |
|
86 |
public static Geometry createGeometry(com.vividsolutions.jts.geom.Geometry jtsGeom) { |
87 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Point){ |
88 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
89 |
if(coordinate instanceof MCoordinate){ |
90 |
if(Double.isNaN(coordinate.z)){ |
91 |
return new Point2DM(coordinate); |
92 |
} else {
|
93 |
return new Point3DM(coordinate); |
94 |
} |
95 |
} else {
|
96 |
if(Double.isNaN(coordinate.z)){ |
97 |
return new Point2D(coordinate); |
98 |
} else {
|
99 |
return new Point3D(coordinate); |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.LineString){ |
105 |
Coordinate[] coordinates = jtsGeom.getCoordinates();
|
106 |
Coordinate coordinate = jtsGeom.getCoordinate(); |
107 |
if(coordinate instanceof MCoordinate){ |
108 |
if(Double.isNaN(coordinate.z)){ |
109 |
return new Line2DM(coordinates); |
110 |
} else {
|
111 |
return new Line3DM(coordinates); |
112 |
} |
113 |
} else {
|
114 |
if(Double.isNaN(coordinate.z)){ |
115 |
return new Line2D(coordinates); |
116 |
} else {
|
117 |
return new Line3D(coordinates); |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.Polygon){ |
123 |
|
124 |
} |
125 |
|
126 |
if (jtsGeom instanceof com.vividsolutions.jts.geom.GeometryCollection){ |
127 |
|
128 |
} |
129 |
|
130 |
|
131 |
return null; |
132 |
} |
133 |
|
134 |
/**
|
135 |
* This function is called when the we need force types, that is the
|
136 |
* destination
|
137 |
* type does not match with the input geometry type
|
138 |
*
|
139 |
* @param g
|
140 |
* @param sourceType
|
141 |
* @param destinationType
|
142 |
* @return
|
143 |
*/
|
144 |
public static com.vividsolutions.jts.geom.Geometry convertTypes(com.vividsolutions.jts.geom.Geometry g, |
145 |
int sourceType, int destinationType) { |
146 |
if ((sourceType == Geometry.TYPES.CURVE || sourceType == Geometry.TYPES.SPLINE
|
147 |
|| sourceType == Geometry.TYPES.ARC || sourceType == Geometry.TYPES.ELLIPTICARC) |
148 |
&& destinationType == Geometry.TYPES.MULTISURFACE) { |
149 |
if (g instanceof MultiLineString) { |
150 |
Polygon[] poly = new Polygon[((MultiLineString) g).getNumGeometries()]; |
151 |
for (int i = 0; i < ((MultiLineString) g).getNumGeometries(); i++) { |
152 |
com.vividsolutions.jts.geom.Geometry lineString = ((MultiLineString) g).getGeometryN(i); |
153 |
poly[i] = convertLineStringToPolygon((LineString) lineString); |
154 |
} |
155 |
return factory.createMultiPolygon(poly);
|
156 |
} else {
|
157 |
return convertLineStringToPolygon((LineString) g);
|
158 |
} |
159 |
} |
160 |
|
161 |
if ((sourceType == Geometry.TYPES.CIRCLE || sourceType == Geometry.TYPES.ELLIPSE)
|
162 |
&& destinationType == Geometry.TYPES.MULTICURVE) { |
163 |
if (g instanceof Polygon) { |
164 |
Polygon poly = (Polygon) g; |
165 |
LineString lineString = factory.createLinearRing(poly.getCoordinates()); |
166 |
return factory.createMultiLineString(new LineString[] { lineString }); |
167 |
} |
168 |
} |
169 |
return g;
|
170 |
} |
171 |
|
172 |
private static com.vividsolutions.jts.geom.Polygon convertLineStringToPolygon(LineString line) { |
173 |
Coordinate[] coordinates = line.getCoordinates();
|
174 |
LinearRing shell = factory.createLinearRing(coordinates); |
175 |
Polygon pol = factory.createPolygon(shell, null); |
176 |
return pol;
|
177 |
} |
178 |
|
179 |
public static MCoordinate createMCoordinate(double x, double y, double m) { |
180 |
return (MCoordinate) MCoordinate.create2dWithMeasure(x, y, m);
|
181 |
} |
182 |
|
183 |
public static MCoordinate createMCoordinate(double x, double y, double z, double m) { |
184 |
return (MCoordinate) MCoordinate.create3dWithMeasure(x, y, z, m);
|
185 |
} |
186 |
|
187 |
} |