Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1010 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / write / SHPPolygon.java @ 12804

History | View | Annotate | Download (6.95 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 com.iver.cit.gvsig.fmap.drivers.shp.write;
42

    
43
import com.iver.cit.gvsig.fmap.core.FPoint2D;
44
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
45
import com.iver.cit.gvsig.fmap.core.IGeometry;
46
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
47
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
48

    
49
import java.awt.geom.Rectangle2D;
50

    
51
import java.nio.ByteBuffer;
52
import java.nio.MappedByteBuffer;
53

    
54

    
55
/**
56
 * Elemento shape de tipo Pol?gono.
57
 *
58
 * @author Vicente Caballero Navarro
59
 */
60
public class SHPPolygon extends SHPMultiLine {
61

    
62
        /**
63
         * Crea un nuevo SHPPolygon.
64
         */
65
        public SHPPolygon() {
66
                m_type = FConstant.SHAPE_TYPE_POLYGON;
67
        }
68

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

    
83
                m_type = type;
84
        }
85

    
86
        /**
87
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
88
         */
89
        public int getShapeType() {
90
                return m_type;
91
        }
92

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

    
106
                int[] partOffsets = new int[numParts];
107

    
108
                for (int i = 0; i < numParts; i++) {
109
                        partOffsets[i] = buffer.getInt();
110
                }
111

    
112
                FPoint2D[] points = readPoints(buffer, numPoints);
113

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

    
127
                for (int part = 0; part < numParts; part++) {
128
                        start = partOffsets[part];
129

    
130
                        if (part == (numParts - 1)) {
131
                                finish = numPoints;
132
                        } else {
133
                                finish = partOffsets[part + 1];
134
                        }
135

    
136
                        length = finish - start;
137

    
138
                        FPoint2D[] pointsPart = new FPoint2D[length];
139

    
140
                        for (int i = 0; i < length; i++) {
141
                                pointsPart[i] = points[offset++];
142
                        }
143
                }
144

    
145
                return (IGeometry) new FPolygon2D(getGeneralPathX(points, partOffsets));
146
        }
147

    
148
        /**
149
         * Lee los puntos del buffer.
150
         *
151
         * @param buffer
152
         * @param numPoints N?mero de puntos.
153
         *
154
         * @return Vector de Puntos.
155
         */
156
        private synchronized FPoint2D[] readPoints(final MappedByteBuffer buffer,
157
                final int numPoints) {
158
                FPoint2D[] points = new FPoint2D[numPoints];
159

    
160
                for (int t = 0; t < numPoints; t++) {
161
                        points[t] = new FPoint2D(buffer.getDouble(), buffer.getDouble());
162
                }
163

    
164
                return points;
165
        }
166

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

    
179
                //////
180
                ///obtainsPoints(geometry.getGeneralPathXIterator());
181

    
182
                //int[] parts=polyLine.getParts();
183
                //FPoint2D[] points=polyLine.getPoints();
184
                int nparts = parts.length;
185
                int npoints = points.length;
186

    
187
                //////
188
                ///int npoints = polyLine.getNumPoints();
189
                ///int nparts = polyLine.getNumParts();
190
                buffer.putInt(nparts);
191
                buffer.putInt(npoints);
192

    
193
                int count = 0;
194

    
195
                for (int t = 0; t < nparts; t++) {
196
                        ///buffer.putInt(polyLine.getPart(t));
197
                        buffer.putInt(parts[t]);
198
                }
199

    
200
                ///FPoint[] points = polyLine.getPoints();
201
                for (int t = 0; t < points.length; t++) {
202
                        ///buffer.putDouble(points[t].x);
203
                        ///buffer.putDouble(points[t].y);
204
                        buffer.putDouble(points[t].getX());
205
                        buffer.putDouble(points[t].getY());
206
                }
207

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

    
227
                if ((m_type == FConstant.SHAPE_TYPE_POLYGONM) ||
228
                                (m_type == FConstant.SHAPE_TYPE_POLYGONZ)) {
229
                        buffer.putDouble(-10E40);
230
                        buffer.putDouble(-10E40);
231

    
232
                        for (int t = 0; t < npoints; t++) {
233
                                buffer.putDouble(-10E40);
234
                        }
235
                }
236
        }
237

    
238
        /**
239
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
240
         */
241
        public synchronized int getLength(IGeometry fgeometry) {
242
                // FPolygon2D multi;
243
                //multi = (FPolygon2D) fgeometry.getShape();
244
                ///int nrings = 0;
245
                ///obtainsPoints(fgeometry.getGeneralPathXIterator());
246

    
247
                //int[] parts=multi.getParts();
248
                //FPoint2D[] points;
249
                /////////
250
                //points = multi.getPoints();
251
                int npoints = points.length;
252

    
253
                ///////////
254
                ///nrings = multi.getNumParts();
255
                ///int npoints = multi.getNumPoints();
256
                int length;
257

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

    
271
                return length;
272
        }
273
}