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 @ 40559

History | View | Annotate | Download (7.29 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.shp.utils;
25

    
26
import java.awt.geom.PathIterator;
27
import java.awt.geom.Point2D;
28
import java.awt.geom.Rectangle2D;
29
import java.nio.ByteBuffer;
30
import java.nio.MappedByteBuffer;
31
import java.util.ArrayList;
32

    
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.GeometryLocator;
35
import org.gvsig.fmap.geom.GeometryManager;
36
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
37
import org.gvsig.fmap.geom.Geometry.TYPES;
38
import org.gvsig.fmap.geom.aggregate.MultiPoint;
39
import org.gvsig.fmap.geom.exception.CreateGeometryException;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
/**
45
 * Elemento shape de tipo multipunto.
46
 *
47
 * @author Vicente Caballero Navarro
48
 */
49
public class SHPMultiPoint implements SHPShape {
50
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
51
        private static final Logger logger = LoggerFactory.getLogger(SHPMultiPoint.class);
52
        private int m_type;
53
        private int numpoints;
54
        private Point2D[] points;
55
        private double[] zs;
56

    
57
        /**
58
         * Crea un nuevo SHPMultiPoint.
59
         */
60
        public SHPMultiPoint() {
61
                m_type = SHP.MULTIPOINT2D;
62
        }
63

    
64
        /**
65
         * Crea un nuevo SHPMultiPoint.
66
         *
67
         * @param type Tipo de multipunto.
68
         *
69
         * @throws ShapefileException
70
         */
71
        public SHPMultiPoint(int type) {
72
                if ((type != SHP.MULTIPOINT2D) &&
73
                                (type != SHP.MULTIPOINTM) &&
74
                                (type != SHP.MULTIPOINT3D)) {
75
//                        throw new ShapefileException("No es de tipo 8, 18, o 28");
76
                }
77

    
78
                m_type = type;
79
        }
80

    
81
        /**
82
         * Devuelve el tipo de multipoint en concreto.
83
         *
84
         * @return Tipo de multipoint.
85
         */
86
        public int getShapeType() {
87
                return m_type;
88
        }
89

    
90
        /**
91
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
92
         */
93
        public Geometry read(MappedByteBuffer buffer, int type) {
94
                double minX = buffer.getDouble();
95
                double minY = buffer.getDouble();
96
                double maxX = buffer.getDouble();
97
                double maxY = buffer.getDouble();
98
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
99
                                maxY - maxY);
100
                int numpoints = buffer.getInt();
101
                org.gvsig.fmap.geom.primitive.Point[] p = new org.gvsig.fmap.geom.primitive.Point[numpoints];
102

    
103
                for (int t = 0; t < numpoints; t++) {
104
                        double x = buffer.getDouble();
105
                        double y = buffer.getDouble();
106
                        try {
107
                                p[t] = geomManager.createPoint(x, y, SUBTYPES.GEOM2D);
108
                        } catch (CreateGeometryException e) {
109
                                logger.error("Error creating a point", e);
110
                        }
111
                }
112

    
113
                /*   if (m_type == FConstant.SHAPE_TYPE_MULTIPOINTZ) {
114
                   buffer.position(buffer.position() + (2 * 8));
115
                   for (int t = 0; t < numpoints; t++) {
116
                       p[t].z = buffer.getDouble(); //z
117
                   }
118
                   }
119
                 */
120
                MultiPoint multipoint = null;
121
                try {
122
                        multipoint = (MultiPoint)geomManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
123
                        for (int i=0 ; i<p.length ; i++){
124
                                multipoint.addPoint(p[i]);
125
                        }                        
126
                } catch (CreateGeometryException e) {
127
                        logger.error("Error creating the multipoint", e);
128
                }
129
                return multipoint;                
130
        }
131

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

    
139
                Envelope env = geometry.getEnvelope();
140

    
141
                buffer.putDouble(env.getMinimum(0));
142
                buffer.putDouble(env.getMinimum(1));
143
                buffer.putDouble(env.getMaximum(0));
144
                buffer.putDouble(env.getMaximum(1));
145
                ///obtainsPoints(geometry.getGeneralPathXIterator());
146
                buffer.putInt(numpoints);
147

    
148
                for (int t = 0, tt = numpoints; t < tt; t++) {
149
                        Point2D point = points[t];
150
                        buffer.putDouble(point.getX());
151
                        buffer.putDouble(point.getY());
152
                }
153

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

    
181
        }
182

    
183
        /**
184
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
185
         */
186
        public int getLength(Geometry fgeometry) {
187
                //FMultiPoint2D mp = (FMultiPoint2D) fgeometry.getShape();
188
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
189

    
190
                int length;
191

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

    
206
                return length;
207
        }
208

    
209
        /**
210
         * @see com.iver.cit.gvsig.fmap.drivers.shp.write.SHPShape#obtainsPoints(com.iver.cit.gvsig.fmap.core.GeneralPathXIterator)
211
         */
212
        public void obtainsPoints(Geometry g) {
213
                if (SHP.MULTIPOINT3D == m_type){
214
                        MultiPoint multipoint = (MultiPoint)g;
215
                        zs = new double[multipoint.getPrimitivesNumber()];
216
                        for (int i=0 ; i<zs.length ; i++){
217
                                zs[i] = multipoint.getPointAt(i).getCoordinateAt(2);
218
                        }                
219
                }
220
                PathIterator theIterator = g.getPathIterator(null); //polyLine.getPathIterator(null, flatness);
221
                double[] theData = new double[6];
222
                ArrayList ps=new ArrayList();
223
                while (!theIterator.isDone()) {
224
                        //while not done
225
//                        int theType = theIterator.currentSegment(theData);
226
                        theIterator.currentSegment(theData);
227

    
228
                        ps.add(new Point2D.Double(theData[0], theData[1]));
229
                        theIterator.next();
230
                } //end while loop
231
                points=(Point2D[])ps.toArray(new Point2D.Double[0]);
232
                numpoints=points.length;
233
        }
234
//        public void setFlatness(double flatness) {
235
//                //this.flatness=flatness;
236
//        }
237
}