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 @ 6489

History | View | Annotate | Download (5.57 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.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39

    
40
import org.gvsig.fmap.crs.CRSFactory;
41
import org.gvsig.fmap.dal.raster.spi.OpenRasterStoreParameters;
42
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
43
import org.gvsig.tools.dynobject.DynClass_v2;
44
import org.gvsig.tools.dynobject.Tags;
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 gdalDataset = null;
103
            try{
104
            gdalDataset = gdal.Open(this.getFile().getAbsolutePath(),gdalconstConstants.GA_ReadOnly);
105
            String projection=gdalDataset.GetProjection();
106
            if (StringUtils.isNotEmpty(projection)){
107
                ICRSFactory crsFactory = CRSFactory.getCRSFactory();
108
                IProjection crs=crsFactory.get(ICRSFactory.FORMAT_WKT, projection);
109
                setCRS(crs);
110
            }
111
            } finally {
112
                if(gdalDataset!=null){
113
                    gdalDataset.delete();
114
                }
115
            }
116
        }
117

    
118
    }
119

    
120
    private void loadPRJ(File file){
121
        File prjFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+".prj");
122
        if (prjFile.exists()) {
123
            try {
124
                String contentFile = FileUtils.readFileToString(prjFile);
125
                if (StringUtils.isNotEmpty(contentFile)){
126
                    IProjection crs=CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, contentFile);
127
                    setCRS(crs);
128
                }
129

    
130
            } catch (IOException e) {
131
                logger.warn("Couldn't read prj file");
132
            }
133
        }
134
    }
135

    
136
    private void loadWLD(File file){
137
        String extWorldFile = null;
138
        File wldFile=null;
139

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

    
160
            } catch (IOException e) {
161
                logger.warn("Couldn't read wld file");
162
            }
163
        }
164
    }
165

    
166
}