Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-file / org / gvsig / data / datastores / vectorial / file / shp / utils / SHPMultiPoint.java @ 21865

History | View | Annotate | Download (6.35 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.data.datastores.vectorial.file.shp.utils;
42

    
43
import java.awt.geom.PathIterator;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
import java.nio.ByteBuffer;
47
import java.nio.MappedByteBuffer;
48
import java.util.ArrayList;
49

    
50
import org.gvsig.datasources.common.IByteBuffer;
51
import org.gvsig.fmap.geom.Geometry;
52
import org.gvsig.fmap.geom.aggregate.MultiPoint2D;
53
import org.gvsig.fmap.geom.primitive.AbstractPrimitive;
54

    
55
/**
56
 * Elemento shape de tipo multipunto.
57
 *
58
 * @author Vicente Caballero Navarro
59
 */
60
public class SHPMultiPoint implements SHPShape {
61
        private int m_type;
62
        private int numpoints;
63
        private Point2D[] points;
64
        private double[] zs;
65

    
66
        /**
67
         * Crea un nuevo SHPMultiPoint.
68
         */
69
        public SHPMultiPoint() {
70
                m_type = SHP.MULTIPOINT2D;
71
        }
72

    
73
        /**
74
         * Crea un nuevo SHPMultiPoint.
75
         *
76
         * @param type Tipo de multipunto.
77
         *
78
         * @throws ShapefileException
79
         */
80
        public SHPMultiPoint(int type) {
81
                if ((type != SHP.MULTIPOINT2D) &&
82
                                (type != SHP.MULTIPOINTM) &&
83
                                (type != SHP.MULTIPOINT3D)) {
84
//                        throw new ShapefileException("No es de tipo 8, 18, o 28");
85
                }
86

    
87
                m_type = type;
88
        }
89

    
90
        /**
91
         * Devuelve el tipo de multipoint en concreto.
92
         *
93
         * @return Tipo de multipoint.
94
         */
95
        public int getShapeType() {
96
                return m_type;
97
        }
98

    
99
        /**
100
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
101
         */
102
        public Geometry read(MappedByteBuffer buffer, int type) {
103
                double minX = buffer.getDouble();
104
                double minY = buffer.getDouble();
105
                double maxX = buffer.getDouble();
106
                double maxY = buffer.getDouble();
107
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
108
                                maxY - maxY);
109
                int numpoints = buffer.getInt();
110
                org.gvsig.fmap.geom.primitive.Point2D[] p = new org.gvsig.fmap.geom.primitive.Point2D[numpoints];
111

    
112
                for (int t = 0; t < numpoints; t++) {
113
                        double x = buffer.getDouble();
114
                        double y = buffer.getDouble();
115
                        p[t] = new org.gvsig.fmap.geom.primitive.Point2D(x, y);
116
                }
117

    
118
                /*   if (m_type == FConstant.SHAPE_TYPE_MULTIPOINTZ) {
119
                   buffer.position(buffer.position() + (2 * 8));
120
                   for (int t = 0; t < numpoints; t++) {
121
                       p[t].z = buffer.getDouble(); //z
122
                   }
123
                   }
124
                 */
125
                return (Geometry) new MultiPoint2D(p);
126
        }
127

    
128
        /**
129
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
130
         */
131
        public void write(IByteBuffer buffer, Geometry geometry) {
132
                // FMultiPoint2D mp = (FMultiPoint2D) geometry.getShape();
133
                int p = buffer.position();
134

    
135
                Rectangle2D box = geometry.getBounds2D();
136
                buffer.putDouble(box.getMinX());
137
                buffer.putDouble(box.getMinY());
138
                buffer.putDouble(box.getMaxX());
139
                buffer.putDouble(box.getMaxY());
140
                ///obtainsPoints(geometry.getGeneralPathXIterator());
141
                buffer.putInt(numpoints);
142

    
143
                for (int t = 0, tt = numpoints; t < tt; t++) {
144
                        Point2D point = points[t];
145
                        buffer.putDouble(point.getX());
146
                        buffer.putDouble(point.getY());
147
                }
148

    
149
                  if (m_type == SHP.MULTIPOINT3D) {
150
                   double[] zExtreame = SHP.getZMinMax(zs);
151
                   if (Double.isNaN(zExtreame[0])) {
152
                       buffer.putDouble(0.0);
153
                       buffer.putDouble(0.0);
154
                   } else {
155
                       buffer.putDouble(zExtreame[0]);
156
                       buffer.putDouble(zExtreame[1]);
157
                   }
158
                   for (int t = 0; t < numpoints; t++) {
159
                       double z = zs[t];
160
                       if (Double.isNaN(z)) {
161
                           buffer.putDouble(0.0);
162
                       } else {
163
                           buffer.putDouble(z);
164
                       }
165
                   }
166
                   }
167
                   if ((m_type == SHP.MULTIPOINTM) ||
168
                           (m_type == SHP.MULTIPOINT3D)) {
169
                       buffer.putDouble(-10E40);
170
                       buffer.putDouble(-10E40);
171
                       for (int t = 0; t < numpoints; t++) {
172
                           buffer.putDouble(-10E40);
173
                       }
174
                   }
175

    
176
        }
177

    
178
        /**
179
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
180
         */
181
        public int getLength(Geometry fgeometry) {
182
                //FMultiPoint2D mp = (FMultiPoint2D) fgeometry.getShape();
183
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
184

    
185
                int length;
186

    
187
                if (m_type == SHP.MULTIPOINT2D) {
188
                        // two doubles per coord (16 * numgeoms) + 40 for header
189
                        length = (numpoints * 16) + 40;
190
                } else if (m_type == SHP.MULTIPOINTM) {
191
                        // add the additional MMin, MMax for 16, then 8 per measure
192
                        length = (numpoints * 16) + 40 + 16 + (8 * numpoints);
193
                } else if (m_type == SHP.MULTIPOINT3D) {
194
                        // add the additional ZMin,ZMax, plus 8 per Z
195
                        length = (numpoints * 16) + 40 + 16 + (8 * numpoints);
196
                } else {
197
                        throw new IllegalStateException("Expected ShapeType of Arc, got " +
198
                                m_type);
199
                }
200

    
201
                return length;
202
        }
203

    
204
        /**
205
         * @see com.iver.cit.gvsig.fmap.drivers.shp.write.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
206
         */
207
        public void obtainsPoints(Geometry g) {
208
                if (SHP.MULTIPOINT3D == m_type){
209
                        zs=((AbstractPrimitive)g).getZs();
210
                }
211
                PathIterator theIterator = g.getPathIterator(null); //polyLine.getPathIterator(null, flatness);
212
                double[] theData = new double[6];
213
                ArrayList ps=new ArrayList();
214
                while (!theIterator.isDone()) {
215
                        //while not done
216
                        int theType = theIterator.currentSegment(theData);
217

    
218
                        ps.add(new Point2D.Double(theData[0], theData[1]));
219
                        theIterator.next();
220
                } //end while loop
221
                points=(Point2D[])ps.toArray(new Point2D.Double[0]);
222
                numpoints=points.length;
223
        }
224
//        public void setFlatness(double flatness) {
225
//                //this.flatness=flatness;
226
//        }
227

    
228
}