Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / shp / utils / SHPPolygon.java @ 40368

History | View | Annotate | Download (7.52 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
                //FPolygon2D polyLine;
185
                //polyLine = (FPolygon2D) geometry.getShape();
186
                Envelope env = geometry.getEnvelope();
187

    
188
                buffer.putDouble(env.getMinimum(0));
189
                buffer.putDouble(env.getMinimum(1));
190
                buffer.putDouble(env.getMaximum(0));
191
                buffer.putDouble(env.getMaximum(1));
192
                //////
193
                ///obtainsPoints(geometry.getGeneralPathXIterator());
194

    
195
                //int[] parts=polyLine.getParts();
196
                //FPoint2D[] points=polyLine.getPoints();
197
                int nparts = parts.length;
198
                int npoints = points.length;
199

    
200
                //////
201
                ///int npoints = polyLine.getNumPoints();
202
                ///int nparts = polyLine.getNumParts();
203
                buffer.putInt(nparts);
204
                buffer.putInt(npoints);
205

    
206
//                int count = 0;
207

    
208
                for (int t = 0; t < nparts; t++) {
209
                        ///buffer.putInt(polyLine.getPart(t));
210
                        buffer.putInt(parts[t]);
211
                }
212

    
213
                ///FPoint[] points = polyLine.getPoints();
214
                for (int t = 0; t < points.length; t++) {
215
                        ///buffer.putDouble(points[t].x);
216
                        ///buffer.putDouble(points[t].y);
217
                        buffer.putDouble(points[t].getX());
218
                        buffer.putDouble(points[t].getY());
219
                }
220

    
221
                if (m_type == SHP.POLYGON3D) {
222
                        double[] zExtreame = SHP.getZMinMax(zs);
223
                        if (Double.isNaN(zExtreame[0])) {
224
                                buffer.putDouble(0.0);
225
                                buffer.putDouble(0.0);
226
                        } else {
227
                                buffer.putDouble(zExtreame[0]);
228
                                buffer.putDouble(zExtreame[1]);
229
                        }
230
                        for (int t = 0; t < npoints; t++) {
231
                                double z = zs[t];
232
                                if (Double.isNaN(z)) {
233
                                        buffer.putDouble(0.0);
234
                                } else {
235
                                        buffer.putDouble(z);
236
                                }
237
                        }
238
                }
239

    
240
                if ((m_type == SHP.POLYGONM) ||
241
                                (m_type == SHP.POLYGON3D)) {
242
                        buffer.putDouble(-10E40);
243
                        buffer.putDouble(-10E40);
244

    
245
                        for (int t = 0; t < npoints; t++) {
246
                                buffer.putDouble(-10E40);
247
                        }
248
                }
249
        }
250

    
251
        /**
252
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
253
         */
254
        public synchronized int getLength(Geometry fgeometry) {
255
                // FPolygon2D multi;
256
                //multi = (FPolygon2D) fgeometry.getShape();
257
                ///int nrings = 0;
258
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
259

    
260
                //int[] parts=multi.getParts();
261
                //FPoint2D[] points;
262
                /////////
263
                //points = multi.getPoints();
264
                int npoints = points.length;
265

    
266
                ///////////
267
                ///nrings = multi.getNumParts();
268
                ///int npoints = multi.getNumPoints();
269
                int length;
270

    
271
                if (m_type == SHP.POLYGON3D) {
272
                        length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) +
273
                                16;
274
                } else if (m_type == SHP.POLYGONM) {
275
                        length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) +
276
                                16;
277
                } else if (m_type == SHP.POLYGON2D) {
278
                        length = 44 + (4 * parts.length) + (16 * npoints);
279
                } else {
280
                        throw new IllegalStateException(
281
                                "Expected ShapeType of Polygon, got " + m_type);
282
                }
283

    
284
                return length;
285
        }
286
}