Revision 646
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.gpe.exportto.kml.ExporttoKMLProviderLibrary |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/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.41/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++) { |
|
531 |
resp.reProject(transfList.get(i)); |
|
532 |
} |
|
533 |
return resp; |
|
534 |
} |
|
535 |
|
|
536 |
} |
|
537 |
|
|
538 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/ExporttoKMLProviderFactory.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.IProjection; |
|
27 |
|
|
28 |
import org.gvsig.exportto.swing.ExporttoSwingManager; |
|
29 |
import org.gvsig.exportto.swing.prov.file.AbstractExporttoFileProviderFactory; |
|
30 |
import org.gvsig.fmap.dal.DataTypes; |
|
31 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
32 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
33 |
import org.gvsig.tools.ToolsLocator; |
|
34 |
import org.gvsig.tools.dynobject.DynObject; |
|
35 |
import org.gvsig.tools.service.ServiceException; |
|
36 |
import org.gvsig.tools.service.spi.Provider; |
|
37 |
import org.gvsig.tools.service.spi.ProviderServices; |
|
38 |
|
|
39 |
/** |
|
40 |
* Factory of file {@link ExportoProvider} objects. |
|
41 |
* |
|
42 |
* @author gvSIG Team |
|
43 |
* @version $Id$ |
|
44 |
*/ |
|
45 |
public class ExporttoKMLProviderFactory extends |
|
46 |
AbstractExporttoFileProviderFactory { |
|
47 |
|
|
48 |
private static final String PROVIDER_NAME = "KML"; |
|
49 |
protected static final String PARAMETER_LAYER = "FLyrVect"; |
|
50 |
|
|
51 |
public ExporttoKMLProviderFactory() { |
|
52 |
super(new int[] { |
|
53 |
ExporttoSwingManager.VECTORIAL_TABLE_WITH_GEOMETRY |
|
54 |
}); |
|
55 |
} |
|
56 |
|
|
57 |
public void initialize() { |
|
58 |
parametersDefinition = |
|
59 |
ToolsLocator.getDynObjectManager().createDynClass(getName(), |
|
60 |
getDescription()); |
|
61 |
parametersDefinition.addDynField(PARAMETER_FEATURESTORE) |
|
62 |
.setType(DataTypes.OBJECT).setMandatory(true) |
|
63 |
.setClassOfValue(FeatureStore.class); |
|
64 |
parametersDefinition.addDynField(PARAMETER_PROJECTION) |
|
65 |
.setType(DataTypes.CRS).setMandatory(false); |
|
66 |
parametersDefinition.addDynField(PARAMETER_LAYER) |
|
67 |
.setType(DataTypes.OBJECT).setMandatory(false) |
|
68 |
.setClassOfValue(FLyrVect.class); |
|
69 |
} |
|
70 |
|
|
71 |
public Provider create(DynObject parameters, ProviderServices services) |
|
72 |
throws ServiceException { |
|
73 |
|
|
74 |
return new ExporttoKMLProvider(services, |
|
75 |
(FLyrVect) parameters.getDynValue(PARAMETER_LAYER)); |
|
76 |
} |
|
77 |
|
|
78 |
public String getName() { |
|
79 |
return PROVIDER_NAME; |
|
80 |
} |
|
81 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/panel/KMLOptionsPanel.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.panel; |
|
25 |
|
|
26 |
import java.awt.BorderLayout; |
|
27 |
import java.awt.FlowLayout; |
|
28 |
import java.awt.GridBagConstraints; |
|
29 |
import java.awt.GridBagLayout; |
|
30 |
import java.awt.Insets; |
|
31 |
import java.util.ArrayList; |
|
32 |
|
|
33 |
import javax.swing.JCheckBox; |
|
34 |
import javax.swing.JComboBox; |
|
35 |
import javax.swing.JComponent; |
|
36 |
import javax.swing.JLabel; |
|
37 |
import javax.swing.JPanel; |
|
38 |
|
|
39 |
import org.slf4j.Logger; |
|
40 |
import org.slf4j.LoggerFactory; |
|
41 |
|
|
42 |
import org.gvsig.exportto.swing.ExporttoSwingLocator; |
|
43 |
import org.gvsig.exportto.swing.ExporttoSwingManager; |
|
44 |
import org.gvsig.exportto.swing.spi.ExporttoPanelValidationException; |
|
45 |
import org.gvsig.exportto.swing.spi.ExporttoSwingProviderPanel; |
|
46 |
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandler; |
|
47 |
import org.gvsig.gpe.lib.spi.GPEProviderLocator; |
|
48 |
|
|
49 |
|
|
50 |
/** |
|
51 |
* |
|
52 |
* This addditional panel lets user choose KML |
|
53 |
* version and other options. |
|
54 |
* |
|
55 |
* @author gvSIG Team |
|
56 |
* @version $Id$ |
|
57 |
* |
|
58 |
*/ |
|
59 |
public class KMLOptionsPanel extends JPanel implements ExporttoSwingProviderPanel { |
|
60 |
|
|
61 |
private static final long serialVersionUID = -3278172717881233447L; |
|
62 |
|
|
63 |
private static final Logger LOG = LoggerFactory |
|
64 |
.getLogger(KMLOptionsPanel.class); |
|
65 |
|
|
66 |
private static final ExporttoSwingManager EXPORTTO_SWING_MANAGER = |
|
67 |
ExporttoSwingLocator.getSwingManager(); |
|
68 |
|
|
69 |
private boolean offerLabels = false; |
|
70 |
private boolean offerReproj = false; |
|
71 |
|
|
72 |
private JCheckBox labelsChk = null; |
|
73 |
private JCheckBox reproChk = null; |
|
74 |
private JCheckBox balloonsChk = null; |
|
75 |
|
|
76 |
private JComboBox versionCombo = null; |
|
77 |
|
|
78 |
public KMLOptionsPanel(boolean ofrLabels, boolean ofrReproj) { |
|
79 |
super(); |
|
80 |
offerLabels = ofrLabels; |
|
81 |
offerReproj = ofrReproj; |
|
82 |
init(); |
|
83 |
} |
|
84 |
|
|
85 |
private void init() { |
|
86 |
|
|
87 |
this.setLayout(new BorderLayout()); |
|
88 |
|
|
89 |
FlowLayout fl = new FlowLayout(FlowLayout.LEFT); |
|
90 |
JPanel topPanel = new JPanel(fl); |
|
91 |
// ==================================================== |
|
92 |
JPanel gridPanel = new JPanel(); |
|
93 |
gridPanel.setLayout(new GridBagLayout()); |
|
94 |
GridBagConstraints gbc = new GridBagConstraints(); |
|
95 |
|
|
96 |
gbc.anchor = GridBagConstraints.WEST; |
|
97 |
gbc.insets = new Insets(3, 3, 3, 3); |
|
98 |
|
|
99 |
gbc.gridx = 0; |
|
100 |
gbc.gridy = 0; |
|
101 |
gbc.gridwidth = 3; |
|
102 |
gridPanel.add(new JLabel(EXPORTTO_SWING_MANAGER.getTranslation("_Choose_KML_version")), gbc); |
|
103 |
gbc.gridx = 3; |
|
104 |
gbc.gridwidth = 3; |
|
105 |
gridPanel.add(getVersionsCombo(), gbc); |
|
106 |
// ===================== |
|
107 |
gbc.gridx = 0; |
|
108 |
gbc.gridy = 1; |
|
109 |
gbc.gridwidth = 1; |
|
110 |
gridPanel.add(getBalloonsCheckbox(), gbc); |
|
111 |
gbc.gridx = 1; |
|
112 |
gbc.gridwidth = 5; |
|
113 |
gridPanel.add(new JLabel(EXPORTTO_SWING_MANAGER.getTranslation( |
|
114 |
"_Show_attributes_in_balloon")), gbc); |
|
115 |
// ===================== |
|
116 |
gbc.gridx = 0; |
|
117 |
gbc.gridy = 2; |
|
118 |
gbc.gridwidth = 1; |
|
119 |
gridPanel.add(getLabelsCheckbox(offerLabels), gbc); |
|
120 |
gbc.gridx = 1; |
|
121 |
gbc.gridwidth = 5; |
|
122 |
JLabel lbl_lbl = new JLabel(EXPORTTO_SWING_MANAGER.getTranslation("" + |
|
123 |
"_Use_labels_Label_point_will_be_added_to_non_points")); |
|
124 |
lbl_lbl.setEnabled(offerLabels); |
|
125 |
gridPanel.add(lbl_lbl, gbc); |
|
126 |
// ==================================================== |
|
127 |
gbc.gridx = 0; |
|
128 |
gbc.gridy = 3; |
|
129 |
gbc.gridwidth = 1; |
|
130 |
gridPanel.add(getReproCheckbox(offerReproj), gbc); |
|
131 |
gbc.gridx = 1; |
|
132 |
gbc.gridwidth = 5; |
|
133 |
JLabel rep_lbl = new JLabel(EXPORTTO_SWING_MANAGER.getTranslation( |
|
134 |
"_Force_to_EPSG_4326")); |
|
135 |
rep_lbl.setEnabled(offerReproj); |
|
136 |
gridPanel.add(rep_lbl, gbc); |
|
137 |
// ==================================================== |
|
138 |
topPanel.add(gridPanel); |
|
139 |
this.add(topPanel, BorderLayout.NORTH); |
|
140 |
} |
|
141 |
|
|
142 |
private JCheckBox getLabelsCheckbox(boolean enabled) { |
|
143 |
|
|
144 |
if (labelsChk == null) { |
|
145 |
labelsChk = new JCheckBox(""); |
|
146 |
labelsChk.setSelected(false); |
|
147 |
labelsChk.setEnabled(enabled); |
|
148 |
} |
|
149 |
return labelsChk; |
|
150 |
} |
|
151 |
|
|
152 |
private JCheckBox getReproCheckbox(boolean enabled) { |
|
153 |
|
|
154 |
if (reproChk == null) { |
|
155 |
reproChk = new JCheckBox(""); |
|
156 |
reproChk.setSelected(true); |
|
157 |
reproChk.setEnabled(enabled); |
|
158 |
} |
|
159 |
return reproChk; |
|
160 |
} |
|
161 |
|
|
162 |
private JCheckBox getBalloonsCheckbox() { |
|
163 |
|
|
164 |
if (balloonsChk == null) { |
|
165 |
balloonsChk = new JCheckBox(""); |
|
166 |
balloonsChk.setSelected(false); |
|
167 |
} |
|
168 |
return balloonsChk; |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
private JComboBox getVersionsCombo() { |
|
173 |
|
|
174 |
if (versionCombo == null) { |
|
175 |
versionCombo = new JComboBox(); |
|
176 |
ArrayList list = GPEProviderLocator.getGPEProviderManager().getWriterHandlerByFormat("kml"); |
|
177 |
IGPEWriterHandler item = null; |
|
178 |
String longn = null; |
|
179 |
String shortn = null; |
|
180 |
for (int i=0; i<list.size(); i++) { |
|
181 |
item = (IGPEWriterHandler) list.get(i); |
|
182 |
longn = item.getFormat(); |
|
183 |
shortn = getShortWithVersion(longn); |
|
184 |
if (longn != null && shortn != null) { |
|
185 |
versionCombo.addItem(new FormatListItem(longn, shortn)); |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
return versionCombo; |
|
190 |
} |
|
191 |
|
|
192 |
public String getPanelTitle() { |
|
193 |
return EXPORTTO_SWING_MANAGER.getTranslation("_KML_options"); |
|
194 |
} |
|
195 |
|
|
196 |
public boolean isValidPanel() throws ExporttoPanelValidationException { |
|
197 |
return getLongFormat() != null; |
|
198 |
} |
|
199 |
|
|
200 |
|
|
201 |
/** |
|
202 |
* Returns null if not valid format |
|
203 |
*/ |
|
204 |
private String getShortWithVersion(String fmt) { |
|
205 |
// Example: "text/xml; subtype=gml/2.1.2" => "gml" |
|
206 |
if (fmt == null) { |
|
207 |
return null; |
|
208 |
} |
|
209 |
String[] parts = fmt.split(";"); |
|
210 |
String aux = ""; |
|
211 |
if (parts.length > 1) { |
|
212 |
aux = parts[1].trim(); |
|
213 |
} else { |
|
214 |
return null; |
|
215 |
} |
|
216 |
parts = aux.split("="); |
|
217 |
if (parts.length > 1) { |
|
218 |
aux = parts[1].trim(); |
|
219 |
// Example: aux = "gml/2.1.2" |
|
220 |
aux = aux.replaceAll("/", " "); |
|
221 |
return aux.toUpperCase(); |
|
222 |
} else { |
|
223 |
return null; |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
public void enterPanel() { |
|
228 |
|
|
229 |
} |
|
230 |
|
|
231 |
public JComponent asJComponent() { |
|
232 |
return this; |
|
233 |
} |
|
234 |
|
|
235 |
public class FormatListItem { |
|
236 |
|
|
237 |
private String longName = ""; |
|
238 |
private String shortName = ""; |
|
239 |
|
|
240 |
public FormatListItem(String longname, String shortname) { |
|
241 |
longName = longname; |
|
242 |
shortName = shortname; |
|
243 |
} |
|
244 |
|
|
245 |
public String toString() { |
|
246 |
return shortName; |
|
247 |
} |
|
248 |
|
|
249 |
public String getLongFormat() { |
|
250 |
return longName; |
|
251 |
} |
|
252 |
} |
|
253 |
|
|
254 |
// ================================================== |
|
255 |
// ================================================== |
|
256 |
|
|
257 |
public String getLongFormat() { |
|
258 |
Object obj = this.getVersionsCombo().getSelectedItem(); |
|
259 |
if (obj instanceof FormatListItem) { |
|
260 |
return ((FormatListItem) obj).getLongFormat(); |
|
261 |
} else { |
|
262 |
return null; |
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
public boolean useLabels() { |
|
267 |
return this.getLabelsCheckbox(false).isSelected(); |
|
268 |
} |
|
269 |
|
|
270 |
public boolean useBalloons() { |
|
271 |
return this.getBalloonsCheckbox().isSelected(); |
|
272 |
} |
|
273 |
|
|
274 |
public boolean mustReprojectToEpsg4326() { |
|
275 |
return this.getReproCheckbox(true).isSelected(); |
|
276 |
} |
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlLineStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags; |
|
6 |
import org.gvsig.gpe.prov.kml.utils.KmlUtilsParser; |
|
7 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
8 |
|
|
9 |
public class KmlLineStyle extends KmlColorStyle { |
|
10 |
public float getWidth() { |
|
11 |
return width; |
|
12 |
} |
|
13 |
|
|
14 |
public void setWidth(float width) { |
|
15 |
this.width = width; |
|
16 |
} |
|
17 |
|
|
18 |
protected float width = 1; |
|
19 |
|
|
20 |
@Override |
|
21 |
public void writeXml(IXmlStreamWriter writer) throws IOException { |
|
22 |
writer.writeStartElement(Kml2_1_Tags.LINE_STYLE); |
|
23 |
|
|
24 |
writer.writeStartElement(Kml2_1_Tags.COLOR); |
|
25 |
writer.writeValue(KmlUtilsParser.fromColorToABGR(color)); |
|
26 |
writer.writeEndElement(); |
|
27 |
writer.writeStartElement(Kml2_1_Tags.WIDTH); |
|
28 |
writer.writeValue(width); |
|
29 |
writer.writeEndElement(); |
|
30 |
|
|
31 |
writer.writeEndElement(); |
|
32 |
} |
|
33 |
|
|
34 |
|
|
35 |
} |
|
36 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlPolygonStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags; |
|
6 |
import org.gvsig.gpe.prov.kml.utils.KmlUtilsParser; |
|
7 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
8 |
|
|
9 |
/** |
|
10 |
* @author Francisco Jos? Pe?arrubia (fjp@scolab.es) |
|
11 |
* |
|
12 |
* To convert from a symbol to a polygon style we will need a PolygonStyle |
|
13 |
* and a LineStyle if the outline has some width or to specify the color |
|
14 |
* @see @KmlCompoundStyle |
|
15 |
*/ |
|
16 |
public class KmlPolygonStyle extends KmlColorStyle { |
|
17 |
protected boolean fill = true; |
|
18 |
protected boolean outline; |
|
19 |
|
|
20 |
public boolean isFill() { |
|
21 |
return fill; |
|
22 |
} |
|
23 |
|
|
24 |
public void setFill(boolean fill) { |
|
25 |
this.fill = fill; |
|
26 |
} |
|
27 |
|
|
28 |
public boolean isOutline() { |
|
29 |
return outline; |
|
30 |
} |
|
31 |
|
|
32 |
public void setOutline(boolean outline) { |
|
33 |
this.outline = outline; |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public void writeXml(IXmlStreamWriter writer) throws IOException { |
|
38 |
writer.writeStartElement(Kml2_1_Tags.POLY_STYLE); |
|
39 |
|
|
40 |
writer.writeStartElement(Kml2_1_Tags.COLOR); |
|
41 |
writer.writeValue(KmlUtilsParser.fromColorToABGR(color)); |
|
42 |
writer.writeEndElement(); |
|
43 |
if (!fill) { |
|
44 |
writer.writeStartElement(Kml2_1_Tags.POLY_FILL); |
|
45 |
writer.writeValue(0); |
|
46 |
writer.writeEndElement(); |
|
47 |
} |
|
48 |
if (outline) { |
|
49 |
writer.writeStartElement(Kml2_1_Tags.POLY_OUTLINE); |
|
50 |
writer.writeValue(1); |
|
51 |
writer.writeEndElement(); |
|
52 |
} |
|
53 |
|
|
54 |
writer.writeEndElement(); |
|
55 |
|
|
56 |
} |
|
57 |
|
|
58 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlIconStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags; |
|
6 |
import org.gvsig.gpe.prov.kml.utils.KmlUtilsParser; |
|
7 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
8 |
|
|
9 |
public class KmlIconStyle extends KmlColorStyle { |
|
10 |
|
|
11 |
protected float heading = 0; |
|
12 |
protected String href = null; |
|
13 |
protected float scale = 1; |
|
14 |
|
|
15 |
public float getScale() { |
|
16 |
return scale; |
|
17 |
} |
|
18 |
|
|
19 |
public void setScale(float scale) { |
|
20 |
this.scale = scale; |
|
21 |
} |
|
22 |
|
|
23 |
|
|
24 |
public void writeXml(IXmlStreamWriter writer) throws IOException { |
|
25 |
writer.writeStartElement(Kml2_1_Tags.ICON_STYLE); |
|
26 |
|
|
27 |
writer.writeStartElement(Kml2_1_Tags.COLOR); |
|
28 |
writer.writeValue(KmlUtilsParser.fromColorToABGR(color)); |
|
29 |
writer.writeEndElement(); |
|
30 |
|
|
31 |
writer.writeStartElement(Kml2_1_Tags.HEADING); |
|
32 |
writer.writeValue(heading); |
|
33 |
writer.writeEndElement(); |
|
34 |
|
|
35 |
if (scale != 1) { |
|
36 |
writer.writeStartElement(Kml2_1_Tags.SCALE); |
|
37 |
writer.writeValue(scale); |
|
38 |
writer.writeEndElement(); |
|
39 |
} |
|
40 |
if (href != null) { |
|
41 |
writer.writeStartElement(Kml2_1_Tags.ICON); |
|
42 |
writer.writeStartElement(Kml2_1_Tags.HREF); |
|
43 |
writer.writeValue(href); |
|
44 |
writer.writeEndElement(); |
|
45 |
|
|
46 |
writer.writeEndElement(); |
|
47 |
} |
|
48 |
|
|
49 |
|
|
50 |
writer.writeEndElement(); |
|
51 |
|
|
52 |
|
|
53 |
} |
|
54 |
|
|
55 |
public float getHeading() { |
|
56 |
return heading; |
|
57 |
} |
|
58 |
|
|
59 |
public void setHeading(float heading) { |
|
60 |
this.heading = heading; |
|
61 |
} |
|
62 |
|
|
63 |
public String getHref() { |
|
64 |
return href; |
|
65 |
} |
|
66 |
|
|
67 |
public void setHref(String href) { |
|
68 |
this.href = href; |
|
69 |
} |
|
70 |
|
|
71 |
} |
|
72 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/StyleUtils.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.HashMap; |
|
5 |
import java.util.List; |
|
6 |
import java.util.Map; |
|
7 |
|
|
8 |
import org.gvsig.fmap.dal.exception.DataException; |
|
9 |
import org.gvsig.fmap.dal.feature.Feature; |
|
10 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
11 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
|
12 |
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend; |
|
13 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
|
14 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
|
15 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.IFillSymbol; |
|
16 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.line.ILineSymbol; |
|
17 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
|
18 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol; |
|
19 |
import org.gvsig.tools.dispose.DisposableIterator; |
|
20 |
|
|
21 |
|
|
22 |
public class StyleUtils { |
|
23 |
|
|
24 |
public static Map<ISymbol, KmlStyle> getSymbolStyles( |
|
25 |
FLyrVect vlayer, |
|
26 |
FeatureSet fset, |
|
27 |
boolean balloon, |
|
28 |
String[] flds) throws Exception { |
|
29 |
|
|
30 |
boolean preventMarker = false; |
|
31 |
|
|
32 |
IVectorLegend leg = (IVectorLegend) vlayer.getLegend(); |
|
33 |
Map<ISymbol, KmlStyle> resp = new HashMap<ISymbol, KmlStyle>(); |
|
34 |
|
|
35 |
ISymbol defaultSym = leg.getDefaultSymbol(); |
|
36 |
KmlStyle defaultStyle = getStyle(defaultSym); |
|
37 |
if (defaultSym != null && defaultStyle != null) { |
|
38 |
/* |
|
39 |
* This is used to set an empty icon style because |
|
40 |
* the geometry is not originally a point or multipoint |
|
41 |
*/ |
|
42 |
preventMarker = !(defaultStyle instanceof KmlIconStyle); |
|
43 |
KmlCompoundStyle cs = new KmlCompoundStyle(); |
|
44 |
cs.addStyle(defaultStyle); |
|
45 |
if (balloon) { |
|
46 |
// Not used, see javadoc of KmlBalloonStyle |
|
47 |
// cs.addStyle(new KmlBalloonStyle(flds, "balloonStyle")); |
|
48 |
} |
|
49 |
if (preventMarker) { |
|
50 |
cs.addStyle(new KmlIconStyle()); |
|
51 |
} |
|
52 |
cs.setId("defaultStyle"); |
|
53 |
resp.put(defaultSym, cs); |
|
54 |
} |
|
55 |
|
|
56 |
if (leg instanceof IClassifiedVectorLegend) { |
|
57 |
|
|
58 |
IClassifiedVectorLegend cLeg = (IClassifiedVectorLegend) leg; |
|
59 |
ISymbol[] symbolsAux = cLeg.getSymbols(); |
|
60 |
if (fset == null || symbolsAux.length < fset.getSize()) { |
|
61 |
|
|
62 |
for (int i=0; i < symbolsAux.length; i++) { |
|
63 |
ISymbol s = symbolsAux[i]; |
|
64 |
KmlStyle style = getStyle(s); |
|
65 |
if (s != null && style != null) { |
|
66 |
preventMarker = !(style instanceof KmlIconStyle); |
|
67 |
KmlCompoundStyle cs = new KmlCompoundStyle(); |
|
68 |
cs.addStyle(style); |
|
69 |
if (balloon) { |
|
70 |
// Not used, see javadoc of KmlBalloonStyle |
|
71 |
// cs.addStyle(new KmlBalloonStyle(flds, "bs" + Integer.toString(i))); |
|
72 |
} |
|
73 |
if (preventMarker) { |
|
74 |
cs.addStyle(new KmlIconStyle()); |
|
75 |
} |
|
76 |
cs.setId(Integer.toString(i)); |
|
77 |
resp.put(s, cs); |
|
78 |
} |
|
79 |
} |
|
80 |
} else { |
|
81 |
DisposableIterator diter = null; |
|
82 |
diter = fset.fastIterator(); |
|
83 |
Feature feat = null; |
|
84 |
int ind = 0; |
|
85 |
while (diter.hasNext()) { |
|
86 |
feat = (Feature) diter.next(); |
|
87 |
ISymbol s = leg.getSymbolByFeature(feat); |
|
88 |
KmlStyle style = getStyle(s); |
|
89 |
if (s != null && style != null) { |
|
90 |
preventMarker = !(style instanceof KmlIconStyle); |
|
91 |
KmlCompoundStyle cs = new KmlCompoundStyle(); |
|
92 |
cs.addStyle(style); |
|
93 |
if (balloon) { |
|
94 |
// Not used, see javadoc of KmlBalloonStyle |
|
95 |
// cs.addStyle(new KmlBalloonStyle(flds, "bs" + Integer.toString(ind))); |
|
96 |
} |
|
97 |
if (preventMarker) { |
|
98 |
cs.addStyle(new KmlIconStyle()); |
|
99 |
} |
|
100 |
cs.setId(Integer.toString(ind)); |
|
101 |
resp.put(s, cs); |
|
102 |
ind++; |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
return resp; |
|
108 |
} |
|
109 |
|
|
110 |
/** |
|
111 |
* Returns null if it cannot be rendered in KML |
|
112 |
* |
|
113 |
* @param s |
|
114 |
* @return |
|
115 |
*/ |
|
116 |
public static KmlStyle getStyle(ISymbol s) { |
|
117 |
|
|
118 |
if (s == null) { |
|
119 |
return null; |
|
120 |
} |
|
121 |
|
|
122 |
if (s instanceof IPictureMarkerSymbol) { |
|
123 |
IPictureMarkerSymbol pms = (IPictureMarkerSymbol) s; |
|
124 |
String str = pms.getSource().toString(); |
|
125 |
if (str != null && str.indexOf("http") == 0) { |
|
126 |
KmlIconStyle icoStyle = new KmlIconStyle(); |
|
127 |
icoStyle.setColor(((IMarkerSymbol) s).getColor()); |
|
128 |
icoStyle.setHref(str); |
|
129 |
float angKml = (float) radToKmlDeg(((IMarkerSymbol) s).getRotation()); |
|
130 |
icoStyle.setHeading(angKml); |
|
131 |
return icoStyle; |
|
132 |
} else { |
|
133 |
// Simple marker not supported |
|
134 |
return null; |
|
135 |
} |
|
136 |
} |
|
137 |
|
|
138 |
if (s instanceof ILineSymbol) { |
|
139 |
ILineSymbol lSym = (ILineSymbol) s; |
|
140 |
KmlLineStyle lStyle = new KmlLineStyle(); |
|
141 |
lStyle.setColor(lSym.getColor()); |
|
142 |
lStyle.setWidth((float) lSym.getLineWidth()); |
|
143 |
return lStyle; |
|
144 |
} |
|
145 |
|
|
146 |
if (s instanceof IFillSymbol) { |
|
147 |
|
|
148 |
IFillSymbol fSym = (IFillSymbol) s; |
|
149 |
KmlCompoundStyle cStyle = new KmlCompoundStyle(); |
|
150 |
KmlLineStyle lStyle = new KmlLineStyle(); |
|
151 |
ILineSymbol lSym = fSym.getOutline(); |
|
152 |
lStyle.setColor(lSym.getColor()); |
|
153 |
lStyle.setWidth((float) lSym.getLineWidth()); |
|
154 |
|
|
155 |
KmlPolygonStyle fStyle = new KmlPolygonStyle(); |
|
156 |
fStyle.setColor(fSym.getFillColor()); |
|
157 |
fStyle.setOutline(fSym.hasOutline()); |
|
158 |
fStyle.setFill(fSym.hasFill()); |
|
159 |
|
|
160 |
cStyle.addStyle(lStyle); |
|
161 |
cStyle.addStyle(fStyle); |
|
162 |
return cStyle; |
|
163 |
} |
|
164 |
// Not supported |
|
165 |
return null; |
|
166 |
} |
|
167 |
|
|
168 |
|
|
169 |
public static double kmlDegToRad(double angleDegree) { |
|
170 |
double deg = 90 - angleDegree; |
|
171 |
return Math.toRadians(deg); |
|
172 |
} |
|
173 |
|
|
174 |
public static double radToKmlDeg(double rad) { |
|
175 |
double angleDegree = Math.toDegrees(rad); |
|
176 |
double deg = 90 - angleDegree; |
|
177 |
if (deg >= 360) { |
|
178 |
deg = deg - 360; |
|
179 |
} |
|
180 |
if (deg < 0) { |
|
181 |
deg = deg + 360; |
|
182 |
} |
|
183 |
return deg; |
|
184 |
} |
|
185 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlColorStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.awt.Color; |
|
4 |
|
|
5 |
public abstract class KmlColorStyle extends KmlStyle { |
|
6 |
protected Color color = new Color(220, 10, 20, 80); |
|
7 |
|
|
8 |
public Color getColor() { |
|
9 |
return color; |
|
10 |
} |
|
11 |
|
|
12 |
public void setColor(Color color) { |
|
13 |
this.color = color; |
|
14 |
} |
|
15 |
} |
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlLabelStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags; |
|
6 |
import org.gvsig.gpe.prov.kml.utils.KmlUtilsParser; |
|
7 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
8 |
|
|
9 |
/** |
|
10 |
* @author Francisco Jos? Pe?arrubia (fjp@scolab.es) |
|
11 |
* |
|
12 |
* Labels are exported as features. Google Earth will render the attribute <name> |
|
13 |
* with this color. |
|
14 |
*/ |
|
15 |
public class KmlLabelStyle extends KmlColorStyle { |
|
16 |
|
|
17 |
protected float scale = 1; |
|
18 |
|
|
19 |
public float getScale() { |
|
20 |
return scale; |
|
21 |
} |
|
22 |
|
|
23 |
public void setScale(float scale) { |
|
24 |
this.scale = scale; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public void writeXml(IXmlStreamWriter writer) throws IOException { |
|
29 |
writer.writeStartElement(Kml2_1_Tags.LABEL_STYLE); |
|
30 |
|
|
31 |
writer.writeStartElement(Kml2_1_Tags.COLOR); |
|
32 |
writer.writeValue(KmlUtilsParser.fromColorToABGR(color)); |
|
33 |
writer.writeEndElement(); |
|
34 |
if (scale != 1) { |
|
35 |
writer.writeStartElement(Kml2_1_Tags.SCALE); |
|
36 |
writer.writeValue(scale); |
|
37 |
writer.writeEndElement(); |
|
38 |
} |
|
39 |
|
|
40 |
writer.writeEndElement(); |
|
41 |
|
|
42 |
|
|
43 |
} |
|
44 |
|
|
45 |
} |
|
46 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
6 |
|
|
7 |
/** |
|
8 |
* @author Francisco Jos? Pe?arrubia (fjp@scolab.es) |
|
9 |
* |
|
10 |
* Useful to extend it and support writing styles and exchanging info from and to |
|
11 |
* outside of the library (in order to avoid dependencies) |
|
12 |
*/ |
|
13 |
public abstract class KmlStyle { |
|
14 |
protected String id; |
|
15 |
|
|
16 |
public String getId() { |
|
17 |
return id; |
|
18 |
} |
|
19 |
|
|
20 |
public void setId(String id) { |
|
21 |
this.id = id; |
|
22 |
} |
|
23 |
|
|
24 |
public abstract void writeXml(IXmlStreamWriter writer) throws IOException; |
|
25 |
|
|
26 |
|
|
27 |
} |
|
28 |
|
org.gvsig.gpe/library/tags/org.gvsig.gpe-2.1.41/org.gvsig.gpe.exportto/org.gvsig.gpe.exportto.kml/src/main/java/org/gvsig/gpe/exportto/kml/style/KmlCompoundStyle.java | ||
---|---|---|
1 |
package org.gvsig.gpe.exportto.kml.style; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter; |
|
8 |
|
|
9 |
public class KmlCompoundStyle extends KmlStyle { |
|
10 |
|
|
11 |
protected KmlIconStyle iconStyle = null; |
|
12 |
protected KmlLineStyle lineStyle = null; |
|
13 |
protected KmlPolygonStyle polygonStyle = null; |
|
14 |
protected KmlLabelStyle labelStyle = null; |
|
15 |
// protected KmlBalloonStyle balloonStyle = null; |
|
16 |
|
|
17 |
|
|
18 |
public void writeXml(IXmlStreamWriter writer) throws IOException { |
|
19 |
if (iconStyle != null) |
|
20 |
iconStyle.writeXml(writer); |
|
21 |
if (lineStyle != null) |
|
22 |
lineStyle.writeXml(writer); |
|
23 |
if (polygonStyle != null) |
|
24 |
polygonStyle.writeXml(writer); |
|
25 |
if (labelStyle != null) |
|
26 |
labelStyle.writeXml(writer); |
|
27 |
/* |
|
28 |
if (balloonStyle != null) |
|
29 |
balloonStyle.writeXml(writer); |
|
30 |
*/ |
|
31 |
} |
|
32 |
|
|
33 |
public void addStyles(List<KmlStyle> list) { |
|
34 |
|
|
35 |
if (list != null) { |
|
36 |
for (int i=0; i<list.size(); i++) { |
|
37 |
addStyle(list.get(i)); |
Also available in: Unified diff