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 / SHPPolygon3DWriter.java @ 42773

History | View | Annotate | Download (9.78 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.shp.utils;
25

    
26
import java.nio.ByteBuffer;
27
import java.util.ArrayList;
28

    
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31

    
32
import org.gvsig.fmap.dal.exception.WriteException;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.GeometryException;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
38
import org.gvsig.fmap.geom.aggregate.MultiSurface;
39
import org.gvsig.fmap.geom.operation.GeometryOperationException;
40
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
41
import org.gvsig.fmap.geom.primitive.Envelope;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.primitive.Polygon;
44
import org.gvsig.fmap.geom.primitive.Primitive;
45
import org.gvsig.fmap.geom.primitive.Ring;
46
import org.gvsig.fmap.geom.primitive.Surface;
47
import org.gvsig.tools.exception.BaseException;
48

    
49
/**
50
 *
51
 *
52
 */
53
public class SHPPolygon3DWriter implements SHPShapeWriter {
54
    private Geometry geometry;
55
    private int m_type;
56
    private int[] parts;
57
    private Point[] points;
58

    
59
    private static final Logger logger = LoggerFactory.getLogger(SHPPolygon3DWriter.class);
60

    
61

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

    
69
        /**
70
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
71
         */
72
        public int getShapeType() {
73
                return m_type;
74
        }
75

    
76
        /**
77
         * @throws WriteException
78
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
79
         */
80
    public synchronized void write(ByteBuffer buffer) throws WriteException {
81
        Envelope env = geometry.getEnvelope();
82

    
83
        buffer.putDouble(env.getMinimum(0));
84
        buffer.putDouble(env.getMinimum(1));
85
        buffer.putDouble(env.getMaximum(0));
86
        buffer.putDouble(env.getMaximum(1));
87

    
88
        try {
89
            initialize(geometry);
90
        } catch (BaseException e) {
91
            throw new WriteException("SHPPolygon2DWriter", e);
92
        }
93

    
94
        double minM = Double.MAX_VALUE;
95
        double maxM = Double.MIN_NORMAL;
96
        double minZ = Double.MAX_VALUE;
97
        double maxZ = Double.MIN_NORMAL;
98
        int numParts = parts.length;
99
        int npoints = points.length;
100

    
101
        buffer.putInt(numParts);
102
        buffer.putInt(npoints);
103

    
104
        for (int i = 0; i < numParts; i++) {
105
            buffer.putInt(parts[i]);
106
        }
107

    
108
        for (int t = 0; t < npoints; t++) {
109
            Point point = points[t];
110
            buffer.putDouble(point.getX());
111
            buffer.putDouble(point.getY());
112
            double z = point.getCoordinateAt(Geometry.DIMENSIONS.Z);
113
            if (z < minZ) {
114
                minZ = z;
115
            }
116
            if (z > maxZ) {
117
                maxZ = z;
118
            }
119
            if (geometry.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3DM) {
120
                double m = point.getCoordinateAt(point.getDimension()-1);
121
                if (m < minM) {
122
                    minM = m;
123
                }
124
                if (m > maxM) {
125
                    maxM = m;
126
                }
127
            }
128
        }
129
        buffer.putDouble(minZ);
130
        buffer.putDouble(maxZ);
131
        for (int t = 0; t < npoints; t++) {
132
            Point point = points[t];
133
            buffer.putDouble(points[t].getCoordinateAt(Geometry.DIMENSIONS.Z));
134
        }
135

    
136
        if (geometry.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3DM) {
137
            buffer.putDouble(minM);
138
            buffer.putDouble(maxM);
139
            for (int t = 0; t < npoints; t++) {
140
                Point point = points[t];
141
                buffer.putDouble(points[t].getCoordinateAt(point.getDimension()-1));
142
            }
143
        }
144
    }
145

    
146
           /**
147
     * @throws GeometryException
148
         * @throws GeometryOperationException
149
         * @throws GeometryOperationNotSupportedException
150
     *
151
     */
152
    public void initialize(Geometry g) throws GeometryException, GeometryOperationNotSupportedException, GeometryOperationException {
153

    
154
        geometry = g;
155
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
156

    
157
        ArrayList<Point> arrayPoints = new ArrayList<Point>();
158
        ArrayList<Integer> arrayParts = new ArrayList<Integer>();
159

    
160
        if(geometry instanceof Polygon){
161
            Polygon polygon = (Polygon)geometry;
162
            polygon.ensureOrientation(false);
163
            int index = 0;
164
            arrayParts.add(index);
165
            for (int i = 0; i < polygon.getNumVertices(); i++) {
166
                arrayPoints.add(polygon.getVertex(i));
167
            }
168
            if(polygon.getNumInteriorRings()!=0){
169
                index += polygon.getNumVertices();
170
                arrayParts.add(index);
171
            }
172
            for (int r=0; r<polygon.getNumInteriorRings(); r++){
173
                Ring ring = polygon.getInteriorRing(r);
174
                ring.ensureOrientation(true);
175
                for (int i = 0; i < ring.getNumVertices(); i++) {
176
                    arrayPoints.add(ring.getVertex(i));
177
                }
178
                if(r<polygon.getNumInteriorRings()-1){
179
                    index += ring.getNumVertices();
180
                    arrayParts.add(index);
181
                }
182
            }
183
        } else {
184

    
185
            MultiPolygon multiPolygon = null;
186
            if (geometry instanceof MultiPolygon) {
187
                multiPolygon = (MultiPolygon) geometry;
188
            } else if (geometry instanceof MultiSurface) {
189
                multiPolygon = geomManager.createMultiPolygon(geometry.getGeometryType().getSubType());
190
                MultiSurface multiSurface = (MultiSurface) geometry;
191
                for (int i = 0; i < multiSurface.getPrimitivesNumber(); i++) {
192
                    Surface surface = (Surface) multiSurface.getPrimitiveAt(i);
193
                    if (surface instanceof Polygon) {
194
                        Polygon polygon = (Polygon)surface;
195
                        polygon.ensureOrientation(false);
196
                        multiPolygon.addPrimitive(surface);
197
                    } else {
198
                        MultiPolygon polygons = surface.toPolygons();
199
                        for (int j = 0; j < polygons.getPrimitivesNumber(); j++) {
200
                            Primitive polygon = polygons.getPrimitiveAt(j);
201
                            polygon.ensureOrientation(false);
202
                            multiPolygon.addPrimitive(polygon);
203
                        }
204
                    }
205
                }
206
            } else {
207
                multiPolygon = geometry.toPolygons();
208
            }
209

    
210
            arrayParts.add(0);
211
            int index = 0;
212
            for (int i = 0; i < multiPolygon.getPrimitivesNumber(); i++) {
213
                Polygon polygon = (Polygon) multiPolygon.getPrimitiveAt(i);
214

    
215
                polygon.ensureOrientation(false);
216
                for (int j = 0; j < polygon.getNumVertices(); j++) {
217
                    arrayPoints.add(polygon.getVertex(j));
218
                }
219
                if(polygon.getNumInteriorRings()!=0 || i<multiPolygon.getPrimitivesNumber()-1){
220
                    index += polygon.getNumVertices();
221
                    arrayParts.add(index);
222
                }
223
                for (int r=0; r<polygon.getNumInteriorRings(); r++){
224
                    Ring ring = polygon.getInteriorRing(r);
225
                    ring.ensureOrientation(true);
226
                    for (int j = 0; j < ring.getNumVertices(); j++) {
227
                        arrayPoints.add(ring.getVertex(j));
228
                    }
229
                    if((i<multiPolygon.getPrimitivesNumber()-1) || (r<polygon.getNumInteriorRings()-1)){
230
                        index += ring.getNumVertices();
231
                        arrayParts.add(index);
232
                    }
233
                }
234
            }
235
        }
236
        points = arrayPoints.toArray(new Point[0]);
237
        parts = new int[arrayParts.size()];
238
        for (int i = 0; i < parts.length; i++) {
239
            parts[i] = arrayParts.get(i);
240
        }
241
    }
242

    
243

    
244
        /**
245
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
246
         */
247
        public synchronized int getLength() {
248
        int numlines;
249
        int numpoints;
250
        int length;
251

    
252
        numlines = parts.length;
253
        numpoints = points.length;
254
        // 44 = Shape Type + Box + NumParts + NumPoints
255
        // (4 * numlines) = Parts
256
        // (numpoints * 16) = Points
257
        // 16 = ZMin + ZMax
258
        // (numpoints * 8) = Zarray
259
        length = 44 + (4 * numlines) + (numpoints * 16) + 16 + (numpoints * 8);
260
        if (geometry.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3DM) {
261
            // 16 = MMin + MMax
262
            // (numpoints * 8) = Marray
263
            length += 16 + (numpoints * 8);
264
        }
265

    
266
        return length;
267
    }
268
}