Statistics
| Revision:

gvsig-projects-pool / org.gvsig.lidar.app / org.gvsig.lidar.app.mainplugin / src / main / java / org / gvsig / exportto / swing / prov / lidar / ExporttoLASService.java @ 281

History | View | Annotate | Download (13 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2016 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.exportto.swing.prov.lidar;
24

    
25
import java.io.File;
26
import java.util.Date;
27
import java.util.HashMap;
28
import java.util.List;
29

    
30
import org.cresques.cts.ICoordTrans;
31
import org.cresques.cts.IProjection;
32
import org.gvsig.exportto.ExporttoService;
33
import org.gvsig.exportto.ExporttoServiceException;
34
import org.gvsig.exportto.ExporttoServiceFinishAction;
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.DataStoreParameters;
38
import org.gvsig.fmap.dal.DataTypes;
39
import org.gvsig.fmap.dal.exception.DataException;
40
import org.gvsig.fmap.dal.feature.EditableFeature;
41
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
42
import org.gvsig.fmap.dal.feature.EditableFeatureType;
43
import org.gvsig.fmap.dal.feature.Feature;
44
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
45
import org.gvsig.fmap.dal.feature.FeatureReference;
46
import org.gvsig.fmap.dal.feature.FeatureSet;
47
import org.gvsig.fmap.dal.feature.FeatureStore;
48
import org.gvsig.fmap.dal.feature.FeatureType;
49
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
50
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
51
import org.gvsig.fmap.geom.Geometry;
52
import org.gvsig.fmap.geom.primitive.Envelope;
53
import org.gvsig.fmap.geom.type.GeometryType;
54
import org.gvsig.lidar.prov.LASDataStoreParameters;
55
import org.gvsig.lidar.prov.jgrasstools.JGrassLASDataStoreProvider;
56
import org.gvsig.lidar.prov.jgrasstools.JGrassLASWriter;
57
import org.gvsig.timesupport.Instant;
58
import org.gvsig.timesupport.Interval;
59
import org.gvsig.tools.dispose.DisposableIterator;
60
import org.gvsig.tools.dispose.DisposeUtils;
61
import org.gvsig.tools.dynobject.DynObject;
62
import org.gvsig.tools.evaluator.EvaluatorData;
63
import org.gvsig.tools.task.AbstractMonitorableTask;
64
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
66
import org.gvsig.exportto.swing.prov.lidar.ExporttoLASOptions;
67

    
68
/**
69
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
70
 *
71
 */
72
public class ExporttoLASService extends AbstractMonitorableTask implements
73
        ExporttoService {
74

    
75
    private static final Logger logger = LoggerFactory.getLogger(ExporttoLASService.class);
76

    
77
    public static final int CHECK_NONE = 0;
78
    public static final int CHECK_IF_CORRUPT = 1;
79
    public static final int CHECK_IF_VALID = 2;
80

    
81
    public static final int ACTION_SET_GEOMETRY_TO_NULL = 0;
82
    public static final int ACTION_SKIP_FEATURE = 1;
83
    public static final int ACTION_ABORT = 2;
84

    
85
    private ExporttoServiceFinishAction exporttoServiceFinishAction = null;
86
    private ExporttoLASOptions options;
87

    
88
    public ExporttoLASService(ExporttoLASOptions options) {
89
        this.options = options;
90
    }
91

    
92

    
93
    public void export(FeatureSet featureSet) throws ExporttoServiceException {
94
            File outFile = this.options.getOutputFile();
95
        DisposableIterator it = null;
96
        try {
97
                
98
                
99
            // ======================================
100
            // Reprojection 
101
            FeatureAttributeDescriptor geo_att = this.options.getSource().getDefaultFeatureType().getDefaultGeometryAttribute();
102
            IProjection sourceProjection = null;
103
            ICoordTrans coord_trans = null;
104
            Geometry reproj_geom = null;
105
            Envelope filterEnvelope = this.options.getFilterEnvelope();
106
            
107
            if (geo_att != null) {
108
                sourceProjection = geo_att.getSRS();
109
                // FIXME: we should get the transformation from the options and not from the layer projection.
110
                // If the layer contains a CT, it has been set to convert from layer to view projection,
111
                // which is not necessarily the right one for the target export projection.
112
                // We will be able to improve these corner cases when the new CRS API is fully implemented
113
                if (sourceProjection != null && sourceProjection != this.options.getTargetProjection()) {
114
                    coord_trans = sourceProjection.getCT(this.options.getTargetProjection());
115
                }
116
            }
117
            
118
            // ============================================
119

    
120
            LASDataStoreParameters openParams = new LASDataStoreParameters(JGrassLASDataStoreProvider.NAME);
121
            openParams.setFile(outFile);
122
            openParams.setCRS(this.options.getTargetProjection());
123

    
124
            FeatureType sourceFeatureType = featureSet.getDefaultFeatureType();
125
            
126
            Envelope outEnvelope = null;
127
            if (coord_trans!=null) {
128
                    outEnvelope = this.options.getSource().getEnvelope().convert(coord_trans);
129
            }
130
            else {
131
                    outEnvelope = this.options.getSource().getEnvelope();
132
            }
133
            
134
            JGrassLASWriter writer = new JGrassLASWriter(openParams, sourceFeatureType, outEnvelope);            
135
            writer.open();
136
            
137
            int featureCount = 1;
138
            taskStatus.setRangeOfValues(0, featureSet.getSize());
139
            logger.debug("Inserting rows");
140
            taskStatus.message("Inserting rows");
141
            it = featureSet.fastIterator();
142
            while (it.hasNext()) {
143
                Feature feature = (Feature) it.next();
144
                this.taskStatus.setCurValue(featureCount);
145
                Geometry geometry = feature.getDefaultGeometry();
146
                if (filterEnvelope==null
147
                                || filterEnvelope.intersects(geometry)) {
148
                        // ================================================
149
                        // Reprojection 
150
                        if (geometry != null && 
151
                                        geo_att != null && coord_trans != null) {
152
                                CustomFeatureImpl customFeature = new CustomFeatureImpl(feature);
153
                                reproj_geom = geometry.cloneGeometry();
154
                                reproj_geom.reProject(coord_trans);
155
                                customFeature.setDefaultGeometry(reproj_geom);
156
                                feature = customFeature;
157
                        }
158
                        // ================================================
159
                    writer.write(feature);
160
                }
161

    
162
                if (this.taskStatus.isCancellationRequested()) {
163
                    return;
164
                }
165
                featureCount++;
166
            }
167
            writer.close();
168

    
169
            finishAction(this.options.getOutputFile().getName(), this.options.getOutputFile(), this.options.getTargetProjection());
170
            taskStatus.message("Exportation finished");
171

    
172
        } catch (Exception e) {
173
            logger.warn("Can't export data.", e);
174
            taskStatus.message("Problems exporting data");
175
            throw new ExporttoServiceException(e);
176

    
177
        } finally {
178
                DisposeUtils.disposeQuietly(it);
179
                DisposeUtils.disposeQuietly(featureSet);
180
            this.taskStatus.terminate();
181
            this.taskStatus.remove();
182
        }
183
    }
184
    
185
    protected void finishAction(String layerName, File out_file, IProjection proj)
186
    throws ExporttoServiceException {
187

    
188
        if (exporttoServiceFinishAction != null) {
189
            /*
190
             * Export is done. We notify with a SHPStoreParameters,
191
             * not with the NewSHPParameters we have used:
192
             */
193
                DataStoreParameters las_params = null;
194
                
195
            try {
196
                    las_params = DALLocator.getDataManager().createStoreParameters(JGrassLASDataStoreProvider.NAME);
197
                    las_params.setDynValue("file", out_file);
198
                    las_params.setDynValue("CRS", proj);
199

    
200
                    las_params.validate();
201
            } catch (Exception e) {
202
                throw new ExporttoServiceException(e);
203
            }
204
            exporttoServiceFinishAction.finished(layerName, las_params);
205
        }
206
    }
207

    
208
    public void setFinishAction(
209
            ExporttoServiceFinishAction exporttoServiceFinishAction) {
210
        this.exporttoServiceFinishAction = exporttoServiceFinishAction;
211
    }
212
    
213
    /**
214
     * Incomplete implementation of editable feature, only used to feed
215
     * reprojected features to the LAS Writer
216
     * 
217
     * @author cesar
218
     *
219
     */
220
    protected class CustomFeatureImpl implements Feature {
221
            private Feature feature;
222
            private Geometry theGeom;
223
            private String defaultGeomName;
224
            private int defaultGeomIndex;
225
            
226
            public CustomFeatureImpl(Feature feat) {
227
                    this.feature = feat;
228
                    this.defaultGeomName = feat.getType().getDefaultGeometryAttributeName();
229
                    this.defaultGeomIndex = feat.getType().getDefaultGeometryAttributeIndex();
230
            }
231

    
232
                public FeatureReference getReference() {
233
                        throw new UnsupportedOperationException();
234
                }
235

    
236
                public FeatureType getType() {
237
                        return feature.getType();
238
                }
239

    
240
                public Feature getCopy() {
241
                        CustomFeatureImpl copy = new CustomFeatureImpl(feature.getCopy());
242
                        copy.setDefaultGeometry(theGeom);
243
                        return copy;
244
                }
245

    
246
                public void validate(int mode) throws DataException {
247
                        feature.validate(mode);
248
                }
249
                public EditableFeature getEditable() {
250
                        return feature.getEditable();
251
                }
252
                public Object get(String name) {
253
                        if (name.equals(defaultGeomName) && theGeom!=null) {
254
                                return theGeom;
255
                        }
256
                        return feature.get(name);
257
                }
258
                public Object get(int index) {
259
                        if (index==defaultGeomIndex && theGeom!=null) {
260
                                return theGeom;
261
                        }
262
                        return feature.get(index);
263
                }
264
                public int getInt(String name) {
265
                        return feature.getInt(name);
266
                }
267
                public int getInt(int index) {
268
                        return feature.getInt(index);
269
                }
270
                public boolean getBoolean(String name) {
271
                        return feature.getBoolean(name);
272
                }
273
                public boolean getBoolean(int index) {
274
                        return feature.getBoolean(index);
275
                }
276
                public long getLong(String name) {
277
                        return feature.getLong(name);
278
                }
279
                public long getLong(int index) {
280
                        return feature.getLong(index);
281
                }
282
                public float getFloat(String name) {
283
                        return feature.getFloat(name);
284
                }
285
                public float getFloat(int index) {
286
                        return feature.getFloat(index);
287
                }
288
                public double getDouble(String name) {
289
                        return feature.getDouble(name);
290
                }
291
                public double getDouble(int index) {
292
                        return feature.getDouble(index);
293
                }
294
                public Date getDate(String name) {
295
                        return feature.getDate(name);
296
                }
297
                public Date getDate(int index) {
298
                        return feature.getDate(index);
299
                }
300
                public String getString(String name) {
301
                        return feature.getString(name);
302
                }
303
                public String getString(int index) {
304
                        return feature.getString(index);
305
                }
306
                public byte getByte(String name) {
307
                        return feature.getByte(name);
308
                }
309
                public byte getByte(int index) {
310
                        return feature.getByte(index);
311
                }
312
                public Geometry getGeometry(String name) {
313
                        if (name.equals(this.defaultGeomName) && theGeom!=null) {
314
                                return theGeom;
315
                        }
316
                        return feature.getGeometry(name);
317
                }
318
                public Geometry getGeometry(int index) {
319
                        if (index==defaultGeomIndex && theGeom!=null) {
320
                                return theGeom;
321
                        }
322
                        return feature.getGeometry(index);
323
                }
324
                public Object[] getArray(String name) {
325
                        return feature.getArray(name);
326
                }
327
                public Object[] getArray(int index) {
328
                        return feature.getArray(index);
329
                }
330
                public Feature getFeature(String name) {
331
                        return feature.getFeature(name);
332
                }
333
                public Feature getFeature(int index) {
334
                        return feature.getFeature(index);
335
                }
336
                public Envelope getDefaultEnvelope() {
337
                        return feature.getDefaultEnvelope();
338
                }
339
                public Geometry getDefaultGeometry() {
340
                        if (theGeom!=null) {
341
                                return theGeom;
342
                        }
343
                        return feature.getDefaultGeometry();
344
                }
345
                public void setDefaultGeometry(Geometry geom) {
346
                        this.theGeom = geom;
347
                }
348
                public List getGeometries() {
349
                        throw new UnsupportedOperationException();
350
                }
351
                public IProjection getDefaultSRS() {
352
                        return feature.getDefaultSRS();
353
                }
354
                public List getSRSs() {
355
                        return feature.getSRSs();
356
                }
357
                public Instant getInstant(int index) {
358
                        return feature.getInstant(index);
359
                }
360
                public Instant getInstant(String name) {
361
                        return feature.getInstant(name);
362
                }
363
                public Interval getInterval(int index) {
364
                        return feature.getInterval(index);
365
                }
366
                public Interval getInterval(String name) {
367
                        return feature.getInterval(name);
368
                }
369
                public DynObject getAsDynObject() {
370
                        return feature.getAsDynObject();
371
                }
372
                public EvaluatorData getEvaluatorData() {
373
                        return feature.getEvaluatorData();
374
                }
375
                
376
                public FeatureStore getStore() {
377
                        return feature.getStore();
378
                }
379
    }
380

    
381

    
382
}