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 / utils / SHP.java @ 42464

History | View | Annotate | Download (8.15 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
package org.gvsig.fmap.dal.store.shp.utils;
25

    
26
import java.io.File;
27

    
28

    
29

    
30
/**
31
 * Clase con las constantes que representan los diferentes tipos de Shape y
32
 * m?todos est?ticos relativos a los shapes.
33
 *
34
 * @author Vicente Caballero Navarro
35
 */
36
public class SHP {
37

    
38
    public static final int NULL = 0;
39
    public static final int POINT2D = 1;
40
    public static final int POLYLINE2D = 3;
41
    public static final int POLYGON2D = 5;
42
    public static final int MULTIPOINT2D = 8;
43
    public static final int POINT3D = 11;
44
    public static final int POLYLINE3D = 13;
45
    public static final int POLYGON3D = 15;
46
    public static final int MULTIPOINT3D = 18;
47
        public final static int POINTM = 21;
48
        public final static int POLYLINEM = 23;
49
        public final static int POLYGONM = 25;
50
        public final static int MULTIPOINTM = 28;
51
    /**
52
     * Crea a partir del tipo de geometr?a un shape writer del tipo m?s adecuado.
53
     *
54
     * @param type Tipo de geometr?a.
55
     *
56
     * @return ShapeWriter m?s adecuado.
57
     *
58
     * @throws ShapefileException Se lanza cuando es causada por la creaci?n del shape.
59
     */
60
    public static SHPShapeWriter create(int type) {
61
        SHPShapeWriter writer;
62

    
63
        switch (type) {
64
        case NULL:
65
            writer = new SHPNullWriter();
66
            break;
67

    
68
        case POINT2D:
69
        case POINT3D:
70
        case POINTM:
71
            writer = new SHPPointWriter(type);
72
            break;
73

    
74
        case POLYLINE2D:
75
            writer = new SHPMultiLine2DWriter();
76
            break;
77
        case POLYLINE3D:
78
            writer = new SHPMultiLine3DWriter();
79
            break;
80
        case POLYLINEM:
81
            writer = new SHPMultiLine2DMWriter();
82
            break;
83

    
84
        case POLYGON2D:
85
            writer = new SHPPolygon2DWriter();
86
            break;
87
        case POLYGON3D:
88
            writer = new SHPPolygon3DWriter();
89
            break;
90
        case POLYGONM:
91
            writer = new SHPPolygon2DMWriter();
92
            break;
93

    
94
        case MULTIPOINT2D:
95
        case MULTIPOINT3D:
96
        case MULTIPOINTM:
97
            writer = new SHPMultiPointWriter(type);
98
            break;
99

    
100
        default:
101
            writer = null;
102
        }
103

    
104
        return writer;
105
    }
106

    
107
    public static String getTypeName(int type) {
108

    
109
        switch (type) {
110
        case NULL:
111
            return "NULL";
112
        case POINT2D:
113
            return "POINT2D";
114
        case POINT3D:
115
            return "POINT3D";
116
        case POINTM:
117
            return "POINTM";
118
        case POLYLINE2D:
119
            return "POLYLINE2D";
120
        case POLYLINE3D:
121
            return "POLYLINE3D";
122
        case POLYLINEM:
123
            return "POLYLINEM";
124
        case POLYGON2D:
125
            return "POLYGON2D";
126
        case POLYGON3D:
127
            return "POLYGON3D";
128
        case POLYGONM:
129
            return "POLYGONM";
130
        case MULTIPOINT2D:
131
            return "MULTIPOINT2D";
132
        case MULTIPOINT3D:
133
            return "MULTIPOINT3D";
134
        case MULTIPOINTM:
135
            return "MULTIPOINTM";
136
        default:
137
            return Integer.toString(type);
138
        }
139
    }
140

    
141
    /**
142
     * Devuelve un array con dos doubles, el primero representa el m?nimo valor
143
     * y el segundo el m?ximo de entre los valores que se pasan como par?metro
144
     * en forma de array.
145
     *
146
     * @param zs Valores a comprobar.
147
     *
148
     * @return Array de doubles con el valor m?nimo y el valor m?ximo.
149
     */
150
    public static double[] getZMinMax(double[] zs) {
151
        if (zs == null) {
152
            return null;
153
        }
154

    
155
        double min = Double.MAX_VALUE;
156
        double max = Double.NEGATIVE_INFINITY;
157

    
158
        for (int i = 0; i < zs.length; i++) {
159
            if (zs[i] > max) {
160
                max = zs[i];
161
            }
162

    
163
            if (zs[i] < min) {
164
                min = zs[i];
165
            }
166
        }
167

    
168
        return new double[] { min, max };
169
    }
170
    public static File getDbfFile(File shpFile){
171
            String str = shpFile.getAbsolutePath();
172
                File directory=shpFile.getParentFile();
173
                File[] files=new File[0];
174
                if (directory!=null){
175
                        MyFileFilter myFileFilter = new MyFileFilter(str);
176
                        files=directory.listFiles(myFileFilter);
177
                }
178
                String[] ends=new String[] {"dbf","DBF","Dbf","dBf","DBf","dbF","DbF","dBF"};
179
                File dbfFile=findEnd(str,files,ends);
180
                return dbfFile;
181
    }
182

    
183
    public static File getShpFile(File dbfFile){
184
            String str = dbfFile.getAbsolutePath();
185
                File directory=dbfFile.getParentFile();
186
                File[] files=new File[0];
187
                if (directory!=null){
188
                        MyFileFilter myFileFilter = new MyFileFilter(str);
189
                        files=directory.listFiles(myFileFilter);
190
                }
191
                String[] ends=new String[] {"shp","SHP","Shp","sHp","SHp","shP","ShP","sHP"};
192
                File shpFile=findEnd(str,files,ends);
193
                return shpFile;
194
    }
195

    
196
    public static File getShxFile(File shpFile){
197
            String str = shpFile.getAbsolutePath();
198
                File directory=shpFile.getParentFile();
199
                File[] files=new File[0];
200
                if (directory!=null){
201
                        MyFileFilter myFileFilter = new MyFileFilter(str);
202
                        files=directory.listFiles(myFileFilter);
203
                }
204
                String[] ends=new String[] {"shx","SHX","Shx","sHx","SHx","shX","ShX","sHX"};
205
                File shxFile=findEnd(str,files,ends);
206
                return shxFile;
207
    }
208

    
209
    public static File getPrjFile(File shpFile) {
210
                String str = shpFile.getAbsolutePath();
211
                File directory = shpFile.getParentFile();
212
                File[] files = new File[0];
213
                if (directory != null) {
214
                        MyFileFilter myFileFilter = new MyFileFilter(str);
215
                        files = directory.listFiles(myFileFilter);
216
                }
217
                String[] ends = new String[] { "prj", "PRJ", "Prj", "pRj", "PRj",
218
                                "prJ", "PrJ", "pRJ" };
219
                File prjFile = findEnd(str, files, ends);
220
                return prjFile;
221
        }
222

    
223
    private static File findEnd(String str,File[] files, String[] ends) {
224
            for (int i=0;i<files.length;i++) {
225
                        File dbfFile=files[i];
226
                        if (dbfFile.getAbsolutePath().endsWith(ends[0])) {
227
                                return dbfFile;
228
                        }
229
                }
230
                for (int i=0;i<files.length;i++) {
231
                        File dbfFile=files[i];
232
                        if (dbfFile.getAbsolutePath().endsWith(ends[1])) {
233
                                return dbfFile;
234
                        }
235
                }
236
                for (int i=0;i<files.length;i++) {
237
                        File dbfFile=files[i];
238
                        if (dbfFile.getAbsolutePath().endsWith(ends[2])) {
239
                                return dbfFile;
240
                        }
241
                }
242
                for (int i=0;i<files.length;i++) {
243
                        File dbfFile=files[i];
244
                        if (dbfFile.getAbsolutePath().endsWith(ends[3])) {
245
                                return dbfFile;
246
                        }
247
                }
248
                for (int i=0;i<files.length;i++) {
249
                        File dbfFile=files[i];
250
                        if (dbfFile.getAbsolutePath().endsWith(ends[4])) {
251
                                return dbfFile;
252
                        }
253
                }
254
                for (int i=0;i<files.length;i++) {
255
                        File dbfFile=files[i];
256
                        if (dbfFile.getAbsolutePath().endsWith(ends[5])) {
257
                                return dbfFile;
258
                        }
259
                }
260
                for (int i=0;i<files.length;i++) {
261
                        File dbfFile=files[i];
262
                        if (dbfFile.getAbsolutePath().endsWith(ends[6])) {
263
                                return dbfFile;
264
                        }
265
                }
266
                for (int i=0;i<files.length;i++) {
267
                        File dbfFile=files[i];
268
                        if (dbfFile.getAbsolutePath().endsWith(ends[7])) {
269
                                return dbfFile;
270
                        }
271
                }
272
                return new File(removeExtension(str)+"."+ends[0]);
273
    }
274

    
275
    private static String removeExtension(String fname){
276
            int i = fname.lastIndexOf(".");
277
            if (i<0){
278
                    return fname;
279
            }
280
            return fname.substring(0,i);
281
    }
282

    
283
}