Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / write / SHPPolygon.java @ 2196

History | View | Annotate | Download (6.93 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
        private int m_type;
62

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

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

    
84
                m_type = type;
85
        }
86

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

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

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

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

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

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

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

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

    
137
                        length = finish - start;
138

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

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

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

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

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

    
165
                return points;
166
        }
167

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

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

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

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

    
194
                int count = 0;
195

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

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

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

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

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

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

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

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

    
272
                return length;
273
        }
274
}