svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / SHPStoreParameters.java @ 43246
History | View | Annotate | Download (5.76 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 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 3
|
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 |
|
25 |
package org.gvsig.fmap.dal.store.shp; |
26 |
|
27 |
import java.io.File; |
28 |
import java.io.IOException; |
29 |
|
30 |
import org.apache.commons.io.FileUtils; |
31 |
import org.apache.commons.lang3.BooleanUtils; |
32 |
import org.cresques.cts.ICRSFactory; |
33 |
import org.cresques.cts.IProjection; |
34 |
|
35 |
import org.gvsig.fmap.crs.CRSFactory; |
36 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
37 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
38 |
import org.gvsig.fmap.dal.store.shp.utils.SHP; |
39 |
|
40 |
public class SHPStoreParameters extends DBFStoreParameters { |
41 |
|
42 |
public static final String PARAMETERS_DEFINITION_NAME = "SHPStoreParameters"; |
43 |
|
44 |
private static final String SHXFILE_PARAMTER_NAME = "shxfile"; |
45 |
/* friend */ static final String SHPFILE_PARAMTER_NAME = "shpfile"; |
46 |
private static final String CRS_PARAMTER_NAME = "CRS"; |
47 |
private static final String USE_NULLGEOMETRY_PARAMTER_NAME = "useNullGeometry"; |
48 |
private static final String ALLOW_INCONSISTENCIES_IN_GEOMETRY_TYPE = "allowInconsistenciesInGeometryType"; |
49 |
private static final String LOAD_CORRUPT_GEOMETRIES_AS_NULL = "loadCorruptGeometriesAsNull"; |
50 |
private static final String FIX_LINEARRINGS = "fixLinearRings"; |
51 |
|
52 |
public SHPStoreParameters() {
|
53 |
this(PARAMETERS_DEFINITION_NAME);
|
54 |
} |
55 |
|
56 |
@Override
|
57 |
public void validate() throws ValidateDataParametersException { |
58 |
fixParameters(); |
59 |
super.validate();
|
60 |
} |
61 |
|
62 |
public void fixParameters() { |
63 |
File file = this.getSHPFile(); |
64 |
if( file!=null ) { |
65 |
if (this.getDBFFile() == null){ |
66 |
this.setDBFFile(SHP.getDbfFile(file));
|
67 |
} |
68 |
if (this.getSHXFile() == null) { |
69 |
this.setSHXFile(SHP.getShxFile(file));
|
70 |
} |
71 |
} |
72 |
} |
73 |
|
74 |
public SHPStoreParameters(String parametersDefinitionName) { |
75 |
super(parametersDefinitionName, SHPStoreProvider.NAME);
|
76 |
} |
77 |
|
78 |
public boolean isValid() { |
79 |
return super.isValid() && (this.getSHPFileName() != null); |
80 |
} |
81 |
|
82 |
public File getFile() { |
83 |
return this.getSHPFile(); |
84 |
} |
85 |
|
86 |
public void setFile(File file) { |
87 |
this.setSHPFile(file);
|
88 |
} |
89 |
public void setFile(String fileName) { |
90 |
this.setSHPFile(fileName);
|
91 |
} |
92 |
|
93 |
|
94 |
public String getSHPFileName() { |
95 |
if( this.getSHPFile()==null ) { |
96 |
return null; |
97 |
} |
98 |
return this.getSHPFile().getAbsolutePath(); |
99 |
} |
100 |
|
101 |
public File getSHPFile() { |
102 |
return (File) this.getDynValue(SHPFILE_PARAMTER_NAME); |
103 |
} |
104 |
|
105 |
public void setSHPFile(File file) { |
106 |
this.setDynValue(SHPFILE_PARAMTER_NAME, file);
|
107 |
if (this.getDBFFile() == null){ |
108 |
this.setDBFFile(SHP.getDbfFile(file));
|
109 |
} |
110 |
if (this.getSHXFile() == null) { |
111 |
this.setSHXFile(SHP.getShxFile(file));
|
112 |
} |
113 |
if (getCRS() == null){ |
114 |
String wktEsri = loadPrj(file);
|
115 |
if (wktEsri != null) { |
116 |
IProjection proj = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, wktEsri); |
117 |
setCRS(proj); |
118 |
} |
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
public void setSHPFile(String fileName) { |
124 |
this.setDynValue(SHPFILE_PARAMTER_NAME, fileName);
|
125 |
File file = (File) this.getDynValue(SHPFILE_PARAMTER_NAME); |
126 |
if (this.getDBFFile() == null){ |
127 |
this.setDBFFile(SHP.getDbfFile(file));
|
128 |
} |
129 |
if (this.getSHXFile() == null) { |
130 |
this.setSHXFile(SHP.getShxFile(file));
|
131 |
} |
132 |
if (getCRS() == null){ |
133 |
String wktEsri = loadPrj(file);
|
134 |
if (wktEsri != null) { |
135 |
IProjection proj = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, wktEsri); |
136 |
setCRS(proj); |
137 |
} |
138 |
} |
139 |
} |
140 |
|
141 |
private String loadPrj(File shpFile){ |
142 |
File prjFile = SHP.getPrjFile(shpFile);
|
143 |
if (prjFile.exists()) {
|
144 |
try {
|
145 |
return FileUtils.readFileToString(prjFile);
|
146 |
} catch (IOException e) { |
147 |
return null; |
148 |
} |
149 |
} |
150 |
return null; |
151 |
} |
152 |
|
153 |
public String getSHXFileName() { |
154 |
if( this.getSHXFile()==null ) { |
155 |
return null; |
156 |
} |
157 |
return this.getSHXFile().getPath(); |
158 |
} |
159 |
|
160 |
public File getSHXFile() { |
161 |
return (File) this.getDynValue(SHXFILE_PARAMTER_NAME); |
162 |
} |
163 |
public void setSHXFile(File file) { |
164 |
this.setDynValue(SHXFILE_PARAMTER_NAME, file);
|
165 |
} |
166 |
|
167 |
public void setSHXFile(String fileName) { |
168 |
this.setDynValue(SHXFILE_PARAMTER_NAME, fileName);
|
169 |
} |
170 |
|
171 |
public void setCRS(IProjection srs) { |
172 |
setDynValue(CRS_PARAMTER_NAME, srs); |
173 |
} |
174 |
|
175 |
public void setCRS(String srs) { |
176 |
setDynValue(CRS_PARAMTER_NAME, srs); |
177 |
} |
178 |
|
179 |
public IProjection getCRS() {
|
180 |
return (IProjection) getDynValue(CRS_PARAMTER_NAME);
|
181 |
} |
182 |
|
183 |
public boolean getUseNullGeometry() { |
184 |
return BooleanUtils.isTrue((Boolean) getDynValue(USE_NULLGEOMETRY_PARAMTER_NAME)); |
185 |
} |
186 |
|
187 |
public boolean getAllowInconsistenciesInGeometryType() { |
188 |
return BooleanUtils.isTrue((Boolean) getDynValue(ALLOW_INCONSISTENCIES_IN_GEOMETRY_TYPE)); |
189 |
} |
190 |
|
191 |
public boolean getLoadCorruptGeometriesAsNull() { |
192 |
return BooleanUtils.isTrue((Boolean) getDynValue(LOAD_CORRUPT_GEOMETRIES_AS_NULL)); |
193 |
} |
194 |
|
195 |
public boolean getFixLinearRings() { |
196 |
return BooleanUtils.isTrue((Boolean) getDynValue(FIX_LINEARRINGS)); |
197 |
} |
198 |
} |