Statistics
| Revision:

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

History | View | Annotate | Download (5.3 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
    private 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

    
50
        public SHPStoreParameters() {
51
                this(PARAMETERS_DEFINITION_NAME);
52
        }
53

    
54
        @Override
55
        public void validate() throws ValidateDataParametersException {
56
                fixParameters();
57
                super.validate();
58
        }
59

    
60
        public void fixParameters() {
61
                File file = this.getSHPFile();
62
                if( file!=null ) {
63
                        if (this.getDBFFile() == null){
64
                                this.setDBFFile(SHP.getDbfFile(file));
65
                        }
66
                        if (this.getSHXFile() == null) {
67
                                this.setSHXFile(SHP.getShxFile(file));
68
                        }
69
                }
70
        }
71

    
72
        public SHPStoreParameters(String parametersDefinitionName) {
73
                super(parametersDefinitionName, SHPStoreProvider.NAME);
74
        }
75

    
76
        public boolean isValid() {
77
                return super.isValid() && (this.getSHPFileName() != null);
78
        }
79

    
80
        public File getFile() {
81
                return this.getSHPFile();
82
        }
83

    
84
        public void setFile(File file) {
85
                this.setSHPFile(file);
86
        }
87
        public void setFile(String fileName) {
88
                this.setSHPFile(fileName);
89
        }
90

    
91

    
92
        public String getSHPFileName() {
93
                if( this.getSHPFile()==null ) {
94
                        return null;
95
                }
96
                return this.getSHPFile().getAbsolutePath();
97
        }
98

    
99
        public File getSHPFile() {
100
                return (File) this.getDynValue(SHPFILE_PARAMTER_NAME);
101
        }
102

    
103
        public void setSHPFile(File file) {
104
                this.setDynValue(SHPFILE_PARAMTER_NAME, file);
105
                if (this.getDBFFile() == null){
106
                        this.setDBFFile(SHP.getDbfFile(file));
107
                }
108
                if (this.getSHXFile() == null) {
109
                        this.setSHXFile(SHP.getShxFile(file));
110
                }
111
        if (getCRS() == null){
112
            String wktEsri = loadPrj(file);
113
            if (wktEsri != null) {
114
                IProjection proj = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, wktEsri);
115
                setCRS(proj);
116
            }
117
        }
118

    
119
        }
120

    
121
        public void setSHPFile(String fileName) {
122
                this.setDynValue(SHPFILE_PARAMTER_NAME, fileName);
123
                File file = (File) this.getDynValue(SHPFILE_PARAMTER_NAME);
124
                if (this.getDBFFile() == null){
125
                        this.setDBFFile(SHP.getDbfFile(file));
126
                }
127
                if (this.getSHXFile() == null) {
128
                        this.setSHXFile(SHP.getShxFile(file));
129
                }
130
        if (getCRS() == null){
131
            String wktEsri = loadPrj(file);
132
            if (wktEsri != null) {
133
                IProjection proj = CRSFactory.getCRSFactory().get(ICRSFactory.FORMAT_WKT_ESRI, wktEsri);
134
                setCRS(proj);
135
            }
136
        }
137
        }
138

    
139
        private String loadPrj(File shpFile){
140
        File prjFile = SHP.getPrjFile(shpFile);
141
        if (prjFile.exists()) {
142
            try {
143
                return FileUtils.readFileToString(prjFile);
144
            } catch (IOException e) {
145
                return null;
146
            }
147
        }
148
        return null;
149
        }
150

    
151
        public String getSHXFileName() {
152
                if( this.getSHXFile()==null ) {
153
                        return null;
154
                }
155
                return this.getSHXFile().getPath();
156
        }
157

    
158
        public File getSHXFile() {
159
                return (File) this.getDynValue(SHXFILE_PARAMTER_NAME);
160
        }
161
        public void setSHXFile(File file) {
162
                this.setDynValue(SHXFILE_PARAMTER_NAME, file);
163
        }
164

    
165
        public void setSHXFile(String fileName) {
166
                this.setDynValue(SHXFILE_PARAMTER_NAME, fileName);
167
        }
168

    
169
        public void setCRS(IProjection srs) {
170
                setDynValue(CRS_PARAMTER_NAME, srs);
171
        }
172

    
173
        public void setCRS(String srs) {
174
                setDynValue(CRS_PARAMTER_NAME, srs);
175
        }
176

    
177
        public IProjection getCRS() {
178
                return (IProjection) getDynValue(CRS_PARAMTER_NAME);
179
        }
180

    
181
        public boolean getUseNullGeometry() {
182
                return BooleanUtils.isTrue((Boolean) getDynValue(USE_NULLGEOMETRY_PARAMTER_NAME));
183
        }
184

    
185
        public boolean getAllowInconsistenciesInGeometryType() {
186
                return BooleanUtils.isTrue((Boolean) getDynValue(ALLOW_INCONSISTENCIES_IN_GEOMETRY_TYPE));
187
        }
188
}