Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / utils / SHPMultiPoint.java @ 40435

History | View | Annotate | Download (7.57 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.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.fmap.geom.Geometry;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.GeometryManager;
53
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
54
import org.gvsig.fmap.geom.Geometry.TYPES;
55
import org.gvsig.fmap.geom.aggregate.MultiPoint;
56
import org.gvsig.fmap.geom.exception.CreateGeometryException;
57
import org.gvsig.fmap.geom.primitive.Envelope;
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

    
61
/**
62
 * Elemento shape de tipo multipunto.
63
 *
64
 * @author Vicente Caballero Navarro
65
 */
66
public class SHPMultiPoint implements SHPShape {
67
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
68
        private static final Logger logger = LoggerFactory.getLogger(SHPMultiPoint.class);
69
        private int m_type;
70
        private int numpoints;
71
        private Point2D[] points;
72
        private double[] zs;
73

    
74
        /**
75
         * Crea un nuevo SHPMultiPoint.
76
         */
77
        public SHPMultiPoint() {
78
                m_type = SHP.MULTIPOINT2D;
79
        }
80

    
81
        /**
82
         * Crea un nuevo SHPMultiPoint.
83
         *
84
         * @param type Tipo de multipunto.
85
         *
86
         * @throws ShapefileException
87
         */
88
        public SHPMultiPoint(int type) {
89
                if ((type != SHP.MULTIPOINT2D) &&
90
                                (type != SHP.MULTIPOINTM) &&
91
                                (type != SHP.MULTIPOINT3D)) {
92
//                        throw new ShapefileException("No es de tipo 8, 18, o 28");
93
                }
94

    
95
                m_type = type;
96
        }
97

    
98
        /**
99
         * Devuelve el tipo de multipoint en concreto.
100
         *
101
         * @return Tipo de multipoint.
102
         */
103
        public int getShapeType() {
104
                return m_type;
105
        }
106

    
107
        /**
108
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
109
         */
110
        public Geometry read(MappedByteBuffer buffer, int type) {
111
                double minX = buffer.getDouble();
112
                double minY = buffer.getDouble();
113
                double maxX = buffer.getDouble();
114
                double maxY = buffer.getDouble();
115
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
116
                                maxY - maxY);
117
                int numpoints = buffer.getInt();
118
                org.gvsig.fmap.geom.primitive.Point[] p = new org.gvsig.fmap.geom.primitive.Point[numpoints];
119

    
120
                for (int t = 0; t < numpoints; t++) {
121
                        double x = buffer.getDouble();
122
                        double y = buffer.getDouble();
123
                        try {
124
                                p[t] = geomManager.createPoint(x, y, SUBTYPES.GEOM2D);
125
                        } catch (CreateGeometryException e) {
126
                                logger.error("Error creating a point", e);
127
                        }
128
                }
129

    
130
                /*   if (m_type == FConstant.SHAPE_TYPE_MULTIPOINTZ) {
131
                   buffer.position(buffer.position() + (2 * 8));
132
                   for (int t = 0; t < numpoints; t++) {
133
                       p[t].z = buffer.getDouble(); //z
134
                   }
135
                   }
136
                 */
137
                MultiPoint multipoint = null;
138
                try {
139
                        multipoint = (MultiPoint)geomManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
140
                        for (int i=0 ; i<p.length ; i++){
141
                                multipoint.addPoint(p[i]);
142
                        }                        
143
                } catch (CreateGeometryException e) {
144
                        logger.error("Error creating the multipoint", e);
145
                }
146
                return multipoint;                
147
        }
148

    
149
        /**
150
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
151
         */
152
        public void write(ByteBuffer buffer, Geometry geometry) {
153
                // FMultiPoint2D mp = (FMultiPoint2D) geometry.getShape();
154
//                int p = buffer.position();
155

    
156
                Envelope env = geometry.getEnvelope();
157

    
158
                buffer.putDouble(env.getMinimum(0));
159
                buffer.putDouble(env.getMinimum(1));
160
                buffer.putDouble(env.getMaximum(0));
161
                buffer.putDouble(env.getMaximum(1));
162
                ///obtainsPoints(geometry.getGeneralPathXIterator());
163
                buffer.putInt(numpoints);
164

    
165
                for (int t = 0, tt = numpoints; t < tt; t++) {
166
                        Point2D point = points[t];
167
                        buffer.putDouble(point.getX());
168
                        buffer.putDouble(point.getY());
169
                }
170

    
171
                  if (m_type == SHP.MULTIPOINT3D) {
172
                   double[] zExtreame = SHP.getZMinMax(zs);
173
                   if (Double.isNaN(zExtreame[0])) {
174
                       buffer.putDouble(0.0);
175
                       buffer.putDouble(0.0);
176
                   } else {
177
                       buffer.putDouble(zExtreame[0]);
178
                       buffer.putDouble(zExtreame[1]);
179
                   }
180
                   for (int t = 0; t < numpoints; t++) {
181
                       double z = zs[t];
182
                       if (Double.isNaN(z)) {
183
                           buffer.putDouble(0.0);
184
                       } else {
185
                           buffer.putDouble(z);
186
                       }
187
                   }
188
                   }
189
                   if ((m_type == SHP.MULTIPOINTM) ||
190
                           (m_type == SHP.MULTIPOINT3D)) {
191
                       buffer.putDouble(-10E40);
192
                       buffer.putDouble(-10E40);
193
                       for (int t = 0; t < numpoints; t++) {
194
                           buffer.putDouble(-10E40);
195
                       }
196
                   }
197

    
198
        }
199

    
200
        /**
201
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
202
         */
203
        public int getLength(Geometry fgeometry) {
204
                //FMultiPoint2D mp = (FMultiPoint2D) fgeometry.getShape();
205
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
206

    
207
                int length;
208

    
209
                if (m_type == SHP.MULTIPOINT2D) {
210
                        // two doubles per coord (16 * numgeoms) + 40 for header
211
                        length = (numpoints * 16) + 40;
212
                } else if (m_type == SHP.MULTIPOINTM) {
213
                        // add the additional MMin, MMax for 16, then 8 per measure
214
                        length = (numpoints * 16) + 40 + 16 + (8 * numpoints);
215
                } else if (m_type == SHP.MULTIPOINT3D) {
216
                        // add the additional ZMin,ZMax, plus 8 per Z
217
                        length = (numpoints * 16) + 40 + 16 + (8 * numpoints);
218
                } else {
219
                        throw new IllegalStateException("Expected ShapeType of Arc, got " +
220
                                m_type);
221
                }
222

    
223
                return length;
224
        }
225

    
226
        /**
227
         * @see com.iver.cit.gvsig.fmap.drivers.shp.write.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
228
         */
229
        public void obtainsPoints(Geometry g) {
230
                if (SHP.MULTIPOINT3D == m_type){
231
                        MultiPoint multipoint = (MultiPoint)g;
232
                        zs = new double[multipoint.getPrimitivesNumber()];
233
                        for (int i=0 ; i<zs.length ; i++){
234
                                zs[i] = multipoint.getPointAt(i).getCoordinateAt(2);
235
                        }                
236
                }
237
                PathIterator theIterator = g.getPathIterator(null); //polyLine.getPathIterator(null, flatness);
238
                double[] theData = new double[6];
239
                ArrayList ps=new ArrayList();
240
                while (!theIterator.isDone()) {
241
                        //while not done
242
//                        int theType = theIterator.currentSegment(theData);
243
                        theIterator.currentSegment(theData);
244

    
245
                        ps.add(new Point2D.Double(theData[0], theData[1]));
246
                        theIterator.next();
247
                } //end while loop
248
                points=(Point2D[])ps.toArray(new Point2D.Double[0]);
249
                numpoints=points.length;
250
        }
251
//        public void setFlatness(double flatness) {
252
//                //this.flatness=flatness;
253
//        }
254
}