Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FPolyline2DM.java @ 35335

History | View | Annotate | Download (4.86 KB)

1
package com.iver.cit.gvsig.fmap.core;
2

    
3
import java.awt.geom.PathIterator;
4
import java.util.ArrayList;
5

    
6
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
7
import com.vividsolutions.jts.geom.Coordinate;
8
import com.vividsolutions.jts.geom.CoordinateArrays;
9
import com.vividsolutions.jts.geom.GeometryFactory;
10
import com.vividsolutions.jts.geom.LineString;
11

    
12
//import com.iver.andami.PluginServices;
13

    
14

    
15
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
16
 *
17
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
18
 *
19
 * This program is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU General Public License
21
 * as published by the Free Software Foundation; either version 2
22
 * of the License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program; if not, write to the Free Software
31
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
32
 *
33
 * For more information, contact:
34
 *
35
 *  Generalitat Valenciana
36
 *   Conselleria d'Infraestructures i Transport
37
 *   Av. Blasco Ib��ez, 50
38
 *   46010 VALENCIA
39
 *   SPAIN
40
 *
41
 *      +34 963862235
42
 *   gvsig@gva.es
43
 *      www.gvsig.gva.es
44
 *
45
 *    or
46
 *
47
 *   IVER T.I. S.A
48
 *   Salamanca 50
49
 *   46005 Valencia
50
 *   Spain
51
 *
52
 *   +34 963163400
53
 *   dac@iver.es
54
 */
55
/* CVS MESSAGES:
56
 *
57
 * $Id: FPolyline2DM.java,v 1.1 2007/10/19 10:03:45 jorpiell Exp $
58
 * $Log: FPolyline2DM.java,v $
59
 * Revision 1.1  2007/10/19 10:03:45  jorpiell
60
 * First commit
61
 *
62
 *
63
 */
64
/**
65
 * This object represents a polyline with the M coordinate.
66
 * @author Jorge Piera LLodr� (jorge.piera@iver.es)
67
 */
68
public class FPolyline2DM extends FPolyline2D implements FShapeM{
69
        private static final long serialVersionUID = -617233536274899782L;
70
        private static final String NAME = "MULTILINESTRINGM";
71
        double[] pM = null;
72

    
73
        public FPolyline2DM(GeneralPathX gpx, double[] pM) {
74
                super(gpx);        
75
                this.pM = pM;
76
        }
77

    
78
        /*
79
         * (non-Javadoc)
80
         * @see com.iver.cit.gvsig.fmap.core.FShapeDS#getMs()
81
         */
82
        public double[] getMs() {
83
                return pM;
84
        }
85

    
86
        /*
87
         * (non-Javadoc)
88
         * @see com.iver.cit.gvsig.fmap.core.FPolyline3D#getShapeType()
89
         */
90
        public int getShapeType() {
91
                return FShape.LINE | FShape.M;
92
        }
93

    
94
        /*
95
         * (non-Javadoc)
96
         * @see com.iver.cit.gvsig.fmap.core.FPolyline3D#cloneFShape()
97
         */
98
        public FShape cloneFShape() {
99
                return new FPolyline2DM((GeneralPathX) gp.clone(), (double[]) pM);
100
        }
101

    
102
        /*
103
         * (non-Javadoc)
104
         * @see com.iver.cit.gvsig.fmap.core.FShapeM#setMAt(int, double)
105
         */
106
        public void setMAt(int i, double value) {
107
                if (i < pM.length){
108
                        pM[i] = value;                
109
                }
110
        }
111

    
112
        /*
113
         * (non-Javadoc)
114
         * @see com.iver.cit.gvsig.fmap.core.FShapeM#isDecreasing()
115
         */
116
        public boolean isDecreasing() {
117
                if (pM.length == 0){
118
                        return false;
119
                }
120
                return (pM[0] > pM[pM.length-1]);
121
        }
122

    
123
        public void revertMs(){
124
                double totalDistance = Math.abs(pM[0] - pM[pM.length-1]);        
125
                double[] percentages = new double[pM.length];
126
                for (int i=1 ; i<percentages.length ; i++)
127
                {
128
                        percentages [i] = (Math.abs(pM[i]-pM[i-1]))/totalDistance;
129
                }                
130
                //The first value
131
                double pm0 = pM[0];                
132
                if (!isDecreasing()){
133
                        pM[0] = pM[pM.length-1];
134
                        for (int i=1 ; i<pM.length-1 ; i++)
135
                        {
136
                                double increasing = percentages[i] * totalDistance;
137
                                pM[i] = pM[i-1] - increasing;                                
138
                        }
139
                }else{
140
                        pM[0] = pM[pM.length-1];
141
                        for (int i=1 ; i<pM.length-1 ; i++)
142
                        {
143
                                double decreasing = percentages[i] * totalDistance;
144
                                pM[i] = pM[i-1] + decreasing;                                
145
                        }
146
                }
147
                pM[pM.length-1] = pm0;        
148
        }
149

    
150
        /**
151
         * This method is used instead of the JTS ToString method. The reason is because
152
         * JTS doesn't support the M coordinate
153
         */
154
        public String toText(){
155
                StringBuffer str = new StringBuffer();
156
                str.append(NAME);
157
                str.append(" ((");
158
                int theType;                
159
                double[] theData = new double[6];                
160

    
161
                PathIterator theIterator = getPathIterator(null, FConverter.FLATNESS);
162
                int i = 0;
163

    
164
                while (!theIterator.isDone()) {
165
                        //while not done
166
                        theType = theIterator.currentSegment(theData);
167

    
168
                        double m = 0.0;
169
                        if (i < pM.length){
170
                                m = pM[i]; 
171
                        }
172
                        
173
                        switch (theType) {
174
                        case PathIterator.SEG_MOVETO:                                        
175
                                str.append(theData[0] + " " + theData[1] + " " + m + ",");
176
                                break;
177

    
178
                        case PathIterator.SEG_LINETO:
179
                                str.append(theData[0] + " " + theData[1] + " " + m + ",");
180

    
181
                                break;
182

    
183
                        case PathIterator.SEG_QUADTO:
184
                                System.out.println("Not supported here");
185

    
186
                                break;
187

    
188
                        case PathIterator.SEG_CUBICTO:
189
                                System.out.println("Not supported here");
190

    
191
                                break;
192

    
193
                        case PathIterator.SEG_CLOSE:
194
                                break;
195
                        } //end switch
196

    
197
                        theIterator.next();
198
                        i++;
199
                } //end while loop                
200
                return str.delete(str.length()-1, str.length()) + "))";
201
        }
202
}