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

History | View | Annotate | Download (7.24 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.dal.store.shp.utils;
42

    
43
import java.io.File;
44

    
45

    
46

    
47
/**
48
 * Clase con las constantes que representan los diferentes tipos de Shape y
49
 * m?todos est?ticos relativos a los shapes.
50
 *
51
 * @author Vicente Caballero Navarro
52
 */
53
public class SHP {
54

    
55
    public static final int NULL = 0;
56
    public static final int POINT2D = 1;
57
    public static final int POLYLINE2D = 3;
58
    public static final int POLYGON2D = 5;
59
    public static final int MULTIPOINT2D = 8;
60
    public static final int POINT3D = 11;
61
    public static final int POLYLINE3D = 13;
62
    public static final int POLYGON3D = 15;
63
    public static final int MULTIPOINT3D = 18;
64
        public final static int POINTM = 21;
65
        public final static int POLYLINEM = 23;
66
        public final static int POLYGONM = 25;
67
        public final static int MULTIPOINTM = 28;
68
    /**
69
     * Crea a partir del tipo de geometr?a un shape del tipo m?s adecuado.
70
     *
71
     * @param type Tipo de geometr?a.
72
     *
73
     * @return Geometr?a m?s adecuada.
74
     *
75
     * @throws ShapefileException Se lanza cuando es causada por la creaci?n
76
     *         del shape.
77
     */
78
    public static SHPShape create(int type) {
79
        SHPShape shape;
80

    
81
        switch (type) {
82
                case 0:
83
                        shape = new SHPNull(type);
84
                        break;
85

    
86
            case 1:
87
            case 11:
88
            case 21:
89
                shape = new SHPPoint(type);
90

    
91
                break;
92

    
93
            case 3:
94
            case 13:
95
            case 23:
96
                shape = new SHPMultiLine(type);
97

    
98
                break;
99

    
100
            case 5:
101
            case 15:
102
            case 25:
103
                shape = new SHPPolygon(type);
104

    
105
                break;
106

    
107
            case 8:
108
            case 18:
109
            case 28:
110
                shape = new SHPMultiPoint(type);
111

    
112
                break;
113

    
114
            default:
115
                shape = null;
116
        }
117

    
118
        return shape;
119
    }
120

    
121
    /**
122
     * Devuelve un array con dos doubles, el primero representa el m?nimo valor
123
     * y el segundo el m?ximo de entre los valores que se pasan como par?metro
124
     * en forma de array.
125
     *
126
     * @param zs Valores a comprobar.
127
     *
128
     * @return Array de doubles con el valor m?nimo y el valor m?ximo.
129
     */
130
    public static double[] getZMinMax(double[] zs) {
131
        if (zs == null) {
132
            return null;
133
        }
134

    
135
        double min = Double.MAX_VALUE;
136
        double max = Double.NEGATIVE_INFINITY;
137

    
138
        for (int i = 0; i < zs.length; i++) {
139
            if (zs[i] > max) {
140
                max = zs[i];
141
            }
142

    
143
            if (zs[i] < min) {
144
                min = zs[i];
145
            }
146
        }
147

    
148
        return new double[] { min, max };
149
    }
150
    public static File getDbfFile(File shpFile){
151
            String str = shpFile.getAbsolutePath();
152
                File directory=shpFile.getParentFile();
153
                File[] files=new File[0];
154
                if (directory!=null){
155
                        MyFileFilter myFileFilter = new MyFileFilter(str);
156
                        files=directory.listFiles(myFileFilter);
157
                }
158
                String[] ends=new String[] {"dbf","DBF","Dbf","dBf","DBf","dbF","DbF","dBF"};
159
                File dbfFile=findEnd(str,files,ends);
160
                return dbfFile;
161
    }
162

    
163
    public static File getShpFile(File dbfFile){
164
            String str = dbfFile.getAbsolutePath();
165
                File directory=dbfFile.getParentFile();
166
                File[] files=new File[0];
167
                if (directory!=null){
168
                        MyFileFilter myFileFilter = new MyFileFilter(str);
169
                        files=directory.listFiles(myFileFilter);
170
                }
171
                String[] ends=new String[] {"shp","SHP","Shp","sHp","SHp","shP","ShP","sHP"};
172
                File shpFile=findEnd(str,files,ends);
173
                return shpFile;
174
    }
175

    
176
    public static File getShxFile(File shpFile){
177
            String str = shpFile.getAbsolutePath();
178
                File directory=shpFile.getParentFile();
179
                File[] files=new File[0];
180
                if (directory!=null){
181
                        MyFileFilter myFileFilter = new MyFileFilter(str);
182
                        files=directory.listFiles(myFileFilter);
183
                }
184
                String[] ends=new String[] {"shx","SHX","Shx","sHx","SHx","shX","ShX","sHX"};
185
                File shxFile=findEnd(str,files,ends);
186
                return shxFile;
187
    }
188

    
189
    public static File getPrjFile(File shpFile) {
190
                String str = shpFile.getAbsolutePath();
191
                File directory = shpFile.getParentFile();
192
                File[] files = new File[0];
193
                if (directory != null) {
194
                        MyFileFilter myFileFilter = new MyFileFilter(str);
195
                        files = directory.listFiles(myFileFilter);
196
                }
197
                String[] ends = new String[] { "prj", "PRJ", "Prj", "pRj", "PRj",
198
                                "prJ", "PrJ", "pRJ" };
199
                File shxFile = findEnd(str, files, ends);
200
                return shxFile;
201
        }
202

    
203
    private static File findEnd(String str,File[] files, String[] ends) {
204
            for (int i=0;i<files.length;i++) {
205
                        File dbfFile=files[i];
206
                        if (dbfFile.getAbsolutePath().endsWith(ends[0])) {
207
                                return dbfFile;
208
                        }
209
                }
210
                for (int i=0;i<files.length;i++) {
211
                        File dbfFile=files[i];
212
                        if (dbfFile.getAbsolutePath().endsWith(ends[1])) {
213
                                return dbfFile;
214
                        }
215
                }
216
                for (int i=0;i<files.length;i++) {
217
                        File dbfFile=files[i];
218
                        if (dbfFile.getAbsolutePath().endsWith(ends[2])) {
219
                                return dbfFile;
220
                        }
221
                }
222
                for (int i=0;i<files.length;i++) {
223
                        File dbfFile=files[i];
224
                        if (dbfFile.getAbsolutePath().endsWith(ends[3])) {
225
                                return dbfFile;
226
                        }
227
                }
228
                for (int i=0;i<files.length;i++) {
229
                        File dbfFile=files[i];
230
                        if (dbfFile.getAbsolutePath().endsWith(ends[4])) {
231
                                return dbfFile;
232
                        }
233
                }
234
                for (int i=0;i<files.length;i++) {
235
                        File dbfFile=files[i];
236
                        if (dbfFile.getAbsolutePath().endsWith(ends[5])) {
237
                                return dbfFile;
238
                        }
239
                }
240
                for (int i=0;i<files.length;i++) {
241
                        File dbfFile=files[i];
242
                        if (dbfFile.getAbsolutePath().endsWith(ends[6])) {
243
                                return dbfFile;
244
                        }
245
                }
246
                for (int i=0;i<files.length;i++) {
247
                        File dbfFile=files[i];
248
                        if (dbfFile.getAbsolutePath().endsWith(ends[7])) {
249
                                return dbfFile;
250
                        }
251
                }
252
                return new File(removeExtension(str)+"."+ends[0]);
253
    }
254
    
255
    private static String removeExtension(String fname){
256
            int i = fname.lastIndexOf(".");
257
            if (i<0){
258
                    return fname;
259
            }
260
            return fname.substring(0,i);
261
    }
262

    
263
}