gvsig-raster / org.gvsig.raster.georeferencing / trunk / org.gvsig.raster.georeferencing / org.gvsig.raster.georeferencing.lib / org.gvsig.raster.georeferencing.lib.impl / src / main / java / org / gvsig / raster / georeferencing / lib / impl / GeoreferencingProcess.java @ 1752
History | View | Annotate | Download (10.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Instituto de Desarrollo Regional 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 Iba?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 |
* Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
|
34 |
* Campus Universitario s/n
|
35 |
* 02071 Alabacete
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 967 599 200
|
39 |
*/
|
40 |
|
41 |
package org.gvsig.raster.georeferencing.lib.impl; |
42 |
|
43 |
import java.awt.geom.AffineTransform; |
44 |
import java.util.HashMap; |
45 |
|
46 |
import javax.swing.SwingUtilities; |
47 |
|
48 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
49 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
50 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
51 |
import org.gvsig.fmap.dal.coverage.datastruct.GeoPointList; |
52 |
import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
53 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
54 |
import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
55 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
56 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
57 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
58 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
59 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
60 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
61 |
import org.gvsig.i18n.Messages; |
62 |
import org.gvsig.raster.tools.algorithm.base.RasterBaseAlgorithmLibrary; |
63 |
import org.gvsig.raster.tools.algorithm.base.process.IProcessActions; |
64 |
import org.gvsig.raster.tools.algorithm.base.process.ProcessException; |
65 |
import org.gvsig.raster.tools.algorithm.base.process.RasterProcess; |
66 |
import org.gvsig.raster.tools.algorithm.base.util.Interpolation; |
67 |
|
68 |
/**
|
69 |
* Resamples a image with the georeferencing parameters
|
70 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
71 |
**/
|
72 |
public class GeoreferencingProcess extends RasterProcess implements IProcessActions { |
73 |
public static String RASTER_STORE = "RasterStore"; |
74 |
public static String GCPS = "gcps"; |
75 |
public static String ORDER = "Order"; |
76 |
public static String TIME = "Time"; |
77 |
public static String RESULT = "RESULT"; |
78 |
public static String FILENAME = "FileName"; |
79 |
public static String XCELLSIZE = "xCellSize"; |
80 |
public static String YCELLSIZE = "yCellSize"; |
81 |
public static String INTERPOLATION = "InterpolationMethod"; |
82 |
|
83 |
//Capa a georreferenciar
|
84 |
private RasterDataStore store = null; |
85 |
|
86 |
//Fichero de salida
|
87 |
private String filename = null; |
88 |
|
89 |
// Lista puntos de control
|
90 |
private GeoPointList gcps = null; |
91 |
|
92 |
// Metodo de resampleado utilizado
|
93 |
private int rMethod = 0; |
94 |
|
95 |
private int orden = 0; |
96 |
|
97 |
private int[] bands = null; |
98 |
|
99 |
//Tama?o de celda en X si es pasada por el usuario
|
100 |
private double xCellSize = 0; |
101 |
//Tama?o de celda en Y si es pasada por el usuario
|
102 |
private double yCellSize = 0; |
103 |
|
104 |
private DataServerWriter writerBufferServer = null; |
105 |
private long milis = 0; |
106 |
|
107 |
public static void registerParameters() { |
108 |
RASTER_STORE = RasterBaseAlgorithmLibrary.registerInputParameter(RASTER_STORE, RasterDataStore.class); |
109 |
INTERPOLATION = RasterBaseAlgorithmLibrary.registerInputParameter(INTERPOLATION, Integer.class);
|
110 |
GCPS = RasterBaseAlgorithmLibrary.registerInputParameter(GCPS, GeoPointList.class); |
111 |
ORDER = RasterBaseAlgorithmLibrary.registerInputParameter(ORDER, Integer.class);
|
112 |
TIME = RasterBaseAlgorithmLibrary.registerOutputParameter(TIME, Long.class);
|
113 |
} |
114 |
|
115 |
/** Metodo que recoge los parametros del proceso georreferenciacion de un raster
|
116 |
* <LI>rasterSE: capa a georeferenciar</LI>
|
117 |
* <LI>filename: path con el fichero de salida</LI>
|
118 |
* <LI>method: metodo de resampleo </LI>
|
119 |
*/
|
120 |
public void init() { |
121 |
store = getParam(RASTER_STORE) != null ? (RasterDataStore)getParam(RASTER_STORE) : null; |
122 |
gcps = getParam(GCPS) != null ? (GeoPointList) getParam(GCPS) : null; |
123 |
filename = (String)getParam(FILENAME);
|
124 |
rMethod = (int)getIntParam(INTERPOLATION);
|
125 |
orden = (int)getIntParam(ORDER);
|
126 |
bands = new int[store.getBandCount()]; |
127 |
xCellSize = (double)getDoubleParam("xCellSize"); |
128 |
yCellSize = (double)getDoubleParam("yCellSize"); |
129 |
|
130 |
for(int i = 0; i < store.getBandCount(); i++) |
131 |
bands[i]= i; |
132 |
} |
133 |
|
134 |
|
135 |
public void process() throws ProcessInterruptedException, ProcessException { |
136 |
long t1 = new java.util.Date().getTime(); |
137 |
|
138 |
RasterQuery query = RasterLocator.getManager().createQuery(); |
139 |
query.setAllDrawableBands(); |
140 |
query.setAreaOfInterest(); |
141 |
query.setReadOnly(true);
|
142 |
Buffer bufInput;
|
143 |
try {
|
144 |
bufInput = store.query(query); |
145 |
} catch (RasterDriverException e1) {
|
146 |
throw new ProcessException("Error getting the input buffer", e1); |
147 |
} catch (InvalidSetViewException e1) {
|
148 |
throw new ProcessException("Error getting the input buffer", e1); |
149 |
} |
150 |
|
151 |
GeoTransformProcess transform = new GeoTransformProcess();
|
152 |
transform.setActions(this);
|
153 |
transform.addParam(GCPS, gcps); |
154 |
transform.addParam(ORDER, new Integer(orden)); |
155 |
transform.execute(); |
156 |
|
157 |
// Obtenida la transformacion la aplicamos a los puntos extremos de la imagen
|
158 |
double p1[] = transform.getCoordMap(0, 0); |
159 |
double p2[] = transform.getCoordMap(store.getWidth(), 0); |
160 |
double p3[] = transform.getCoordMap(0, store.getHeight()); |
161 |
double p4[] = transform.getCoordMap(store.getWidth(), store.getHeight()); |
162 |
|
163 |
double xmin = Math.min(Math.min(p1[0], p3[0]), Math.min(p2[0], p4[0])); |
164 |
double ymin = Math.min(Math.min(p3[1], p4[1]), Math.min(p1[1], p2[1])); |
165 |
double xmax = Math.max(Math.max(p2[0], p4[0]), Math.max(p1[0], p3[0])); |
166 |
double ymax = Math.max(Math.max(p1[1], p2[1]), Math.max(p3[1], p4[1])); |
167 |
|
168 |
//Extent newBbox = RasterLocator.getManager().getDataStructFactory().createExtent(xmin, ymax, xmax, ymin);
|
169 |
int w = 0; |
170 |
int h = 0; |
171 |
if(xCellSize >= 0 && yCellSize >= 0) { |
172 |
w = (int)((xmax - xmin) / xCellSize);
|
173 |
h = (int)((ymax - ymin) / yCellSize);
|
174 |
} |
175 |
|
176 |
int dataType = store.getDataType()[0]; |
177 |
|
178 |
Buffer bufResult = RasterLocator.getManager().createBuffer(dataType, w, h, store.getBandCount(), true); |
179 |
|
180 |
NoData nd = RasterLocator.getManager().getDataStructFactory().createDefaultNoData( |
181 |
store.getBandCount(), dataType); |
182 |
|
183 |
Interpolation interpolation = new Interpolation(bufInput);
|
184 |
|
185 |
double coord[] = null; |
186 |
double values[] = new double[bands.length]; |
187 |
|
188 |
for(int row = 0; row < bufResult.getHeight(); row++) { |
189 |
for(int col = 0; col < bufResult.getWidth(); col++) { |
190 |
coord = transform.getCoordPixel(col * xCellSize + xmin, ymax - row * yCellSize); |
191 |
for (int iBand = 0; iBand < store.getBandCount(); iBand++) { |
192 |
if( (coord[0] < 0) || (coord[1] < 0) || |
193 |
(coord[0] > bufInput.getWidth() - 1) || |
194 |
(coord[1] > bufInput.getHeight() - 1)) |
195 |
values[iBand] = nd.getValue().doubleValue(); |
196 |
else {
|
197 |
if(rMethod == Buffer.INTERPOLATION_Bilinear) |
198 |
values[iBand] = interpolation.getBilinearValue(coord[0], coord[1], iBand); |
199 |
else if(rMethod == Buffer.INTERPOLATION_InverseDistance) |
200 |
values[iBand] = interpolation.getInverseDistance(coord[0], coord[1], iBand); |
201 |
else
|
202 |
values[iBand] = interpolation.getNearestNeighbour(coord[0], coord[1], iBand); |
203 |
} |
204 |
|
205 |
if(dataType == Buffer.TYPE_BYTE) |
206 |
bufResult.setElem(row, col, iBand, (byte)values[iBand]);
|
207 |
else if(dataType == Buffer.TYPE_DOUBLE) |
208 |
bufResult.setElem(row, col, iBand, (double)values[iBand]);
|
209 |
else if(dataType == Buffer.TYPE_FLOAT) |
210 |
bufResult.setElem(row, col, iBand, (float)values[iBand]);
|
211 |
else if(dataType == Buffer.TYPE_SHORT) |
212 |
bufResult.setElem(row, col, iBand, (short)values[iBand]);
|
213 |
else if(dataType == Buffer.TYPE_INT) |
214 |
bufResult.setElem(row, col, iBand, (int)values[iBand]);
|
215 |
} |
216 |
} |
217 |
updatePercent(row, bufResult.getHeight()); |
218 |
} |
219 |
|
220 |
export(filename, bufResult, xCellSize, yCellSize, xmin, ymax);
|
221 |
|
222 |
long t2 = new java.util.Date().getTime(); |
223 |
milis = t2 - t1; |
224 |
|
225 |
SwingUtilities.invokeLater(new Runnable() { |
226 |
public void run() { |
227 |
if (externalActions != null) { |
228 |
externalActions.end(getResult()); |
229 |
} |
230 |
} |
231 |
}); |
232 |
} |
233 |
|
234 |
public Object getResult() { |
235 |
HashMap<String, Object> map = new HashMap<String, Object>(); |
236 |
map.put(FILENAME, filename); |
237 |
map.put(TIME, new Long(milis)); |
238 |
return map;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* Saves the result in a file
|
243 |
* @param sFilename
|
244 |
* @param buf
|
245 |
* @param xcellsize
|
246 |
* @param ycellsize
|
247 |
* @param minX
|
248 |
* @param maxY
|
249 |
* @return
|
250 |
*/
|
251 |
public boolean export(final String sFilename, Buffer buf, double xcellsize, double ycellsize, double minX, double maxY) { |
252 |
|
253 |
try {
|
254 |
RasterManager manager = RasterLocator.getManager(); |
255 |
writerBufferServer = manager.createDataServerWriter(); |
256 |
writerBufferServer.setBuffer(buf, -1);
|
257 |
final Params params = manager.createWriterParams(sFilename);
|
258 |
final AffineTransform affineTransform = |
259 |
new AffineTransform(xcellsize, 0, 0, |
260 |
-ycellsize, minX, maxY); |
261 |
|
262 |
final RasterWriter writer =
|
263 |
manager.createWriter(writerBufferServer, sFilename, |
264 |
buf.getBandCount(), affineTransform, buf.getWidth(), |
265 |
buf.getHeight(), buf.getDataType(), params, null);
|
266 |
writer.dataWrite(); |
267 |
writer.writeClose(); |
268 |
|
269 |
} catch (final Exception e) { |
270 |
e.printStackTrace(); |
271 |
return false; |
272 |
} |
273 |
|
274 |
return true; |
275 |
|
276 |
} |
277 |
|
278 |
public String getTitle() { |
279 |
return Messages.getText("georreferenciacion_process"); |
280 |
} |
281 |
|
282 |
public int getPercent() { |
283 |
if(writerBufferServer == null) |
284 |
return percent;
|
285 |
else
|
286 |
return writerBufferServer.getPercent();
|
287 |
} |
288 |
|
289 |
public String getLog() { |
290 |
return Messages.getText("georreferencing_log_message"); |
291 |
} |
292 |
|
293 |
public void interrupted() { |
294 |
} |
295 |
|
296 |
public void end(Object param) { |
297 |
} |
298 |
} |