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 / SHPMultiLine2DWriter.java @ 42323
History | View | Annotate | Download (5.58 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 SHPMultiLine2DWriter implements SHPShapeWriter { |
50 |
private Geometry geometry;
|
51 |
private int m_type; |
52 |
private int[] parts; |
53 |
private Point[] points; |
54 |
private GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
55 |
private static final Logger logger = LoggerFactory.getLogger(SHPMultiLine2DWriter.class); |
56 |
|
57 |
/**
|
58 |
* Crea un nuevo SHPMultiLine.
|
59 |
*/
|
60 |
public SHPMultiLine2DWriter() {
|
61 |
m_type = SHP.POLYLINE2D; |
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("SHPMultiLine2D write", e); |
87 |
} |
88 |
|
89 |
int numParts = parts.length;
|
90 |
int npoints = points.length;
|
91 |
buffer.putInt(numParts); |
92 |
buffer.putInt(npoints); |
93 |
|
94 |
for (int i = 0; i < numParts; i++) { |
95 |
buffer.putInt(parts[i]); |
96 |
} |
97 |
|
98 |
for (int t = 0; t < npoints; t++) { |
99 |
buffer.putDouble(points[t].getX()); |
100 |
buffer.putDouble(points[t].getY()); |
101 |
} |
102 |
} |
103 |
|
104 |
/**
|
105 |
* @throws GeometryException
|
106 |
*
|
107 |
*/
|
108 |
public void initialize(Geometry g) throws GeometryException { |
109 |
|
110 |
geometry = g; |
111 |
|
112 |
ArrayList<Point> arrayPoints = new ArrayList<Point>(); |
113 |
ArrayList<Integer> arrayParts = new ArrayList<Integer>(); |
114 |
|
115 |
if(g instanceof Line){ |
116 |
Line line = (Line)g; |
117 |
arrayParts.add(0);
|
118 |
for (int i = 0; i < line.getNumVertices(); i++) { |
119 |
arrayPoints.add(line.getVertex(i)); |
120 |
} |
121 |
} else {
|
122 |
|
123 |
MultiLine multiLine = null;
|
124 |
if (g instanceof MultiLine) { |
125 |
multiLine = (MultiLine) g; |
126 |
} else if (g instanceof MultiCurve) { |
127 |
multiLine = geomManager.createMultiLine(Geometry.SUBTYPES.GEOM2D); |
128 |
MultiCurve multiCurve = (MultiCurve) g; |
129 |
for (int i = 0; i < multiCurve.getPrimitivesNumber(); i++) { |
130 |
Curve curve = (Curve) multiCurve.getPrimitiveAt(i); |
131 |
if (curve instanceof Line) { |
132 |
multiLine.addPrimitive(curve); |
133 |
} else {
|
134 |
MultiLine lines = curve.toLines(); |
135 |
for (int j = 0; j < lines.getPrimitivesNumber(); j++) { |
136 |
multiLine.addPrimitive(lines.getPrimitiveAt(j)); |
137 |
} |
138 |
} |
139 |
} |
140 |
} else {
|
141 |
multiLine = g.toLines(); |
142 |
} |
143 |
|
144 |
int index = 0; |
145 |
arrayParts.add(index); |
146 |
for (int i = 0; i < multiLine.getPrimitivesNumber(); i++) { |
147 |
Line line = (Line) multiLine.getPrimitiveAt(i); |
148 |
for (int j = 0; j < line.getNumVertices(); j++) { |
149 |
arrayPoints.add(line.getVertex(j)); |
150 |
} |
151 |
if (i < multiLine.getPrimitivesNumber() - 1) { |
152 |
index += line.getNumVertices(); |
153 |
arrayParts.add(index); |
154 |
} |
155 |
} |
156 |
} |
157 |
points = arrayPoints.toArray(new Point[0]); |
158 |
parts = new int[arrayParts.size()]; |
159 |
for (int i = 0; i < parts.length; i++) { |
160 |
parts[i] = arrayParts.get(i); |
161 |
} |
162 |
} |
163 |
|
164 |
/**
|
165 |
* @see com.iver.cit.gvsig.fmap.shp.SHPShape#getLength(int)
|
166 |
*/
|
167 |
public int getLength() { |
168 |
int numlines;
|
169 |
int numpoints;
|
170 |
int length;
|
171 |
|
172 |
numlines = parts.length; |
173 |
numpoints = points.length; |
174 |
length = 44 + (4 * numlines) + (numpoints * 16); |
175 |
|
176 |
return length;
|
177 |
} |
178 |
} |