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 / flip / Flip.java @ 40559
History | View | Annotate | Download (4.11 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | package org.gvsig.fmap.geom.operation.flip; |
25 | |||
26 | import java.awt.geom.PathIterator; |
||
27 | import java.util.ArrayList; |
||
28 | |||
29 | import org.gvsig.fmap.geom.Geometry; |
||
30 | import org.gvsig.fmap.geom.GeometryLocator; |
||
31 | import org.gvsig.fmap.geom.GeometryManager; |
||
32 | import org.gvsig.fmap.geom.operation.GeometryOperation; |
||
33 | import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
||
34 | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
||
35 | import org.gvsig.fmap.geom.primitive.GeneralPathX; |
||
36 | import org.gvsig.fmap.geom.util.Converter; |
||
37 | |||
38 | import com.vividsolutions.jts.geom.Coordinate; |
||
39 | import com.vividsolutions.jts.geom.CoordinateList; |
||
40 | import com.vividsolutions.jts.geom.CoordinateSequences; |
||
41 | import com.vividsolutions.jts.geom.impl.CoordinateArraySequence; |
||
42 | |||
43 | /**
|
||
44 | * This class converts the path into points, then flip it down.
|
||
45 | *
|
||
46 | * @author Carlos S?nchez Peri??n <a href = "mailto:csanchez@prodevelop.es"> e-mail </a>
|
||
47 | */
|
||
48 | public class Flip extends GeometryOperation{ |
||
49 | public static final String NAME = "flip"; |
||
50 | private static GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
51 | public static final int CODE = geomManager.getGeometryOperationCode(NAME); |
||
52 | |||
53 | private GeneralPathX generalPathX = null; |
||
54 | |||
55 | public int getOperationIndex() { |
||
56 | return CODE;
|
||
57 | } |
||
58 | |||
59 | public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException { |
||
60 | generalPathX = geom.getGeneralPath(); |
||
61 | if(generalPathX == null){ |
||
62 | //if there isn't path the operation hasn't sense.
|
||
63 | return null; |
||
64 | } |
||
65 | PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness()); //polyLine.getPathIterator(null, flatness); |
||
66 | double[] theData = new double[6]; |
||
67 | //Coordinate first = null;
|
||
68 | CoordinateList coordList = new CoordinateList();
|
||
69 | Coordinate c1; |
||
70 | GeneralPathX newGp = new GeneralPathX();
|
||
71 | ArrayList listOfParts = new ArrayList(); |
||
72 | while (!theIterator.isDone()) {
|
||
73 | //while not done
|
||
74 | int type = theIterator.currentSegment(theData);
|
||
75 | switch (type)
|
||
76 | { |
||
77 | case (byte) PathIterator.SEG_MOVETO: |
||
78 | coordList = new CoordinateList();
|
||
79 | listOfParts.add(coordList); |
||
80 | c1= new Coordinate(theData[0], theData[1]); |
||
81 | coordList.add(c1, true);
|
||
82 | break;
|
||
83 | case (byte) PathIterator.SEG_LINETO: |
||
84 | c1= new Coordinate(theData[0], theData[1]); |
||
85 | coordList.add(c1, true);
|
||
86 | break;
|
||
87 | case (byte) PathIterator.SEG_CLOSE: |
||
88 | coordList.add(coordList.getCoordinate(0));
|
||
89 | break;
|
||
90 | } |
||
91 | theIterator.next(); |
||
92 | } |
||
93 | |||
94 | for (int i=listOfParts.size()-1; i>=0; i--) |
||
95 | { |
||
96 | coordList = (CoordinateList) listOfParts.get(i); |
||
97 | Coordinate[] coords = coordList.toCoordinateArray();
|
||
98 | CoordinateArraySequence seq = new CoordinateArraySequence(coords);
|
||
99 | CoordinateSequences.reverse(seq); |
||
100 | coords = seq.toCoordinateArray(); |
||
101 | newGp.moveTo(coords[0].x, coords[0].y); |
||
102 | for (int j=1; j < coords.length; j++) |
||
103 | { |
||
104 | newGp.lineTo(coords[j].x, coords[j].y); |
||
105 | } |
||
106 | } |
||
107 | generalPathX.reset(); |
||
108 | generalPathX.append(newGp.getPathIterator(null), false); |
||
109 | |||
110 | return null; |
||
111 | } |
||
112 | } |