Statistics
| Revision:

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 @ 40559

History | View | Annotate | Download (5.1 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
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2009 {Iver T.I.}   {Task}
49
*/
50
 
51
package org.gvsig.fmap.geom.operation.tojts;
52

    
53
import java.awt.geom.PathIterator;
54
import java.util.ArrayList;
55

    
56
import org.gvsig.fmap.geom.Geometry;
57
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
58
import org.gvsig.fmap.geom.operation.GeometryOperationException;
59
import org.gvsig.fmap.geom.primitive.Point;
60

    
61
import com.vividsolutions.jts.geom.Coordinate;
62
import com.vividsolutions.jts.geom.CoordinateArrays;
63
import com.vividsolutions.jts.geom.GeometryFactory;
64
import com.vividsolutions.jts.geom.LineString;
65
import com.vividsolutions.jts.geom.MultiLineString;
66

    
67
/**
68
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
69
 */
70
public class Curve2DToJTS extends ToJTS{
71
    
72
        /*
73
         * (non-Javadoc)
74
         * @see org.gvsig.fmap.geom.operation.tojts.ToJTS#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
75
         */
76
        public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException {
77
                int srid = -1;
78
                if (ctx != null){
79
                        srid = ((JTSGeometryOperationContext)ctx).getSrid();
80
                }
81
                ArrayList arrayLines = new ArrayList();
82
                PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness());
83
                int theType;
84
                double[] theData = new double[6];
85
                ArrayList arrayCoords = null;
86
                LineString lin;
87
                int numParts = 0;
88
                Coordinate coord;
89
                
90
                while (!theIterator.isDone()) {
91
                        //while not done
92
                        theType = theIterator.currentSegment(theData);
93

    
94
                        //Populate a segment of the new
95
                        // GeneralPathX object.
96
                        //Process the current segment to populate a new
97
                        // segment of the new GeneralPathX object.
98
                        switch (theType) {
99
                        case PathIterator.SEG_MOVETO:
100

    
101
                                // System.out.println("SEG_MOVETO");
102
                                if (arrayCoords == null) {
103
                                        arrayCoords = new ArrayList();
104
                                } else {
105
                                        lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
106
                                                        arrayCoords));
107
                                        lin.setSRID(srid);
108
                                        arrayLines.add(lin);
109
                                        arrayCoords = new ArrayList();
110
                                }
111

    
112
                                numParts++;
113
                                coord = new Coordinate(theData[0], theData[1]);
114

    
115
                                arrayCoords.add(coord);
116

    
117
                                break;
118

    
119
                        case PathIterator.SEG_LINETO:
120

    
121
                                // System.out.println("SEG_LINETO");
122
                                arrayCoords.add(new Coordinate(theData[0],
123
                                                theData[1]));
124

    
125
                                break;
126

    
127
                        case PathIterator.SEG_QUADTO:
128
                                System.out.println("Not supported here");
129

    
130
                                break;
131

    
132
                        case PathIterator.SEG_CUBICTO:
133
                                System.out.println("Not supported here");
134

    
135
                                break;
136

    
137
                        case PathIterator.SEG_CLOSE:
138
                                // A�adimos el primer punto para cerrar.
139
                                Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
140
                                        // Solo anyadimos cuando no esta ya cerrado
141
                                arrayCoords.add(new Coordinate(firstCoord.x,
142
                                                firstCoord.y));
143

    
144
                                break;
145
                        } //end switch
146

    
147
                        theIterator.next();
148
                } //end while loop
149

    
150
                if (arrayCoords.size()<2) {
151
                        throw new GeometryOperationException(geom.getType(), geom.getGeometryType().getSubType());
152
                }
153
                lin = geomFactory.createLineString(CoordinateArrays.toCoordinateArray(
154
                                arrayCoords));
155

    
156
                lin.setSRID(srid);
157
                arrayLines.add(lin);
158
                
159
                LineString[] lineString = GeometryFactory.toLineStringArray(arrayLines);
160
                
161
                if (lineString.length == 1) {
162
                    return lineString[0];
163
                } else {
164
                MultiLineString resp = geomFactory.createMultiLineString(lineString);
165
                resp.setSRID(srid);
166
                return resp;
167
                }
168
        }        
169
}