Statistics
| Revision:

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

History | View | Annotate | Download (8.32 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 java.awt.geom.PathIterator;
44
import java.awt.geom.Rectangle2D;
45
import java.nio.ByteBuffer;
46
import java.nio.MappedByteBuffer;
47
import java.util.ArrayList;
48

    
49
import com.iver.cit.gvsig.fmap.core.FPoint2D;
50
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
51
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
52
import com.iver.cit.gvsig.fmap.core.GeneralPathXIterator;
53
import com.iver.cit.gvsig.fmap.core.IGeometry;
54
import com.iver.cit.gvsig.fmap.core.IGeometry3D;
55
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
56
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
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
        protected double[] zs;
69

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

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

    
91
                m_type = type;
92
        }
93

    
94
        /**
95
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
96
         */
97
        public int getShapeType() {
98
                return m_type;
99
        }
100

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

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

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

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

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

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

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

    
138
        /**
139
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
140
         */
141
        public void write(ByteBuffer buffer, IGeometry geometry) {
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
                int numParts = parts.length;
148
                int npoints = points.length;
149
                buffer.putInt(numParts);
150
                buffer.putInt(npoints);
151

    
152
                for (int i = 0; i < numParts; i++) {
153
                        buffer.putInt(parts[i]);
154
                }
155

    
156
                for (int t = 0; t < npoints; t++) {
157
                        buffer.putDouble(points[t].getX());
158
                        buffer.putDouble(points[t].getY());
159
                }
160

    
161
                  if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
162
                   double[] zExtreame = SHP.getZMinMax(zs);
163
                   if (Double.isNaN(zExtreame[0])) {
164
                       buffer.putDouble(0.0);
165
                       buffer.putDouble(0.0);
166
                   } else {
167
                       buffer.putDouble(zExtreame[0]);
168
                       buffer.putDouble(zExtreame[1]);
169
                   }
170
                   for (int t = 0; t < npoints; t++) {
171
                       double z = zs[t];
172
                       if (Double.isNaN(z)) {
173
                           buffer.putDouble(0.0);
174
                       } else {
175
                           buffer.putDouble(z);
176
                       }
177
                   }
178
                  
179
                   }
180
                   if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
181
                       buffer.putDouble(-10E40);
182
                       buffer.putDouble(-10E40);
183
                       for (int t = 0; t < npoints; t++) {
184
                           buffer.putDouble(-10E40);
185
                       }
186
                   }
187
                 
188
        }
189

    
190
        /**
191
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
192
         */
193
        public int getLength(IGeometry fgeometry) {
194
                int numlines;
195
                int numpoints;
196
                int length;
197

    
198
                numlines = parts.length;
199
                numpoints = points.length;
200
                if (m_type == FConstant.SHAPE_TYPE_POLYLINE) {
201
                        length = 44 + (4 * numlines) + (numpoints * 16);
202
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
203
                        length = 44 + (4 * numlines) + (numpoints * 16) + 8 + 8 +
204
                                (8 * numpoints);
205
                } else if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
206
                        length = 44 + (4 * numlines) + (numpoints * 16) +
207
                                (8 * numpoints) + 8 + 8;
208
                } else {
209
                        throw new IllegalStateException("Expected ShapeType of Arc, got " +
210
                                m_type);
211
                }
212

    
213
                return length;
214
        }
215

    
216
        /**
217
         * DOCUMENT ME!
218
         *
219
         * @param po DOCUMENT ME!
220
         * @param pa DOCUMENT ME!
221
         *
222
         * @return DOCUMENT ME!
223
         */
224
        protected GeneralPathX getGeneralPathX(FPoint2D[] po, int[] pa) {
225
                GeneralPathX gPX = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
226
                                po.length);
227
                int j = 0;
228

    
229
                for (int i = 0; i < po.length; i++) {
230
                        if (i == pa[j]) {
231
                                gPX.moveTo(po[i].getX(), po[i].getY());
232

    
233
                                if (j < (pa.length - 1)) {
234
                                        j++;
235
                                }
236
                        } else {
237
                                gPX.lineTo(po[i].getX(), po[i].getY());
238
                        }
239
                }
240

    
241
                return gPX;
242
        }
243

    
244
        /**
245
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
246
         */
247
        public void obtainsPoints(IGeometry g) {
248
                if (FConstant.SHAPE_TYPE_POLYLINEZ == m_type || FConstant.SHAPE_TYPE_POLYGONZ == m_type){
249
                        zs=((IGeometry3D)g).getZs();
250
                }
251
                ArrayList arrayPoints = null;
252
                ArrayList arrayParts = new ArrayList();
253
                GeneralPathXIterator theIterator = g.getGeneralPathXIterator(); //polyLine.getPathIterator(null, flatness);
254
                double[] theData = new double[6];
255
                int numParts = 0;
256
                while (!theIterator.isDone()) {
257
                        //while not done
258
                        int theType = theIterator.currentSegment(theData);
259

    
260
                        //Populate a segment of the new
261
                        // GeneralPathX object.
262
                        //Process the current segment to populate a new
263
                        // segment of the new GeneralPathX object.
264
                        
265
                        switch (theType) {
266
                                case PathIterator.SEG_MOVETO:
267

    
268
                                        // System.out.println("SEG_MOVETO");
269
                                        if (arrayPoints == null) {
270
                                                arrayPoints = new ArrayList();
271
                                                arrayParts.add(new Integer(0));
272
                                        } else {
273
                                                arrayParts.add(new Integer(arrayPoints.size()));
274
                                        }
275

    
276
                                        numParts++;
277
                                        
278
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
279

    
280
                                        break;
281

    
282
                                case PathIterator.SEG_LINETO:
283

    
284
                                        // System.out.println("SEG_LINETO");
285
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
286

    
287
                                        break;
288

    
289
                                case PathIterator.SEG_QUADTO:
290
                                        System.out.println("Not supported here");
291

    
292
                                        break;
293

    
294
                                case PathIterator.SEG_CUBICTO:
295
                                        System.out.println("Not supported here");
296

    
297
                                        break;
298

    
299
                                case PathIterator.SEG_CLOSE:
300
                                        System.out.println("SEG_CLOSE");
301

    
302
                                        // A?adimos el primer punto para cerrar.
303
                                        FPoint2D firstPoint = (FPoint2D) arrayPoints.get(0);
304
                                        arrayPoints.add(new FPoint2D(firstPoint.getX(),
305
                                                        firstPoint.getY()));
306

    
307
                                        break;
308
                        } //end switch
309

    
310
                        theIterator.next();
311
                } 
312

    
313
                Integer[] integers = (Integer[]) arrayParts.toArray(new Integer[0]);
314
                parts = new int[integers.length];
315
                for (int i = 0; i < integers.length; i++) {
316
                        parts[i] = integers[i].intValue();
317
                }
318

    
319
                points = (FPoint2D[]) arrayPoints.toArray(new FPoint2D[0]);
320

    
321
        }
322
}