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 / SHPMultiLine3DWriter.java @ 42876

History | View | Annotate | Download (7.34 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.MultiCurve;
38
import org.gvsig.fmap.geom.aggregate.MultiLine;
39
import org.gvsig.fmap.geom.primitive.Curve;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.gvsig.fmap.geom.primitive.Line;
42
import org.gvsig.fmap.geom.primitive.Point;
43

    
44

    
45
/**
46
 *
47
 * @author fdiaz
48
 */
49
public class SHPMultiLine3DWriter implements SHPShapeWriter {
50
    private Geometry geometry;
51
        protected int m_type;
52
        protected int[] parts;
53
        protected Point[] points;
54
        private GeometryManager geomManager = GeometryLocator.getGeometryManager();
55
        private static final Logger logger = LoggerFactory.getLogger(SHPMultiLine3DWriter.class);
56

    
57
        /**
58
         * Crea un nuevo SHPMultiLine.
59
         */
60
        public SHPMultiLine3DWriter() {
61
                m_type = SHP.POLYLINE3D;
62
        }
63

    
64
        /**
65
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getShapeType()
66
         */
67
        public int getShapeType() {
68
                return m_type;
69
        }
70

    
71
        /**
72
         * @throws WriteException
73
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#write(ByteBuffer, IGeometry)
74
         */
75
        public void write(ByteBuffer buffer) throws WriteException {
76
                Envelope env = geometry.getEnvelope();
77

    
78
                buffer.putDouble(env.getMinimum(0));
79
                buffer.putDouble(env.getMinimum(1));
80
                buffer.putDouble(env.getMaximum(0));
81
                buffer.putDouble(env.getMaximum(1));
82

    
83
                try {
84
            initialize(geometry);
85
        } catch (GeometryException e) {
86
            throw new WriteException("SHPMultiLine3D write", e);
87
        }
88

    
89
                double minM=Double.POSITIVE_INFINITY;
90
        double maxM=Double.NEGATIVE_INFINITY;
91
        double minZ=Double.POSITIVE_INFINITY;
92
        double maxZ=Double.NEGATIVE_INFINITY;
93
                int numParts = parts.length;
94
                int npoints = points.length;
95
                buffer.putInt(numParts);
96
                buffer.putInt(npoints);
97

    
98
                for (int i = 0; i < numParts; i++) {
99
                        buffer.putInt(parts[i]);
100
                }
101

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

    
139
        /**
140
         * @throws GeometryException
141
     *
142
     */
143
    public void initialize(Geometry g) throws GeometryException {
144

    
145
        ArrayList<Point> arrayPoints = new ArrayList<Point>();
146
        ArrayList<Integer> arrayParts = new ArrayList<Integer>();
147

    
148
        geometry = g;
149

    
150
        if(geometry instanceof Line){
151
            Line line = (Line)g;
152
            arrayParts.add(0);
153
            for (int i = 0; i < line.getNumVertices(); i++) {
154
                arrayPoints.add(line.getVertex(i));
155
            }
156
        } else {
157

    
158
            MultiLine multiLine = null;
159
            if (geometry instanceof MultiLine) {
160
                multiLine = (MultiLine) g;
161
            } else if (g instanceof MultiCurve) {
162
                multiLine = geomManager.createMultiLine(g.getGeometryType().getSubType());
163
                MultiCurve multiCurve = (MultiCurve) geometry;
164
                for (int i = 0; i < multiCurve.getPrimitivesNumber(); i++) {
165
                    Curve curve = (Curve) multiCurve.getPrimitiveAt(i);
166
                    if (curve instanceof Line) {
167
                        multiLine.addPrimitive(curve);
168
                    } else {
169
                        MultiLine lines = curve.toLines();
170
                        for (int j = 0; j < lines.getPrimitivesNumber(); j++) {
171
                            multiLine.addPrimitive(lines.getPrimitiveAt(j));
172
                        }
173
                    }
174
                }
175
            } else {
176
                multiLine = g.toLines();
177
            }
178

    
179
            int index = 0;
180
            arrayParts.add(index);
181
            for (int i = 0; i < multiLine.getPrimitivesNumber(); i++) {
182
                Line line = (Line) multiLine.getPrimitiveAt(i);
183
                for (int j = 0; j < line.getNumVertices(); j++) {
184
                    arrayPoints.add(line.getVertex(j));
185
                }
186
                if (i < multiLine.getPrimitivesNumber() - 1) {
187
                    index += line.getNumVertices();
188
                    arrayParts.add(index);
189
                }
190
            }
191
        }
192
        points = arrayPoints.toArray(new Point[0]);
193
        parts = new int[arrayParts.size()];
194
        for (int i = 0; i < parts.length; i++) {
195
            parts[i] = arrayParts.get(i);
196
        }
197
    }
198

    
199
    /**
200
         * @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
201
         */
202
        public int getLength() {
203
                int numlines;
204
                int numpoints;
205
                int length;
206

    
207
                numlines = parts.length;
208
                numpoints = points.length;
209
                // 44 = Shape Type + Box + NumParts + NumPoints
210
                // (4 * numlines) = Parts
211
                // (numpoints * 16) = Points
212
                // 16 = ZMin + ZMax
213
                // (numpoints * 8) = Zarray
214
        length = 44 + (4 * numlines) + (numpoints * 16) + 16 + (numpoints * 8);
215
        if (geometry.getGeometryType().getSubType() == Geometry.SUBTYPES.GEOM3DM) {
216
            // 16 = MMin + MMax
217
            // (numpoints * 8) = Marray
218
            length += 16 + (numpoints * 8);
219
        }
220

    
221
                return length;
222
        }
223
}