Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / shp / utils / SHPPolygon.java @ 21606

History | View | Annotate | Download (7.17 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.Rectangle2D;
44

    
45
import org.gvsig.datasources.common.IByteBuffer;
46

    
47
import es.prodevelop.gvsig.mobile.fmap.core.FPoint2D;
48
import es.prodevelop.gvsig.mobile.fmap.core.FPolygon2D;
49
import es.prodevelop.gvsig.mobile.fmap.core.IGeometry;
50
import es.prodevelop.gvsig.mobile.fmap.symbol.FConstant;
51

    
52

    
53
//import com.iver.cit.gvsig.fmap.core.FPoint2D;
54
//import com.iver.cit.gvsig.fmap.core.FPolygon2D;
55
//import com.iver.cit.gvsig.fmap.core.IGeometry;
56
//import com.iver.cit.gvsig.fmap.core.v02.FConstant;
57
//import com.iver.cit.gvsig.fmap.drivers.shp.write.ShapefileException;
58

    
59

    
60
/**
61
 * Elemento shape de tipo Pol?gono.
62
 *
63
 * @author Vicente Caballero Navarro
64
 */
65
public class SHPPolygon extends SHPMultiLine {
66

    
67
        /**
68
         * Crea un nuevo SHPPolygon.
69
         */
70
        public SHPPolygon() {
71
                m_type = FConstant.SHAPE_TYPE_POLYGON;
72
        }
73

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

    
88
                m_type = type;
89
        }
90

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

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

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

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

    
117
                FPoint2D[] points = readPoints(buffer, numPoints);
118

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

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

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

    
141
                        length = finish - start;
142

    
143
                        FPoint2D[] pointsPart = new FPoint2D[length];
144

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

    
150
                return (IGeometry) new FPolygon2D(getGeneralPathX(points, partOffsets));
151
        }
152

    
153
        /**
154
         * Lee los puntos del buffer.
155
         *
156
         * @param buffer
157
         * @param numPoints N?mero de puntos.
158
         *
159
         * @return Vector de Puntos.
160
         */
161
        private synchronized FPoint2D[] readPoints(final IByteBuffer buffer,
162
                final int numPoints) {
163
                FPoint2D[] points = new FPoint2D[numPoints];
164

    
165
                for (int t = 0; t < numPoints; t++) {
166
                        points[t] = new FPoint2D(buffer.getDouble(), buffer.getDouble());
167
                }
168

    
169
                return points;
170
        }
171

    
172
        /**
173
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
174
         */
175
        public synchronized void write(IByteBuffer buffer, IGeometry geometry) {
176
                //FPolygon2D polyLine;
177
                //polyLine = (FPolygon2D) geometry.getShape();
178
                Rectangle2D rec = geometry.getBounds2D();
179
                buffer.putDouble(rec.getMinX());
180
                buffer.putDouble(rec.getMinY());
181
                buffer.putDouble(rec.getMaxX());
182
                buffer.putDouble(rec.getMaxY());
183

    
184
                //////
185
                ///obtainsPoints(geometry.getGeneralPathXIterator());
186

    
187
                //int[] parts=polyLine.getParts();
188
                //FPoint2D[] points=polyLine.getPoints();
189
                int nparts = parts.length;
190
                int npoints = points.length;
191

    
192
                //////
193
                ///int npoints = polyLine.getNumPoints();
194
                ///int nparts = polyLine.getNumParts();
195
                buffer.putInt(nparts);
196
                buffer.putInt(npoints);
197

    
198
                int count = 0;
199

    
200
                for (int t = 0; t < nparts; t++) {
201
                        ///buffer.putInt(polyLine.getPart(t));
202
                        buffer.putInt(parts[t]);
203
                }
204

    
205
                ///FPoint[] points = polyLine.getPoints();
206
                for (int t = 0; t < points.length; t++) {
207
                        ///buffer.putDouble(points[t].x);
208
                        ///buffer.putDouble(points[t].y);
209
                        buffer.putDouble(points[t].getX());
210
                        buffer.putDouble(points[t].getY());
211
                }
212

    
213
                   if (m_type == FConstant.SHAPE_TYPE_POLYGONZ) {
214
                   double[] zExtreame = SHP.getZMinMax(zs);
215
                   if (Double.isNaN(zExtreame[0])) {
216
                       buffer.putDouble(0.0);
217
                       buffer.putDouble(0.0);
218
                   } else {
219
                       buffer.putDouble(zExtreame[0]);
220
                       buffer.putDouble(zExtreame[1]);
221
                   }
222
                   for (int t = 0; t < npoints; t++) {
223
                       double z = zs[t];
224
                       if (Double.isNaN(z)) {
225
                           buffer.putDouble(0.0);
226
                       } else {
227
                           buffer.putDouble(z);
228
                       }
229
                   }
230
                   }
231

    
232
                if ((m_type == FConstant.SHAPE_TYPE_POLYGONM) ||
233
                                (m_type == FConstant.SHAPE_TYPE_POLYGONZ)) {
234
                        buffer.putDouble(-10E40);
235
                        buffer.putDouble(-10E40);
236

    
237
                        for (int t = 0; t < npoints; t++) {
238
                                buffer.putDouble(-10E40);
239
                        }
240
                }
241
        }
242

    
243
        /**
244
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
245
         */
246
        public synchronized int getLength(IGeometry fgeometry) {
247
                // FPolygon2D multi;
248
                //multi = (FPolygon2D) fgeometry.getShape();
249
                ///int nrings = 0;
250
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
251

    
252
                //int[] parts=multi.getParts();
253
                //FPoint2D[] points;
254
                /////////
255
                //points = multi.getPoints();
256
                int npoints = points.length;
257

    
258
                ///////////
259
                ///nrings = multi.getNumParts();
260
                ///int npoints = multi.getNumPoints();
261
                int length;
262

    
263
                if (m_type == FConstant.SHAPE_TYPE_POLYGONZ) {
264
                        length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) +
265
                                16 + (8 * npoints) + 16;
266
                } else if (m_type == FConstant.SHAPE_TYPE_POLYGONM) {
267
                        length = 44 + (4 * parts.length) + (16 * npoints) + (8 * npoints) +
268
                                16;
269
                } else if (m_type == FConstant.SHAPE_TYPE_POLYGON) {
270
                        length = 44 + (4 * parts.length) + (16 * npoints);
271
                } else {
272
                        throw new IllegalStateException(
273
                                "Expected ShapeType of Polygon, got " + m_type);
274
                }
275

    
276
                return length;
277
        }
278
}