Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / shp / utils / SHPMultiLine.java @ 24498

History | View | Annotate | Download (8.54 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 org.gvsig.fmap.dal.store.shp.utils;
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 org.gvsig.fmap.geom.Geometry;
50
import org.gvsig.fmap.geom.primitive.AbstractPrimitive;
51
import org.gvsig.fmap.geom.primitive.Curve2D;
52
import org.gvsig.fmap.geom.primitive.GeneralPathX;
53
import org.gvsig.fmap.geom.primitive.Point2D;
54
import org.gvsig.fmap.geom.util.Converter;
55

    
56

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

    
69
        /**
70
         * Crea un nuevo SHPMultiLine.
71
         */
72
        public SHPMultiLine() {
73
                m_type = SHP.POLYLINE2D;
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) {
84
                if ((type != SHP.POLYLINE2D) &&
85
                                (type != SHP.POLYLINEM) &&
86
                                (type != SHP.POLYLINE3D)) {
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 Geometry 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
                Point2D[] points = new Point2D[numPoints];
121

    
122
                for (int t = 0; t < numPoints; t++) {
123
                        points[t] = new Point2D(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 (Geometry) new Curve2D(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, Geometry geometry) {
141
                Rectangle2D rec = geometry.getBounds2D();
142
                buffer.putDouble(rec.getMinX());
143
                buffer.putDouble(rec.getMinY());
144
                buffer.putDouble(rec.getMaxX());
145
                buffer.putDouble(rec.getMaxY());
146
                int numParts = parts.length;
147
                int npoints = points.length;
148
                buffer.putInt(numParts);
149
                buffer.putInt(npoints);
150

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

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

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

    
178
                   }
179
                   if (m_type == SHP.POLYLINEM) {
180
                       buffer.putDouble(-10E40);
181
                       buffer.putDouble(-10E40);
182
                       for (int t = 0; t < npoints; t++) {
183
                           buffer.putDouble(-10E40);
184
                       }
185
                   }
186

    
187
        }
188

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

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

    
212
                return length;
213
        }
214

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

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

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

    
240
                return gPX;
241
        }
242

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

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

    
264
                        switch (theType) {
265
                                case PathIterator.SEG_MOVETO:
266

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

    
275
                                        numParts++;
276

    
277
                                        arrayPoints.add(new Point2D(theData[0], theData[1]));
278

    
279
                                        break;
280

    
281
                                case PathIterator.SEG_LINETO:
282

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

    
286
                                        break;
287

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

    
291
                                        break;
292

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

    
296
                                        break;
297

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

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

    
306
                                        break;
307
                        } //end switch
308

    
309
                        theIterator.next();
310
                }
311

    
312
                Integer[] integers = (Integer[]) arrayParts.toArray(new Integer[0]);
313
                parts = new int[integers.length];
314
                for (int i = 0; i < integers.length; i++) {
315
                        parts[i] = integers[i].intValue();
316
                }
317
                if (arrayPoints==null){
318
                        points=new Point2D[0];
319
                        return;
320
                }
321
                points = (Point2D[]) arrayPoints.toArray(new Point2D[0]);
322

    
323
        }
324

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