Revision 16326 trunk/extensions/extRasterTools-SE/src/org/gvsig/rastertools/geolocation/listener/GeoLocationPanelListener.java
GeoLocationPanelListener.java | ||
---|---|---|
21 | 21 |
import java.awt.event.ActionEvent; |
22 | 22 |
import java.awt.event.ActionListener; |
23 | 23 |
import java.awt.geom.AffineTransform; |
24 |
import java.awt.geom.Rectangle2D; |
|
25 |
import java.io.BufferedReader; |
|
26 |
import java.io.File; |
|
27 |
import java.io.FileInputStream; |
|
28 |
import java.io.FileNotFoundException; |
|
24 | 29 |
import java.io.IOException; |
30 |
import java.io.InputStreamReader; |
|
25 | 31 |
import java.util.EventObject; |
26 | 32 |
|
33 |
import javax.swing.JFileChooser; |
|
34 |
|
|
35 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
|
27 | 36 |
import org.gvsig.gui.beans.datainput.DataInputContainerListener; |
37 |
import org.gvsig.raster.datastruct.Extent; |
|
28 | 38 |
import org.gvsig.raster.util.Historical; |
29 | 39 |
import org.gvsig.raster.util.RasterToolsUtil; |
30 | 40 |
import org.gvsig.rastertools.geolocation.ui.GeoLocationPanel; |
31 | 41 |
|
32 | 42 |
import com.iver.andami.PluginServices; |
43 |
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard; |
|
44 |
import com.iver.cit.gvsig.fmap.ViewPort; |
|
33 | 45 |
|
34 | 46 |
/** |
35 | 47 |
* Listener para el panel de geolocalizaci?n. |
... | ... | |
108 | 120 |
if(e.getSource() == panel.getResetButton()) |
109 | 121 |
at = panel.getLayer().getDataSource().getOwnAffineTransform(); |
110 | 122 |
|
123 |
//Cargar la georreferenciaci?n desde tfw |
|
124 |
if(e.getSource() == panel.getTfwLoad()) { |
|
125 |
JFileChooser chooser = new JFileChooser(FileOpenWizard.getLastPath()); |
|
126 |
chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero")); |
|
127 |
chooser.addChoosableFileFilter(new GeorreferencingFileFilter("tfw")); |
|
128 |
chooser.addChoosableFileFilter(new GeorreferencingFileFilter("wld")); |
|
129 |
int returnVal = chooser.showOpenDialog(panel); |
|
130 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
|
131 |
String fName = chooser.getSelectedFile().toString(); |
|
132 |
at = readTfw(fName); |
|
133 |
} |
|
134 |
} |
|
111 | 135 |
|
136 |
//Centrar el raster en la vista |
|
137 |
if(e.getSource() == panel.getCenterToView()) { |
|
138 |
Extent extentView = calcCenterExtent(panel.getViewPort()); |
|
139 |
double x = extentView.minX(); |
|
140 |
double y = extentView.maxY(); |
|
141 |
double psX = extentView.width() / ((FLyrRasterSE)panel.getLayer()).getPxWidth(); |
|
142 |
double psY = -(extentView.height() / ((FLyrRasterSE)panel.getLayer()).getPxHeight()); |
|
143 |
at = new AffineTransform(psX, 0, 0, psY, x, y); |
|
144 |
panel.setModify(true); |
|
145 |
} |
|
146 |
|
|
112 | 147 |
//Entrar? en el caso de que se haya seleccionado alguna transformaci?n |
113 | 148 |
if(at != null) { |
114 | 149 |
panel.getLayer().setAT(at); |
... | ... | |
147 | 182 |
public void setEnableValueChangeEvent(boolean enableValueChangeEvent) { |
148 | 183 |
this.enableValueChangeEvent = enableValueChangeEvent; |
149 | 184 |
} |
185 |
|
|
186 |
/** |
|
187 |
* Centra el raster asociado a la capa en al extent del viewport pasado |
|
188 |
* por par?metro. |
|
189 |
* @param vp ViewPort |
|
190 |
* @return Extent para la imagen |
|
191 |
*/ |
|
192 |
private Extent calcCenterExtent(ViewPort vp) { |
|
193 |
Extent tempExtent = null; |
|
194 |
double widthPxImg, heightPxImg; |
|
195 |
|
|
196 |
widthPxImg = ((FLyrRasterSE)panel.getLayer()).getPxWidth(); |
|
197 |
heightPxImg = ((FLyrRasterSE)panel.getLayer()).getPxHeight(); |
|
198 |
|
|
199 |
if(vp == null || vp.getAdjustedExtent() == null) { |
|
200 |
vp = new ViewPort(null); |
|
201 |
Rectangle2D r2d = new Rectangle2D.Double(0, 0, widthPxImg, heightPxImg); |
|
202 |
vp.setExtent(r2d); |
|
203 |
tempExtent = new Extent(0, 0, widthPxImg, heightPxImg); |
|
204 |
} else |
|
205 |
tempExtent = new Extent(vp.getAdjustedExtent()); |
|
206 |
|
|
207 |
double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D; |
|
208 |
if(widthPxImg > heightPxImg) { |
|
209 |
double widthView = tempExtent.maxX() - tempExtent.minX(); |
|
210 |
ulX = tempExtent.minX() + (widthView / 4); |
|
211 |
lrX = ulX + (widthView / 2); |
|
212 |
double newAlto = ((heightPxImg * (widthView / 2)) / widthPxImg); |
|
213 |
double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2); |
|
214 |
ulY = centroY - (newAlto / 2); |
|
215 |
lrY = centroY + (newAlto / 2); |
|
216 |
} else { |
|
217 |
double heightView = tempExtent.maxY() - tempExtent.minY(); |
|
218 |
ulY = tempExtent.minY() + (heightView / 4); |
|
219 |
lrY = ulY + (heightView / 2); |
|
220 |
double newAncho = ((widthPxImg * (heightView / 2)) / heightPxImg); |
|
221 |
double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2); |
|
222 |
ulX = centroX - (newAncho / 2); |
|
223 |
lrX = centroX + (newAncho / 2); |
|
224 |
} |
|
225 |
return new Extent(ulX, ulY, lrX, lrY); |
|
226 |
} |
|
227 |
|
|
228 |
/** |
|
229 |
* Lee las coordenadas de un fichero de tfw con una transformaci?n y |
|
230 |
* devuelve la clase AffineTransform con dicha transformaci?n. Esta llamada gestiona los |
|
231 |
* errores producidos actuando en consecuencia. Muestra los mensajes al usuario y retorna |
|
232 |
* null en caso de tener problemas. |
|
233 |
* @param fName Nombre del fichero tfw |
|
234 |
* @return AffineTransform |
|
235 |
*/ |
|
236 |
private AffineTransform readTfw(String fName) { |
|
237 |
BufferedReader inGrf = null; |
|
238 |
double[] result = new double[6]; |
|
239 |
try { |
|
240 |
inGrf = new BufferedReader(new InputStreamReader(new FileInputStream(fName))); |
|
241 |
String str = inGrf.readLine(); |
|
242 |
int count = 0; |
|
243 |
while(str != null && count < 6) { |
|
244 |
try { |
|
245 |
Double value = new Double(str); |
|
246 |
result[count] = value.doubleValue(); |
|
247 |
} catch (NumberFormatException ex) { |
|
248 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_file_not_valid"), panel, ex); |
|
249 |
return null; |
|
250 |
} |
|
251 |
str = inGrf.readLine(); |
|
252 |
count ++; |
|
253 |
} |
|
254 |
} catch (FileNotFoundException e) { |
|
255 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_file_not_found"), panel, e); |
|
256 |
return null; |
|
257 |
} catch (IOException ex) { |
|
258 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_lectura"), panel, ex); |
|
259 |
return null; |
|
260 |
} |
|
261 |
return new AffineTransform(result[0], result[1], result[2], result[3], result[4], result[5]); |
|
262 |
} |
|
263 |
|
|
264 |
/** |
|
265 |
* Filtro para el selector de formatos de georreferenciaci?n |
|
266 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
|
267 |
*/ |
|
268 |
class GeorreferencingFileFilter extends javax.swing.filechooser.FileFilter { |
|
269 |
private String filter; |
|
270 |
|
|
271 |
public GeorreferencingFileFilter(String fil) { |
|
272 |
this.filter = fil; |
|
273 |
} |
|
274 |
|
|
275 |
public boolean accept(File f) { |
|
276 |
return f.isDirectory() || f.getName().toLowerCase().endsWith("." + filter); |
|
277 |
} |
|
278 |
|
|
279 |
public String getDescription() { |
|
280 |
return "." + filter; |
|
281 |
} |
|
282 |
} |
|
150 | 283 |
} |
Also available in: Unified diff