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 / SHPPolygon2DMWriter.java @ 44001

History | View | Annotate | Download (8.72 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 SHPPolygon2DMWriter 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(SHPPolygon2DMWriter.class);
60

    
61
        /**
62
         * Crea un nuevo SHPPolygon.
63
         */
64
        public SHPPolygon2DMWriter() {
65
                m_type = SHP.POLYGONM;
66
        }
67

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

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

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

    
87

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

    
94
        double Mmin=Double.POSITIVE_INFINITY;
95
        double Mmax=Double.NEGATIVE_INFINITY;
96
                int numParts = parts.length;
97
                int npoints = points.length;
98

    
99
                buffer.putInt(numParts);
100
                buffer.putInt(npoints);
101

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

    
106
        for (int t = 0; t < npoints; t++) {
107
            Point point = points[t];
108
            buffer.putDouble(point.getX());
109
            buffer.putDouble(point.getY());
110
            double m = point.getCoordinateAt(point.getDimension()-1);
111
            if(m<Mmin){
112
                Mmin = m;
113
            }
114
            if(m>Mmax){
115
                Mmax = m;
116
            }
117
        }
118
        buffer.putDouble(Mmin);
119
        buffer.putDouble(Mmax);
120
        for (int t = 0; t < npoints; t++) {
121
            Point point = points[t];
122
            buffer.putDouble(point.getCoordinateAt(point.getDimension()-1));
123
        }
124
        }
125

    
126
           /**
127
     * @throws GeometryException
128
         * @throws GeometryOperationException
129
         * @throws GeometryOperationNotSupportedException
130
     *
131
     */
132
    public void initialize(Geometry g) throws GeometryException, GeometryOperationNotSupportedException, GeometryOperationException {
133

    
134
        geometry = g;
135
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
136

    
137
        ArrayList<Point> arrayPoints = new ArrayList<Point>();
138
        ArrayList<Integer> arrayParts = new ArrayList<Integer>();
139

    
140
        if(geometry instanceof Polygon){
141
            Polygon polygon = (Polygon)geometry;
142
            polygon.ensureOrientation(false);
143
            int index = 0;
144
            arrayParts.add(index);
145
            for (int i = 0; i < polygon.getNumVertices(); i++) {
146
                arrayPoints.add(polygon.getVertex(i));
147
            }
148
            if(polygon.getNumInteriorRings()!=0){
149
                index += polygon.getNumVertices();
150
                arrayParts.add(index);
151
            }
152
            for (int r=0; r<polygon.getNumInteriorRings(); r++){
153
                Ring ring = polygon.getInteriorRing(r);
154
                ring.ensureOrientation(true);
155
                for (int i = 0; i < ring.getNumVertices(); i++) {
156
                    arrayPoints.add(ring.getVertex(i));
157
                }
158
                if(r<polygon.getNumInteriorRings()-1){
159
                    index += ring.getNumVertices();
160
                    arrayParts.add(index);
161
                }
162
            }
163
        } else {
164

    
165
            MultiPolygon multiPolygon = null;
166
            if (geometry instanceof MultiPolygon) {
167
                multiPolygon = (MultiPolygon) geometry;
168
            } else if (geometry instanceof MultiSurface) {
169
                multiPolygon = geomManager.createMultiPolygon(Geometry.SUBTYPES.GEOM2DM);
170
                MultiSurface multiSurface = (MultiSurface) geometry;
171
                for (int i = 0; i < multiSurface.getPrimitivesNumber(); i++) {
172
                    Surface surface = (Surface) multiSurface.getPrimitiveAt(i);
173
                    if (surface instanceof Polygon) {
174
                        Polygon polygon = (Polygon)surface;
175
                        polygon.ensureOrientation(false);
176
                        multiPolygon.addPrimitive(surface);
177
                    } else {
178
                        MultiPolygon polygons = surface.toPolygons();
179
                        for (int j = 0; j < polygons.getPrimitivesNumber(); j++) {
180
                            Primitive polygon = polygons.getPrimitiveAt(j);
181
                            polygon.ensureOrientation(false);
182
                            multiPolygon.addPrimitive(polygon);
183
                        }
184
                    }
185
                }
186
            } else {
187
                multiPolygon = geometry.toPolygons();
188
            }
189

    
190
            arrayParts.add(0);
191
            int index = 0;
192
            for (int i = 0; i < multiPolygon.getPrimitivesNumber(); i++) {
193
                Polygon polygon = (Polygon) multiPolygon.getPrimitiveAt(i);
194

    
195
                polygon.ensureOrientation(false);
196
                for (int j = 0; j < polygon.getNumVertices(); j++) {
197
                    arrayPoints.add(polygon.getVertex(j));
198
                }
199
                if(polygon.getNumInteriorRings()!=0 || i<multiPolygon.getPrimitivesNumber()-1){
200
                    index += polygon.getNumVertices();
201
                    arrayParts.add(index);
202
                }
203
                for (int r=0; r<polygon.getNumInteriorRings(); r++){
204
                    Ring ring = polygon.getInteriorRing(r);
205
                    ring.ensureOrientation(true);
206
                    for (int j = 0; j < ring.getNumVertices(); j++) {
207
                        arrayPoints.add(ring.getVertex(j));
208
                    }
209
                    if((i<multiPolygon.getPrimitivesNumber()-1) || (r<polygon.getNumInteriorRings()-1)){
210
                        index += ring.getNumVertices();
211
                        arrayParts.add(index);
212
                    }
213
                }
214
            }
215
        }
216
        points = arrayPoints.toArray(new Point[0]);
217
        parts = new int[arrayParts.size()];
218
        for (int i = 0; i < parts.length; i++) {
219
            parts[i] = arrayParts.get(i);
220
        }
221
    }
222

    
223

    
224
        /**
225
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(com.iver.cit.gvsig.core.BasicShape.FGeometry)
226
         */
227
        public synchronized int getLength() {
228
        int numlines;
229
        int numpoints;
230
        int length;
231

    
232
        numlines = parts.length;
233
        numpoints = points.length;
234
        // 44 = Shape Type + Box + NumParts + NumPoints
235
        // (4 * numlines) = Parts
236
        // (numpoints * 16) = Points
237
        // 16 = MMin + MMax
238
        // (numpoints * 8) = Marray
239
        length = 44 + (4 * numlines) + (numpoints * 16) + 16 + (numpoints * 8);
240

    
241
        return length;
242
    }
243
}