Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.gdal / org.gvsig.raster.gdal.provider / src / main / java / org / gvsig / raster / gdal / provider / RasterGdalFileStoreParameters.java @ 6335

History | View | Annotate | Download (5.43 KB)

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

    
25
import java.io.File;
26
import java.io.IOException;
27
import java.util.List;
28

    
29
import org.apache.commons.io.FileUtils;
30
import org.apache.commons.io.FilenameUtils;
31
import org.apache.commons.lang3.StringUtils;
32
import org.cresques.cts.ICRSFactory;
33
import org.cresques.cts.IProjection;
34
import org.gdal.gdal.Dataset;
35
import org.gdal.gdal.gdal;
36
import org.gdal.gdalconst.gdalconstConstants;
37
import org.gvsig.fmap.crs.CRSFactory;
38
import org.gvsig.fmap.dal.raster.spi.OpenRasterStoreParameters;
39
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
40
import org.gvsig.tools.dynobject.DynClass_v2;
41
import org.gvsig.tools.dynobject.DynStruct_v2;
42
import org.gvsig.tools.dynobject.Tags;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
/**
47
 * Parameters to create a GDAL Provider
48
 * @author dmartinezizquierdo
49
 *
50
 */
51
public class RasterGdalFileStoreParameters extends RasterGdalStoreParameters implements
52
OpenRasterStoreParameters, FilesystemStoreParameters{
53

    
54
    private static final Logger logger = LoggerFactory.getLogger(RasterGdalFileStoreParameters.class);
55

    
56
    /**
57
     * Definition parameters name used to open a store
58
     */
59
    public static final String PARAMETERS_DEFINITION_NAME = "RasterGdalFileStoreParameters";
60
    /**
61
     * Parameter name to identify the file
62
     */
63
    public static final String FILE_PARAMTER_NAME = "file";
64

    
65

    
66
    /**
67
     * Constructor
68
     * @param parametersDefinitionName
69
     * @param name
70
     */
71
    public RasterGdalFileStoreParameters(String parametersDefinitionName, String name) {
72
        super(parametersDefinitionName,name);
73
    }
74

    
75
    @Override
76
    public boolean isValid() {
77
        if (this.getFile() != null){
78
            try {
79
                gdal.Open(this.getFile().getAbsolutePath(),gdalconstConstants.GA_ReadOnly);
80
            } catch (Exception e) {
81
                return false;
82
            }
83
            return true;
84
        }
85
        return false;
86
    }
87

    
88
    @Override
89
    public File getFile() {
90
        return (File) this.getDynValue(FILE_PARAMTER_NAME);
91
    }
92

    
93
    @Override
94
    public void setFile(File file) {
95
        this.setDynValue(FILE_PARAMTER_NAME, file);
96

    
97
        if (getCRS()==null){
98
            loadPRJ(file);
99
        }
100
        loadWLD(file);
101
        if (getCRS()==null){
102
            Dataset gdalDatasSet = gdal.Open(this.getFile().getAbsolutePath(),gdalconstConstants.GA_ReadOnly);
103
            String projection=gdalDatasSet.GetProjection();
104
            if (StringUtils.isNotEmpty(projection)){
105
                ICRSFactory crsFactory = CRSFactory.getCRSFactory();
106
                IProjection crs=crsFactory.get(ICRSFactory.FORMAT_WKT, projection);
107
                setCRS(crs);
108
            }
109
        }
110

    
111
    }
112

    
113
    private void loadPRJ(File file){
114
        File prjFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+".prj");
115
        if (prjFile.exists()) {
116
            try {
117
                String contentFile = FileUtils.readFileToString(prjFile);
118
                if (StringUtils.isNotEmpty(contentFile)){
119
                    IProjection crs=CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, contentFile);
120
                    setCRS(crs);
121
                }
122

    
123
            } catch (IOException e) {
124
                logger.warn("Couldn't read prj file");
125
            }
126
        }
127
    }
128

    
129
    private void loadWLD(File file){
130
        String extWorldFile = null;
131
        File wldFile=null;
132

    
133
        Tags paramsTags=((DynClass_v2)this.getDynClass()).getTags();
134
        if (paramsTags!=null && paramsTags.has(RasterGdalFileStoreParameters.WLD_EXTENSION_TAG_NAME)){
135
            extWorldFile=(String)paramsTags.get(RasterGdalFileStoreParameters.WLD_EXTENSION_TAG_NAME);
136
        }
137
        //If it is a tif or jpg/jpeg we try to use specific extensions, if they exist
138
        if (StringUtils.isNotEmpty(extWorldFile)){
139
            wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+FilenameUtils.EXTENSION_SEPARATOR_STR+extWorldFile);
140
        }
141
        //by default we try to find a .wld file
142
        if (wldFile==null||!wldFile.exists()){
143
            extWorldFile = ".wld";
144
            wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+extWorldFile);
145
        }
146
        if (wldFile.exists()) {
147
            try {
148
                List<String> lines = FileUtils.readLines(wldFile);
149
                if (lines!=null && lines.size()==6){
150
                    setWldParams(lines);
151
                }
152

    
153
            } catch (IOException e) {
154
                logger.warn("Couldn't read wld file");
155
            }
156
        }
157
    }
158

    
159
}