13 |
13 |
import java.util.Iterator;
|
14 |
14 |
import java.util.List;
|
15 |
15 |
import java.util.Map;
|
16 |
|
import java.util.logging.Level;
|
17 |
16 |
import org.cresques.cts.ICoordTrans;
|
18 |
17 |
import org.cresques.cts.IProjection;
|
19 |
18 |
import org.gvsig.export.ExportException;
|
... | ... | |
25 |
24 |
import org.gvsig.fmap.dal.DALLocator;
|
26 |
25 |
import org.gvsig.fmap.dal.DataManager;
|
27 |
26 |
import org.gvsig.fmap.dal.DataServerExplorer;
|
28 |
|
import org.gvsig.fmap.dal.DataStoreParameters;
|
29 |
27 |
import org.gvsig.fmap.dal.NewDataStoreParameters;
|
30 |
28 |
import org.gvsig.fmap.dal.OpenDataStoreParameters;
|
31 |
29 |
import org.gvsig.fmap.dal.exception.DataException;
|
32 |
|
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
|
33 |
30 |
import org.gvsig.fmap.dal.feature.Feature;
|
34 |
31 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
|
35 |
32 |
import org.gvsig.fmap.dal.feature.FeatureSet;
|
... | ... | |
38 |
35 |
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
|
39 |
36 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
|
40 |
37 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
|
41 |
|
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
|
42 |
38 |
import org.gvsig.fmap.geom.DataTypes;
|
43 |
39 |
import org.gvsig.fmap.geom.Geometry;
|
44 |
40 |
import org.gvsig.fmap.geom.GeometryLocator;
|
... | ... | |
47 |
43 |
import org.gvsig.fmap.geom.primitive.Envelope;
|
48 |
44 |
import org.gvsig.fmap.mapcontext.MapContextException;
|
49 |
45 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
|
50 |
|
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
|
51 |
46 |
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
|
52 |
47 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
|
53 |
48 |
import org.gvsig.gpe.exportto.generic.util.CoordinatesSequenceBbox;
|
... | ... | |
59 |
54 |
import org.gvsig.gpe.lib.api.writer.IGPEWriterHandlerImplementor;
|
60 |
55 |
import org.gvsig.gpe.prov.kml.utils.Kml2_1_Tags;
|
61 |
56 |
import org.gvsig.gpe.prov.kml.writer.GPEKmlWriterHandlerImplementor;
|
62 |
|
import org.gvsig.tools.ToolsLocator;
|
63 |
57 |
import org.gvsig.tools.dispose.DisposableIterator;
|
|
58 |
import org.gvsig.tools.locator.LocatorException;
|
64 |
59 |
import org.gvsig.tools.util.HasAFile;
|
65 |
60 |
import org.gvsig.xmlpull.lib.api.stream.IXmlStreamWriter;
|
66 |
61 |
import org.slf4j.Logger;
|
... | ... | |
71 |
66 |
* @author osc
|
72 |
67 |
*/
|
73 |
68 |
public class ExportKMLService extends AbstractExportService
|
74 |
|
implements ExportService {
|
|
69 |
implements ExportService {
|
75 |
70 |
|
76 |
|
private static Logger logger = LoggerFactory.getLogger(ExportKMLService.class);
|
|
71 |
private static Logger logger = LoggerFactory.getLogger(ExportKMLService.class);
|
77 |
72 |
|
78 |
|
ExportKMLService(ExportServiceFactory factory, ExportParameters parameters) {
|
79 |
|
super(factory, parameters);
|
80 |
|
}
|
|
73 |
ExportKMLService(ExportServiceFactory factory, ExportParameters parameters) {
|
|
74 |
super(factory, parameters);
|
|
75 |
}
|
81 |
76 |
|
82 |
|
@Override
|
83 |
|
protected DataServerExplorer createServerExplorer() throws ExportException {
|
|
77 |
@Override
|
|
78 |
protected DataServerExplorer createServerExplorer() throws ExportException {
|
84 |
79 |
|
85 |
|
DataManager dataManager = DALLocator.getDataManager();
|
|
80 |
DataManager dataManager = DALLocator.getDataManager();
|
86 |
81 |
|
87 |
|
FilesystemServerExplorerParameters explorerParams;
|
88 |
|
try {
|
89 |
|
explorerParams
|
90 |
|
= (FilesystemServerExplorerParameters) dataManager
|
91 |
|
.createServerExplorerParameters(FilesystemServerExplorer.NAME);
|
92 |
|
} catch (Exception e) {
|
93 |
|
throw new ExportException(e);
|
94 |
|
}
|
|
82 |
FilesystemServerExplorerParameters explorerParams;
|
|
83 |
try {
|
|
84 |
explorerParams
|
|
85 |
= (FilesystemServerExplorerParameters) dataManager
|
|
86 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME);
|
|
87 |
} catch (Exception e) {
|
|
88 |
throw new ExportException(e);
|
|
89 |
}
|
95 |
90 |
|
96 |
|
explorerParams.setRoot(this.getParameters().getFile().getParent());
|
|
91 |
explorerParams.setRoot(this.getParameters().getFile().getParent());
|
97 |
92 |
|
98 |
|
FilesystemServerExplorer explorer;
|
99 |
|
try {
|
100 |
|
explorer = (FilesystemServerExplorer) dataManager.openServerExplorer(
|
101 |
|
"FilesystemExplorer", explorerParams
|
102 |
|
);
|
103 |
|
return explorer;
|
104 |
|
} catch (Exception e) {
|
105 |
|
throw new ExportException(e);
|
106 |
|
}
|
107 |
|
}
|
|
93 |
FilesystemServerExplorer explorer;
|
|
94 |
try {
|
|
95 |
explorer = (FilesystemServerExplorer) dataManager.openServerExplorer(
|
|
96 |
"FilesystemExplorer", explorerParams
|
|
97 |
);
|
|
98 |
return explorer;
|
|
99 |
} catch (Exception e) {
|
|
100 |
throw new ExportException(e);
|
|
101 |
}
|
|
102 |
}
|
108 |
103 |
|
109 |
|
@Override
|
110 |
|
protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException {
|
111 |
|
try {
|
112 |
|
FilesystemServerExplorer explorer = (FilesystemServerExplorer) this.createServerExplorer();
|
113 |
|
NewFeatureStoreParameters newStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters(
|
114 |
|
this.getParameters().getFile()
|
115 |
|
);
|
116 |
|
newStoreParameters.setDynValue("CRS", this.getParameters().getTargetProjection());
|
117 |
|
// Usamos el featureType por defecto del KML
|
118 |
|
return newStoreParameters;
|
119 |
|
} catch (DataException ex) {
|
120 |
|
throw new ExportException(ex);
|
121 |
|
}
|
122 |
|
}
|
|
104 |
@Override
|
|
105 |
protected NewDataStoreParameters createTargetNewStoreParameters() throws ExportException {
|
|
106 |
try {
|
|
107 |
FilesystemServerExplorer explorer = (FilesystemServerExplorer) this.createServerExplorer();
|
|
108 |
NewFeatureStoreParameters newStoreParameters = (NewFeatureStoreParameters) explorer.getAddParameters(
|
|
109 |
this.getParameters().getFile()
|
|
110 |
);
|
|
111 |
newStoreParameters.setDynValue("CRS", this.getParameters().getTargetProjection());
|
|
112 |
// Usamos el featureType por defecto del KML
|
|
113 |
return newStoreParameters;
|
|
114 |
} catch (DataException ex) {
|
|
115 |
throw new ExportException(ex);
|
|
116 |
}
|
|
117 |
}
|
123 |
118 |
|
124 |
|
@Override
|
125 |
|
protected OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException {
|
126 |
|
try {
|
127 |
|
DataManager dataManager = DALLocator.getDataManager();
|
128 |
|
OpenFeatureStoreParameters openStoreParameters = (OpenFeatureStoreParameters) dataManager.createStoreParameters("GPE");
|
129 |
|
((HasAFile) openStoreParameters).setFile(getParameters().getFile());
|
130 |
|
openStoreParameters.setDynValue("CRS", this.getParameters().getTargetProjection());
|
131 |
|
return openStoreParameters;
|
132 |
|
} catch (DataException ex) {
|
133 |
|
throw new ExportException(ex);
|
134 |
|
}
|
135 |
|
}
|
|
119 |
@Override
|
|
120 |
protected OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException {
|
|
121 |
try {
|
|
122 |
DataManager dataManager = DALLocator.getDataManager();
|
|
123 |
OpenFeatureStoreParameters openStoreParameters = (OpenFeatureStoreParameters) dataManager.createStoreParameters("GPE");
|
|
124 |
((HasAFile) openStoreParameters).setFile(getParameters().getFile());
|
|
125 |
openStoreParameters.setDynValue("CRS", this.getParameters().getTargetProjection());
|
|
126 |
return openStoreParameters;
|
|
127 |
} catch (DataException ex) {
|
|
128 |
throw new ExportException(ex);
|
|
129 |
}
|
|
130 |
}
|
136 |
131 |
|
137 |
|
@Override
|
138 |
|
public ExportKMLParameters getParameters() {
|
139 |
|
return (ExportKMLParameters) super.getParameters();
|
140 |
|
}
|
|
132 |
@Override
|
|
133 |
public ExportKMLParameters getParameters() {
|
|
134 |
return (ExportKMLParameters) super.getParameters();
|
|
135 |
}
|
141 |
136 |
|
142 |
|
@Override
|
143 |
|
public void export(
|
144 |
|
FeatureSet featureSet) throws ExportException {
|
145 |
|
DataServerExplorer explorer = this.createServerExplorer();
|
146 |
|
NewFeatureStoreParameters params = (NewFeatureStoreParameters) this.createTargetNewStoreParameters();
|
147 |
|
|
148 |
|
String providerName = params.getDataStoreName();
|
149 |
|
String explorerName = explorer.getProviderName();
|
150 |
|
|
151 |
|
File outFile = this.getParameters().getFile(); //kmlFile;
|
152 |
|
FeatureSet featureStore = featureSet;
|
153 |
|
|
154 |
|
|
155 |
|
FLyrVect vectorLayer = null;
|
156 |
|
String mimeType = this.getParameters().getMimeType(); //mtype;
|
157 |
|
boolean useLabels = this.getParameters().getUseLabels(); //doLabels;
|
158 |
|
boolean attsAsBalloon = this.getParameters().getAttsAsBalloon();
|
159 |
|
boolean reprojectTo4326 = this.getParameters().getReprojectTo4326();
|
160 |
|
ArrayList<ICoordTrans> ICoordTrans;
|
|
137 |
@Override
|
|
138 |
public void export(
|
|
139 |
FeatureSet featureSet) throws ExportException {
|
|
140 |
DataServerExplorer explorer = this.createServerExplorer();
|
|
141 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) this.createTargetNewStoreParameters();
|
161 |
142 |
|
162 |
|
List<ICoordTrans> transfList = new ArrayList<ICoordTrans>();
|
|
143 |
String providerName = params.getDataStoreName();
|
|
144 |
String explorerName = explorer.getProviderName();
|
163 |
145 |
|
164 |
|
ICoordTrans ct = this.getParameters().getSourceTransformation(); //vlayer.getCoordTrans();
|
165 |
|
IProjection targetproj;
|
166 |
|
if (reprojectTo4326) {
|
167 |
|
targetproj = CRSFactory.getCRS("EPSG:4326");
|
168 |
|
if (ct == null) {
|
169 |
|
transfList.add(this.getParameters().getSourceProjection().getCT(targetproj));
|
170 |
|
} else {
|
171 |
|
transfList.add(ct);
|
172 |
|
transfList.add(ct.getPDest().getCT(targetproj));
|
173 |
|
}
|
174 |
|
} else {
|
175 |
|
if (ct == null) {
|
176 |
|
targetproj = this.getParameters().getSourceProjection();
|
177 |
|
} else {
|
178 |
|
targetproj = ct.getPDest();
|
179 |
|
transfList.add(ct);
|
180 |
|
}
|
181 |
|
}
|
|
146 |
File outFile = this.getParameters().getFile(); //kmlFile;
|
|
147 |
FeatureSet featureStore = featureSet;
|
182 |
148 |
|
183 |
|
IGPEWriterHandler wh = null;
|
184 |
|
FileOutputStream fos = null;
|
185 |
|
String srs = targetproj.getAbrev();
|
|
149 |
FLyrVect vectorLayer = null;
|
|
150 |
String mimeType = this.getParameters().getMimeType(); //mtype;
|
|
151 |
boolean useLabels = this.getParameters().getUseLabels(); //doLabels;
|
|
152 |
boolean attsAsBalloon = this.getParameters().getAttsAsBalloon();
|
|
153 |
ArrayList<ICoordTrans> ICoordTrans;
|
186 |
154 |
|
187 |
|
String[] fldNames = null;
|
188 |
|
Envelope env = null;
|
189 |
|
long count = 0;
|
|
155 |
List<ICoordTrans> transfList = new ArrayList<ICoordTrans>();
|
190 |
156 |
|
191 |
|
try {
|
192 |
|
count = featureSet.getSize();
|
193 |
|
this.getTaskStatus().setRangeOfValues(0, count);
|
194 |
|
fldNames = getAttributes(featureStore.getDefaultFeatureType());
|
195 |
|
//env = featureStore.getEnvelope();
|
196 |
|
env = GeometryLocator.getGeometryManager().createEnvelope(Geometry.SUBTYPES.GEOM2D);
|
197 |
|
} catch (Exception e) {
|
198 |
|
throw new ExportException(e);
|
199 |
|
}
|
|
157 |
ICoordTrans ct = this.getParameters().getSourceTransformation(); //vlayer.getCoordTrans();
|
|
158 |
IProjection targetproj = this.getParameters().getTargetProjection();
|
|
159 |
// if (reprojectTo4326) {
|
|
160 |
// 8
|
|
161 |
// targetproj = CRSFactory.getCRS("EPSG:4326");
|
|
162 |
// if (ct == null) {
|
|
163 |
// transfList.add(this.getParameters().getSourceProjection().getCT(targetproj));
|
|
164 |
// } else {
|
|
165 |
// transfList.add(ct);
|
|
166 |
// transfList.add(ct.getPDest().getCT(targetproj));
|
|
167 |
// }
|
|
168 |
// } else {
|
|
169 |
if (ct == null) {
|
|
170 |
if (targetproj == null) {
|
|
171 |
targetproj = this.getParameters().getSourceProjection();
|
|
172 |
}
|
|
173 |
ct = this.getParameters().getSourceProjection().getCT(targetproj);
|
|
174 |
transfList.add(ct);
|
|
175 |
} else {
|
|
176 |
targetproj = ct.getPDest();
|
|
177 |
transfList.add(ct);
|
|
178 |
}
|
200 |
179 |
|
201 |
|
try {
|
202 |
|
env = reproject(env, transfList);
|
203 |
|
} catch (CreateEnvelopeException ex) {
|
204 |
|
throw new ExportException(ex);
|
205 |
|
}
|
|
180 |
IGPEWriterHandler wh = null;
|
|
181 |
FileOutputStream fos = null;
|
|
182 |
String srs = targetproj.getAbrev();
|
206 |
183 |
|
207 |
|
Rectangle2D rect = new Rectangle2D.Double(
|
208 |
|
env.getMinimum(0),
|
209 |
|
env.getMinimum(1),
|
210 |
|
env.getLength(0),
|
211 |
|
env.getLength(1));
|
|
184 |
String[] fldNames = null;
|
|
185 |
Envelope env = null;
|
|
186 |
long count = 0;
|
|
187 |
FeatureType ftype;
|
212 |
188 |
|
213 |
|
File fixedFile = outFile;
|
214 |
|
try {
|
|
189 |
try {
|
|
190 |
count = featureSet.getSize();
|
|
191 |
this.getTaskStatus().setRangeOfValues(0, count);
|
|
192 |
if (this.getParameters().getExportAttributes().isActive()) {
|
|
193 |
ftype = this.getParameters().getExportAttributes().getTargetFeatureType();
|
|
194 |
} else {
|
|
195 |
ftype = featureStore.getDefaultFeatureType();
|
|
196 |
}
|
|
197 |
fldNames = getAttributes(ftype);
|
|
198 |
env = GeometryLocator.getGeometryManager().createEnvelope(Geometry.SUBTYPES.GEOM2D);
|
|
199 |
this.initEnvelope(env, featureSet);
|
|
200 |
} catch (DataException | CreateEnvelopeException | LocatorException e) {
|
|
201 |
throw new ExportException(e);
|
|
202 |
}
|
215 |
203 |
|
216 |
|
if (!outFile.getAbsolutePath().toLowerCase().endsWith(
|
217 |
|
"." + this.getFileExtension().toLowerCase())) {
|
|
204 |
try {
|
|
205 |
env = reproject(env, transfList);
|
|
206 |
} catch (CreateEnvelopeException ex) {
|
|
207 |
throw new ExportException(ex);
|
|
208 |
}
|
218 |
209 |
|
219 |
|
fixedFile = new File(outFile.getAbsolutePath() + "." + getFileExtension());
|
220 |
|
}
|
|
210 |
Rectangle2D rect = new Rectangle2D.Double(
|
|
211 |
env.getMinimum(0),
|
|
212 |
env.getMinimum(1),
|
|
213 |
env.getLength(0),
|
|
214 |
env.getLength(1));
|
221 |
215 |
|
222 |
|
wh = GPELocator.getGPEManager().createWriterByMimeType(mimeType);
|
223 |
|
fos = new FileOutputStream(fixedFile);
|
|
216 |
File fixedFile = outFile;
|
|
217 |
try {
|
224 |
218 |
|
225 |
|
wh.setOutputStream(fos);
|
226 |
|
wh.initialize();
|
227 |
|
// ==========================================
|
228 |
|
wh.startLayer(null, null, fixedFile.getName(), null, srs);
|
229 |
|
// ============== Styles =============
|
230 |
|
Map<ISymbol, KmlStyle> symsty = null;
|
231 |
|
IXmlStreamWriter xmlw = getXmlStreamWriter(wh);
|
232 |
|
if (true && xmlw != null && vectorLayer instanceof FLyrVect) {
|
233 |
|
symsty = StyleUtils.getSymbolStyles((FLyrVect) vectorLayer,
|
234 |
|
featureSet,
|
235 |
|
attsAsBalloon,
|
236 |
|
fldNames);
|
237 |
|
Iterator<KmlStyle> iter = symsty.values().iterator();
|
238 |
|
KmlStyle sty = null;
|
239 |
|
while (iter.hasNext()) {
|
240 |
|
sty = iter.next();
|
241 |
|
writeStyle(xmlw, sty);
|
242 |
|
}
|
243 |
|
}
|
244 |
|
// ==========================================
|
245 |
|
wh.startBbox(null, new CoordinatesSequenceBbox(rect), srs);
|
246 |
|
wh.endBbox();
|
247 |
|
// ============= Writing feature ============
|
248 |
|
IVectorLegend legend;
|
249 |
|
if (vectorLayer instanceof FLyrVect) {
|
250 |
|
legend = (IVectorLegend)((FLyrVect) vectorLayer).getLegend();
|
251 |
|
} else {
|
252 |
|
legend = null;
|
253 |
|
}
|
254 |
|
writeFeatures(wh, xmlw, featureSet, fldNames, symsty, legend, transfList);
|
|
219 |
if (!outFile.getAbsolutePath().toLowerCase().endsWith(
|
|
220 |
"." + this.getFileExtension().toLowerCase())) {
|
255 |
221 |
|
|
222 |
fixedFile = new File(outFile.getAbsolutePath() + "." + getFileExtension());
|
|
223 |
}
|
|
224 |
|
|
225 |
wh = GPELocator.getGPEManager().createWriterByMimeType(mimeType);
|
|
226 |
fos = new FileOutputStream(fixedFile);
|
|
227 |
|
|
228 |
wh.setOutputStream(fos);
|
|
229 |
wh.initialize();
|
|
230 |
// ==========================================
|
|
231 |
wh.startLayer(null, null, fixedFile.getName(), null, srs);
|
|
232 |
// ============== Styles =============
|
|
233 |
Map<ISymbol, KmlStyle> symsty = null;
|
|
234 |
IXmlStreamWriter xmlw = getXmlStreamWriter(wh);
|
|
235 |
if (true && xmlw != null && vectorLayer instanceof FLyrVect) {
|
|
236 |
symsty = StyleUtils.getSymbolStyles((FLyrVect) vectorLayer,
|
|
237 |
featureSet,
|
|
238 |
attsAsBalloon,
|
|
239 |
fldNames);
|
|
240 |
Iterator<KmlStyle> iter = symsty.values().iterator();
|
|
241 |
KmlStyle sty = null;
|
|
242 |
while (iter.hasNext()) {
|
|
243 |
sty = iter.next();
|
|
244 |
writeStyle(xmlw, sty);
|
|
245 |
}
|
|
246 |
}
|
|
247 |
// ==========================================
|
|
248 |
wh.startBbox(null, new CoordinatesSequenceBbox(rect), srs);
|
|
249 |
wh.endBbox();
|
|
250 |
// ============= Writing feature ============
|
|
251 |
IVectorLegend legend;
|
|
252 |
if (vectorLayer instanceof FLyrVect) {
|
|
253 |
legend = (IVectorLegend) ((FLyrVect) vectorLayer).getLegend();
|
|
254 |
} else {
|
|
255 |
legend = null;
|
|
256 |
}
|
|
257 |
writeFeatures(wh, xmlw, featureSet, fldNames, symsty, legend, transfList);
|
|
258 |
|
256 |
259 |
// writeFeatures(wh, xmlw, featureSet, fldNames, symsty,
|
257 |
260 |
// (IVectorLegend) vectorLayer.getLegend());
|
|
261 |
// ==========================================
|
|
262 |
wh.endLayer();
|
|
263 |
// ==========================================
|
|
264 |
wh.close();
|
|
265 |
fos.close();
|
|
266 |
} catch (Exception exc) {
|
|
267 |
throw new ExportException(exc);
|
|
268 |
}
|
258 |
269 |
|
259 |
|
// ==========================================
|
260 |
|
wh.endLayer();
|
261 |
|
// ==========================================
|
262 |
|
wh.close();
|
263 |
|
fos.close();
|
264 |
|
} catch (Exception exc) {
|
265 |
|
throw new ExportException(exc);
|
266 |
|
}
|
267 |
|
|
268 |
|
this.getTaskStatus().setCurValue(count);
|
|
270 |
this.getTaskStatus().setCurValue(count);
|
269 |
271 |
// this.finishAction(fixedFile, targetproj);
|
270 |
|
this.getTaskStatus().terminate();
|
271 |
|
this.getTaskStatus().remove();
|
|
272 |
this.getTaskStatus().terminate();
|
|
273 |
this.getTaskStatus().remove();
|
272 |
274 |
|
273 |
|
}
|
|
275 |
}
|
274 |
276 |
|
275 |
|
private String[] getAttributes(FeatureType ftype) {
|
|
277 |
private String[] getAttributes(FeatureType ftype) {
|
276 |
278 |
|
277 |
|
FeatureAttributeDescriptor[] atts = ftype.getAttributeDescriptors();
|
278 |
|
FeatureAttributeDescriptor desc = null;
|
279 |
|
List<String> list = new ArrayList<String>();
|
280 |
|
for (int i = 0; i < atts.length; i++) {
|
281 |
|
desc = atts[i];
|
282 |
|
if (desc.getDataType().getType() != DataTypes.GEOMETRY) {
|
283 |
|
list.add(desc.getName());
|
284 |
|
}
|
285 |
|
}
|
286 |
|
return list.toArray(new String[0]);
|
287 |
|
}
|
|
279 |
FeatureAttributeDescriptor[] atts = ftype.getAttributeDescriptors();
|
288 |
280 |
|
289 |
|
private Geometry reproject(Geometry geom, List<ICoordTrans> transfList) {
|
|
281 |
FeatureAttributeDescriptor desc = null;
|
|
282 |
List<String> list = new ArrayList<String>();
|
|
283 |
for (int i = 0; i < atts.length; i++) {
|
|
284 |
desc = atts[i];
|
|
285 |
if (desc.getDataType().getType() != DataTypes.GEOMETRY) {
|
|
286 |
list.add(desc.getName());
|
|
287 |
}
|
|
288 |
}
|
|
289 |
return list.toArray(new String[0]);
|
|
290 |
}
|
290 |
291 |
|
291 |
|
int sz = transfList.size();
|
292 |
|
if (sz == 0) {
|
293 |
|
return geom;
|
294 |
|
} else {
|
295 |
|
Geometry resp = geom.cloneGeometry();
|
296 |
|
for (int i = 0; i < sz; i++) {
|
297 |
|
resp.reProject(transfList.get(i));
|
298 |
|
}
|
299 |
|
return resp;
|
300 |
|
}
|
|
292 |
private Geometry reproject(Geometry geom, List<ICoordTrans> transfList) {
|
301 |
293 |
|
302 |
|
}
|
|
294 |
int sz = transfList.size();
|
|
295 |
if (sz == 0) {
|
|
296 |
return geom;
|
|
297 |
} else {
|
|
298 |
Geometry resp = geom.cloneGeometry();
|
|
299 |
for (int i = 0; i < sz; i++) {
|
|
300 |
resp.reProject(transfList.get(i));
|
|
301 |
}
|
|
302 |
return resp;
|
|
303 |
}
|
303 |
304 |
|
304 |
|
public String getFileExtension() {
|
305 |
|
return "kml";
|
306 |
|
}
|
|
305 |
}
|
307 |
306 |
|
|
307 |
public String getFileExtension() {
|
|
308 |
return "kml";
|
|
309 |
}
|
|
310 |
|
308 |
311 |
// private void finishAction(File kmlfile, IProjection proj)
|
309 |
312 |
// throws ExportException {
|
310 |
313 |
//
|
... | ... | |
337 |
340 |
// exporttoServiceFinishAction.finished(kmlfile.getName(), dsp);
|
338 |
341 |
// }
|
339 |
342 |
// }
|
|
343 |
private void writeStyle(IXmlStreamWriter xmlw, KmlStyle sty) throws IOException {
|
340 |
344 |
|
341 |
|
private void writeStyle(IXmlStreamWriter xmlw, KmlStyle sty) throws IOException {
|
|
345 |
xmlw.writeStartElement(Kml2_1_Tags.STYLE);
|
|
346 |
xmlw.writeStartAttribute(Kml2_1_Tags.ID);
|
|
347 |
xmlw.writeValue(sty.getId());
|
|
348 |
xmlw.writeEndAttributes();
|
|
349 |
sty.writeXml(xmlw);
|
|
350 |
xmlw.writeEndElement();
|
|
351 |
}
|
342 |
352 |
|
343 |
|
xmlw.writeStartElement(Kml2_1_Tags.STYLE);
|
344 |
|
xmlw.writeStartAttribute(Kml2_1_Tags.ID);
|
345 |
|
xmlw.writeValue(sty.getId());
|
346 |
|
xmlw.writeEndAttributes();
|
347 |
|
sty.writeXml(xmlw);
|
348 |
|
xmlw.writeEndElement();
|
349 |
|
}
|
350 |
|
|
351 |
|
private IXmlStreamWriter getXmlStreamWriter(IGPEWriterHandler wh) {
|
|
353 |
private IXmlStreamWriter getXmlStreamWriter(IGPEWriterHandler wh) {
|
352 |
354 |
|
353 |
|
IGPEWriterHandlerImplementor imple = wh.getImplementor();
|
354 |
|
if (!(imple instanceof GPEKmlWriterHandlerImplementor)) {
|
355 |
|
/*
|
|
355 |
IGPEWriterHandlerImplementor imple = wh.getImplementor();
|
|
356 |
if (!(imple instanceof GPEKmlWriterHandlerImplementor)) {
|
|
357 |
/*
|
356 |
358 |
* Unexpected class
|
357 |
|
*/
|
358 |
|
return null;
|
359 |
|
}
|
360 |
|
GPEKmlWriterHandlerImplementor kmlimple = null;
|
361 |
|
kmlimple = (GPEKmlWriterHandlerImplementor) imple;
|
362 |
|
IXmlStreamWriter xmlw = kmlimple.getXMLStreamWriter();
|
363 |
|
return xmlw;
|
364 |
|
}
|
365 |
|
|
366 |
|
private void writeFeatures(
|
367 |
|
IGPEWriterHandler gwh,
|
368 |
|
IXmlStreamWriter xmlw,
|
369 |
|
FeatureSet fset,
|
370 |
|
String[] fieldNames,
|
371 |
|
Map<ISymbol, KmlStyle> symsty,
|
372 |
|
IVectorLegend lege,
|
373 |
|
List<ICoordTrans> transfList) throws Exception {
|
|
359 |
*/
|
|
360 |
return null;
|
|
361 |
}
|
|
362 |
GPEKmlWriterHandlerImplementor kmlimple = null;
|
|
363 |
kmlimple = (GPEKmlWriterHandlerImplementor) imple;
|
|
364 |
IXmlStreamWriter xmlw = kmlimple.getXMLStreamWriter();
|
|
365 |
return xmlw;
|
|
366 |
}
|
374 |
367 |
|
375 |
|
GeometryToGPEWriter gw = new GeometryToGPEWriter(gwh);
|
376 |
|
DisposableIterator diter = fset.fastIterator();
|
377 |
|
Feature feat = null;
|
378 |
|
long count = 0;
|
379 |
|
this.getTaskStatus().setCurValue(count);
|
380 |
|
ISymbol sym = null;
|
381 |
|
int nullGeometries = 0;
|
382 |
|
while (diter.hasNext()) {
|
383 |
|
feat = (Feature) diter.next();
|
384 |
|
try {
|
385 |
|
if (lege instanceof IVectorLegend) {
|
386 |
|
sym = lege.getSymbolByFeature(feat);
|
387 |
|
}
|
388 |
|
} catch (MapContextException mce) {
|
389 |
|
logger.info("While getting legend symbol.", mce);
|
390 |
|
}
|
391 |
|
KmlStyle kmlStyle;
|
392 |
|
try {
|
393 |
|
kmlStyle = symsty.get(sym);
|
394 |
|
} catch (Exception e) {
|
395 |
|
kmlStyle = null;
|
396 |
|
}
|
|
368 |
private void writeFeatures(
|
|
369 |
IGPEWriterHandler gwh,
|
|
370 |
IXmlStreamWriter xmlw,
|
|
371 |
FeatureSet fset,
|
|
372 |
String[] fieldNames,
|
|
373 |
Map<ISymbol, KmlStyle> symsty,
|
|
374 |
IVectorLegend lege,
|
|
375 |
List<ICoordTrans> transfList) throws Exception {
|
397 |
376 |
|
398 |
|
if (!writeFeature(feat, gwh, xmlw, gw, count, fieldNames, kmlStyle, transfList)) {
|
399 |
|
nullGeometries++;
|
400 |
|
};
|
401 |
|
count++;
|
402 |
|
this.getTaskStatus().setCurValue(count);
|
403 |
|
}
|
404 |
|
if (nullGeometries > 0) {
|
405 |
|
logger.warn("Can't export " + nullGeometries + " features because source geometries are null.");
|
406 |
|
}
|
407 |
|
diter.dispose();
|
408 |
|
}
|
|
377 |
GeometryToGPEWriter gw = new GeometryToGPEWriter(gwh);
|
|
378 |
DisposableIterator diter = fset.fastIterator();
|
|
379 |
Feature feat = null;
|
|
380 |
long count = 0;
|
|
381 |
this.getTaskStatus().setCurValue(count);
|
|
382 |
ISymbol sym = null;
|
|
383 |
int nullGeometries = 0;
|
|
384 |
while (diter.hasNext()) {
|
|
385 |
feat = (Feature) diter.next();
|
|
386 |
try {
|
|
387 |
if (lege instanceof IVectorLegend) {
|
|
388 |
sym = lege.getSymbolByFeature(feat);
|
|
389 |
}
|
|
390 |
} catch (MapContextException mce) {
|
|
391 |
logger.info("While getting legend symbol.", mce);
|
|
392 |
}
|
|
393 |
KmlStyle kmlStyle;
|
|
394 |
try {
|
|
395 |
kmlStyle = symsty.get(sym);
|
|
396 |
} catch (Exception e) {
|
|
397 |
kmlStyle = null;
|
|
398 |
}
|
409 |
399 |
|
410 |
|
private boolean writeFeature(
|
411 |
|
Feature feat,
|
412 |
|
IGPEWriterHandler gwh,
|
413 |
|
IXmlStreamWriter xmlw,
|
414 |
|
GeometryToGPEWriter gw,
|
415 |
|
long index,
|
416 |
|
String[] fieldNames,
|
417 |
|
KmlStyle ksty,
|
418 |
|
List<ICoordTrans> transfList) throws IOException {
|
|
400 |
if (!writeFeature(feat, gwh, xmlw, gw, count, fieldNames, kmlStyle, transfList)) {
|
|
401 |
nullGeometries++;
|
|
402 |
};
|
|
403 |
count++;
|
|
404 |
this.getTaskStatus().setCurValue(count);
|
|
405 |
}
|
|
406 |
if (nullGeometries > 0) {
|
|
407 |
logger.warn("Can't export " + nullGeometries + " features because source geometries are null.");
|
|
408 |
}
|
|
409 |
diter.dispose();
|
|
410 |
}
|
419 |
411 |
|
420 |
|
Geometry geom = feat.getDefaultGeometry();
|
|
412 |
private boolean writeFeature(
|
|
413 |
Feature feat,
|
|
414 |
IGPEWriterHandler gwh,
|
|
415 |
IXmlStreamWriter xmlw,
|
|
416 |
GeometryToGPEWriter gw,
|
|
417 |
long index,
|
|
418 |
String[] fieldNames,
|
|
419 |
KmlStyle ksty,
|
|
420 |
List<ICoordTrans> transfList) throws IOException {
|
421 |
421 |
|
422 |
|
if (geom == null) {
|
423 |
|
return false;
|
424 |
|
}
|
|
422 |
Geometry geom = feat.getDefaultGeometry();
|
425 |
423 |
|
426 |
|
String strindex = String.valueOf(index);
|
|
424 |
if (geom == null) {
|
|
425 |
return false;
|
|
426 |
}
|
427 |
427 |
|
428 |
|
if (this.getParameters().getUseLabels()) {
|
429 |
|
String lbl = getLabelForFeature(feat);
|
430 |
|
gwh.startFeature(strindex, "FEATURE", lbl);
|
431 |
|
} else {
|
432 |
|
gwh.startFeature(strindex, "FEATURE", "");
|
433 |
|
}
|
434 |
|
// =========================
|
435 |
|
// Style
|
436 |
|
if (ksty != null) {
|
437 |
|
xmlw.writeStartElement(Kml2_1_Tags.STYLEURL);
|
438 |
|
xmlw.writeValue("#" + ksty.getId());
|
439 |
|
xmlw.writeEndElement();
|
440 |
|
}
|
441 |
|
// ===== Balloon ============
|
442 |
|
if (this.getParameters().getAttsAsBalloon()) {
|
443 |
|
writeBalloon(xmlw, feat, fieldNames);
|
444 |
|
}
|
|
428 |
String strindex = String.valueOf(index);
|
445 |
429 |
|
446 |
|
// ============= Geometry
|
|
430 |
if (this.getParameters().getUseLabels()) {
|
|
431 |
String lbl = getLabelForFeature(feat);
|
|
432 |
gwh.startFeature(strindex, "FEATURE", lbl);
|
|
433 |
} else {
|
|
434 |
gwh.startFeature(strindex, "FEATURE", "");
|
|
435 |
}
|
|
436 |
// =========================
|
|
437 |
// Style
|
|
438 |
if (ksty != null) {
|
|
439 |
xmlw.writeStartElement(Kml2_1_Tags.STYLEURL);
|
|
440 |
xmlw.writeValue("#" + ksty.getId());
|
|
441 |
xmlw.writeEndElement();
|
|
442 |
}
|
|
443 |
// ===== Balloon ============
|
|
444 |
if (this.getParameters().getAttsAsBalloon()) {
|
|
445 |
writeBalloon(xmlw, feat, fieldNames);
|
|
446 |
}
|
447 |
447 |
|
448 |
|
/*
|
|
448 |
// ============= Geometry
|
|
449 |
|
|
450 |
/*
|
449 |
451 |
* This has no effect if reprojection is not necessary
|
450 |
|
*/
|
451 |
|
geom = reproject(geom, transfList);
|
452 |
|
gw.writeGeometry(geom, this.getParameters().getUseLabels());
|
453 |
|
// ============= Attributes
|
454 |
|
Object val = null;
|
455 |
|
String fldname = null;
|
456 |
|
for (int i = 0; i < fieldNames.length; i++) {
|
457 |
|
val = feat.get(fieldNames[i]);
|
458 |
|
fldname = fieldNames[i].replace(' ', '_');
|
459 |
|
gwh.startElement("", fldname, val == null ? "" : val.toString());
|
460 |
|
gwh.endElement();
|
461 |
|
}
|
462 |
|
// =========================
|
463 |
|
gwh.endFeature();
|
464 |
|
return true;
|
465 |
|
}
|
466 |
|
private String getLabelForFeature(Feature feat) {
|
467 |
|
return "";
|
|
452 |
*/
|
|
453 |
geom = reproject(geom, transfList);
|
|
454 |
gw.writeGeometry(geom, this.getParameters().getUseLabels());
|
|
455 |
// ============= Attributes
|
|
456 |
Object val = null;
|
|
457 |
String fldname = null;
|
|
458 |
for (int i = 0; i < fieldNames.length; i++) {
|
|
459 |
val = feat.get(fieldNames[i]);
|
|
460 |
fldname = fieldNames[i].replace(' ', '_');
|
|
461 |
gwh.startElement("", fldname, val == null ? "" : val.toString());
|
|
462 |
gwh.endElement();
|
|
463 |
}
|
|
464 |
// =========================
|
|
465 |
gwh.endFeature();
|
|
466 |
return true;
|
|
467 |
}
|
468 |
468 |
|
|
469 |
private String getLabelForFeature(Feature feat) {
|
|
470 |
return "";
|
|
471 |
|
469 |
472 |
// if (this.vectorLayer.isLabeled()) {
|
470 |
473 |
//
|
471 |
474 |
// String[] flds = vectorLayer.getLabelingStrategy().getUsedFields();
|
... | ... | |
491 |
494 |
// } else {
|
492 |
495 |
// return "";
|
493 |
496 |
// }
|
494 |
|
}
|
495 |
|
private void writeBalloon(IXmlStreamWriter xmlw, Feature feat, String[] fieldNames)
|
496 |
|
throws IOException {
|
|
497 |
}
|
497 |
498 |
|
498 |
|
xmlw.writeStartElement(Kml2_1_Tags.EXTENDED_DATA);
|
499 |
|
String fldrep = null;
|
500 |
|
Object val = null;
|
501 |
|
for (int i = 0; i < fieldNames.length; i++) {
|
502 |
|
fldrep = fieldNames[i].replace(' ', '_');
|
503 |
|
xmlw.writeStartElement(Kml2_1_Tags.DATA);
|
504 |
|
// Att =====================================================
|
505 |
|
xmlw.writeStartAttribute(null, "name");
|
506 |
|
xmlw.writeValue(fldrep);
|
507 |
|
xmlw.writeEndAttributes();
|
508 |
|
// Value =====================================================
|
509 |
|
xmlw.writeStartElement(Kml2_1_Tags.VALUE);
|
510 |
|
val = feat.get(fieldNames[i]);
|
511 |
|
xmlw.writeValue(val == null ? "" : val.toString());
|
512 |
|
xmlw.writeEndElement();
|
513 |
|
// =============================================
|
514 |
|
xmlw.writeEndElement();
|
515 |
|
}
|
516 |
|
xmlw.writeEndElement();
|
|
499 |
private void writeBalloon(IXmlStreamWriter xmlw, Feature feat, String[] fieldNames)
|
|
500 |
throws IOException {
|
517 |
501 |
|
518 |
|
/*
|
|
502 |
xmlw.writeStartElement(Kml2_1_Tags.EXTENDED_DATA);
|
|
503 |
String fldrep = null;
|
|
504 |
Object val = null;
|
|
505 |
for (int i = 0; i < fieldNames.length; i++) {
|
|
506 |
fldrep = fieldNames[i].replace(' ', '_');
|
|
507 |
xmlw.writeStartElement(Kml2_1_Tags.DATA);
|
|
508 |
// Att =====================================================
|
|
509 |
xmlw.writeStartAttribute(null, "name");
|
|
510 |
xmlw.writeValue(fldrep);
|
|
511 |
xmlw.writeEndAttributes();
|
|
512 |
// Value =====================================================
|
|
513 |
xmlw.writeStartElement(Kml2_1_Tags.VALUE);
|
|
514 |
val = feat.get(fieldNames[i]);
|
|
515 |
xmlw.writeValue(val == null ? "" : val.toString());
|
|
516 |
xmlw.writeEndElement();
|
|
517 |
// =============================================
|
|
518 |
xmlw.writeEndElement();
|
|
519 |
}
|
|
520 |
xmlw.writeEndElement();
|
|
521 |
|
|
522 |
/*
|
519 |
523 |
*
|
520 |
524 |
<ExtendedData>
|
521 |
525 |
<Data name="holeNumber">
|
... | ... | |
529 |
533 |
</Data>
|
530 |
534 |
</ExtendedData>
|
531 |
535 |
*
|
532 |
|
*/
|
533 |
|
}
|
534 |
|
|
535 |
|
private Envelope reproject(Envelope env, List<ICoordTrans> transfList) throws CreateEnvelopeException {
|
|
536 |
*/
|
|
537 |
}
|
536 |
538 |
|
537 |
|
int sz = transfList.size();
|
538 |
|
if (sz == 0) {
|
539 |
|
return env;
|
540 |
|
} else {
|
541 |
|
Envelope resp = env;
|
542 |
|
try {
|
543 |
|
for (int i = 0; i < sz; i++) {
|
544 |
|
resp = resp.convert(transfList.get(i));
|
545 |
|
}
|
546 |
|
} catch (Exception exc) {
|
|
539 |
private Envelope reproject(Envelope env, List<ICoordTrans> transfList) throws CreateEnvelopeException {
|
547 |
540 |
|
548 |
|
// If this process fails, we'll use "emergency values":
|
549 |
|
GeometryManager gm = GeometryLocator.getGeometryManager();
|
550 |
|
double[] min = new double[2];
|
551 |
|
double[] max = new double[2];
|
552 |
|
IProjection targetproj = this.getParameters().getTargetProjection();
|
553 |
|
if (targetproj.isProjected()) {
|
554 |
|
min = new double[]{-20000000, -20000000};
|
555 |
|
max = new double[]{20000000, 20000000};
|
556 |
|
} else {
|
557 |
|
min = new double[]{-180, -90};
|
558 |
|
max = new double[]{180, 90};
|
559 |
|
}
|
|
541 |
int sz = transfList.size();
|
|
542 |
if (sz == 0) {
|
|
543 |
return env;
|
|
544 |
} else {
|
|
545 |
Envelope resp = env;
|
|
546 |
try {
|
|
547 |
for (int i = 0; i < sz; i++) {
|
|
548 |
resp = resp.convert(transfList.get(i));
|
|
549 |
}
|
|
550 |
} catch (Exception exc) {
|
560 |
551 |
|
561 |
|
resp = gm.createEnvelope(
|
562 |
|
min[0], min[1],
|
563 |
|
max[0], max[1],
|
564 |
|
Geometry.SUBTYPES.GEOM2D);
|
|
552 |
// If this process fails, we'll use "emergency values":
|
|
553 |
GeometryManager gm = GeometryLocator.getGeometryManager();
|
|
554 |
double[] min = new double[2];
|
|
555 |
double[] max = new double[2];
|
|
556 |
IProjection targetproj = this.getParameters().getTargetProjection();
|
|
557 |
if (targetproj.isProjected()) {
|
|
558 |
min = new double[]{-20000000, -20000000};
|
|
559 |
max = new double[]{20000000, 20000000};
|
|
560 |
} else {
|
|
561 |
min = new double[]{-180, -90};
|
|
562 |
max = new double[]{180, 90};
|
|
563 |
}
|
565 |
564 |
|
566 |
|
}
|
567 |
|
return resp;
|
568 |
|
}
|
569 |
|
}
|
|
565 |
resp = gm.createEnvelope(
|
|
566 |
min[0], min[1],
|
|
567 |
max[0], max[1],
|
|
568 |
Geometry.SUBTYPES.GEOM2D);
|
|
569 |
|
|
570 |
}
|
|
571 |
return resp;
|
|
572 |
}
|
|
573 |
}
|
|
574 |
|
|
575 |
private void initEnvelope(Envelope env, FeatureSet featureSet) {
|
|
576 |
for (Feature feature : featureSet) {
|
|
577 |
Geometry geometry = feature.getDefaultGeometry();
|
|
578 |
if (geometry != null && geometry.isValid()) {
|
|
579 |
env.add(geometry);
|
|
580 |
}
|
|
581 |
}
|
|
582 |
}
|
570 |
583 |
}
|