Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / shp / SHP.java @ 13521

History | View | Annotate | Download (7.13 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 com.iver.cit.gvsig.fmap.drivers.shp;
42

    
43
import java.io.File;
44
import java.util.BitSet;
45

    
46
import com.iver.cit.gvsig.fmap.core.IGeometry;
47
import com.iver.cit.gvsig.fmap.drivers.shp.write.SHPMultiLine;
48
import com.iver.cit.gvsig.fmap.drivers.shp.write.SHPMultiPoint;
49
import com.iver.cit.gvsig.fmap.drivers.shp.write.SHPPoint;
50
import com.iver.cit.gvsig.fmap.drivers.shp.write.SHPPolygon;
51
import com.iver.cit.gvsig.fmap.drivers.shp.write.SHPShape;
52
import com.iver.cit.gvsig.fmap.drivers.shp.write.ShapefileException;
53
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
54

    
55

    
56
/**
57
 * Clase con las constantes que representan los diferentes tipos de Shape y
58
 * m?todos est?ticos relativos a los shapes.
59
 *
60
 * @author Vicente Caballero Navarro
61
 */
62
public class SHP {
63
    public static final int NULL = 0;
64
    public static final int POINT2D = 1;
65
    public static final int POLYLINE2D = 3;
66
    public static final int POLYGON2D = 5;
67
    public static final int MULTIPOINT2D = 8;
68
    public static final int POINT3D = 11;
69
    public static final int POLYLINE3D = 13;
70
    public static final int POLYGON3D = 15;
71
    public static final int MULTIPOINT3D = 18;
72

    
73
    /**
74
     * Crea a partir del tipo de geometr?a un shape del tipo m?s adecuado.
75
     *
76
     * @param type Tipo de geometr?a.
77
     *
78
     * @return Geometr?a m?s adecuada.
79
     *
80
     * @throws ShapefileException Se lanza cuando es causada por la creaci?n
81
     *         del shape.
82
     */
83
    public static SHPShape create(int type) throws ShapefileException {
84
        SHPShape shape;
85

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

    
92
                break;
93

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

    
99
                break;
100

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

    
106
                break;
107

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

    
113
                break;
114

    
115
            default:
116
                shape = null;
117
        }
118

    
119
        return shape;
120
    }
121

    
122
    /**
123
     * Crea un fichero en formato shape con las geometr?as que se pasan como
124
     * par?metro en forma de array, un bitset para saber las seleccionadas, un
125
     * SelectableDataSource para obtener los valores y el fichero a crear.
126
     *
127
     * @param fgs Array de geometr?as.
128
     * @param bitset Bitset con la selecci?n.
129
     * @param sds SelectableDataSource.
130
     * @param f Fichero a crear o modificar.
131
     */
132
    public static void SHPFileFromGeometries(IGeometry[] fgs, BitSet bitset,
133
        SelectableDataSource sds, File f) {
134
        SHPOnlyFromGeometries(fgs, f);
135

    
136
        DBFFromGeometries dfg = new DBFFromGeometries(fgs, f);
137
        dfg.create(sds, bitset);
138
    }
139

    
140
    /**
141
     * Obtiene un fichero shape con las geometr?as que se pasan como par?metro,
142
     * sin crear un dbf.
143
     *
144
     * @param fgs Array de IGeometries.
145
     * @param f Fichero que hay que crear.
146
     */
147
    public static void SHPOnlyFromGeometries(IGeometry[] fgs, File f) {
148
        SHPSHXFromGeometries ssfg = new SHPSHXFromGeometries(fgs, f);
149
        ssfg.create();
150
    }
151

    
152
    /**
153
     * Devuelve un array con dos doubles, el primero representa el m?nimo valor
154
     * y el segundo el m?ximo de entre los valores que se pasan como par?metro
155
     * en forma de array.
156
     *
157
     * @param zs Valores a comprobar.
158
     *
159
     * @return Array de doubles con el valor m?nimo y el valor m?ximo.
160
     */
161
    public static double[] getZMinMax(double[] zs) {
162
        if (zs == null) {
163
            return null;
164
        }
165

    
166
        double min = Double.MAX_VALUE;
167
        double max = Double.NEGATIVE_INFINITY;
168

    
169
        for (int i = 0; i < zs.length; i++) {
170
            if (zs[i] > max) {
171
                max = zs[i];
172
            }
173

    
174
            if (zs[i] < min) {
175
                min = zs[i];
176
            }
177
        }
178

    
179
        return new double[] { min, max };
180
    }
181
    public static File getDbfFile(File shpFile){
182
            String str = shpFile.getAbsolutePath();
183
                File directory=shpFile.getParentFile();
184
                MyFileFilter myFileFilter = new MyFileFilter(str);
185
                File[] files=directory.listFiles(myFileFilter);
186
                String[] ends=new String[] {"dbf","DBF","Dbf","dBf","DBf","dbF","DbF","dBF"};
187
                File dbfFile=findEnd(str,files,ends);
188
                return dbfFile;
189
    }
190
    public static File getShxFile(File shpFile){
191
            String str = shpFile.getAbsolutePath();
192
                File directory=shpFile.getParentFile();
193
                MyFileFilter myFileFilter = new MyFileFilter(str);
194
                File[] files=directory.listFiles(myFileFilter);
195
                String[] ends=new String[] {"shx","SHX","Shx","sHx","SHx","shX","ShX","sHX"};
196
                File shxFile=findEnd(str,files,ends);
197
                return shxFile;
198
    } 
199
    private static File findEnd(String str,File[] files, String[] ends) {
200
            for (int i=0;i<files.length;i++) {
201
                        File dbfFile=files[i];
202
                        if (dbfFile.getAbsolutePath().endsWith(ends[0]))
203
                                return dbfFile;
204
                }
205
                for (int i=0;i<files.length;i++) {
206
                        File dbfFile=files[i];
207
                        if (dbfFile.getAbsolutePath().endsWith(ends[1]))
208
                                return dbfFile;
209
                }
210
                for (int i=0;i<files.length;i++) {
211
                        File dbfFile=files[i];
212
                        if (dbfFile.getAbsolutePath().endsWith(ends[2]))
213
                                return dbfFile;
214
                }
215
                for (int i=0;i<files.length;i++) {
216
                        File dbfFile=files[i];
217
                        if (dbfFile.getAbsolutePath().endsWith(ends[3]))
218
                                return dbfFile;
219
                }
220
                for (int i=0;i<files.length;i++) {
221
                        File dbfFile=files[i];
222
                        if (dbfFile.getAbsolutePath().endsWith(ends[4]))
223
                                return dbfFile;
224
                }
225
                for (int i=0;i<files.length;i++) {
226
                        File dbfFile=files[i];
227
                        if (dbfFile.getAbsolutePath().endsWith(ends[5]))
228
                                return dbfFile;
229
                }
230
                for (int i=0;i<files.length;i++) {
231
                        File dbfFile=files[i];
232
                        if (dbfFile.getAbsolutePath().endsWith(ends[6]))
233
                                return dbfFile;
234
                }
235
                for (int i=0;i<files.length;i++) {
236
                        File dbfFile=files[i];
237
                        if (dbfFile.getAbsolutePath().endsWith(ends[7]))
238
                                return dbfFile;
239
                }
240
                return new File(str.substring(0, str.length() - 3) + ends[0]);
241
    }
242

    
243
}