Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / write / SHPMultiLine.java @ 1132

History | View | Annotate | Download (9.3 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 com.iver.cit.gvsig.fmap.drivers.shp.write;
42

    
43
import com.iver.cit.gvsig.fmap.core.FPoint2D;
44
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
45
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
46
import com.iver.cit.gvsig.fmap.core.GeneralPathXIterator;
47
import com.iver.cit.gvsig.fmap.core.IGeometry;
48
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
49

    
50
import java.awt.geom.PathIterator;
51
import java.awt.geom.Rectangle2D;
52

    
53
import java.nio.ByteBuffer;
54
import java.nio.MappedByteBuffer;
55

    
56
import java.util.ArrayList;
57

    
58

    
59
/**
60
 * DOCUMENT ME!
61
 *
62
 * @author Vicente Caballero Navarro
63
 */
64
public class SHPMultiLine implements SHPShape {
65
        private int m_type;
66
        protected int[] parts;
67
        protected FPoint2D[] points;
68

    
69
        /**
70
         * Crea un nuevo SHPMultiLine.
71
         */
72
        public SHPMultiLine() {
73
                m_type = FConstant.SHAPE_TYPE_POLYLINE;
74
        }
75

    
76
        /**
77
         * Crea un nuevo SHPMultiLine.
78
         *
79
         * @param type DOCUMENT ME!
80
         *
81
         * @throws ShapefileException DOCUMENT ME!
82
         */
83
        public SHPMultiLine(int type) throws ShapefileException {
84
                if ((type != FConstant.SHAPE_TYPE_POLYLINE) &&
85
                                (type != FConstant.SHAPE_TYPE_POLYLINEM) &&
86
                                (type != FConstant.SHAPE_TYPE_POLYLINEZ)) {
87
                        throw new ShapefileException("No es de tipo 3,13 ni 23");
88
                }
89

    
90
                m_type = type;
91
        }
92

    
93
        /**
94
         * DOCUMENT ME!
95
         *
96
         * @return DOCUMENT ME!
97
         */
98
        public int getShapeType() {
99
                return m_type;
100
        }
101

    
102
        /**
103
         * DOCUMENT ME!
104
         *
105
         * @param buffer DOCUMENT ME!
106
         * @param type DOCUMENT ME!
107
         *
108
         * @return DOCUMENT ME!
109
         */
110
        public IGeometry read(MappedByteBuffer buffer, int type) {
111
                double minX = buffer.getDouble();
112
                double minY = buffer.getDouble();
113
                double maxX = buffer.getDouble();
114
                double maxY = buffer.getDouble();
115
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
116
                                maxY - maxY);
117

    
118
                int numParts = buffer.getInt();
119
                int numPoints = buffer.getInt(); //total number of points
120

    
121
                int[] partOffsets = new int[numParts];
122

    
123
                for (int i = 0; i < numParts; i++) {
124
                        partOffsets[i] = buffer.getInt();
125
                }
126

    
127
                FPoint2D[] points = new FPoint2D[numPoints];
128

    
129
                for (int t = 0; t < numPoints; t++) {
130
                        points[t] = new FPoint2D(buffer.getDouble(), buffer.getDouble());
131
                }
132

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

    
144
        /**
145
         * DOCUMENT ME!
146
         *
147
         * @param buffer DOCUMENT ME!
148
         * @param geometry DOCUMENT ME!
149
         */
150
        public void write(ByteBuffer buffer, IGeometry geometry) {
151
                //FPolyline2D polyLine = (FPolyline2D) geometry.getShape();
152
                Rectangle2D rec = geometry.getBounds2D();
153
                buffer.putDouble(rec.getMinX());
154
                buffer.putDouble(rec.getMinY());
155
                buffer.putDouble(rec.getMaxX());
156
                buffer.putDouble(rec.getMaxY());
157
                obtainsPoints(geometry.getGeneralPathXIterator());
158

    
159
                //int[] parts=polyLine.getParts();
160
                //FPoint2D[] points=polyLine.getPoints();
161
                int numParts = parts.length;
162
                int npoints = points.length;
163

    
164
                ///int numParts = polyLine.getNumParts();
165
                buffer.putInt(numParts);
166

    
167
                ///int npoints = polyLine.getNumPoints();
168
                buffer.putInt(npoints);
169

    
170
                //Aqu? es posible que falle algo referente a Parts
171
                for (int i = 0; i < numParts; i++) {
172
                        ///buffer.putInt(polyLine.getPart(i));
173
                        buffer.putInt(parts[i]);
174
                }
175

    
176
                /// FPoint2D[] points = polyLine.getPoints();
177
                for (int t = 0; t < npoints; t++) {
178
                        buffer.putDouble(points[t].getX());
179
                        buffer.putDouble(points[t].getY());
180
                }
181

    
182
                /*  if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
183
                   double[] zExtreame = JTSUtilities.zMinMax(points);
184
                   if (Double.isNaN(zExtreame[0])) {
185
                       buffer.putDouble(0.0);
186
                       buffer.putDouble(0.0);
187
                   } else {
188
                       buffer.putDouble(zExtreame[0]);
189
                       buffer.putDouble(zExtreame[1]);
190
                   }
191
                   for (int t = 0; t < npoints; t++) {
192
                       double z = points[t].z;
193
                       if (Double.isNaN(z)) {
194
                           buffer.putDouble(0.0);
195
                       } else {
196
                           buffer.putDouble(z);
197
                       }
198
                   }
199
                   }
200
                   if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
201
                       buffer.putDouble(-10E40);
202
                       buffer.putDouble(-10E40);
203
                       for (int t = 0; t < npoints; t++) {
204
                           buffer.putDouble(-10E40);
205
                       }
206
                   }
207
                 */
208
        }
209

    
210
        /**
211
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
212
         */
213
        public int getLength(IGeometry fgeometry) {
214
                ///FPolyline2D multi = (FPolyline2D) fgeometry.getShape();
215
                int numlines;
216
                int numpoints;
217
                int length;
218

    
219
                obtainsPoints(fgeometry.getGeneralPathXIterator());
220

    
221
                //int[] parts=multi.getParts();
222
                //FPoint2D[] points=multi.getPoints();
223
                numlines = parts.length;
224
                numpoints = points.length;
225

    
226
                //numlines = multi.getNumParts();
227
                //numpoints = multi.getNumPoints();
228
                if (m_type == FConstant.SHAPE_TYPE_POLYLINE) {
229
                        length = 44 + (4 * numlines) + (numpoints * 16);
230
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
231
                        length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8 +
232
                                (8 * numpoints);
233
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
234
                        length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8 +
235
                                (8 * numpoints) + 8 + 8 + (8 * numpoints);
236
                } else {
237
                        throw new IllegalStateException("Expected ShapeType of Arc, got " +
238
                                m_type);
239
                }
240

    
241
                return length;
242
        }
243

    
244
        /**
245
         * DOCUMENT ME!
246
         *
247
         * @param po DOCUMENT ME!
248
         * @param pa DOCUMENT ME!
249
         *
250
         * @return DOCUMENT ME!
251
         */
252
        protected GeneralPathX getGeneralPathX(FPoint2D[] po, int[] pa) {
253
                GeneralPathX gPX = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
254
                                po.length);
255
                int j = 0;
256

    
257
                for (int i = 0; i < po.length; i++) {
258
                        if (i == pa[j]) {
259
                                gPX.moveTo(po[i].getX(), po[i].getY());
260

    
261
                                if (j < (pa.length - 1)) {
262
                                        j++;
263
                                }
264
                        } else {
265
                                gPX.lineTo(po[i].getX(), po[i].getY());
266
                        }
267
                }
268

    
269
                return gPX;
270
        }
271

    
272
        /**
273
         * DOCUMENT ME!
274
         *
275
         * @param iter DOCUMENT ME!
276
         */
277
        public void obtainsPoints(GeneralPathXIterator iter) {
278
                double flatness = 0.8;
279
                ArrayList arrayLines = new ArrayList();
280
                ArrayList arrayPoints = null;
281
                ArrayList arrayParts = new ArrayList();
282
                GeneralPathXIterator theIterator = iter; //polyLine.getPathIterator(null, flatness);
283
                double[] theData = new double[6];
284
                int numParts = 0;
285

    
286
                while (!theIterator.isDone()) {
287
                        //while not done
288
                        int theType = theIterator.currentSegment(theData);
289

    
290
                        //Populate a segment of the new
291
                        // GeneralPathX object.
292
                        //Process the current segment to populate a new
293
                        // segment of the new GeneralPathX object.
294
                        switch (theType) {
295
                                case PathIterator.SEG_MOVETO:
296

    
297
                                        // System.out.println("SEG_MOVETO");
298
                                        if (arrayPoints == null) {
299
                                                arrayPoints = new ArrayList();
300
                                        } else {
301
                                                //lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
302
                                                //                        arrayCoords));
303
                                                //arrayLines.add(lin);
304
                                                arrayPoints = new ArrayList();
305
                                        }
306

    
307
                                        numParts++;
308
                                        arrayParts.add(new Integer(0));
309
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
310

    
311
                                        break;
312

    
313
                                case PathIterator.SEG_LINETO:
314

    
315
                                        // System.out.println("SEG_LINETO");
316
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
317

    
318
                                        break;
319

    
320
                                case PathIterator.SEG_QUADTO:
321
                                        System.out.println("Not supported here");
322

    
323
                                        break;
324

    
325
                                case PathIterator.SEG_CUBICTO:
326
                                        System.out.println("Not supported here");
327

    
328
                                        break;
329

    
330
                                case PathIterator.SEG_CLOSE:
331
                                        System.out.println("SEG_CLOSE");
332

    
333
                                        // A?adimos el primer punto para cerrar.
334
                                        FPoint2D firstPoint = (FPoint2D) arrayPoints.get(0);
335
                                        arrayPoints.add(new FPoint2D(firstPoint.getX(),
336
                                                        firstPoint.getY()));
337

    
338
                                        break;
339
                        } //end switch
340

    
341
                        theIterator.next();
342
                } //end while loop
343

    
344
                //lin = new GeometryFactory().createLineString(CoordinateArrays.toCoordinateArray(
345
                //                        arrayCoords));
346
                if (numParts > 1) // Generamos una MultiLineString
347
                 {
348
                        System.out.println("MultiLine");
349

    
350
                        //arrayLines.add(lin);
351
                        //geoJTS = new GeometryFactory().createMultiLineString(GeometryFactory.toLineStringArray(
352
                        //                        arrayLines));
353
                } else {
354
                        //geoJTS = lin;
355
                }
356

    
357
                Integer[] integers = (Integer[]) arrayParts.toArray(new Integer[0]);
358
                parts = new int[integers.length];
359

    
360
                for (int i = 0; i < integers.length; i++) {
361
                        parts[i] = integers[i].intValue();
362
                }
363

    
364
                points = (FPoint2D[]) arrayPoints.toArray(new FPoint2D[0]);
365
        }
366
}