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 @ 8697
History | View | Annotate | Download (5.62 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.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 |
if( !file.exists() ) {
|
97 |
return;
|
98 |
} |
99 |
if (getCRS()==null){ |
100 |
loadPRJ(file); |
101 |
} |
102 |
loadWLD(file); |
103 |
if (getCRS()==null){ |
104 |
Dataset gdalDataset = null;
|
105 |
try{
|
106 |
gdalDataset = gdal.Open(this.getFile().getAbsolutePath(),gdalconstConstants.GA_ReadOnly);
|
107 |
String projection=gdalDataset.GetProjection();
|
108 |
if (StringUtils.isNotEmpty(projection)){
|
109 |
ICRSFactory crsFactory = CRSFactory.getCRSFactory(); |
110 |
IProjection crs=crsFactory.get(ICRSFactory.FORMAT_WKT, projection); |
111 |
setCRS(crs); |
112 |
} |
113 |
} finally {
|
114 |
if(gdalDataset!=null){ |
115 |
gdalDataset.delete(); |
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
} |
121 |
|
122 |
private void loadPRJ(File file){ |
123 |
File prjFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+".prj"); |
124 |
if (prjFile.exists()) {
|
125 |
try {
|
126 |
String contentFile = FileUtils.readFileToString(prjFile);
|
127 |
if (StringUtils.isNotEmpty(contentFile)){
|
128 |
IProjection crs=CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, contentFile); |
129 |
setCRS(crs); |
130 |
} |
131 |
|
132 |
} catch (IOException e) { |
133 |
logger.warn("Couldn't read prj file");
|
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
private void loadWLD(File file){ |
139 |
String extWorldFile = null; |
140 |
File wldFile=null; |
141 |
|
142 |
Tags paramsTags=((DynClass_v2)this.getDynClass()).getTags();
|
143 |
if (paramsTags!=null && paramsTags.has(RasterGdalFileStoreParameters.WLD_EXTENSION_TAG_NAME)){ |
144 |
extWorldFile=(String)paramsTags.get(RasterGdalFileStoreParameters.WLD_EXTENSION_TAG_NAME);
|
145 |
} |
146 |
//If it is a tif or jpg/jpeg we try to use specific extensions, if they exist
|
147 |
if (StringUtils.isNotEmpty(extWorldFile)){
|
148 |
wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+FilenameUtils.EXTENSION_SEPARATOR_STR+extWorldFile); |
149 |
} |
150 |
//by default we try to find a .wld file
|
151 |
if (wldFile==null||!wldFile.exists()){ |
152 |
extWorldFile = ".wld";
|
153 |
wldFile = new File(FilenameUtils.removeExtension(file.getAbsolutePath())+extWorldFile); |
154 |
} |
155 |
if (wldFile.exists()) {
|
156 |
try {
|
157 |
List<String> lines = FileUtils.readLines(wldFile); |
158 |
if (lines!=null && lines.size()==6){ |
159 |
setWldParams(lines); |
160 |
} |
161 |
|
162 |
} catch (IOException e) { |
163 |
logger.warn("Couldn't read wld file");
|
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
} |