Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / shp / utils / SHPMultiLine.java @ 21606

History | View | Annotate | Download (8.94 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.data.datastores.vectorial.file.shp.utils;
42

    
43
import java.awt.geom.PathIterator;
44
import java.awt.geom.Rectangle2D;
45
import java.util.ArrayList;
46

    
47
import org.gvsig.datasources.common.IByteBuffer;
48

    
49
import es.prodevelop.gvsig.mobile.fmap.core.FPoint2D;
50
import es.prodevelop.gvsig.mobile.fmap.core.FPolyline2D;
51
import es.prodevelop.gvsig.mobile.fmap.core.GeneralPathX;
52
import es.prodevelop.gvsig.mobile.fmap.core.IGeometry;
53
import es.prodevelop.gvsig.mobile.fmap.core.IGeometry3D;
54
import es.prodevelop.gvsig.mobile.fmap.symbol.FConstant;
55
import es.prodevelop.gvsig.mobile.fmap.symbol.FConverter;
56

    
57
//import com.iver.cit.gvsig.fmap.core.FPoint2D;
58
//import com.iver.cit.gvsig.fmap.core.FPolyline2D;
59
//import com.iver.cit.gvsig.fmap.core.GeneralPathX;
60
//import com.iver.cit.gvsig.fmap.core.IGeometry;
61
//import com.iver.cit.gvsig.fmap.core.IGeometry3D;
62
//import com.iver.cit.gvsig.fmap.core.v02.FConstant;
63
//import com.iver.cit.gvsig.fmap.core.v02.FConverter;
64
//import com.iver.cit.gvsig.fmap.drivers.shp.write.ShapefileException;
65

    
66

    
67
/**
68
 * Elemento shape de tipo multil?nea.
69
 *
70
 * @author Vicente Caballero Navarro
71
 */
72
public class SHPMultiLine implements SHPShape {
73
        protected int m_type;
74
        protected int[] parts;
75
        protected FPoint2D[] points;
76
        protected double[] zs;
77
        //double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva
78

    
79
        /**
80
         * Crea un nuevo SHPMultiLine.
81
         */
82
        public SHPMultiLine() {
83
                m_type = FConstant.SHAPE_TYPE_POLYLINE;
84
        }
85

    
86
        /**
87
         * Crea un nuevo SHPMultiLine.
88
         *
89
         * @param type Tipo de multil?nea.
90
         *
91
         * @throws ShapefileException
92
         */
93
        public SHPMultiLine(int type) {
94
                if ((type != FConstant.SHAPE_TYPE_POLYLINE) &&
95
                                (type != FConstant.SHAPE_TYPE_POLYLINEM) &&
96
                                (type != FConstant.SHAPE_TYPE_POLYLINEZ)) {
97
//                        throw new ShapefileException("No es de tipo 3,13 ni 23");
98
                }
99

    
100
                m_type = type;
101
        }
102

    
103
        /**
104
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
105
         */
106
        public int getShapeType() {
107
                return m_type;
108
        }
109

    
110
        /**
111
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
112
         */
113
        public IGeometry read(IByteBuffer buffer, int type) {
114
                double minX = buffer.getDouble();
115
                double minY = buffer.getDouble();
116
                double maxX = buffer.getDouble();
117
                double maxY = buffer.getDouble();
118
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
119
                                maxY - maxY);
120

    
121
                int numParts = buffer.getInt();
122
                int numPoints = buffer.getInt(); //total number of points
123

    
124
                int[] partOffsets = new int[numParts];
125

    
126
                for (int i = 0; i < numParts; i++) {
127
                        partOffsets[i] = buffer.getInt();
128
                }
129

    
130
                FPoint2D[] points = new FPoint2D[numPoints];
131

    
132
                for (int t = 0; t < numPoints; t++) {
133
                        points[t] = new FPoint2D(buffer.getDouble(), buffer.getDouble());
134
                }
135

    
136
                /*   if (type == FConstant.SHAPE_TYPE_POLYLINEZ) {
137
                   //z min, max
138
                   buffer.position(buffer.position() + (2 * 8));
139
                   for (int t = 0; t < numPoints; t++) {
140
                       points[t].z = buffer.getDouble(); //z value
141
                   }
142
                   }
143
                 */
144
                return (IGeometry) new FPolyline2D(getGeneralPathX(points, partOffsets));
145
        }
146

    
147
        /**
148
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
149
         */
150
        public void write(IByteBuffer buffer, IGeometry geometry) {
151
                Rectangle2D rec = geometry.getBounds2D();
152
                buffer.putDouble(rec.getMinX());
153
                buffer.putDouble(rec.getMinY());
154
                buffer.putDouble(rec.getMaxX());
155
                buffer.putDouble(rec.getMaxY());
156
                int numParts = parts.length;
157
                int npoints = points.length;
158
                buffer.putInt(numParts);
159
                buffer.putInt(npoints);
160

    
161
                for (int i = 0; i < numParts; i++) {
162
                        buffer.putInt(parts[i]);
163
                }
164

    
165
                for (int t = 0; t < npoints; t++) {
166
                        buffer.putDouble(points[t].getX());
167
                        buffer.putDouble(points[t].getY());
168
                }
169

    
170
                  if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
171
                   double[] zExtreame = SHP.getZMinMax(zs);
172
                   if (Double.isNaN(zExtreame[0])) {
173
                       buffer.putDouble(0.0);
174
                       buffer.putDouble(0.0);
175
                   } else {
176
                       buffer.putDouble(zExtreame[0]);
177
                       buffer.putDouble(zExtreame[1]);
178
                   }
179
                   for (int t = 0; t < npoints; t++) {
180
                       double z = zs[t];
181
                       if (Double.isNaN(z)) {
182
                           buffer.putDouble(0.0);
183
                       } else {
184
                           buffer.putDouble(z);
185
                       }
186
                   }
187

    
188
                   }
189
                   if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
190
                       buffer.putDouble(-10E40);
191
                       buffer.putDouble(-10E40);
192
                       for (int t = 0; t < npoints; t++) {
193
                           buffer.putDouble(-10E40);
194
                       }
195
                   }
196

    
197
        }
198

    
199
        /**
200
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
201
         */
202
        public int getLength(IGeometry fgeometry) {
203
                int numlines;
204
                int numpoints;
205
                int length;
206

    
207
                numlines = parts.length;
208
                numpoints = points.length;
209
                if (m_type == FConstant.SHAPE_TYPE_POLYLINE) {
210
                        length = 44 + (4 * numlines) + (numpoints * 16);
211
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
212
                        length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8 +
213
                                (8 * numpoints);
214
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
215
                        length = 44 + (4 * numlines) + (numpoints * 16) +
216
                                (8 * numpoints) + 8 + 8;
217
                } else {
218
                        throw new IllegalStateException("Expected ShapeType of Arc, got " +
219
                                m_type);
220
                }
221

    
222
                return length;
223
        }
224

    
225
        /**
226
         * DOCUMENT ME!
227
         *
228
         * @param po DOCUMENT ME!
229
         * @param pa DOCUMENT ME!
230
         *
231
         * @return DOCUMENT ME!
232
         */
233
        protected GeneralPathX getGeneralPathX(FPoint2D[] po, int[] pa) {
234
                GeneralPathX gPX = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
235
                                po.length);
236
                int j = 0;
237

    
238
                for (int i = 0; i < po.length; i++) {
239
                        if (i == pa[j]) {
240
                                gPX.moveTo(po[i].getX(), po[i].getY());
241

    
242
                                if (j < (pa.length - 1)) {
243
                                        j++;
244
                                }
245
                        } else {
246
                                gPX.lineTo(po[i].getX(), po[i].getY());
247
                        }
248
                }
249

    
250
                return gPX;
251
        }
252

    
253
        /**
254
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
255
         */
256
        public void obtainsPoints(IGeometry g) {
257
                if (FConstant.SHAPE_TYPE_POLYLINEZ == m_type || FConstant.SHAPE_TYPE_POLYGONZ == m_type){
258
                        zs=((IGeometry3D)g).getZs();
259
                }
260
                ArrayList arrayPoints = null;
261
                ArrayList arrayParts = new ArrayList();
262
                PathIterator theIterator = g.getPathIterator(null, FConverter.FLATNESS); //polyLine.getPathIterator(null, flatness);
263
                double[] theData = new double[6];
264
                int numParts = 0;
265
                while (!theIterator.isDone()) {
266
                        //while not done
267
                        int theType = theIterator.currentSegment(theData);
268

    
269
                        //Populate a segment of the new
270
                        // GeneralPathX object.
271
                        //Process the current segment to populate a new
272
                        // segment of the new GeneralPathX object.
273

    
274
                        switch (theType) {
275
                                case PathIterator.SEG_MOVETO:
276

    
277
                                        // System.out.println("SEG_MOVETO");
278
                                        if (arrayPoints == null) {
279
                                                arrayPoints = new ArrayList();
280
                                                arrayParts.add(new Integer(0));
281
                                        } else {
282
                                                arrayParts.add(new Integer(arrayPoints.size()));
283
                                        }
284

    
285
                                        numParts++;
286

    
287
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
288

    
289
                                        break;
290

    
291
                                case PathIterator.SEG_LINETO:
292

    
293
                                        // System.out.println("SEG_LINETO");
294
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
295

    
296
                                        break;
297

    
298
                                case PathIterator.SEG_QUADTO:
299
                                        System.out.println("Not supported here");
300

    
301
                                        break;
302

    
303
                                case PathIterator.SEG_CUBICTO:
304
                                        System.out.println("Not supported here");
305

    
306
                                        break;
307

    
308
                                case PathIterator.SEG_CLOSE:
309
//                                        System.out.println("SEG_CLOSE");
310

    
311
                                        // A?adimos el primer punto para cerrar.
312
                                        FPoint2D firstPoint = (FPoint2D) arrayPoints.get(0);
313
                                        arrayPoints.add(new FPoint2D(firstPoint.getX(),
314
                                                        firstPoint.getY()));
315

    
316
                                        break;
317
                        } //end switch
318

    
319
                        theIterator.next();
320
                }
321

    
322
                Integer[] integers = (Integer[]) arrayParts.toArray(new Integer[0]);
323
                parts = new int[integers.length];
324
                for (int i = 0; i < integers.length; i++) {
325
                        parts[i] = integers[i].intValue();
326
                }
327
                if (arrayPoints==null){
328
                        points=new FPoint2D[0];
329
                        return;
330
                }
331
                points = (FPoint2D[]) arrayPoints.toArray(new FPoint2D[0]);
332

    
333
        }
334

    
335
//        public void setFlatness(double flatness) {
336
//                this.flatness=flatness;
337
//        }
338
}