svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.operation / src / main / java / org / gvsig / fmap / geom / operation / tojts / Curve2DToJTS.java @ 40767
History | View | Annotate | Download (4.17 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.geom.operation.tojts; |
26 |
|
27 |
import java.awt.geom.PathIterator; |
28 |
import java.util.ArrayList; |
29 |
|
30 |
import org.gvsig.fmap.geom.Geometry; |
31 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
32 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
33 |
import org.gvsig.fmap.geom.primitive.Point; |
34 |
|
35 |
import com.vividsolutions.jts.geom.Coordinate; |
36 |
import com.vividsolutions.jts.geom.CoordinateArrays; |
37 |
import com.vividsolutions.jts.geom.GeometryFactory; |
38 |
import com.vividsolutions.jts.geom.LineString; |
39 |
import com.vividsolutions.jts.geom.MultiLineString; |
40 |
|
41 |
/**
|
42 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
43 |
*/
|
44 |
public class Curve2DToJTS extends ToJTS{ |
45 |
|
46 |
/*
|
47 |
* (non-Javadoc)
|
48 |
* @see org.gvsig.fmap.geom.operation.tojts.ToJTS#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
49 |
*/
|
50 |
public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException { |
51 |
int srid = -1; |
52 |
if (ctx != null){ |
53 |
srid = ((JTSGeometryOperationContext)ctx).getSrid(); |
54 |
} |
55 |
ArrayList arrayLines = new ArrayList(); |
56 |
PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness()); |
57 |
int theType;
|
58 |
double[] theData = new double[6]; |
59 |
ArrayList arrayCoords = null; |
60 |
LineString lin; |
61 |
int numParts = 0; |
62 |
Coordinate coord; |
63 |
|
64 |
while (!theIterator.isDone()) {
|
65 |
//while not done
|
66 |
theType = theIterator.currentSegment(theData); |
67 |
|
68 |
//Populate a segment of the new
|
69 |
// GeneralPathX object.
|
70 |
//Process the current segment to populate a new
|
71 |
// segment of the new GeneralPathX object.
|
72 |
switch (theType) {
|
73 |
case PathIterator.SEG_MOVETO: |
74 |
|
75 |
// System.out.println("SEG_MOVETO");
|
76 |
if (arrayCoords == null) { |
77 |
arrayCoords = new ArrayList(); |
78 |
} else {
|
79 |
lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray( |
80 |
arrayCoords)); |
81 |
lin.setSRID(srid); |
82 |
arrayLines.add(lin); |
83 |
arrayCoords = new ArrayList(); |
84 |
} |
85 |
|
86 |
numParts++; |
87 |
coord = new Coordinate(theData[0], theData[1]); |
88 |
|
89 |
arrayCoords.add(coord); |
90 |
|
91 |
break;
|
92 |
|
93 |
case PathIterator.SEG_LINETO: |
94 |
|
95 |
// System.out.println("SEG_LINETO");
|
96 |
arrayCoords.add(new Coordinate(theData[0], |
97 |
theData[1]));
|
98 |
|
99 |
break;
|
100 |
|
101 |
case PathIterator.SEG_QUADTO: |
102 |
System.out.println("Not supported here"); |
103 |
|
104 |
break;
|
105 |
|
106 |
case PathIterator.SEG_CUBICTO: |
107 |
System.out.println("Not supported here"); |
108 |
|
109 |
break;
|
110 |
|
111 |
case PathIterator.SEG_CLOSE: |
112 |
// A�adimos el primer punto para cerrar.
|
113 |
Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
114 |
// Solo anyadimos cuando no esta ya cerrado
|
115 |
arrayCoords.add(new Coordinate(firstCoord.x,
|
116 |
firstCoord.y)); |
117 |
|
118 |
break;
|
119 |
} //end switch
|
120 |
|
121 |
theIterator.next(); |
122 |
} //end while loop
|
123 |
|
124 |
if (arrayCoords.size()<2) { |
125 |
throw new GeometryOperationException(geom.getType(), geom.getGeometryType().getSubType()); |
126 |
} |
127 |
lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray( |
128 |
arrayCoords)); |
129 |
|
130 |
lin.setSRID(srid); |
131 |
arrayLines.add(lin); |
132 |
|
133 |
LineString[] lineString = GeometryFactory.toLineStringArray(arrayLines);
|
134 |
|
135 |
if (lineString.length == 1) { |
136 |
return lineString[0]; |
137 |
} else {
|
138 |
MultiLineString resp = geomFactory.createMultiLineString(lineString); |
139 |
resp.setSRID(srid); |
140 |
return resp;
|
141 |
} |
142 |
} |
143 |
} |