gvsig-geoprocess / org.gvsig.geoprocess / branches / refactor-2018 / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / FLyrRasterIRasterLayer.java @ 1059
History | View | Annotate | Download (11.6 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 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 2
|
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.geoprocess.lib.sextante.dataObjects; |
25 |
|
26 |
import java.awt.geom.Rectangle2D; |
27 |
import java.io.File; |
28 |
|
29 |
import org.gvsig.fmap.dal.DALLocator; |
30 |
import org.gvsig.fmap.dal.DataManager; |
31 |
|
32 |
import org.gvsig.fmap.dal.exception.InitializeException; |
33 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
34 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
35 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
36 |
|
37 |
import es.unex.sextante.core.AnalysisExtent; |
38 |
import es.unex.sextante.core.Sextante; |
39 |
import es.unex.sextante.dataObjects.AbstractRasterLayer; |
40 |
import es.unex.sextante.outputs.IOutputChannel; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.raster.api.NewRasterStoreParameters; |
43 |
import org.gvsig.fmap.dal.raster.api.RasterQuery; |
44 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
45 |
import org.gvsig.fmap.geom.GeometryLocator; |
46 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
47 |
import org.gvsig.fmap.geom.primitive.Envelope; |
48 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
51 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
52 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
53 |
import org.gvsig.raster.lib.buffer.api.NoData; |
54 |
import org.gvsig.tools.dispose.DisposeUtils; |
55 |
import org.gvsig.tools.locator.LocatorException; |
56 |
|
57 |
/**
|
58 |
* A wrapper for a gvSIG Raster layer. Allows only reading, but not writing to
|
59 |
* it
|
60 |
*
|
61 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
62 |
*
|
63 |
*/
|
64 |
public class FLyrRasterIRasterLayer extends AbstractRasterLayer { |
65 |
private RasterLayer lyr = null; |
66 |
private Buffer m_Buffer = null; |
67 |
private int xTranslate = 0; |
68 |
private int yTranslate = 0; |
69 |
private AnalysisExtent layerExtent = null; |
70 |
private String fName = null; |
71 |
|
72 |
public FLyrRasterIRasterLayer() {
|
73 |
|
74 |
} |
75 |
|
76 |
public FLyrRasterIRasterLayer(String fName) { |
77 |
this.fName = fName;
|
78 |
} |
79 |
|
80 |
public void create(final RasterLayer lyr) { |
81 |
this.lyr = lyr;
|
82 |
} |
83 |
|
84 |
private RasterStore getDataStore() {
|
85 |
return lyr == null ? null : lyr.getRasterStore(); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
public int getDataType() { |
90 |
return getDataStore().getBandDescriptor(0).getDataType(); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public void setCellValue(final int x, final int y, final int iBand, |
95 |
final double dValue) { |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void setNoDataValue(final double dNoDataValue) { |
100 |
//TODO aplicar a todas las bandas
|
101 |
getDataStore().getBandDescriptor(0).getNoData().setValue(dNoDataValue);
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void setNoData(final int x, final int y) { |
106 |
|
107 |
} |
108 |
|
109 |
public double getNoDataValue(final int iBand) { |
110 |
NoData noData = getDataStore().getBandDescriptor(0).getNoData();
|
111 |
if(noData!=null && noData.getValue() != null) |
112 |
return noData.getValue().doubleValue();
|
113 |
return -99999.0; |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public double getNoDataValue() { |
118 |
return getNoDataValue(0); |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public double getCellValueInLayerCoords(final int x, final int y, |
123 |
final int iBand) { |
124 |
int newx = x - xTranslate;
|
125 |
int newy = y - yTranslate;
|
126 |
if(m_Buffer == null) |
127 |
open(); |
128 |
|
129 |
if (m_Buffer.isInside(newx, newy)) {
|
130 |
switch (getDataStore().getBandDescriptor(iBand).getDataType()) {
|
131 |
case BufferManager.TYPE_BYTE:
|
132 |
return (m_Buffer.getBandByte(iBand).getValue(newy, newx) & 0xff); |
133 |
case BufferManager.TYPE_SHORT:
|
134 |
return m_Buffer.getBandShort(iBand).getValue(newy, newx);
|
135 |
case BufferManager.TYPE_INT:
|
136 |
return m_Buffer.getBandInt(iBand).getValue(newy, newx);
|
137 |
case BufferManager.TYPE_FLOAT:
|
138 |
return m_Buffer.getBandFloat(iBand).getValue(newy, newx);
|
139 |
case BufferManager.TYPE_DOUBLE:
|
140 |
default:
|
141 |
return m_Buffer.getBandDouble(iBand).getValue(newy, newx);
|
142 |
} |
143 |
} else {
|
144 |
return getNoDataValue();
|
145 |
} |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public int getBandsCount() { |
150 |
return getDataStore().getBands();
|
151 |
} |
152 |
|
153 |
@Override
|
154 |
public String getName() { |
155 |
if(lyr != null) { |
156 |
return lyr.getName();
|
157 |
} |
158 |
if(getDataStore() != null) { |
159 |
String uri = getDataStore().getName();
|
160 |
if(uri.contains(File.separator)) { |
161 |
int index = uri.lastIndexOf(File.separator); |
162 |
return uri.substring(index + 1); |
163 |
} |
164 |
return uri;
|
165 |
} |
166 |
return null; |
167 |
} |
168 |
|
169 |
@Override
|
170 |
public void postProcess() { |
171 |
|
172 |
} |
173 |
|
174 |
@Override
|
175 |
public void open() { |
176 |
if(getDataStore() == null) |
177 |
try {
|
178 |
setBaseDataObject(fName); |
179 |
} catch (LoadLayerException e) {
|
180 |
Sextante.addErrorToLog(e); |
181 |
} catch (ValidateDataParametersException e) {
|
182 |
Sextante.addErrorToLog(e); |
183 |
} |
184 |
RasterQuery query = getDataStore().createRasterQuery(); |
185 |
try {
|
186 |
m_Buffer = getDataStore().getRasterSet(query); |
187 |
} catch (DataException e) {
|
188 |
Sextante.addErrorToLog(e); |
189 |
} |
190 |
} |
191 |
|
192 |
@Override
|
193 |
public void close() { |
194 |
if(m_Buffer != null) |
195 |
m_Buffer.dispose(); |
196 |
} |
197 |
|
198 |
@Override
|
199 |
public Rectangle2D getFullExtent() { |
200 |
try {
|
201 |
return getDataStore().getEnvelope().getGeometry().getBounds().getBounds2D();
|
202 |
} catch (DataException e) {
|
203 |
Sextante.addErrorToLog(e); |
204 |
return null; |
205 |
} catch (LocatorException e) {
|
206 |
Sextante.addErrorToLog(e); |
207 |
return null; |
208 |
} catch (CreateEnvelopeException e) {
|
209 |
Sextante.addErrorToLog(e); |
210 |
return null; |
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
public AnalysisExtent getLayerGridExtent() {
|
217 |
if(layerExtent == null) { |
218 |
try {
|
219 |
layerExtent = new AnalysisExtent();
|
220 |
layerExtent.setCellSize(getLayerCellSize()); |
221 |
Envelope bbox = getDataStore().getEnvelope(); |
222 |
layerExtent.setXRange(bbox.getLowerCorner().getX(), bbox.getUpperCorner().getX(), true);
|
223 |
layerExtent.setYRange(bbox.getLowerCorner().getY(), bbox.getUpperCorner().getY(), true);
|
224 |
} catch (final DataException e) { |
225 |
Sextante.addErrorToLog(e); |
226 |
} catch (final CreateEnvelopeException e) { |
227 |
Sextante.addErrorToLog(e); |
228 |
layerExtent = new AnalysisExtent();
|
229 |
} catch (final LocatorException e) { |
230 |
Sextante.addErrorToLog(e); |
231 |
} |
232 |
} |
233 |
return layerExtent;
|
234 |
} |
235 |
|
236 |
@Override
|
237 |
public void setWindowExtent(final AnalysisExtent extent) { |
238 |
super.setWindowExtent(extent);
|
239 |
|
240 |
RasterQuery query = getDataStore().createRasterQuery(); |
241 |
//int[] bands = new int[getDataStore().getBands()];
|
242 |
//for (int i = 0; i < getDataStore().getBands(); i++) {
|
243 |
// bands[i] = i;
|
244 |
// }
|
245 |
//query.setDrawableBands(bands);
|
246 |
|
247 |
Envelope ext; |
248 |
try {
|
249 |
ext = GeometryLocator.getGeometryManager().createEnvelope( |
250 |
extent.getXMin(), |
251 |
extent.getYMin(), |
252 |
extent.getXMax(), |
253 |
extent.getYMax(), |
254 |
2);
|
255 |
} catch (CreateEnvelopeException e) {
|
256 |
Sextante.addErrorToLog(e); |
257 |
ext = null;
|
258 |
} |
259 |
query.setClip(ext); |
260 |
Buffer entireBuf = m_Buffer;
|
261 |
try {
|
262 |
m_Buffer = getDataStore().getRasterSet(query); |
263 |
//Calculamos la traslaci?n respecto a la ventana recortada pq Sextante pedir?
|
264 |
//p?xeles en relaci?n a la imagen completa
|
265 |
Rectangle2D bbox = getDataStore().getEnvelope().getGeometry().getBounds().getBounds2D();
|
266 |
double distx = bbox.getMaxX() - extent.getXMax();
|
267 |
double disty = bbox.getMaxY() - extent.getYMax();
|
268 |
xTranslate = (int)Math.round((distx * extent.getNX()) / (extent.getXMax() - extent.getXMin())); |
269 |
yTranslate = (int)Math.round((disty * extent.getNY()) / (extent.getYMax() - extent.getYMin())); |
270 |
} catch (final Exception e) { |
271 |
m_Buffer = entireBuf; |
272 |
} |
273 |
} |
274 |
|
275 |
@Override
|
276 |
public double getLayerCellSize() { |
277 |
try {
|
278 |
//return getDataStore().getCellSize();
|
279 |
return getDataStore().getDimensions().getPixelSizeX();
|
280 |
} catch (InitializeException e) {
|
281 |
Sextante.addErrorToLog(e); |
282 |
} |
283 |
return 0; |
284 |
} |
285 |
|
286 |
@Override
|
287 |
public Object getCRS() { |
288 |
try {
|
289 |
return getDataStore().getRasterSet().getProjection();
|
290 |
} catch (DataException e) {
|
291 |
Sextante.addErrorToLog(e); |
292 |
} |
293 |
return null; |
294 |
} |
295 |
|
296 |
@Override
|
297 |
public void setName(final String name) { |
298 |
lyr.setName(name); |
299 |
} |
300 |
|
301 |
@Override
|
302 |
public void free() { |
303 |
DisposeUtils.disposeQuietly(m_Buffer); |
304 |
DisposeUtils.disposeQuietly(lyr); |
305 |
} |
306 |
|
307 |
@Override
|
308 |
public Object getBaseDataObject() { |
309 |
return lyr;
|
310 |
} |
311 |
|
312 |
public void setBaseDataObject(RasterLayer lyr) { |
313 |
this.lyr = lyr;
|
314 |
} |
315 |
|
316 |
public void setBaseDataObject(RasterStore store) throws LoadLayerException { |
317 |
//DefaultRasterLayer newlyr = new DefaultRasterLayer();
|
318 |
FLayer newlyr = MapContextLocator.getMapContextManager().createLayer(store.getName(), store); |
319 |
|
320 |
// try {
|
321 |
// newlyr.setDataStore(store);
|
322 |
// } catch (LoadLayerException e) {
|
323 |
// Sextante.addErrorToLog(e);
|
324 |
// return;
|
325 |
// }
|
326 |
this.lyr = (RasterLayer) newlyr;
|
327 |
} |
328 |
|
329 |
public void setBaseDataObject(String fileName) throws LoadLayerException, ValidateDataParametersException { |
330 |
DataManager dataManager = DALLocator.getDataManager(); |
331 |
try {
|
332 |
NewRasterStoreParameters params; |
333 |
params = (NewRasterStoreParameters)dataManager.createStoreParameters("Gdal Store");
|
334 |
|
335 |
params.setDynValue("uri",new File(fileName).toURI()); |
336 |
RasterStore dataStore; |
337 |
dataStore = (RasterStore)dataManager.openStore(params.getDataStoreName(), params); |
338 |
setBaseDataObject(dataStore); |
339 |
} catch (InitializeException e) {
|
340 |
Sextante.addErrorToLog(e); |
341 |
} catch (ProviderNotRegisteredException e) {
|
342 |
Sextante.addErrorToLog(e); |
343 |
} |
344 |
} |
345 |
|
346 |
|
347 |
|
348 |
@Override
|
349 |
public IOutputChannel getOutputChannel() {
|
350 |
return new IOutputChannel() { |
351 |
@Override
|
352 |
public String getAsCommandLineParameter() { |
353 |
return lyr.getName();
|
354 |
} |
355 |
}; |
356 |
} |
357 |
|
358 |
} |