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 / SHPPolygon.java @ 40435

History | View | Annotate | Download (6.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.Rectangle2D;
44
import java.nio.ByteBuffer;
45
import java.nio.MappedByteBuffer;
46

    
47
import org.gvsig.fmap.geom.Geometry;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.GeometryManager;
50
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
51
import org.gvsig.fmap.geom.exception.CreateGeometryException;
52
import org.gvsig.fmap.geom.primitive.Envelope;
53
import org.gvsig.fmap.geom.primitive.Point;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
/**
58
 * Elemento shape de tipo Pol?gono.
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class SHPPolygon extends SHPMultiLine {
63
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
64
        private static final Logger logger = LoggerFactory.getLogger(SHPPolygon.class);
65

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

    
73
        /**
74
         * Crea un nuevo SHPPolygon.
75
         *
76
         * @param type Tipo de shape.
77
         *
78
         * @throws ShapefileException
79
         */
80
        public SHPPolygon(int type) {
81
                if ((type != SHP.POLYGON2D) &&
82
                                (type != SHP.POLYGONM) &&
83
                                (type != SHP.POLYGON3D)) {
84
//                        throw new ShapefileException("No es de tipo 5, 15, o 25");
85
                }
86

    
87
                m_type = type;
88
        }
89

    
90
        /**
91
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
92
         */
93
        public int getShapeType() {
94
                return m_type;
95
        }
96

    
97
        /**
98
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#read(MappedByteBuffer, int)
99
         */
100
        public synchronized Geometry read(MappedByteBuffer buffer, int type) {
101
                double minX = buffer.getDouble();
102
                double minY = buffer.getDouble();
103
                double maxX = buffer.getDouble();
104
                double maxY = buffer.getDouble();
105
                Rectangle2D rec = new Rectangle2D.Double(minX, minY, maxX - minX,
106
                                maxY - maxY);
107
                int numParts = buffer.getInt();
108
                int numPoints = buffer.getInt();
109

    
110
                int[] partOffsets = new int[numParts];
111

    
112
                for (int i = 0; i < numParts; i++) {
113
                        partOffsets[i] = buffer.getInt();
114
                }
115

    
116
                Point[] points = readPoints(buffer, numPoints);
117

    
118
                /* if (m_type == FConstant.SHAPE_TYPE_POLYGONZ) {
119
                   //z
120
                   buffer.position(buffer.position() + (2 * 8));
121
                   for (int t = 0; t < numPoints; t++) {
122
                       points[t].z = buffer.getDouble();
123
                   }
124
                   }
125
                 */
126
                int offset = 0;
127
                int start;
128
                int finish;
129
                int length;
130

    
131
                for (int part = 0; part < numParts; part++) {
132
                        start = partOffsets[part];
133

    
134
                        if (part == (numParts - 1)) {
135
                                finish = numPoints;
136
                        } else {
137
                                finish = partOffsets[part + 1];
138
                        }
139

    
140
                        length = finish - start;
141

    
142
                        Point[] pointsPart = new Point[length];
143

    
144
                        for (int i = 0; i < length; i++) {
145
                                pointsPart[i] = points[offset++];
146
                        }
147
                }
148

    
149
                try {
150
                        return geomManager.createSurface(getGeneralPathX(points, partOffsets), SUBTYPES.GEOM2D);
151
                } catch (CreateGeometryException e) {
152
                        logger.error("Error creating a surface", e);
153
                        return null;
154
                }
155
        }
156

    
157
        /**
158
         * Lee los puntos del buffer.
159
         *
160
         * @param buffer
161
         * @param numPoints N?mero de puntos.
162
         *
163
         * @return Vector de Puntos.
164
         */
165
        private synchronized Point[] readPoints(final MappedByteBuffer buffer,
166
                final int numPoints) {
167
                Point[] points = new Point[numPoints];
168

    
169
                for (int t = 0; t < numPoints; t++) {
170
                        try {
171
                                points[t] = geomManager.createPoint(buffer.getDouble(), buffer.getDouble(), SUBTYPES.GEOM2D);
172
                        } catch (CreateGeometryException e) {
173
                                logger.error("Error creating a point", e);
174
                        }
175
                }
176

    
177
                return points;
178
        }
179

    
180
        /**
181
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
182
         */
183
        public synchronized void write(ByteBuffer buffer, Geometry geometry) {
184
                Envelope env = geometry.getEnvelope();
185

    
186
                buffer.putDouble(env.getMinimum(0));
187
                buffer.putDouble(env.getMinimum(1));
188
                buffer.putDouble(env.getMaximum(0));
189
                buffer.putDouble(env.getMaximum(1));
190
                
191
                int nparts = parts.length;
192
                int npoints = points.length;
193

    
194
                buffer.putInt(nparts);
195
                buffer.putInt(npoints);
196

    
197
                for (int t = 0; t < nparts; t++) {
198
                        buffer.putInt(parts[t]);
199
                }
200

    
201
                for (int t = 0; t < points.length; t++) {
202
                        buffer.putDouble(points[t].getX());
203
                        buffer.putDouble(points[t].getY());
204
                }
205

    
206
                if (m_type == SHP.POLYGON3D) {
207
                        double[] zExtreame = SHP.getZMinMax(zs);
208
                        if (Double.isNaN(zExtreame[0])) {
209
                                buffer.putDouble(0.0);
210
                                buffer.putDouble(0.0);
211
                        } else {
212
                                buffer.putDouble(zExtreame[0]);
213
                                buffer.putDouble(zExtreame[1]);
214
                        }
215
                        for (int t = 0; t < npoints; t++) {
216
                                double z = zs[t];
217
                                if (Double.isNaN(z)) {
218
                                        buffer.putDouble(0.0);
219
                                } else {
220
                                        buffer.putDouble(z);
221
                                }
222
                        }
223
                }
224

    
225
                if ((m_type == SHP.POLYGONM)) {
226
                        buffer.putDouble(-10E40);
227
                        buffer.putDouble(-10E40);
228

    
229
                        for (int t = 0; t < npoints; t++) {
230
                                buffer.putDouble(-10E40);
231
                        }
232
                }
233
        }
234

    
235
        /**
236
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
237
         */
238
        public synchronized int getLength(Geometry fgeometry) {
239
                int npoints = points.length;
240

    
241
                int length;
242

    
243
                if (m_type == SHP.POLYGON3D || m_type == SHP.POLYGONM) {
244
                        length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) + 16;
245
                } else if (m_type == SHP.POLYGON2D) {
246
                        length = 44 + (4 * parts.length) + (16 * npoints);
247
                } else {
248
                        throw new IllegalStateException("Expected ShapeType of Polygon, got " + m_type);
249
                }
250

    
251
                return length;
252
        }
253
}