Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1006 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / write / SHPMultiLine.java @ 12458

History | View | Annotate | Download (8.48 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.v02.FConstant;
55
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
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
        protected int m_type;
66
        protected int[] parts;
67
        protected FPoint2D[] points;
68
        protected double[] zs;
69
        //double flatness = 0.8; // Por ejemplo. Cuanto m?s peque?o, m?s segmentos necesitar? la curva
70

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

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

    
92
                m_type = type;
93
        }
94

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

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

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

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

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

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

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

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

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

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

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

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

    
180
                   }
181
                   if (m_type == FConstant.SHAPE_TYPE_POLYLINEM) {
182
                       buffer.putDouble(-10E40);
183
                       buffer.putDouble(-10E40);
184
                       for (int t = 0; t < npoints; t++) {
185
                           buffer.putDouble(-10E40);
186
                       }
187
                   }
188

    
189
        }
190

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

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

    
214
                return length;
215
        }
216

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

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

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

    
242
                return gPX;
243
        }
244

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

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

    
266
                        switch (theType) {
267
                                case PathIterator.SEG_MOVETO:
268

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

    
277
                                        numParts++;
278

    
279
                                        arrayPoints.add(new FPoint2D(theData[0], theData[1]));
280

    
281
                                        break;
282

    
283
                                case PathIterator.SEG_LINETO:
284

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

    
288
                                        break;
289

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

    
293
                                        break;
294

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

    
298
                                        break;
299

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

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

    
308
                                        break;
309
                        } //end switch
310

    
311
                        theIterator.next();
312
                }
313

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

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

    
322
        }
323

    
324
//        public void setFlatness(double flatness) {
325
//                this.flatness=flatness;
326
//        }
327
}