Statistics
| Revision:

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

History | View | Annotate | Download (8.79 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
 * Elemento shape de tipo multil?nea.
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 Tipo de multil?nea.
80
         *
81
         * @throws ShapefileException 
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
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
95
         */
96
        public int getShapeType() {
97
                return m_type;
98
        }
99

    
100
        /**
101
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
102
         */
103
        public IGeometry read(MappedByteBuffer buffer, int type) {
104
                double minX = buffer.getDouble();
105
                double minY = buffer.getDouble();
106
                double maxX = buffer.getDouble();
107
                double maxY = buffer.getDouble();
108
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
109
                                maxY - maxY);
110

    
111
                int numParts = buffer.getInt();
112
                int numPoints = buffer.getInt(); //total number of points
113

    
114
                int[] partOffsets = new int[numParts];
115

    
116
                for (int i = 0; i < numParts; i++) {
117
                        partOffsets[i] = buffer.getInt();
118
                }
119

    
120
                FPoint2D[] points = new FPoint2D[numPoints];
121

    
122
                for (int t = 0; t < numPoints; t++) {
123
                        points[t] = new FPoint2D(buffer.getDouble(), buffer.getDouble());
124
                }
125

    
126
                /*   if (type == FConstant.SHAPE_TYPE_POLYLINEZ) {
127
                   //z min, max
128
                   buffer.position(buffer.position() + (2 * 8));
129
                   for (int t = 0; t < numPoints; t++) {
130
                       points[t].z = buffer.getDouble(); //z value
131
                   }
132
                   }
133
                 */
134
                return (IGeometry) new FPolyline2D(getGeneralPathX(points, partOffsets));
135
        }
136

    
137
        /**
138
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
139
         */
140
        public void write(ByteBuffer buffer, IGeometry geometry) {
141
                //FPolyline2D polyLine = (FPolyline2D) geometry.getShape();
142
                Rectangle2D rec = geometry.getBounds2D();
143
                buffer.putDouble(rec.getMinX());
144
                buffer.putDouble(rec.getMinY());
145
                buffer.putDouble(rec.getMaxX());
146
                buffer.putDouble(rec.getMaxY());
147
                ///obtainsPoints(geometry.getGeneralPathXIterator());
148

    
149
                //int[] parts=polyLine.getParts();
150
                //FPoint2D[] points=polyLine.getPoints();
151
                int numParts = parts.length;
152
                int npoints = points.length;
153

    
154
                //int numParts = polyLine.getNumParts();
155
                buffer.putInt(numParts);
156

    
157
                ///int npoints = polyLine.getNumPoints();
158
                buffer.putInt(npoints);
159

    
160
                //Aqu? es posible que falle algo referente a Parts
161
                for (int i = 0; i < numParts; i++) {
162
                        ///buffer.putInt(polyLine.getPart(i));
163
                        buffer.putInt(parts[i]);
164
                }
165

    
166
                /// FPoint2D[] points = polyLine.getPoints();
167
                for (int t = 0; t < npoints; t++) {
168
                        buffer.putDouble(points[t].getX());
169
                        buffer.putDouble(points[t].getY());
170
                }
171

    
172
                /*  if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
173
                   double[] zExtreame = JTSUtilities.zMinMax(points);
174
                   if (Double.isNaN(zExtreame[0])) {
175
                       buffer.putDouble(0.0);
176
                       buffer.putDouble(0.0);
177
                   } else {
178
                       buffer.putDouble(zExtreame[0]);
179
                       buffer.putDouble(zExtreame[1]);
180
                   }
181
                   for (int t = 0; t < npoints; t++) {
182
                       double z = points[t].z;
183
                       if (Double.isNaN(z)) {
184
                           buffer.putDouble(0.0);
185
                       } else {
186
                           buffer.putDouble(z);
187
                       }
188
                   }
189
                   }
190
                   if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
191
                       buffer.putDouble(-10E40);
192
                       buffer.putDouble(-10E40);
193
                       for (int t = 0; t < npoints; t++) {
194
                           buffer.putDouble(-10E40);
195
                       }
196
                   }
197
                 */
198
        }
199

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

    
209
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
210

    
211
                //int[] parts=multi.getParts();
212
                //FPoint2D[] points=multi.getPoints();
213
                numlines = parts.length;
214
                numpoints = points.length;
215

    
216
                //numlines = multi.getNumParts();
217
                //numpoints = multi.getNumPoints();
218
                if (m_type == FConstant.SHAPE_TYPE_POLYLINE) {
219
                        length = 44 + (4 * numlines) + (numpoints * 16);
220
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
221
                        length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8 +
222
                                (8 * numpoints);
223
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
224
                        length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8 +
225
                                (8 * numpoints) + 8 + 8 + (8 * numpoints);
226
                } else {
227
                        throw new IllegalStateException("Expected ShapeType of Arc, got " +
228
                                m_type);
229
                }
230

    
231
                return length;
232
        }
233

    
234
        /**
235
         * DOCUMENT ME!
236
         *
237
         * @param po DOCUMENT ME!
238
         * @param pa DOCUMENT ME!
239
         *
240
         * @return DOCUMENT ME!
241
         */
242
        protected GeneralPathX getGeneralPathX(FPoint2D[] po, int[] pa) {
243
                GeneralPathX gPX = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
244
                                po.length);
245
                int j = 0;
246

    
247
                for (int i = 0; i < po.length; i++) {
248
                        if (i == pa[j]) {
249
                                gPX.moveTo(po[i].getX(), po[i].getY());
250

    
251
                                if (j < (pa.length - 1)) {
252
                                        j++;
253
                                }
254
                        } else {
255
                                gPX.lineTo(po[i].getX(), po[i].getY());
256
                        }
257
                }
258

    
259
                return gPX;
260
        }
261

    
262
        /**
263
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
264
         */
265
        public void obtainsPoints(GeneralPathXIterator iter) {
266
                ArrayList arrayPoints = null;
267
                ArrayList arrayParts = new ArrayList();
268
                GeneralPathXIterator theIterator = iter; //polyLine.getPathIterator(null, flatness);
269
                double[] theData = new double[6];
270
                int numParts = 0;
271
                while (!theIterator.isDone()) {
272
                        //while not done
273
                        int theType = theIterator.currentSegment(theData);
274

    
275
                        //Populate a segment of the new
276
                        // GeneralPathX object.
277
                        //Process the current segment to populate a new
278
                        // segment of the new GeneralPathX object.
279
                        
280
                        switch (theType) {
281
                                case PathIterator.SEG_MOVETO:
282

    
283
                                        // System.out.println("SEG_MOVETO");
284
                                        if (arrayPoints == null) {
285
                                                arrayPoints = new ArrayList();
286
                                                arrayParts.add(new Integer(0));
287
                                        } else {
288
                                                arrayParts.add(new Integer(arrayPoints.size()));
289
                                        }
290

    
291
                                        numParts++;
292
                                        
293
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
294

    
295
                                        break;
296

    
297
                                case PathIterator.SEG_LINETO:
298

    
299
                                        // System.out.println("SEG_LINETO");
300
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
301

    
302
                                        break;
303

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

    
307
                                        break;
308

    
309
                                case PathIterator.SEG_CUBICTO:
310
                                        System.out.println("Not supported here");
311

    
312
                                        break;
313

    
314
                                case PathIterator.SEG_CLOSE:
315
                                        System.out.println("SEG_CLOSE");
316

    
317
                                        // A?adimos el primer punto para cerrar.
318
                                        FPoint2D firstPoint = (FPoint2D) arrayPoints.get(0);
319
                                        arrayPoints.add(new FPoint2D(firstPoint.getX(),
320
                                                        firstPoint.getY()));
321

    
322
                                        break;
323
                        } //end switch
324

    
325
                        theIterator.next();
326
                } 
327

    
328
                Integer[] integers = (Integer[]) arrayParts.toArray(new Integer[0]);
329
                parts = new int[integers.length];
330
                for (int i = 0; i < integers.length; i++) {
331
                        parts[i] = integers[i].intValue();
332
                }
333

    
334
                points = (FPoint2D[]) arrayPoints.toArray(new FPoint2D[0]);
335

    
336
        }
337
}