Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / operation / isCCW / IsCCW.java @ 30323

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
/*
24
* AUTHORS (In addition to CIT):
25
* 2008 PRODEVELOP S.L. Main Development
26
*/
27
 
28
/**
29
 * 
30
 */
31
package org.gvsig.fmap.geom.operation.isCCW;
32

    
33
import java.awt.geom.PathIterator;
34

    
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.GeometryManager;
38
import org.gvsig.fmap.geom.operation.GeometryOperation;
39
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
40
import org.gvsig.fmap.geom.operation.GeometryOperationException;
41
import org.gvsig.fmap.geom.primitive.GeneralPathX;
42
import org.gvsig.fmap.geom.util.Converter;
43

    
44
import com.vividsolutions.jts.algorithm.CGAlgorithms;
45
import com.vividsolutions.jts.geom.Coordinate;
46
import com.vividsolutions.jts.geom.CoordinateList;
47

    
48
/**
49
 * This class checks if the first part from the General Path of a complex geometry is CCW.
50
 * @return Boolean <code>true<code> if is CCW
51
 * @author Carlos S?nchez Peri??n <a href = "mailto:csanchez@prodevelop.es"> e-mail </a>
52
 */
53
public class IsCCW extends GeometryOperation{
54
    public static final String NAME = "isCCW";
55
    private static GeometryManager geomManager = GeometryLocator.getGeometryManager();
56
    public static final int CODE = geomManager.getGeometryOperationCode(NAME);
57
        
58
        private GeneralPathX generalPathX = null;        
59
        
60
        public int getOperationIndex() {
61
                return CODE;
62
        }
63

    
64
        public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException {
65
                generalPathX = geom.getGeneralPath();
66
                if(generalPathX == null){
67
                        //if there isn't path the operation hasn't sense.
68
                        return null;
69
            }
70
                PathIterator theIterator = generalPathX.getPathIterator(null, geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness);
71
                double[] theData = new double[6];
72
        Coordinate first = null;
73
        CoordinateList coordList = new CoordinateList();
74
        Coordinate c1;
75
        boolean bFirst = true;
76
                while (!theIterator.isDone()) {
77
                        //while not done
78
                        int type = theIterator.currentSegment(theData);
79
                switch (type)
80
                {
81
                case GeneralPathX.SEG_MOVETO:
82
                        c1= new Coordinate(theData[0], theData[1]);
83
                        if (bFirst == false) // Ya tenemos la primera parte.
84
                                break;
85
                        if (bFirst)
86
                        {
87
                                bFirst=false;
88
                                first = c1;
89
                        }
90
                        coordList.add(c1, true);
91
                        break;
92
                case GeneralPathX.SEG_LINETO:
93
                        c1= new Coordinate(theData[0], theData[1]);
94
                        coordList.add(c1, true);
95
                        break;
96

    
97
                }
98
                theIterator.next();
99
                }
100
                coordList.add(first, true);
101
                return new Boolean(CGAlgorithms.isCCW(coordList.toCoordinateArray()));
102
        }
103

    
104
}