svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / utils / GeoUtils.java @ 5697
History | View | Annotate | Download (8.27 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 |
package org.gvsig.georeferencing.utils; |
20 |
|
21 |
import java.awt.geom.Point2D; |
22 |
import java.awt.geom.Rectangle2D; |
23 |
import java.io.File; |
24 |
import java.io.FileInputStream; |
25 |
import java.io.FileNotFoundException; |
26 |
import java.io.FileOutputStream; |
27 |
import java.io.IOException; |
28 |
import java.io.InputStream; |
29 |
import java.io.OutputStream; |
30 |
|
31 |
import com.iver.cit.gvsig.fmap.ViewPort; |
32 |
|
33 |
public class GeoUtils { |
34 |
public static double INCHESMTR = 39.73007874D; |
35 |
public static double INCHESCM = 0.3973007874D; |
36 |
public static double INCHESMM = 0.03973007874D; |
37 |
public static double MTRSINCH = 0.0254D; |
38 |
public static double MMSINCH = 25.4D; |
39 |
public static double CMSINCH = 2.54D; |
40 |
|
41 |
|
42 |
/**
|
43 |
* Recorta el n?mero de decimales a n del n?mero pasado por par?metro
|
44 |
* @param num Valor a recortar el n?mero de decimales
|
45 |
* @param n N?mero de decimales a recortar
|
46 |
* @return N?mero recortado
|
47 |
*/
|
48 |
public static double tailDecimals(double num, int n){ |
49 |
long m = (long)Math.pow(10, n); |
50 |
num *= m; |
51 |
long aux = ((long)num); |
52 |
num = (double)((double)aux / (double)m); |
53 |
return num;
|
54 |
} |
55 |
|
56 |
/**
|
57 |
* Recorta el n?mero de decimales a n del n?mero pasado por par?metro
|
58 |
* @param num Valor a recortar el n?mero de decimales
|
59 |
* @param n N?mero de decimales a recortar
|
60 |
* @return N?mero recortado
|
61 |
*/
|
62 |
public static String tailDecimals(String num, int n){ |
63 |
int index = num.indexOf("."); |
64 |
if(index != -1){ |
65 |
try{
|
66 |
num = num.substring(0, index + n);
|
67 |
}catch(IndexOutOfBoundsException e){ |
68 |
//No hacemos nada pq retornar? num
|
69 |
} |
70 |
} |
71 |
return num;
|
72 |
} |
73 |
|
74 |
/**
|
75 |
* Convierte pixels en milimetros dependiendo del los puntos por pulgada
|
76 |
* @param pixels N?mero de pixels
|
77 |
* @param ppp Puntos por pulgada
|
78 |
* @return N?mero de milimetros
|
79 |
*/
|
80 |
public static double convertPixelsToMms(int pixels, int ppp){ |
81 |
return convertPixelsToInches(pixels, ppp) * MMSINCH;
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* Convierte pixels en centimetros dependiendo del los metros por pixel
|
86 |
* @param pixels N?mero de pixels
|
87 |
* @param mtsPixel Metros por pixel
|
88 |
* @return N?mero de centimetros
|
89 |
*/
|
90 |
public static double convertPixelsToCms(int pixels, int ppp){ |
91 |
return convertPixelsToInches(pixels, ppp) * CMSINCH;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Convierte pixels en metros dependiendo del los metros por pixel
|
96 |
* @param pixels N?mero de pixels
|
97 |
* @param mtsPixel Metros por pixel
|
98 |
* @return N?mero de metros
|
99 |
*/
|
100 |
public static double convertPixelsToMts(int pixels, int ppp){ |
101 |
return convertPixelsToInches(pixels, ppp) * MTRSINCH;
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* Convierte pixels en pulgadas dependiendo del los metros por pixel
|
106 |
* @param pixels N?mero de pixels
|
107 |
* @param mtsPixel Metros por pixel
|
108 |
* @return N?mero de pulgadas
|
109 |
*/
|
110 |
public static double convertPixelsToInches(int pixels, int ppp){ |
111 |
return (double)((double)pixels / (double)ppp); |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Convierte metros en pixels dependiendo de los puntos por pulgada
|
116 |
* @param mts N?mero de metros
|
117 |
* @param ppp Puntos por pulgada
|
118 |
* @return N?mero de pixeles
|
119 |
*/
|
120 |
public static int convertMtsToPixels(double mts, int ppp){ |
121 |
return (int)(mts * GeoUtils.INCHESMTR * ppp); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Convierte pulgadas en pixels dependiendo de los puntos por pulgada
|
126 |
* @param mts N?mero de pulgadas
|
127 |
* @param ppp Puntos por pulgada
|
128 |
* @return N?mero de pixeles
|
129 |
*/
|
130 |
public static int convertInchesToPixels(double inches, int ppp){ |
131 |
return (int)(inches * ppp); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Convierte centimetros en pixels dependiendo de los puntos por pulgada
|
136 |
* @param mts N?mero de centmetros
|
137 |
* @param ppp Puntos por pulgada
|
138 |
* @return N?mero de pixeles
|
139 |
*/
|
140 |
public static int convertCmsToPixels(double cms, int ppp){ |
141 |
return (int)(cms * GeoUtils.INCHESCM * ppp); |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Convierte milimetros en pixels dependiendo de los puntos por pulgada
|
146 |
* @param mts N?mero de milimetros
|
147 |
* @param ppp Puntos por pulgada
|
148 |
* @return N?mero de pixeles
|
149 |
*/
|
150 |
public static int convertMmsToPixels(double mms, int ppp){ |
151 |
return (int)(mms * GeoUtils.INCHESMM * ppp); |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Devuelve la traspuesta de una matriz
|
156 |
* @param M Matriz
|
157 |
* @param m N?mero de filas
|
158 |
* @param n N?mero de columnas
|
159 |
* @return Matriz traspuesta
|
160 |
*/
|
161 |
public static double[][] transpose(double[][] M, int m, int n){ |
162 |
double[][] a = new double[n][m]; |
163 |
int i, j;
|
164 |
|
165 |
for (i = 0; i < n; i++) |
166 |
for (j = 0; j < m; j++) |
167 |
a[i][j] = M[j][i]; |
168 |
return a;
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* Producto de matrices (AxB)
|
173 |
* @param a Matriz A
|
174 |
* @param b Matriz B
|
175 |
* @param M
|
176 |
* @param N
|
177 |
* @param O
|
178 |
* @return
|
179 |
*/
|
180 |
public static double[][] multmatrix(double[][] a, double[][] b, int M, int N, int O){ |
181 |
double[][] c = new double[M][O]; |
182 |
double sum;
|
183 |
int i, j, k;
|
184 |
|
185 |
for (i = 0; i < M; i++) { |
186 |
for (j = 0; j < O; j++) { |
187 |
sum = 0;
|
188 |
for (k = 0; k < N; k++) |
189 |
sum += b[k][j] * a[i][k]; |
190 |
c[i][j] = sum; |
191 |
} |
192 |
} |
193 |
return (c);
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* Calcula el zoom dependiendo del factor de escala pasado por
|
198 |
* par?metro.
|
199 |
*/
|
200 |
public static ViewPort calcZoom(ViewPort vp, double factor){ |
201 |
ViewPort viewPortBlock = new ViewPort(vp.getProjection());
|
202 |
viewPortBlock.setExtent(vp.getExtent()); |
203 |
viewPortBlock.setImageSize(vp.getImageSize()); |
204 |
viewPortBlock.setScale(); |
205 |
|
206 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
207 |
double nuevoX = (vp.getExtent().getCenterX()) - ((vp.getExtent().getWidth() * factor) / 2.0); |
208 |
double nuevoY = (vp.getExtent().getCenterY()) - ((vp.getExtent().getHeight() * factor) / 2.0); |
209 |
r.x = nuevoX; |
210 |
r.y = nuevoY; |
211 |
r.width = viewPortBlock.getExtent().getWidth() * factor; |
212 |
r.height = viewPortBlock.getExtent().getHeight() * factor; |
213 |
viewPortBlock.setExtent(r); |
214 |
viewPortBlock.setScale(); |
215 |
|
216 |
return viewPortBlock;
|
217 |
} |
218 |
|
219 |
/**
|
220 |
* Desplaza el extent al nuevo centro
|
221 |
* @param pto
|
222 |
*/
|
223 |
public static ViewPort shiftExtent(Point2D oldCenter, Point2D newCenter, ViewPort vp){ |
224 |
double difX = oldCenter.getX() - newCenter.getX();
|
225 |
double difY = oldCenter.getY() - newCenter.getY();
|
226 |
|
227 |
ViewPort viewPortBlock = new ViewPort(vp.getProjection());
|
228 |
double minX = vp.getExtent().getMinX() - difX;
|
229 |
double minY = vp.getExtent().getMinY() - difY;
|
230 |
Rectangle2D ex = new Rectangle2D.Double(minX, minY, vp.getExtent().getWidth(), vp.getExtent().getHeight()); |
231 |
viewPortBlock.setExtent(ex); |
232 |
viewPortBlock.setImageSize(vp.getImageSize()); |
233 |
viewPortBlock.setScale(); |
234 |
|
235 |
return viewPortBlock;
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Hace una copia de seguridad sobre el fichero que le pasamos por par?metro.
|
240 |
* El fichero de copia se llama como el original pero acabado en ~. Si ya existiese
|
241 |
* un fichero nombrado as? se nombraria como filename.rmf~1, filename.rmf~2, y as?
|
242 |
* sucesivamente.
|
243 |
* @param fName Nombre del fichero que se quiere hacer un backup.
|
244 |
*/
|
245 |
public static void fileBackup(String fName){ |
246 |
File backupFile = new File(fName+"~"); |
247 |
int nBackupFile = 1; |
248 |
while(backupFile.exists()){
|
249 |
backupFile = new File(fName+"~"+nBackupFile); |
250 |
nBackupFile ++; |
251 |
} |
252 |
File inFile = new File(fName); |
253 |
|
254 |
try{
|
255 |
InputStream in = new FileInputStream(inFile); |
256 |
OutputStream out = new FileOutputStream(backupFile); |
257 |
|
258 |
byte[] buf = new byte[1024]; |
259 |
int len;
|
260 |
while ((len = in.read(buf)) > 0) |
261 |
out.write(buf, 0, len);
|
262 |
|
263 |
in.close(); |
264 |
out.close(); |
265 |
}catch(FileNotFoundException exc){ |
266 |
System.err.print("RMF Backup isn't possible: File not found, "+fName); |
267 |
}catch(IOException exc){ |
268 |
System.err.print("RMF Backup isn't possible: IOException, "+fName); |
269 |
} |
270 |
} |
271 |
} |