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 @ 40559
History | View | Annotate | Download (6.96 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 del tipo m?s adecuado.
|
53 |
*
|
54 |
* @param type Tipo de geometr?a.
|
55 |
*
|
56 |
* @return Geometr?a m?s adecuada.
|
57 |
*
|
58 |
* @throws ShapefileException Se lanza cuando es causada por la creaci?n
|
59 |
* del shape.
|
60 |
*/
|
61 |
public static SHPShape create(int type) { |
62 |
SHPShape shape; |
63 |
|
64 |
switch (type) {
|
65 |
case 0: |
66 |
shape = new SHPNull(type);
|
67 |
break;
|
68 |
|
69 |
case 1: |
70 |
case 11: |
71 |
case 21: |
72 |
shape = new SHPPoint(type);
|
73 |
|
74 |
break;
|
75 |
|
76 |
case 3: |
77 |
case 13: |
78 |
case 23: |
79 |
shape = new SHPMultiLine(type);
|
80 |
|
81 |
break;
|
82 |
|
83 |
case 5: |
84 |
case 15: |
85 |
case 25: |
86 |
shape = new SHPPolygon(type);
|
87 |
|
88 |
break;
|
89 |
|
90 |
case 8: |
91 |
case 18: |
92 |
case 28: |
93 |
shape = new SHPMultiPoint(type);
|
94 |
|
95 |
break;
|
96 |
|
97 |
default:
|
98 |
shape = null;
|
99 |
} |
100 |
|
101 |
return shape;
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* Devuelve un array con dos doubles, el primero representa el m?nimo valor
|
106 |
* y el segundo el m?ximo de entre los valores que se pasan como par?metro
|
107 |
* en forma de array.
|
108 |
*
|
109 |
* @param zs Valores a comprobar.
|
110 |
*
|
111 |
* @return Array de doubles con el valor m?nimo y el valor m?ximo.
|
112 |
*/
|
113 |
public static double[] getZMinMax(double[] zs) { |
114 |
if (zs == null) { |
115 |
return null; |
116 |
} |
117 |
|
118 |
double min = Double.MAX_VALUE; |
119 |
double max = Double.NEGATIVE_INFINITY; |
120 |
|
121 |
for (int i = 0; i < zs.length; i++) { |
122 |
if (zs[i] > max) {
|
123 |
max = zs[i]; |
124 |
} |
125 |
|
126 |
if (zs[i] < min) {
|
127 |
min = zs[i]; |
128 |
} |
129 |
} |
130 |
|
131 |
return new double[] { min, max }; |
132 |
} |
133 |
public static File getDbfFile(File shpFile){ |
134 |
String str = shpFile.getAbsolutePath();
|
135 |
File directory=shpFile.getParentFile();
|
136 |
File[] files=new File[0]; |
137 |
if (directory!=null){ |
138 |
MyFileFilter myFileFilter = new MyFileFilter(str);
|
139 |
files=directory.listFiles(myFileFilter); |
140 |
} |
141 |
String[] ends=new String[] {"dbf","DBF","Dbf","dBf","DBf","dbF","DbF","dBF"}; |
142 |
File dbfFile=findEnd(str,files,ends);
|
143 |
return dbfFile;
|
144 |
} |
145 |
|
146 |
public static File getShpFile(File dbfFile){ |
147 |
String str = dbfFile.getAbsolutePath();
|
148 |
File directory=dbfFile.getParentFile();
|
149 |
File[] files=new File[0]; |
150 |
if (directory!=null){ |
151 |
MyFileFilter myFileFilter = new MyFileFilter(str);
|
152 |
files=directory.listFiles(myFileFilter); |
153 |
} |
154 |
String[] ends=new String[] {"shp","SHP","Shp","sHp","SHp","shP","ShP","sHP"}; |
155 |
File shpFile=findEnd(str,files,ends);
|
156 |
return shpFile;
|
157 |
} |
158 |
|
159 |
public static File getShxFile(File shpFile){ |
160 |
String str = shpFile.getAbsolutePath();
|
161 |
File directory=shpFile.getParentFile();
|
162 |
File[] files=new File[0]; |
163 |
if (directory!=null){ |
164 |
MyFileFilter myFileFilter = new MyFileFilter(str);
|
165 |
files=directory.listFiles(myFileFilter); |
166 |
} |
167 |
String[] ends=new String[] {"shx","SHX","Shx","sHx","SHx","shX","ShX","sHX"}; |
168 |
File shxFile=findEnd(str,files,ends);
|
169 |
return shxFile;
|
170 |
} |
171 |
|
172 |
public static File getPrjFile(File shpFile) { |
173 |
String str = shpFile.getAbsolutePath();
|
174 |
File directory = shpFile.getParentFile();
|
175 |
File[] files = new File[0]; |
176 |
if (directory != null) { |
177 |
MyFileFilter myFileFilter = new MyFileFilter(str);
|
178 |
files = directory.listFiles(myFileFilter); |
179 |
} |
180 |
String[] ends = new String[] { "prj", "PRJ", "Prj", "pRj", "PRj", |
181 |
"prJ", "PrJ", "pRJ" }; |
182 |
File shxFile = findEnd(str, files, ends);
|
183 |
return shxFile;
|
184 |
} |
185 |
|
186 |
private static File findEnd(String str,File[] files, String[] ends) { |
187 |
for (int i=0;i<files.length;i++) { |
188 |
File dbfFile=files[i];
|
189 |
if (dbfFile.getAbsolutePath().endsWith(ends[0])) { |
190 |
return dbfFile;
|
191 |
} |
192 |
} |
193 |
for (int i=0;i<files.length;i++) { |
194 |
File dbfFile=files[i];
|
195 |
if (dbfFile.getAbsolutePath().endsWith(ends[1])) { |
196 |
return dbfFile;
|
197 |
} |
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 |
} |
205 |
for (int i=0;i<files.length;i++) { |
206 |
File dbfFile=files[i];
|
207 |
if (dbfFile.getAbsolutePath().endsWith(ends[3])) { |
208 |
return dbfFile;
|
209 |
} |
210 |
} |
211 |
for (int i=0;i<files.length;i++) { |
212 |
File dbfFile=files[i];
|
213 |
if (dbfFile.getAbsolutePath().endsWith(ends[4])) { |
214 |
return dbfFile;
|
215 |
} |
216 |
} |
217 |
for (int i=0;i<files.length;i++) { |
218 |
File dbfFile=files[i];
|
219 |
if (dbfFile.getAbsolutePath().endsWith(ends[5])) { |
220 |
return dbfFile;
|
221 |
} |
222 |
} |
223 |
for (int i=0;i<files.length;i++) { |
224 |
File dbfFile=files[i];
|
225 |
if (dbfFile.getAbsolutePath().endsWith(ends[6])) { |
226 |
return dbfFile;
|
227 |
} |
228 |
} |
229 |
for (int i=0;i<files.length;i++) { |
230 |
File dbfFile=files[i];
|
231 |
if (dbfFile.getAbsolutePath().endsWith(ends[7])) { |
232 |
return dbfFile;
|
233 |
} |
234 |
} |
235 |
return new File(removeExtension(str)+"."+ends[0]); |
236 |
} |
237 |
|
238 |
private static String removeExtension(String fname){ |
239 |
int i = fname.lastIndexOf("."); |
240 |
if (i<0){ |
241 |
return fname;
|
242 |
} |
243 |
return fname.substring(0,i); |
244 |
} |
245 |
|
246 |
} |