Statistics
| Revision:

root / tags / v2_0_0_Build_2050 / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / operation / tojts / Surface2DToJTSLineString.java @ 38677

History | View | Annotate | Download (3.56 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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 2
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
*/
22
 
23
package org.gvsig.fmap.geom.operation.tojts;
24

    
25
import java.awt.geom.PathIterator;
26
import java.util.ArrayList;
27

    
28
import org.gvsig.fmap.geom.Geometry;
29
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
30
import org.gvsig.fmap.geom.operation.GeometryOperationException;
31

    
32
import com.vividsolutions.jts.geom.Coordinate;
33
import com.vividsolutions.jts.geom.CoordinateArrays;
34
import com.vividsolutions.jts.geom.LinearRing;
35
import com.vividsolutions.jts.geom.MultiLineString;
36

    
37
/**
38
 * Converts a Surface2D into a MultiLineString object
39
 * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
40
 */
41
public class Surface2DToJTSLineString extends ToJTS {
42
        public static final String NAME = "toJTSLineString";
43
        public static final int CODE = geomManager.getGeometryOperationCode(NAME);
44
        
45
        /*
46
         * (non-Javadoc)
47
         * @see org.gvsig.fmap.geom.operation.tojts.ToJTS#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
48
         */
49
        public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException {
50
                int srid = -1;
51
                if (ctx != null){
52
                        srid = ((JTSGeometryOperationContext)ctx).getSrid();
53
                }
54
                PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness());
55
                int theType;
56
                double[] theData = new double[6];
57
                ArrayList arrayCoords = null;
58
                Coordinate[] points = null;
59
                ArrayList listRings = new ArrayList();
60
                theIterator = geom.getPathIterator(null, geomManager.getFlatness());
61

    
62
                while (!theIterator.isDone()) {
63
                        theType = theIterator.currentSegment(theData);
64

    
65
                        switch (theType) {
66
                        case PathIterator.SEG_MOVETO:
67
                                if (arrayCoords == null) {
68
                                        arrayCoords = new ArrayList();
69
                                } else
70
                                        arrayCoords.clear();
71
                                arrayCoords.add(new Coordinate(theData[0], theData[1]));
72
                                break;
73

    
74
                        case PathIterator.SEG_LINETO:
75
                                arrayCoords.add(new Coordinate(theData[0],theData[1]));
76
                                break;
77
                        case PathIterator.SEG_QUADTO:
78
                                System.out.println("SEG_QUADTO Not supported here");
79
                                break;
80
                        case PathIterator.SEG_CUBICTO:
81
                                System.out.println("SEG_CUBICTO Not supported here");
82
                                break;
83
                        case PathIterator.SEG_CLOSE:
84
                                break;
85
                        }
86
                        theIterator.next();
87
                } 
88
                
89
                Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
90
                Coordinate lastCoord = (Coordinate) arrayCoords.get(arrayCoords.size() - 1);
91
                if (!isClosed(firstCoord, lastCoord)) {
92
                        arrayCoords.add(firstCoord);
93
                }
94
                points = CoordinateArrays.toCoordinateArray(arrayCoords);
95
                listRings.add(geomFactory.createLinearRing(points));
96
                
97
                LinearRing[] list = (LinearRing[])listRings.toArray(new LinearRing[listRings.size()]);
98
                MultiLineString lineStr = new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(list);
99
                lineStr.setSRID(srid);
100
                return lineStr;
101
        }
102
}