Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / operation / ensureOrientation / EnsureOrientation.java @ 35472

History | View | Annotate | Download (7.43 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
package org.gvsig.fmap.geom.operation.ensureOrientation;
29

    
30
import com.vividsolutions.jts.algorithm.CGAlgorithms;
31
import com.vividsolutions.jts.geom.Coordinate;
32
import com.vividsolutions.jts.geom.CoordinateList;
33
import com.vividsolutions.jts.geom.CoordinateSequences;
34
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
35

    
36
import org.gvsig.fmap.geom.Geometry;
37
import org.gvsig.fmap.geom.GeometryLocator;
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

    
43
/**
44
 * Use this function class to ensure you get real polygons or holes
45
 * En JTS, con bCCW = false obtienes un poligono exterior.
46
 * Nota: Solo se le da la vuelta (si es que lo necesita) al
47
 * poligono exterior. El resto, por ahora, no se tocan.
48
 * Si se necesita tenerlos en cuenta, habr?a que mirar
49
 * si est?n dentro del otro, y entonces revisar que tiene
50
 * un CCW contrario al exterior.
51
 * @param bCCW true if you want the GeneralPath in CCW order
52
 * @return true si se le ha dado la vuelta. (true if flipped)
53
 * TODO: TERMINAR ESTO!! NO EST? COMPLETO!! NO sirve para multipoligonos
54
 */
55
/**
56
 * @author Carlos S?nchez Peri??n <a href = "mailto:csanchez@prodevelop.es"> e-mail </a>
57
 */
58
public class EnsureOrientation extends GeometryOperation{
59
    public static final String NAME = "ensureOrientation";
60
        public static final int CODE = GeometryLocator.getGeometryManager().
61
            getGeometryOperationCode(NAME);
62
        
63
        private GeneralPathX generalPathX = null;
64
        
65
        private Boolean bCCW;
66
                
67
        public int getOperationIndex() {
68
                return CODE;
69
        }
70

    
71
        public Object invoke(Geometry geom, GeometryOperationContext ctx)throws GeometryOperationException {
72
                bCCW = (Boolean) ctx.getAttribute("bCCW");
73
                if (bCCW==null)
74
                        throw new GeometryOperationException(new Exception("The function Parameter hasn't been passed or is null."));
75
                
76
                generalPathX = geom.getGeneralPath();
77
                if (generalPathX==null){
78
                        //if there isn't path the operation hasn't sense.
79
                        return null;
80
                }
81
                
82
        byte[] pointTypesAux = new byte[generalPathX.getNumTypes()+1];
83
        double[] pointCoordsAux = new double[(generalPathX.getNumCoords() * 2) + 2];
84
        int i;
85
        int pointIdx = 0;
86

    
87
        Coordinate c1, c2, c3;
88
        CoordinateList coordList = new CoordinateList();
89
        CoordinateList firstList = new CoordinateList();
90
        boolean bFirstList = true;
91
        Coordinate cInicio = null;
92

    
93
        for (i=0; i< generalPathX.getNumTypes(); i++)
94
        {
95
                int type = generalPathX.getTypeAt(i);
96

    
97
                switch (type)
98
                {
99
                case GeneralPathX.SEG_MOVETO:
100
                        c1= new Coordinate(generalPathX.getPointAt(pointIdx).getX(), generalPathX.getPointAt(pointIdx).getY());
101
                        cInicio = c1;
102
                        coordList.add(c1, true);
103
                        if (i>0) bFirstList = false;
104
                        if (bFirstList)
105
                        {
106
                                firstList.add(c1,true);
107
                        }
108
                        break;
109
                case GeneralPathX.SEG_LINETO:
110
                        c1= new Coordinate(generalPathX.getPointAt(pointIdx).getX(), generalPathX.getPointAt(pointIdx).getY());
111
                        coordList.add(c1, true);
112
                        if (bFirstList)
113
                        {
114
                                firstList.add(c1,true);
115
                        }
116
                        break;
117
                case GeneralPathX.SEG_QUADTO:
118
                        c1= new Coordinate(generalPathX.getPointAt(pointIdx).getX(), generalPathX.getPointAt(pointIdx).getY());
119
                        coordList.add(c1, true);
120
                        c2= new Coordinate(generalPathX.getPointAt(pointIdx+1).getX(),generalPathX.getPointAt(pointIdx+1).getY());
121
                        coordList.add(c2, true);
122
                        if (bFirstList)
123
                        {
124
                                firstList.add(c1,true);
125
                                firstList.add(c2,true);
126
                        }
127

    
128
                        break;
129
                case GeneralPathX.SEG_CUBICTO:
130
                        c1= new Coordinate(generalPathX.getPointAt(pointIdx).getX(), generalPathX.getPointAt(pointIdx).getY());
131
                        coordList.add(c1, true);
132
                        c2= new Coordinate(generalPathX.getPointAt(pointIdx+1).getX(),generalPathX.getPointAt(pointIdx+1).getY());
133
                        coordList.add(c2, true);
134
                        c3= new Coordinate(generalPathX.getPointAt(pointIdx+2).getX(),generalPathX.getPointAt(pointIdx+2).getY());
135
                        coordList.add(c3, true);
136
                        if (bFirstList)
137
                        {
138
                                firstList.add(c1,true);
139
                                firstList.add(c2,true);
140
                                firstList.add(c3,true);
141
                        }
142

    
143
                        break;
144
                case GeneralPathX.SEG_CLOSE:
145
                        coordList.add(cInicio, true);
146
                        if (bFirstList)
147
                        {
148
                                firstList.add(cInicio,true);
149
                        }
150
                        break;
151

    
152
                }
153
                pointIdx += GeneralPathX.curvesize[type];
154
        }
155
                // Guardamos el path dandole la vuelta
156
                Coordinate[] coords = coordList.toCoordinateArray();
157
                boolean bFlipped = false;
158
                if (CGAlgorithms.isCCW(coords) != bCCW.booleanValue()) // Le damos la vuelta
159
                {
160
                        CoordinateArraySequence seq = new CoordinateArraySequence(coords);
161
                        CoordinateSequences.reverse(seq);
162
                        coords = seq.toCoordinateArray();
163

    
164

    
165
                        // En el primer punto metemos un moveto
166
                        pointCoordsAux[0] = coords[0].x;
167
                        pointCoordsAux[1] = coords[0].y;
168
                        pointTypesAux[0] = GeneralPathX.SEG_MOVETO;
169
                        int idx = 2;
170
                        i=0;
171
                        int j=1;
172
                        for (int k=0; k < coords.length; k++)
173
                        {
174
                                pointCoordsAux[idx++] = coords[k].x;
175
                                pointCoordsAux[idx++] = coords[k].y;
176
                        int type = generalPathX.getTypeAt(i++);
177
                        pointIdx += GeneralPathX.curvesize[type];
178
                        switch (type)
179
                        {
180
                        case GeneralPathX.SEG_MOVETO:
181
                                pointTypesAux[j] = GeneralPathX.SEG_LINETO;
182
                                break;
183
                        case GeneralPathX.SEG_LINETO:
184
                                pointTypesAux[j] = GeneralPathX.SEG_LINETO;
185
                                break;
186
                        case GeneralPathX.SEG_QUADTO:
187
                                pointTypesAux[j] = GeneralPathX.SEG_QUADTO;
188
                                break;
189
                        case GeneralPathX.SEG_CUBICTO:
190
                                pointTypesAux[j] = GeneralPathX.SEG_CUBICTO;
191
                                break;
192
                        case GeneralPathX.SEG_CLOSE:
193
                                // TODO: IMPLEMENTAR ESTO!!!
194
                                break;
195

    
196
                        }
197
                        j++;
198

    
199
                        }
200
                        generalPathX.setPointTypes(pointTypesAux);
201
                        generalPathX.setPointCoords(pointCoordsAux);
202
                generalPathX.setNumCoords(generalPathX.getNumCoords()+2);
203
                generalPathX.setNumTypes(generalPathX.getNumTypes()+1);
204
                bFlipped  = true;
205
                }
206
                return new Boolean((boolean) bFlipped);
207
        }
208

    
209
}