Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / shp / utils / SHP.java @ 22373

History | View | Annotate | Download (6.54 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.data.feature.file.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 dbfFile;
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
    private static File findEnd(String str,File[] files, String[] ends) {
189
            for (int i=0;i<files.length;i++) {
190
                        File dbfFile=files[i];
191
                        if (dbfFile.getAbsolutePath().endsWith(ends[0]))
192
                                return dbfFile;
193
                }
194
                for (int i=0;i<files.length;i++) {
195
                        File dbfFile=files[i];
196
                        if (dbfFile.getAbsolutePath().endsWith(ends[1]))
197
                                return dbfFile;
198
                }
199
                for (int i=0;i<files.length;i++) {
200
                        File dbfFile=files[i];
201
                        if (dbfFile.getAbsolutePath().endsWith(ends[2]))
202
                                return dbfFile;
203
                }
204
                for (int i=0;i<files.length;i++) {
205
                        File dbfFile=files[i];
206
                        if (dbfFile.getAbsolutePath().endsWith(ends[3]))
207
                                return dbfFile;
208
                }
209
                for (int i=0;i<files.length;i++) {
210
                        File dbfFile=files[i];
211
                        if (dbfFile.getAbsolutePath().endsWith(ends[4]))
212
                                return dbfFile;
213
                }
214
                for (int i=0;i<files.length;i++) {
215
                        File dbfFile=files[i];
216
                        if (dbfFile.getAbsolutePath().endsWith(ends[5]))
217
                                return dbfFile;
218
                }
219
                for (int i=0;i<files.length;i++) {
220
                        File dbfFile=files[i];
221
                        if (dbfFile.getAbsolutePath().endsWith(ends[6]))
222
                                return dbfFile;
223
                }
224
                for (int i=0;i<files.length;i++) {
225
                        File dbfFile=files[i];
226
                        if (dbfFile.getAbsolutePath().endsWith(ends[7]))
227
                                return dbfFile;
228
                }
229
                return new File(str.substring(0, str.length() - 3) + ends[0]);
230
    }
231

    
232
}