Revision 673
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="ISO-8859-1"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
3 |
<modelVersion>4.0.0</modelVersion> |
|
4 |
<artifactId>org.gvsig.gpe.exportto</artifactId> |
|
5 |
<packaging>pom</packaging> |
|
6 |
|
|
7 |
|
|
8 |
<parent> |
|
9 |
<groupId>org.gvsig</groupId> |
|
10 |
<artifactId>org.gvsig.gpe</artifactId> |
|
11 |
<version>2.1.45</version> |
|
12 |
</parent> |
|
13 |
|
|
14 |
|
|
15 |
<modules> |
|
16 |
<module>org.gvsig.gpe.exportto.kml</module> |
|
17 |
<module>org.gvsig.gpe.exportto.generic</module> |
|
18 |
</modules> |
|
19 |
</project> |
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/java/org/gvsig/gpe/exportto/generic/ExporttoGPEGenericLibrary.java | ||
---|---|---|
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.gpe.exportto.generic; |
|
25 |
|
|
26 |
import org.gvsig.gpe.lib.api.GPELibrary; |
|
27 |
import org.gvsig.tools.library.AbstractLibrary; |
|
28 |
import org.gvsig.tools.library.LibraryException; |
|
29 |
|
|
30 |
/** |
|
31 |
* Library with common things to be used by |
|
32 |
* GPE export libraries |
|
33 |
* |
|
34 |
* @author gvSIG Team |
|
35 |
* @version $Id$ |
|
36 |
*/ |
|
37 |
public class ExporttoGPEGenericLibrary extends AbstractLibrary { |
|
38 |
|
|
39 |
public void doRegistration() { |
|
40 |
require(GPELibrary.class); |
|
41 |
} |
|
42 |
|
|
43 |
protected void doInitialize() throws LibraryException { |
|
44 |
// Nothing to do |
|
45 |
} |
|
46 |
|
|
47 |
protected void doPostInitialize() throws LibraryException { |
|
48 |
|
|
49 |
} |
|
50 |
|
|
51 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/java/org/gvsig/gpe/exportto/generic/util/CoordinatesSequenceBbox.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.generic.util; |
|
2 |
|
|
3 |
import java.awt.geom.Rectangle2D; |
|
4 |
import java.io.IOException; |
|
5 |
|
|
6 |
import org.gvsig.gpe.lib.api.parser.ICoordinateIterator; |
|
7 |
import org.gvsig.gpe.lib.api.writer.ICoordinateSequence; |
|
8 |
|
|
9 |
public class CoordinatesSequenceBbox implements |
|
10 |
ICoordinateSequence, ICoordinateIterator { |
|
11 |
|
|
12 |
Rectangle2D bbox = null; |
|
13 |
int index = 0; |
|
14 |
|
|
15 |
public CoordinatesSequenceBbox(Rectangle2D bbox){ |
|
16 |
this.bbox = bbox; |
|
17 |
} |
|
18 |
|
|
19 |
public int getSize() { |
|
20 |
return 2; |
|
21 |
} |
|
22 |
|
|
23 |
public ICoordinateIterator iterator() { |
|
24 |
return this; |
|
25 |
} |
|
26 |
|
|
27 |
public int getDimension() { |
|
28 |
return 2; |
|
29 |
} |
|
30 |
|
|
31 |
public boolean hasNext() throws IOException { |
|
32 |
if (index <=1){ |
|
33 |
return true; |
|
34 |
} |
|
35 |
return false; |
|
36 |
} |
|
37 |
|
|
38 |
public void next(double[] buffer) throws IOException { |
|
39 |
if (index == 0){ |
|
40 |
buffer[0] = bbox.getMinX(); |
|
41 |
buffer[1] = bbox.getMinY(); |
|
42 |
}else if (index == 1){ |
|
43 |
buffer[0] = bbox.getMaxX(); |
|
44 |
buffer[1] = bbox.getMaxY(); |
|
45 |
} |
|
46 |
index++; |
|
47 |
} |
|
48 |
|
|
49 |
} |
|
50 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/java/org/gvsig/gpe/exportto/generic/util/CoordinatesSequenceGeneralPath.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.generic.util; |
|
2 |
|
|
3 |
import java.awt.geom.PathIterator; |
|
4 |
import java.io.IOException; |
|
5 |
|
|
6 |
import org.gvsig.gpe.lib.api.parser.ICoordinateIterator; |
|
7 |
import org.gvsig.gpe.lib.api.writer.ICoordinateSequence; |
|
8 |
|
|
9 |
public class CoordinatesSequenceGeneralPath implements |
|
10 |
ICoordinateSequence, ICoordinateIterator { |
|
11 |
|
|
12 |
private PathIterator it = null; |
|
13 |
int size = 0; |
|
14 |
boolean hasMoreGeoemtries = true; |
|
15 |
|
|
16 |
public CoordinatesSequenceGeneralPath(PathIterator it) { |
|
17 |
super(); |
|
18 |
this.it = it; |
|
19 |
} |
|
20 |
|
|
21 |
public int getSize() { |
|
22 |
return size; |
|
23 |
} |
|
24 |
|
|
25 |
public void initialize(){ |
|
26 |
size = 0; |
|
27 |
} |
|
28 |
|
|
29 |
public boolean hasMoreGeometries(){ |
|
30 |
return hasMoreGeoemtries; |
|
31 |
} |
|
32 |
|
|
33 |
public ICoordinateIterator iterator() { |
|
34 |
return this; |
|
35 |
} |
|
36 |
|
|
37 |
public int getDimension() { |
|
38 |
return 2; |
|
39 |
} |
|
40 |
|
|
41 |
public boolean hasNext() throws IOException { |
|
42 |
if (it.isDone()){ |
|
43 |
hasMoreGeoemtries = false; |
|
44 |
return false; |
|
45 |
} |
|
46 |
double[] coords = new double[2]; |
|
47 |
int type = it.currentSegment(coords); |
|
48 |
/* |
|
49 |
if (type == PathIterator.SEG_CLOSE){ |
|
50 |
hasMoreGeoemtries = false; |
|
51 |
} |
|
52 |
*/ |
|
53 |
return ((type != PathIterator.SEG_MOVETO) || (size == 0)); |
|
54 |
} |
|
55 |
|
|
56 |
public void next(double[] buffer) throws IOException { |
|
57 |
it.currentSegment(buffer); |
|
58 |
it.next(); |
|
59 |
size++; |
|
60 |
} |
|
61 |
|
|
62 |
} |
|
63 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/java/org/gvsig/gpe/exportto/generic/util/GeometryToGPEWriter.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.generic.util; |
|
2 |
|
|
3 |
import java.awt.geom.PathIterator; |
|
4 |
|
|
5 |
import org.slf4j.Logger; |
|
6 |
import org.slf4j.LoggerFactory; |
|
7 |
|
|
8 |
import org.gvsig.fmap.geom.Geometry; |
|
9 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
10 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
|
11 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
|
12 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
|
13 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
|
14 |
import org.gvsig.fmap.geom.primitive.Curve; |
|
15 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
16 |
import org.gvsig.fmap.geom.primitive.Point; |
|
17 |
import org.gvsig.fmap.geom.primitive.Surface; |
|
18 |
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandler; |
|
19 |
import org.gvsig.tools.locator.LocatorException; |
|
20 |
|
|
21 |
|
|
22 |
/** |
|
23 |
* |
|
24 |
* Utility class to write gvSIG geometries in a GPE writer. |
|
25 |
* |
|
26 |
* @author jldominguez (I have removed the reprojection methods/fields) |
|
27 |
*/ |
|
28 |
public class GeometryToGPEWriter { |
|
29 |
|
|
30 |
private static Logger logger = LoggerFactory.getLogger(GeometryToGPEWriter.class); |
|
31 |
|
|
32 |
private IGPEWriterHandler writer = null; |
|
33 |
//To know if the geometry is multiple |
|
34 |
// private boolean isMultiple = false; |
|
35 |
//To reproject geometries |
|
36 |
/* |
|
37 |
private IProjection projOrig = null; |
|
38 |
private IProjection projDest = null; |
|
39 |
private ICoordTrans coordTrans = null; |
|
40 |
*/ |
|
41 |
private String srs = null; |
|
42 |
|
|
43 |
public GeometryToGPEWriter(IGPEWriterHandler writer) { |
|
44 |
this.writer = writer; |
|
45 |
} |
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
public String getCrs() { |
|
50 |
return srs; |
|
51 |
} |
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
public void setCrs(String thesrs) { |
|
56 |
this.srs = thesrs; |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
/** |
|
61 |
* It writes a geometry |
|
62 |
* @param geom |
|
63 |
* The geometry to write |
|
64 |
* @param crs |
|
65 |
* The coordinates reference system |
|
66 |
*/ |
|
67 |
public void writeGeometry(Geometry geom, boolean addLabelPoint) { |
|
68 |
|
|
69 |
if (geom == null) { |
|
70 |
logger.error("GPE Writer cannot write geometry.", |
|
71 |
new Exception("Geometry is NULL")); |
|
72 |
return; |
|
73 |
} |
|
74 |
// ==================================================== |
|
75 |
if (geom instanceof MultiPoint) { |
|
76 |
writeMultiPoint((MultiPoint) geom); |
|
77 |
return; |
|
78 |
} |
|
79 |
if (geom instanceof MultiCurve) { |
|
80 |
writeMultiLine((MultiCurve) geom, addLabelPoint); |
|
81 |
return; |
|
82 |
} |
|
83 |
if (geom instanceof MultiSurface) { |
|
84 |
writeMultiPolygon((MultiSurface) geom, addLabelPoint); |
|
85 |
return; |
|
86 |
} |
|
87 |
// ==================================================== |
|
88 |
if (geom instanceof Point) { |
|
89 |
writePoint((Point) geom); |
|
90 |
return; |
|
91 |
} |
|
92 |
if (geom instanceof Curve) { |
|
93 |
writeLine((Curve) geom, addLabelPoint); |
|
94 |
return; |
|
95 |
} |
|
96 |
if (geom instanceof Surface) { |
|
97 |
writePolygon((Surface) geom, addLabelPoint); |
|
98 |
return; |
|
99 |
} |
|
100 |
// ==================================================== |
|
101 |
logger.error("GPE Writer cannot write geometry", |
|
102 |
new Exception("Unexpected geometry: " + geom.getClass().getName())); |
|
103 |
} |
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
/** |
|
109 |
* Writes a point in 2D |
|
110 |
* @param point |
|
111 |
* The point to write |
|
112 |
* @param crs |
|
113 |
* The coordinates reference system |
|
114 |
*/ |
|
115 |
private void writePoint(Point point) { |
|
116 |
writer.startPoint(null, new CoordinatesSequencePoint(point), srs); |
|
117 |
writer.endPoint(); |
|
118 |
} |
|
119 |
|
|
120 |
/** |
|
121 |
* Writes a multipoint in 2D |
|
122 |
* @param point |
|
123 |
* The point to write |
|
124 |
* @param crs |
|
125 |
* The coordinates reference system |
|
126 |
*/ |
|
127 |
private void writeMultiPoint(MultiPoint multi){ |
|
128 |
writer.startMultiPoint(null, srs); |
|
129 |
for (int i=0 ; i<multi.getPrimitivesNumber() ; i++) { |
|
130 |
Point p = multi.getPointAt(i); |
|
131 |
writePoint(p); |
|
132 |
} |
|
133 |
writer.endMultiPoint(); |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* Writes a line in 2D |
|
138 |
* @param line |
|
139 |
* The line to write |
|
140 |
* @param crs |
|
141 |
* The coordinates reference system |
|
142 |
* @param geometries |
|
143 |
* The parsed geometries |
|
144 |
*/ |
|
145 |
private void writeLine(Curve curve, boolean addLabelPoint) { |
|
146 |
|
|
147 |
boolean ismulti = isMultiple(curve.getPathIterator(null)); |
|
148 |
/* |
|
149 |
* Start =================================================== |
|
150 |
*/ |
|
151 |
if (addLabelPoint) { |
|
152 |
Point po = null; |
|
153 |
try { |
|
154 |
po = getLabelPoint(curve.getPathIterator(null), curve.getEnvelope()); |
|
155 |
} catch (Exception ex) { |
|
156 |
logger.info("While getting label point.", ex); |
|
157 |
return; |
|
158 |
} |
|
159 |
writer.startMultiGeometry(null, srs); |
|
160 |
CoordinatesSequencePoint pseq = new CoordinatesSequencePoint(po); |
|
161 |
writer.startPoint(null, pseq, srs); |
|
162 |
writer.endPoint(); |
|
163 |
} else { |
|
164 |
if (ismulti) { |
|
165 |
writer.startMultiLineString(null, srs); |
|
166 |
} |
|
167 |
} |
|
168 |
/* |
|
169 |
* Body =================================================== |
|
170 |
*/ |
|
171 |
CoordinatesSequenceGeneralPath sequence = |
|
172 |
new CoordinatesSequenceGeneralPath(curve.getPathIterator(null)); |
|
173 |
writer.startLineString(null, sequence, srs); |
|
174 |
writer.endLineString(); |
|
175 |
if (ismulti) { |
|
176 |
while (sequence.hasMoreGeometries()){ |
|
177 |
sequence.initialize(); |
|
178 |
writer.startLineString(null, sequence, srs); |
|
179 |
writer.endLineString(); |
|
180 |
} |
|
181 |
} |
|
182 |
/* |
|
183 |
* End =================================================== |
|
184 |
*/ |
|
185 |
if (addLabelPoint) { |
|
186 |
writer.endMultiGeometry(); |
|
187 |
} else { |
|
188 |
if (ismulti) { |
|
189 |
writer.endMultiLineString(); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
|
|
195 |
private void writeMultiLine(MultiCurve mcurve, boolean addLabelPoint) { |
|
196 |
|
|
197 |
if (addLabelPoint) { |
|
198 |
Point po = null; |
|
199 |
try { |
|
200 |
po = getLabelPoint(mcurve.getPathIterator(null), mcurve.getEnvelope()); |
|
201 |
} catch (Exception exc) { |
|
202 |
logger.info("While getting label point.", exc); |
|
203 |
return; |
|
204 |
} |
|
205 |
writer.startMultiGeometry(null, srs); |
|
206 |
CoordinatesSequencePoint pseq = new CoordinatesSequencePoint(po); |
|
207 |
writer.startPoint(null, pseq, srs); |
|
208 |
writer.endPoint(); |
|
209 |
} else { |
|
210 |
writer.startMultiLineString(null, srs); |
|
211 |
} |
|
212 |
/* |
|
213 |
* Body ======================================= |
|
214 |
*/ |
|
215 |
CoordinatesSequenceGeneralPath sequence = |
|
216 |
new CoordinatesSequenceGeneralPath(mcurve.getPathIterator(null)); |
|
217 |
|
|
218 |
writer.startLineString(null, sequence, srs); |
|
219 |
writer.endLineString(); |
|
220 |
while (sequence.hasMoreGeometries()){ |
|
221 |
sequence.initialize(); |
|
222 |
writer.startLineString(null, sequence, srs); |
|
223 |
writer.endLineString(); |
|
224 |
} |
|
225 |
/* |
|
226 |
* End ====================================== |
|
227 |
*/ |
|
228 |
if (addLabelPoint) { |
|
229 |
writer.endMultiGeometry(); |
|
230 |
} else { |
|
231 |
writer.endMultiLineString(); |
|
232 |
} |
|
233 |
|
|
234 |
} |
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
/** |
|
240 |
* Writes a polygon in 2D |
|
241 |
* @param polygon |
|
242 |
* The polygon to write |
|
243 |
* @param crs |
|
244 |
* The coordinates reference system |
|
245 |
* @param geometries |
|
246 |
* The parsed geometries |
|
247 |
*/ |
|
248 |
private void writePolygon(Surface surface, boolean addLabelPoint) { |
|
249 |
|
|
250 |
boolean ismulti = isMultiple(surface.getPathIterator(null)); |
|
251 |
CoordinatesSequenceGeneralPath sequence = null; |
|
252 |
/* |
|
253 |
* Start =================================================== |
|
254 |
*/ |
|
255 |
if (addLabelPoint) { |
|
256 |
Point po = null; |
|
257 |
try { |
|
258 |
po = surface.getInteriorPoint(); |
|
259 |
} catch (Exception ex) { |
|
260 |
logger.info("While getting label point.", ex); |
|
261 |
return; |
|
262 |
} |
|
263 |
writer.startMultiGeometry(null, srs); |
|
264 |
CoordinatesSequencePoint pseq = new CoordinatesSequencePoint(po); |
|
265 |
writer.startPoint(null, pseq, srs); |
|
266 |
writer.endPoint(); |
|
267 |
} else { |
|
268 |
if (ismulti) { |
|
269 |
writer.startMultiPolygon(null, srs); |
|
270 |
} |
|
271 |
} |
|
272 |
/* |
|
273 |
* Body =================================================== |
|
274 |
*/ |
|
275 |
sequence = new CoordinatesSequenceGeneralPath(surface.getPathIterator(null)); |
|
276 |
writer.startPolygon(null, sequence, srs); |
|
277 |
writer.endPolygon(); |
|
278 |
if (ismulti) { |
|
279 |
while (sequence.hasMoreGeometries()){ |
|
280 |
sequence.initialize(); |
|
281 |
writer.startPolygon(null, sequence, srs); |
|
282 |
writer.endPolygon(); |
|
283 |
} |
|
284 |
} |
|
285 |
/* |
|
286 |
* End =================================================== |
|
287 |
*/ |
|
288 |
if (addLabelPoint) { |
|
289 |
writer.endMultiGeometry(); |
|
290 |
} else { |
|
291 |
if (ismulti) { |
|
292 |
writer.endMultiPolygon(); |
|
293 |
} |
|
294 |
} |
|
295 |
} |
|
296 |
|
|
297 |
|
|
298 |
private Point getLabelPoint(PathIterator pathIterator, Envelope envelope) throws |
|
299 |
Exception { |
|
300 |
|
|
301 |
if (pathIterator == null || envelope == null) { |
|
302 |
throw new Exception("Null iterator or envelope"); |
|
303 |
} |
|
304 |
|
|
305 |
double bestx = 0; |
|
306 |
double besty = 0; |
|
307 |
double cx = envelope.getCenter(0); |
|
308 |
double cy = envelope.getCenter(1); |
|
309 |
double dist = Double.MAX_VALUE; |
|
310 |
double auxdist = 0; |
|
311 |
|
|
312 |
double[] coords = new double[6]; |
|
313 |
while (!pathIterator.isDone()) { |
|
314 |
pathIterator.currentSegment(coords); |
|
315 |
auxdist = getDist2(cx, cy, coords[0], coords[1]); |
|
316 |
if (auxdist < dist) { |
|
317 |
dist = auxdist; |
|
318 |
bestx = coords[0]; |
|
319 |
besty = coords[1]; |
|
320 |
} |
|
321 |
pathIterator.next(); |
|
322 |
} |
|
323 |
Point resp = GeometryLocator.getGeometryManager().createPoint( |
|
324 |
bestx, besty, Geometry.SUBTYPES.GEOM2D); |
|
325 |
return resp; |
|
326 |
} |
|
327 |
|
|
328 |
|
|
329 |
private Point getLabelPoint(MultiSurface msurf) throws Exception { |
|
330 |
|
|
331 |
if (msurf == null) { |
|
332 |
throw new Exception("Surface is Null"); |
|
333 |
} |
|
334 |
|
|
335 |
int n = msurf.getPrimitivesNumber(); |
|
336 |
Surface surf = null; |
|
337 |
Surface bigsurf = null; |
|
338 |
double importance = 0; |
|
339 |
double auximportance = 0; |
|
340 |
Envelope env = null; |
|
341 |
long nverts = 0; |
|
342 |
for (int i=0; i<n; i++) { |
|
343 |
surf = (Surface) msurf.getPrimitiveAt(i); |
|
344 |
nverts = surf.getNumVertices(); |
|
345 |
env = surf.getEnvelope(); |
|
346 |
auximportance = nverts * env.getLength(0) * env.getLength(1); |
|
347 |
if (auximportance >= importance) { |
|
348 |
importance = auximportance; |
|
349 |
bigsurf = surf; |
|
350 |
} |
|
351 |
} |
|
352 |
if (bigsurf != null) { |
|
353 |
return bigsurf.getInteriorPoint(); |
|
354 |
} else { |
|
355 |
return msurf.getInteriorPoint(); |
|
356 |
} |
|
357 |
} |
|
358 |
|
|
359 |
/** |
|
360 |
* Returns (dist ^ 2) between points a and b |
|
361 |
* @param ax |
|
362 |
* @param ay |
|
363 |
* @param bx |
|
364 |
* @param by |
|
365 |
* @return |
|
366 |
*/ |
|
367 |
private double getDist2( |
|
368 |
double ax, double ay, |
|
369 |
double bx, double by) { |
|
370 |
|
|
371 |
double dx = bx - ax; |
|
372 |
double dy = by - ay; |
|
373 |
return (dx * dx) + (dy * dy); |
|
374 |
} |
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
private void writeMultiPolygon(MultiSurface msurface, boolean addLabelPoint) { |
|
379 |
|
|
380 |
if (addLabelPoint) { |
|
381 |
Point po = null; |
|
382 |
try { |
|
383 |
po = getLabelPoint(msurface); |
|
384 |
} catch (Exception ex) { |
|
385 |
logger.info("While getting label point.", ex); |
|
386 |
return; |
|
387 |
} |
|
388 |
|
|
389 |
writer.startMultiGeometry(null, srs); |
|
390 |
CoordinatesSequencePoint pseq = new CoordinatesSequencePoint(po); |
|
391 |
writer.startPoint(null, pseq, srs); |
|
392 |
writer.endPoint(); |
|
393 |
} else { |
|
394 |
writer.startMultiPolygon(null, srs); |
|
395 |
} |
|
396 |
/* |
|
397 |
* Body ============================================== |
|
398 |
*/ |
|
399 |
CoordinatesSequenceGeneralPath sequence = |
|
400 |
new CoordinatesSequenceGeneralPath(msurface.getPathIterator(null)); |
|
401 |
writer.startPolygon(null, sequence, srs); |
|
402 |
writer.endPolygon(); |
|
403 |
while (sequence.hasMoreGeometries()){ |
|
404 |
sequence.initialize(); |
|
405 |
writer.startPolygon(null, sequence, srs); |
|
406 |
writer.endPolygon(); |
|
407 |
} |
|
408 |
/* |
|
409 |
* End ============================================== |
|
410 |
*/ |
|
411 |
if (addLabelPoint) { |
|
412 |
writer.endMultiGeometry(); |
|
413 |
} else { |
|
414 |
writer.endMultiPolygon(); |
|
415 |
} |
|
416 |
|
|
417 |
} |
|
418 |
|
|
419 |
/** |
|
420 |
* Return if the geometry is multiple |
|
421 |
* @param path |
|
422 |
* @return |
|
423 |
*/ |
|
424 |
public boolean isMultiple(PathIterator path){ |
|
425 |
double[] coords = new double[2]; |
|
426 |
int type = 0; |
|
427 |
int numGeometries = 0; |
|
428 |
while (!path.isDone()){ |
|
429 |
type = path.currentSegment(coords); |
|
430 |
switch (type) { |
|
431 |
case PathIterator.SEG_MOVETO: |
|
432 |
numGeometries++; |
|
433 |
if (numGeometries == 2){ |
|
434 |
return true; |
|
435 |
} |
|
436 |
break; |
|
437 |
default: |
|
438 |
break; |
|
439 |
} |
|
440 |
path.next(); |
|
441 |
} |
|
442 |
return false; |
|
443 |
} |
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
|
453 |
|
|
454 |
|
|
455 |
|
|
456 |
|
|
457 |
|
|
458 |
|
|
459 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/java/org/gvsig/gpe/exportto/generic/util/CoordinatesSequencePoint.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.generic.util; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.gvsig.fmap.geom.primitive.Point; |
|
6 |
import org.gvsig.gpe.lib.api.parser.ICoordinateIterator; |
|
7 |
import org.gvsig.gpe.lib.api.writer.ICoordinateSequence; |
|
8 |
|
|
9 |
public class CoordinatesSequencePoint implements |
|
10 |
ICoordinateSequence, ICoordinateIterator { |
|
11 |
|
|
12 |
double[] points = null; |
|
13 |
int index = 0; |
|
14 |
|
|
15 |
|
|
16 |
public CoordinatesSequencePoint(Point point) { |
|
17 |
|
|
18 |
if (point.getDimension() == 3) { |
|
19 |
// 2dZ |
|
20 |
this.points = new double[3]; |
|
21 |
points[0] = point.getX(); |
|
22 |
points[1] = point.getY(); |
|
23 |
points[2] = point.getCoordinateAt(2); |
|
24 |
index = 3; |
|
25 |
} else { |
|
26 |
// 2d |
|
27 |
this.points = new double[2]; |
|
28 |
points[0] = point.getX(); |
|
29 |
points[1] = point.getY(); |
|
30 |
index = 2; |
|
31 |
} |
|
32 |
|
|
33 |
} |
|
34 |
|
|
35 |
public int getSize() { |
|
36 |
return 1; |
|
37 |
} |
|
38 |
|
|
39 |
public ICoordinateIterator iterator() { |
|
40 |
return this; |
|
41 |
} |
|
42 |
|
|
43 |
public int getDimension() { |
|
44 |
return points.length; |
|
45 |
} |
|
46 |
|
|
47 |
public boolean hasNext() throws IOException { |
|
48 |
return (index > 0); |
|
49 |
} |
|
50 |
|
|
51 |
public void next(double[] buffer) throws IOException { |
|
52 |
buffer[0] = points[0]; |
|
53 |
buffer[1] = points[1]; |
|
54 |
if ((buffer.length == 3) && (points.length == 3)){ |
|
55 |
buffer[2] = points[2]; |
|
56 |
} |
|
57 |
index--; |
|
58 |
} |
|
59 |
|
|
60 |
|
|
61 |
} |
|
62 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.gpe.exportto.generic.ExporttoGPEGenericLibrary |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.generic/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="ISO-8859-1"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
|
3 |
<modelVersion>4.0.0</modelVersion> |
|
4 |
<artifactId>org.gvsig.gpe.exportto.generic</artifactId> |
|
5 |
<packaging>jar</packaging> |
|
6 |
<name>${project.artifactId}</name> |
|
7 |
|
|
8 |
<parent> |
|
9 |
<groupId>org.gvsig</groupId> |
|
10 |
<artifactId>org.gvsig.gpe.exportto</artifactId> |
|
11 |
<version>2.1.45</version> |
|
12 |
</parent> |
|
13 |
|
|
14 |
<dependencies> |
|
15 |
|
|
16 |
<dependency> |
|
17 |
<groupId>org.gvsig</groupId> |
|
18 |
<artifactId>org.gvsig.gpe.lib.api</artifactId> |
|
19 |
<scope>compile</scope> |
|
20 |
</dependency> |
|
21 |
|
|
22 |
<dependency> |
|
23 |
<groupId>org.gvsig</groupId> |
|
24 |
<artifactId>org.gvsig.tools.lib</artifactId> |
|
25 |
<scope>compile</scope> |
|
26 |
</dependency> |
|
27 |
|
|
28 |
<dependency> |
|
29 |
<groupId>org.gvsig</groupId> |
|
30 |
<artifactId>org.gvsig.fmap.geometry.api</artifactId> |
|
31 |
<scope>compile</scope> |
|
32 |
</dependency> |
|
33 |
|
|
34 |
</dependencies> |
|
35 |
</project> |
|
36 |
|
|
37 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/ExporttoKMLProvider.java | ||
---|---|---|
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.gpe.exportto.kml; |
|
25 |
|
|
26 |
import org.cresques.cts.ICoordTrans; |
|
27 |
import org.cresques.cts.IProjection; |
|
28 |
|
|
29 |
import org.gvsig.exportto.ExporttoService; |
|
30 |
import org.gvsig.exportto.swing.prov.file.AbstractExporttoFileProvider; |
|
31 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProvider; |
|
32 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel; |
|
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
34 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
35 |
import org.gvsig.gpe.exportto.kml.panel.KMLOptionsPanel; |
|
36 |
import org.gvsig.tools.service.spi.ProviderServices; |
|
37 |
|
|
38 |
/** |
|
39 |
* Exporto provider which gets Exporto from a file. |
|
40 |
* |
|
41 |
* @author gvSIG Team |
|
42 |
* @version $Id$ |
|
43 |
*/ |
|
44 |
public class ExporttoKMLProvider extends AbstractExporttoFileProvider implements |
|
45 |
ExporttoSwingProvider { |
|
46 |
|
|
47 |
private FLyrVect vectorLayer = null; |
|
48 |
private KMLOptionsPanel optPanel = null; |
|
49 |
|
|
50 |
/** |
|
51 |
* |
|
52 |
* Constructor. |
|
53 |
*/ |
|
54 |
public ExporttoKMLProvider( |
|
55 |
ProviderServices providerServices, |
|
56 |
FLyrVect vlayer) { |
|
57 |
|
|
58 |
super( |
|
59 |
providerServices, |
|
60 |
vlayer.getFeatureStore(), |
|
61 |
getTargetProjection(vlayer)); |
|
62 |
|
|
63 |
vectorLayer = vlayer; |
|
64 |
|
|
65 |
IProjection viewProj = getTargetProjection(vlayer); |
|
66 |
boolean viewIs4326 = (viewProj.getAbrev().compareToIgnoreCase("epsg:4326") == 0); |
|
67 |
optPanel = new KMLOptionsPanel(vlayer.isLabeled(), !viewIs4326); |
|
68 |
} |
|
69 |
|
|
70 |
private static IProjection getTargetProjection(FLyrVect vlayer) { |
|
71 |
ICoordTrans ct = vlayer.getCoordTrans(); |
|
72 |
if (ct == null) { |
|
73 |
return vlayer.getProjection(); |
|
74 |
} else { |
|
75 |
return ct.getPDest(); |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
public ExporttoService createExporttoService() { |
|
80 |
return new ExporttoKMLService( |
|
81 |
selectFileOptionPanel.getSelectedFile(), |
|
82 |
featureStore, |
|
83 |
vectorLayer, |
|
84 |
optPanel.getLongFormat(), |
|
85 |
optPanel.useLabels(), |
|
86 |
optPanel.useBalloons(), |
|
87 |
optPanel.mustReprojectToEpsg4326()); |
|
88 |
} |
|
89 |
|
|
90 |
public int getPanelCount() { |
|
91 |
return 2; |
|
92 |
} |
|
93 |
|
|
94 |
public ExporttoSwingProviderPanel getPanelAt(int index) { |
|
95 |
switch (index) { |
|
96 |
case 0: |
|
97 |
return optPanel; |
|
98 |
case 1: |
|
99 |
return selectFileOptionPanel; |
|
100 |
} |
|
101 |
return null; |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public boolean needsPanelTargetProjection(){ |
|
106 |
return false; |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/ExporttoKMLProviderLibrary.java | ||
---|---|---|
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.gpe.exportto.kml; |
|
25 |
|
|
26 |
import org.gvsig.exportto.swing.ExporttoSwingLibrary; |
|
27 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderLocator; |
|
28 |
import org.gvsig.gpe.exportto.generic.ExporttoGPEGenericLibrary; |
|
29 |
import org.gvsig.tools.library.AbstractLibrary; |
|
30 |
import org.gvsig.tools.library.LibraryException; |
|
31 |
import org.gvsig.tools.service.spi.ProviderManager; |
|
32 |
|
|
33 |
/** |
|
34 |
* Library to initialize and register the file Exporto provider |
|
35 |
* implementation. |
|
36 |
* |
|
37 |
* @author gvSIG Team |
|
38 |
* @version $Id$ |
|
39 |
*/ |
|
40 |
public class ExporttoKMLProviderLibrary extends AbstractLibrary { |
|
41 |
|
|
42 |
public void doRegistration() { |
|
43 |
registerAsServiceOf(ExporttoSwingLibrary.class); |
|
44 |
require(ExporttoGPEGenericLibrary.class); |
|
45 |
} |
|
46 |
|
|
47 |
protected void doInitialize() throws LibraryException { |
|
48 |
// Nothing to do |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
protected void doPostInitialize() throws LibraryException { |
|
53 |
ProviderManager providerManager = |
|
54 |
ExporttoSwingProviderLocator.getManager(); |
|
55 |
providerManager.addProviderFactory(new ExporttoKMLProviderFactory()); |
|
56 |
} |
|
57 |
|
|
58 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.45/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/ExporttoKMLService.java | ||
---|---|---|
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.gpe.exportto.kml; |
|
25 |
|
|
26 |
import java.awt.geom.Rectangle2D; |
|
27 |
import java.io.File; |
|
28 |
import java.io.FileOutputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.Iterator; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
|
|
35 |
import org.cresques.cts.ICoordTrans; |
|
36 |
import org.cresques.cts.IProjection; |
|
37 |
import org.slf4j.Logger; |
|
38 |
import org.slf4j.LoggerFactory; |
|
39 |
|
|
40 |
import org.gvsig.exportto.ExporttoService; |
|
41 |
import org.gvsig.exportto.ExporttoServiceException; |
|
42 |
import org.gvsig.exportto.ExporttoServiceFinishAction; |
|
43 |
import org.gvsig.exportto.swing.prov.file.AbstractExporttoFileService; |
|
44 |
import org.gvsig.fmap.crs.CRSFactory; |
|
45 |
import org.gvsig.fmap.dal.DALLocator; |
|
46 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
47 |
import org.gvsig.fmap.dal.exception.DataException; |
|
48 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
49 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
|
50 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
51 |
import org.gvsig.fmap.dal.feature.Feature; |
|
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
53 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
55 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
56 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
|
57 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
|
58 |
import org.gvsig.fmap.geom.DataTypes; |
|
59 |
import org.gvsig.fmap.geom.Geometry; |
|
60 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
61 |
import org.gvsig.fmap.geom.GeometryManager; |
|
62 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
|
63 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
64 |
import org.gvsig.fmap.mapcontext.MapContextException; |
|
65 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
66 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
|
67 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
|
68 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
|
69 |
import org.gvsig.gpe.exportto.generic.util.CoordinatesSequenceBbox; |
|
70 |
import org.gvsig.gpe.exportto.generic.util.GeometryToGPEWriter; |
|
71 |
import org.gvsig.gpe.exportto.kml.style.KmlStyle; |
|
72 |
import org.gvsig.gpe.exportto.kml.style.StyleUtils; |
|
73 |
import org.gvsig.gpe.lib.api.GPELocator; |
|
74 |
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandler; |
|
75 |
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandlerImplementor; |
|
76 |
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags; |
|
77 |
import org.gvsig.gpe.prov.kml.writer.GPEKmlWriterHandlerImplementor; |
|
78 |
import org.gvsig.tools.dispose.DisposableIterator; |
|
79 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
|
80 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
81 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
82 |
|
|
83 |
/** |
|
84 |
* @author gvSIG Team |
|
85 |
* @version $Id$ |
|
86 |
* |
|
87 |
*/ |
|
88 |
public class ExporttoKMLService extends AbstractMonitorableTask implements |
|
89 |
ExporttoService { |
|
90 |
|
|
91 |
private static Logger logger = LoggerFactory.getLogger(ExporttoKMLService.class); |
|
92 |
|
|
93 |
private FeatureStore featureStore = null; |
|
94 |
private FLyrVect vectorLayer = null; |
|
95 |
private List<ICoordTrans> transfList = new ArrayList<ICoordTrans>(); |
|
96 |
|
|
97 |
private String mimeType = null; |
|
98 |
private File outFile = null; |
|
99 |
|
|
100 |
private boolean useLabels = false; |
|
101 |
private boolean attsAsBalloon = false; |
|
102 |
private boolean reprojectTo4326 = false; |
|
103 |
|
|
104 |
private IProjection targetproj = null; |
|
105 |
|
|
106 |
|
|
107 |
private NewFeatureStoreParameters newFeatureStoreParameters; |
|
108 |
private ExporttoServiceFinishAction exporttoServiceFinishAction; |
|
109 |
|
|
110 |
|
|
111 |
public ExporttoKMLService( |
|
112 |
File kmlFile, |
|
113 |
FeatureStore fstore, |
|
114 |
FLyrVect vlayer, |
|
115 |
String mtype, |
|
116 |
|
|
117 |
boolean doLabels, |
|
118 |
boolean balloon, |
|
119 |
boolean reproject4326) { |
|
120 |
|
|
121 |
super("Export to KML"); |
|
122 |
outFile = kmlFile; |
|
123 |
featureStore = fstore; |
|
124 |
vectorLayer = vlayer; |
|
125 |
mimeType = mtype; |
|
126 |
|
|
127 |
useLabels = doLabels; |
|
128 |
attsAsBalloon = balloon; |
|
129 |
reprojectTo4326 = reproject4326; |
|
130 |
|
|
131 |
ICoordTrans ct = vlayer.getCoordTrans(); |
|
132 |
if (reprojectTo4326) { |
|
133 |
targetproj = CRSFactory.getCRS("EPSG:4326"); |
|
134 |
if (ct == null) { |
|
135 |
transfList.add(vlayer.getProjection().getCT(targetproj)); |
|
136 |
} else { |
|
137 |
transfList.add(ct); |
|
138 |
transfList.add(ct.getPDest().getCT(targetproj)); |
|
139 |
} |
|
140 |
} else { |
|
141 |
if (ct == null) { |
|
142 |
targetproj = vectorLayer.getProjection(); |
|
143 |
} else { |
|
144 |
targetproj = ct.getPDest(); |
|
145 |
transfList.add(ct); |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
public NewFeatureStoreParameters getNewFeatureStoreParameters() { |
|
151 |
return newFeatureStoreParameters; |
|
152 |
} |
|
153 |
|
|
154 |
public void addParameters( |
|
155 |
NewFeatureStoreParameters newFeatureStoreParameters) { |
|
156 |
|
|
157 |
newFeatureStoreParameters.setDynValue("CRS", targetproj); |
|
158 |
} |
|
159 |
|
|
160 |
public String getFileExtension() { |
|
161 |
return "kml"; |
|
162 |
} |
|
163 |
|
|
164 |
public void export(FeatureSet featureSet) throws ExporttoServiceException { |
|
165 |
|
|
166 |
IGPEWriterHandler wh = null; |
|
167 |
FileOutputStream fos = null; |
|
168 |
String srs = targetproj.getAbrev(); |
|
169 |
|
|
170 |
String[] fldNames = null; |
|
171 |
Envelope env = null; |
|
172 |
long count = 0; |
|
173 |
|
|
174 |
try { |
|
175 |
count = featureSet.getSize(); |
|
176 |
this.taskStatus.setRangeOfValues(0, count); |
|
177 |
fldNames = getAttributes(featureStore.getDefaultFeatureType()); |
|
178 |
env = this.featureStore.getEnvelope(); |
|
179 |
} catch (DataException e) { |
|
180 |
throw new ExporttoServiceException(e); |
|
181 |
} |
|
182 |
|
|
183 |
try { |
|
184 |
env = reproject(env); |
|
185 |
} catch (CreateEnvelopeException cee) { |
|
186 |
throw new ExporttoServiceException(cee); |
|
187 |
} |
|
188 |
|
|
189 |
Rectangle2D rect = new Rectangle2D.Double( |
|
190 |
env.getMinimum(0), |
|
191 |
env.getMinimum(1), |
|
192 |
env.getLength(0), |
|
193 |
env.getLength(1)); |
|
194 |
|
|
195 |
File fixedFile = outFile; |
|
196 |
try { |
|
197 |
|
|
198 |
if (!outFile.getAbsolutePath().toLowerCase().endsWith( |
|
199 |
"." + this.getFileExtension().toLowerCase())) { |
|
200 |
|
|
201 |
fixedFile = new File(outFile.getAbsolutePath() + "." + getFileExtension()); |
|
202 |
} |
|
203 |
|
|
204 |
wh = GPELocator.getGPEManager().createWriterByMimeType(mimeType); |
|
205 |
fos = new FileOutputStream(fixedFile); |
|
206 |
|
|
207 |
wh.setOutputStream(fos); |
|
208 |
wh.initialize(); |
|
209 |
// ========================================== |
|
210 |
wh.startLayer(null, null, fixedFile.getName(), null, srs); |
|
211 |
// ============== Styles ============= |
|
212 |
Map<ISymbol, KmlStyle> symsty = null; |
|
213 |
IXmlStreamWriter xmlw = getXmlStreamWriter(wh); |
|
214 |
if (true && xmlw != null) { |
|
215 |
symsty = StyleUtils.getSymbolStyles( |
|
216 |
vectorLayer, |
|
217 |
featureSet, |
|
218 |
attsAsBalloon, |
|
219 |
fldNames); |
|
220 |
Iterator<KmlStyle> iter = symsty.values().iterator(); |
|
221 |
KmlStyle sty = null; |
|
222 |
while (iter.hasNext()) { |
|
223 |
sty = iter.next(); |
|
224 |
writeStyle(xmlw, sty); |
|
225 |
} |
|
226 |
} |
|
227 |
// ========================================== |
|
228 |
wh.startBbox(null, new CoordinatesSequenceBbox(rect), srs); |
|
229 |
wh.endBbox(); |
|
230 |
// ============= Writing feature ============ |
|
231 |
writeFeatures(wh, xmlw, featureSet, fldNames, symsty, |
|
232 |
(IVectorLegend) vectorLayer.getLegend()); |
|
233 |
// ========================================== |
|
234 |
wh.endLayer(); |
|
235 |
// ========================================== |
|
236 |
wh.close(); |
|
237 |
fos.close(); |
|
238 |
} catch (Exception exc) { |
|
239 |
throw new ExporttoServiceException(exc); |
|
240 |
} |
|
241 |
|
|
242 |
this.taskStatus.setCurValue(count); |
|
243 |
this.taskStatus.terminate(); |
|
244 |
this.finishAction(fixedFile, this.targetproj); |
|
245 |
} |
|
246 |
|
|
247 |
|
|
248 |
private Envelope reproject(Envelope env) throws CreateEnvelopeException { |
|
249 |
|
|
250 |
int sz = transfList.size(); |
|
251 |
if (sz == 0) { |
|
252 |
return env; |
|
253 |
} else { |
|
254 |
Envelope resp = env; |
|
255 |
try { |
|
256 |
for (int i=0; i<sz; i++) { |
|
257 |
resp = resp.convert(transfList.get(i)); |
|
258 |
} |
|
259 |
} catch (Exception exc) { |
|
260 |
|
|
261 |
// If this process fails, we'll use "emergency values": |
|
262 |
GeometryManager gm = GeometryLocator.getGeometryManager(); |
|
263 |
double[] min = new double[2]; |
|
264 |
double[] max = new double[2]; |
|
265 |
if (targetproj.isProjected()) { |
|
266 |
min = new double[]{-20000000, -20000000}; |
|
267 |
max = new double[]{ 20000000, 20000000}; |
|
268 |
} else { |
|
269 |
min = new double[]{-180, -90}; |
|
270 |
max = new double[]{ 180, 90}; |
|
271 |
} |
|
272 |
|
|
273 |
resp = gm.createEnvelope( |
|
274 |
min[0], min[1], |
|
275 |
max[0], max[1], |
|
276 |
Geometry.SUBTYPES.GEOM2D); |
|
277 |
|
|
278 |
} |
|
279 |
return resp; |
|
280 |
} |
|
281 |
} |
|
282 |
|
|
283 |
private void writeStyle(IXmlStreamWriter xmlw, KmlStyle sty) throws IOException { |
|
284 |
|
|
285 |
xmlw.writeStartElement(Kml2_1_Tags.STYLE); |
|
286 |
xmlw.writeStartAttribute(Kml2_1_Tags.ID); |
|
287 |
xmlw.writeValue(sty.getId()); |
|
288 |
xmlw.writeEndAttributes(); |
|
289 |
sty.writeXml(xmlw); |
|
290 |
xmlw.writeEndElement(); |
|
291 |
} |
|
292 |
|
|
293 |
|
|
294 |
private IXmlStreamWriter getXmlStreamWriter(IGPEWriterHandler wh) { |
|
295 |
|
|
296 |
IGPEWriterHandlerImplementor imple = wh.getImplementor(); |
|
297 |
if (!(imple instanceof GPEKmlWriterHandlerImplementor)) { |
|
298 |
/* |
|
299 |
* Unexpected class |
|
300 |
*/ |
|
301 |
return null; |
|
302 |
} |
|
303 |
GPEKmlWriterHandlerImplementor kmlimple = null; |
|
304 |
kmlimple = (GPEKmlWriterHandlerImplementor) imple; |
|
305 |
IXmlStreamWriter xmlw = kmlimple.getXMLStreamWriter(); |
|
306 |
return xmlw; |
|
307 |
} |
|
308 |
|
|
309 |
|
|
310 |
private void finishAction(File kmlfile, IProjection proj) |
|
311 |
throws ExporttoServiceException { |
|
312 |
|
|
313 |
if (exporttoServiceFinishAction != null) { |
|
314 |
|
|
315 |
/* |
|
316 |
* Export is done. We notify with a SHPStoreParameters, |
|
317 |
* not with the NewSHPParameters we have used: |
|
318 |
*/ |
|
319 |
DataManagerProviderServices dataman = |
|
320 |
(DataManagerProviderServices) DALLocator.getDataManager(); |
|
321 |
|
|
322 |
DataStoreParameters dsp = null; |
|
323 |
try { |
|
324 |
dsp = dataman.createStoreParameters("GPE"); |
|
325 |
} catch (Exception e) { |
|
326 |
throw new ExporttoServiceException( |
|
327 |
"Cannot add resulting kml file to view", e); |
|
328 |
} |
|
329 |
|
|
330 |
dsp.setDynValue("File", kmlfile); |
|
331 |
dsp.setDynValue("CRS", proj); |
|
332 |
|
|
333 |
try { |
|
334 |
dsp.validate(); |
|
335 |
} catch (ValidateDataParametersException e) { |
|
336 |
throw new ExporttoServiceException(e); |
|
337 |
} |
|
338 |
exporttoServiceFinishAction.finished(kmlfile.getName(), dsp); |
|
339 |
} |
|
340 |
} |
|
341 |
|
|
342 |
private String[] getAttributes(FeatureType ftype) { |
|
343 |
|
|
344 |
FeatureAttributeDescriptor[] atts = ftype.getAttributeDescriptors(); |
|
345 |
FeatureAttributeDescriptor desc = null; |
|
346 |
List<String> list = new ArrayList<String>(); |
|
347 |
for (int i=0; i<atts.length; i++) { |
|
348 |
desc = atts[i]; |
|
349 |
if (desc.getDataType().getType() != DataTypes.GEOMETRY) { |
|
350 |
list.add(desc.getName()); |
|
351 |
} |
|
352 |
} |
|
353 |
return list.toArray(new String[0]); |
|
354 |
} |
|
355 |
|
|
356 |
private void writeFeatures( |
|
357 |
IGPEWriterHandler gwh, |
|
358 |
IXmlStreamWriter xmlw, |
|
359 |
FeatureSet fset, |
|
360 |
String[] fieldNames, |
|
361 |
Map<ISymbol, KmlStyle> symsty, |
|
362 |
IVectorLegend lege) throws Exception { |
|
363 |
|
|
364 |
GeometryToGPEWriter gw = new GeometryToGPEWriter(gwh); |
|
365 |
DisposableIterator diter = fset.fastIterator(); |
|
366 |
Feature feat = null; |
|
367 |
long count = 0; |
|
368 |
this.taskStatus.setCurValue(count); |
|
369 |
ISymbol sym = null; |
|
370 |
int nullGeometries = 0; |
|
371 |
while (diter.hasNext()) { |
|
372 |
feat = (Feature) diter.next(); |
|
373 |
try { |
|
374 |
sym = lege.getSymbolByFeature(feat); |
|
375 |
} catch (MapContextException mce) { |
|
376 |
logger.info("While getting legend symbol.", mce); |
|
377 |
} |
|
378 |
|
|
379 |
if(!writeFeature(feat, gwh, xmlw, gw, count, fieldNames, symsty.get(sym))){ |
|
380 |
nullGeometries++; |
|
381 |
}; |
|
382 |
count++; |
|
383 |
this.taskStatus.setCurValue(count); |
|
384 |
} |
|
385 |
if(nullGeometries>0){ |
|
386 |
logger.warn("Can't export "+nullGeometries+ " features because source geometries are null."); |
|
387 |
} |
|
388 |
diter.dispose(); |
|
389 |
} |
|
390 |
|
|
391 |
private boolean writeFeature( |
|
392 |
Feature feat, |
|
393 |
IGPEWriterHandler gwh, |
|
394 |
IXmlStreamWriter xmlw, |
|
395 |
GeometryToGPEWriter gw, |
|
396 |
long index, |
|
397 |
String[] fieldNames, |
|
398 |
KmlStyle ksty) throws IOException { |
|
399 |
|
|
400 |
|
|
401 |
Geometry geom = feat.getDefaultGeometry(); |
|
402 |
|
|
403 |
if(geom == null){ |
|
404 |
return false; |
|
405 |
} |
|
406 |
|
|
407 |
String strindex = String.valueOf(index); |
|
408 |
|
|
409 |
if (this.useLabels) { |
|
410 |
String lbl = getLabelForFeature(feat); |
|
411 |
gwh.startFeature(strindex, "FEATURE", lbl); |
|
412 |
} else { |
|
413 |
gwh.startFeature(strindex, "FEATURE", ""); |
|
414 |
} |
|
415 |
// ========================= |
|
416 |
// Style |
|
417 |
if (ksty != null) { |
|
418 |
xmlw.writeStartElement(Kml2_1_Tags.STYLEURL); |
|
419 |
xmlw.writeValue("#" + ksty.getId()); |
|
420 |
xmlw.writeEndElement(); |
|
421 |
} |
|
422 |
// ===== Balloon ============ |
|
423 |
if (this.attsAsBalloon) { |
|
424 |
writeBalloon(xmlw, feat, fieldNames); |
|
425 |
} |
|
426 |
|
|
427 |
// ============= Geometry |
|
428 |
|
|
429 |
/* |
|
430 |
* This has no effect if reprojection is not necessary |
|
431 |
*/ |
|
432 |
geom = reproject(geom); |
|
433 |
gw.writeGeometry(geom, useLabels); |
|
434 |
// ============= Attributes |
|
435 |
Object val = null; |
|
436 |
String fldname = null; |
|
437 |
for (int i=0; i<fieldNames.length; i++) { |
|
438 |
val = feat.get(fieldNames[i]); |
|
439 |
fldname = fieldNames[i].replace(' ', '_'); |
|
440 |
gwh.startElement("", fldname, val == null ? "" : val.toString()); |
|
441 |
gwh.endElement(); |
|
442 |
} |
|
443 |
// ========================= |
|
444 |
gwh.endFeature(); |
|
445 |
return true; |
|
446 |
} |
|
447 |
|
|
448 |
private void writeBalloon(IXmlStreamWriter xmlw, Feature feat, String[] fieldNames) |
|
449 |
throws IOException { |
|
450 |
|
|
451 |
xmlw.writeStartElement(Kml2_1_Tags.EXTENDED_DATA); |
|
452 |
String fldrep = null; |
|
453 |
Object val = null; |
|
454 |
for (int i=0; i<fieldNames.length; i++) { |
|
455 |
fldrep = fieldNames[i].replace(' ', '_'); |
|
456 |
xmlw.writeStartElement(Kml2_1_Tags.DATA); |
|
457 |
// Att ===================================================== |
|
458 |
xmlw.writeStartAttribute(null, "name"); |
|
459 |
xmlw.writeValue(fldrep); |
|
460 |
xmlw.writeEndAttributes(); |
|
461 |
// Value ===================================================== |
|
462 |
xmlw.writeStartElement(Kml2_1_Tags.VALUE); |
|
463 |
val = feat.get(fieldNames[i]); |
|
464 |
xmlw.writeValue(val == null ? "" : val.toString()); |
|
465 |
xmlw.writeEndElement(); |
|
466 |
// ============================================= |
|
467 |
xmlw.writeEndElement(); |
|
468 |
} |
|
469 |
xmlw.writeEndElement(); |
|
470 |
|
|
471 |
/* |
|
472 |
* |
|
473 |
<ExtendedData> |
|
474 |
<Data name="holeNumber"> |
|
475 |
<value>1</value> |
|
476 |
</Data> |
|
477 |
<Data name="holeYardage"> |
|
478 |
<value>234</value> |
|
479 |
</Data> |
|
480 |
<Data name="holePar"> |
|
481 |
<value>4</value> |
|
482 |
</Data> |
|
483 |
</ExtendedData> |
|
484 |
* |
|
485 |
*/ |
|
486 |
|
|
487 |
} |
|
488 |
|
|
489 |
private String getLabelForFeature(Feature feat) { |
|
490 |
|
|
491 |
if (this.vectorLayer.isLabeled()) { |
|
492 |
|
|
493 |
String[] flds = vectorLayer.getLabelingStrategy().getUsedFields(); |
|
494 |
int n = Math.min(flds.length, 2); |
|
495 |
if (n == 0) { |
|
496 |
return ""; |
|
497 |
} else { |
|
498 |
String resp = ""; |
|
499 |
Object val = null; |
|
500 |
if (n == 1) { |
|
501 |
val = feat.get(flds[0]); |
|
502 |
resp = (val == null) ? "" : val.toString(); |
|
503 |
} else { |
|
504 |
// n == 2 |
|
505 |
val = feat.get(flds[0]); |
|
506 |
resp = (val == null) ? "" : val.toString(); |
|
507 |
val = feat.get(flds[1]); |
|
508 |
resp = (val == null) ? resp : resp + ", " + val.toString(); |
|
509 |
} |
|
510 |
return resp; |
|
511 |
} |
|
512 |
|
|
513 |
} else { |
|
514 |
return ""; |
|
515 |
} |
|
516 |
} |
|
517 |
|
|
518 |
public void setFinishAction( |
|
519 |
ExporttoServiceFinishAction exporttoServiceFinishAction) { |
|
520 |
this.exporttoServiceFinishAction = exporttoServiceFinishAction; |
|
521 |
} |
|
522 |
|
|
523 |
private Geometry reproject(Geometry geom) { |
|
524 |
|
|
525 |
int sz = transfList.size(); |
|
526 |
if (sz == 0) { |
|
527 |
return geom; |
|
528 |
} else { |
|
529 |
Geometry resp = geom.cloneGeometry(); |
|
530 |
for (int i=0; i<sz; i++) { |
Also available in: Unified diff