Statistics
| Revision:

gvsig-projects-pool / org.gvsig.lidar.prov / org.gvsig.lidar.prov.common / src / main / java / org / gvsig / lidar / prov / LASDataStoreParameters.java @ 281

History | View | Annotate | Download (8.13 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2016 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 2
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.lidar.prov;
25

    
26
import java.io.File;
27
import java.io.IOException;
28

    
29
import org.apache.commons.io.FileUtils;
30
import org.apache.commons.io.FilenameUtils;
31
import org.cresques.cts.ICRSFactory;
32
import org.cresques.cts.IProjection;
33
import org.gvsig.fmap.crs.CRSFactory;
34
import org.gvsig.fmap.dal.DataStoreParameters;
35
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
36
import org.gvsig.fmap.dal.feature.EditableFeatureType;
37
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
41
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
42
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.dynobject.DelegatedDynObject;
45
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
46

    
47
/**
48
 * 
49
 * @author <a href="mailto:cmartinez@scolab.es">Cesar Martinez Izquierdo</a>
50
 *
51
 */
52
public class LASDataStoreParameters extends AbstractDataParameters
53
                implements DataStoreParameters, FilesystemStoreParameters, NewFeatureStoreParameters {
54

    
55
    public static final String FILE_PARAMETER_NAME = "file";
56

    
57
    public static final String CRS_PARAMETER_NAME = "CRS";
58
    
59
    public static final String POINT_DENSITY_PARAMETER_NAME = "PointDensity";
60
    
61
    /**
62
     * Simple method to load a subset of the data, useful for faster
63
     * visualization. The decimation parameter will only keep the n'th
64
     * point from the file. For instance, a value of 2 will skip half
65
     * of the points. A value of 3 will load 1/3 of the original points.
66
     * A value of 10 will load 1/10 of the original points.
67
     */
68
    public static final String THINNING_DIV_PARAMETER_NAME = "thinningDivisor";
69
    
70
    /**
71
     * An alternative method to load a subset of the data. This parameter
72
     * defines how many points should be loaded by square map unit.
73
     * For instance, a value of 0.1 on a layer using EPSG:25830 means that
74
     * 0.1 points should be loaded per square meter (100.000 points per
75
     * square kilometer). The same value for a layer using EPSG:4326 means
76
     * that 0.1 points should be loaded per square degree.
77
     * 
78
     * Note that this is an approximated method which assumes that points are
79
     * evenly separated on x and y axis. When resolution is defined,
80
     * the decimation parameter is ignored.
81
     * 
82
     */
83
    public static final String THINNING_RESOLUTION_PARAMETER_NAME = "thinningResolution";
84
    
85

    
86
    protected DelegatedDynObject parameters;
87

    
88
    public static final String DATASET_CREATION_OPTION_PARAMETER_NAME = "datasetCreationOptions";
89
    
90
    public static final String PARAMETERS_DEFINITION_NAME = "LASDataStoreParameters";
91
    
92
        public LASDataStoreParameters() {
93
                this(PARAMETERS_DEFINITION_NAME, "to be defined");
94
        }
95

    
96
    public LASDataStoreParameters(String providerName) {
97
            this(PARAMETERS_DEFINITION_NAME, providerName);
98
    }
99
    
100
    public LASDataStoreParameters(String parametersDefinitionName, String providerName) {
101
        this.parameters =
102
            (DelegatedDynObject) ToolsLocator.getDynObjectManager().createDynObject(
103
                ToolsLocator.getPersistenceManager().getDefinition(parametersDefinitionName));
104
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, providerName);
105
    }
106

    
107
        public String getDataStoreName() {
108
                return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
109
        }
110

    
111
        public String getDescription() {
112
                return this.getDynClass().getDescription();
113
        }
114

    
115

    
116

    
117
    /**
118
     * @return File
119
     */
120
    public File getFile() {
121
        return (File) this.getDynValue(FILE_PARAMETER_NAME);
122
    }
123

    
124
    /**
125
     * 
126
     * @return String
127
     */
128
    public IProjection getCRS() {
129
        return (IProjection) this.getDynValue(CRS_PARAMETER_NAME);
130
    }
131

    
132
    /**
133
     * @param file
134
     *            File
135
     */
136
    public void setFile(File file) {
137
        this.setDynValue(FILE_PARAMETER_NAME, file);
138
        if (getCRS()==null) {
139
                // only set CRS automatically if it has not been previously set
140
                loadProj();
141
        }
142
    }
143
    
144
    private void loadProj() {
145
            String wktString = readProj();
146
        if (wktString != null) {
147
            IProjection proj = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT, wktString);
148
            setCRS(proj);
149
        }
150
    }
151
    
152
        private String readProj(){
153
        File prjFile = getPrj();
154
        if (prjFile.exists()) {
155
            try {
156
                return FileUtils.readFileToString(prjFile);
157
            } catch (IOException e) {
158
                return null;
159
            }
160
        }
161
        return null;
162
        }
163
    
164
        /**
165
         * Gets the .prj path corresponding to the .las file. It will
166
         * always return a path if getFile is not null, even if none
167
         * of the files actually exists. 
168
         *  
169
         * @return
170
         */
171
    public File getPrj() {
172
            File lasFile = getFile(); 
173
            if (lasFile!=null) {
174
                    String path = lasFile.getPath();
175
                    String baseName = FilenameUtils.getFullPath(path) + FilenameUtils.getBaseName(path);
176
                    return new File(baseName + ".prj");
177
            }
178
            return null;
179
    }
180

    
181
    /**
182
     * 
183
     * @param crs
184
     */
185
    public void setCRS(IProjection crs) {
186
        this.setDynValue(CRS_PARAMETER_NAME, crs);
187
    }
188

    
189
    public int getDecimation() {
190
            try {
191
                    this.hasDynValue(THINNING_DIV_PARAMETER_NAME);
192
                    return (int) this.getDynValue(THINNING_DIV_PARAMETER_NAME);
193
            }
194
            catch (Exception ex) {}
195
            return 0;
196
    }
197
    
198
    public double getResolution() {
199
            try {
200
                    this.hasDynValue(THINNING_RESOLUTION_PARAMETER_NAME);
201
                    return (double) this.getDynValue(THINNING_RESOLUTION_PARAMETER_NAME);
202
            }
203
            catch (Exception ex) {}
204
            return 0.0d;
205
    }
206
    
207
    @Override
208
    public boolean isValid() {
209
        if (this.getFile() != null && this.getFile().exists()) {
210
            return true;
211
        }
212
        return false;
213
    }
214

    
215
    @Override
216
    protected DelegatedDynObject getDelegatedDynObject() {
217
        return parameters;
218
    }
219

    
220
    @Override
221
    public void setDefaultFeatureType(FeatureType defaultFeatureType) {
222
        this.setDynValue(FEATURETYPE_PARAMTER_NAME, defaultFeatureType);
223
    }
224

    
225
    @Override
226
    public EditableFeatureType getDefaultFeatureType() {
227
        return (EditableFeatureType) this.getDynValue(FEATURETYPE_PARAMTER_NAME);
228
    }
229
    
230
        @Override
231
        public void validate() throws ValidateDataParametersException {
232
                fixParameters();
233
                super.validate();
234
        }
235
    
236
    public void fixParameters() {
237
            try {
238
                    EditableFeatureType featureType = getDefaultFeatureType();    
239
                    if (featureType != null) {
240
                            FeatureAttributeDescriptor geometryAttribute =
241
                                            featureType.getAttributeDescriptor(featureType.getDefaultGeometryAttributeName());
242
                            if (geometryAttribute != null) {
243
                                    //If the CRS parameter has not been fixed, but there is
244
                                    //a geometric attribute with the CRS, this attribute
245
                                    //can be used.
246
                                    if (getCRS() == null) {
247
                                            setCRS(geometryAttribute.getSRS());
248
                                    }
249
                            }
250
                    }
251
            }
252
            catch (DynFieldNotFoundException ex) {
253
                    // we are not creating a new NewFeatureStoreParameters,
254
                    // we can safely ignore the error
255
            }
256
    }
257

    
258
}