Statistics
| Revision:

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

History | View | Annotate | Download (8.65 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.IGeometry;
53
import com.iver.cit.gvsig.fmap.core.IGeometry3D;
54
import com.iver.cit.gvsig.fmap.core.IGeometryM;
55
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
56
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
57
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
58

    
59

    
60
/**
61
 * Elemento shape de tipo multil?nea.
62
 *
63
 * @author Vicente Caballero Navarro
64
 */
65
public class SHPMultiLine implements SHPShape {
66
        protected int m_type;
67
        protected int[] parts;
68
        protected FPoint2D[] points;
69
        protected double[] zs;
70
        //double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva
71

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

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

    
93
                m_type = type;
94
        }
95

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

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

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

    
117
                int[] partOffsets = new int[numParts];
118

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

    
123
                FPoint2D[] points = new FPoint2D[numPoints];
124

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

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

    
140
        /**
141
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
142
         */
143
        public void write(ByteBuffer buffer, IGeometry geometry) {
144
                Rectangle2D rec = geometry.getBounds2D();
145
                buffer.putDouble(rec.getMinX());
146
                buffer.putDouble(rec.getMinY());
147
                buffer.putDouble(rec.getMaxX());
148
                buffer.putDouble(rec.getMaxY());
149
                int numParts = parts.length;
150
                int npoints = points.length;
151
                buffer.putInt(numParts);
152
                buffer.putInt(npoints);
153

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

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

    
163
                  if (m_type == FConstant.SHAPE_TYPE_POLYLINEZ) {
164
                   double[] zExtreame = SHP.getZMinMax(zs);
165
                   if (Double.isNaN(zExtreame[0])) {
166
                       buffer.putDouble(0.0);
167
                       buffer.putDouble(0.0);
168
                   } else {
169
                       buffer.putDouble(zExtreame[0]);
170
                       buffer.putDouble(zExtreame[1]);
171
                   }
172
                   for (int t = 0; t < npoints; t++) {
173
                       double z = zs[t];
174
                       if (Double.isNaN(z)) {
175
                           buffer.putDouble(0.0);
176
                       } else {
177
                           buffer.putDouble(z);
178
                       }
179
                   }
180

    
181
                   }
182
                  if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
183
                       buffer.putDouble(-10E40);
184
                       buffer.putDouble(-10E40);
185
                       double[] ms = ((IGeometryM)geometry).getMs();
186
                       for (int t = 0; t < npoints; t++) {                               
187
                               buffer.putDouble(ms[t]);
188
                       }
189
                  }
190

    
191
        }
192

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

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

    
216
                return length;
217
        }
218

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

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

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

    
244
                return gPX;
245
        }
246

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

    
263
                        //Populate a segment of the new
264
                        // GeneralPathX object.
265
                        //Process the current segment to populate a new
266
                        // segment of the new GeneralPathX object.
267

    
268
                        switch (theType) {
269
                                case PathIterator.SEG_MOVETO:
270

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

    
279
                                        numParts++;
280

    
281
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
282

    
283
                                        break;
284

    
285
                                case PathIterator.SEG_LINETO:
286

    
287
                                        // System.out.println("SEG_LINETO");
288
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
289

    
290
                                        break;
291

    
292
                                case PathIterator.SEG_QUADTO:
293
                                        System.out.println("Not supported here");
294

    
295
                                        break;
296

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

    
300
                                        break;
301

    
302
                                case PathIterator.SEG_CLOSE:
303
                                        System.out.println("SEG_CLOSE");
304

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

    
310
                                        break;
311
                        } //end switch
312

    
313
                        theIterator.next();
314
                }
315

    
316
                Integer[] integers = (Integer[]) arrayParts.toArray(new Integer[0]);
317
                parts = new int[integers.length];
318
                for (int i = 0; i < integers.length; i++) {
319
                        parts[i] = integers[i].intValue();
320
                }
321
                if (arrayPoints==null){
322
                        points=new FPoint2D[0];
323
                        return;
324
                }
325
                points = (FPoint2D[]) arrayPoints.toArray(new FPoint2D[0]);
326

    
327
        }
328

    
329
//        public void setFlatness(double flatness) {
330
//                this.flatness=flatness;
331
//        }
332
}