gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / FLyrRasterIRasterLayer.java @ 736
History | View | Annotate | Download (9.41 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 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
32 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
33 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
34 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
35 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
36 |
import org.gvsig.fmap.dal.coverage.exception.QueryException; |
37 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
38 |
import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
39 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterFileStoreParameters; |
40 |
import org.gvsig.fmap.dal.exception.CloseException; |
41 |
import org.gvsig.fmap.dal.exception.InitializeException; |
42 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
43 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
44 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
45 |
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster; |
46 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
47 |
|
48 |
import es.unex.sextante.core.AnalysisExtent; |
49 |
import es.unex.sextante.core.Sextante; |
50 |
import es.unex.sextante.dataObjects.AbstractRasterLayer; |
51 |
import es.unex.sextante.outputs.IOutputChannel; |
52 |
|
53 |
/**
|
54 |
* A wrapper for a gvSIG Raster layer. Allows only reading, but not writing to
|
55 |
* it
|
56 |
*
|
57 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
58 |
*
|
59 |
*/
|
60 |
public class FLyrRasterIRasterLayer extends AbstractRasterLayer { |
61 |
private FLyrRaster lyr = null; |
62 |
private Buffer m_Buffer = null; |
63 |
private int xTranslate = 0; |
64 |
private int yTranslate = 0; |
65 |
private AnalysisExtent layerExtent = null; |
66 |
private String fName = null; |
67 |
|
68 |
public FLyrRasterIRasterLayer() {
|
69 |
|
70 |
} |
71 |
|
72 |
public FLyrRasterIRasterLayer(String fName) { |
73 |
this.fName = fName;
|
74 |
} |
75 |
|
76 |
public void create(final FLyrRaster lyr) { |
77 |
this.lyr = lyr;
|
78 |
} |
79 |
|
80 |
private RasterDataStore getDataStore() {
|
81 |
return lyr == null ? null : lyr.getDataStore(); |
82 |
} |
83 |
|
84 |
public int getDataType() { |
85 |
return getDataStore().getDataType()[0]; |
86 |
} |
87 |
|
88 |
public void setCellValue(final int x, final int y, final int iBand, |
89 |
final double dValue) { |
90 |
} |
91 |
|
92 |
public void setNoDataValue(final double dNoDataValue) { |
93 |
getDataStore().getNoDataValue().setValue(Double.valueOf(dNoDataValue));
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
public void setNoData(final int x, final int y) { |
98 |
|
99 |
} |
100 |
|
101 |
public double getNoDataValue() { |
102 |
if(getDataStore().getNoDataValue().getValue() != null) |
103 |
return getDataStore().getNoDataValue().getValue().doubleValue();
|
104 |
return -99999.0; |
105 |
} |
106 |
|
107 |
public double getCellValueInLayerCoords(final int x, final int y, |
108 |
final int iBand) { |
109 |
int newx = x - xTranslate;
|
110 |
int newy = y - yTranslate;
|
111 |
if(m_Buffer == null) |
112 |
open(); |
113 |
if (m_Buffer.isInside(newx, newy)) {
|
114 |
switch (getDataStore().getDataType()[0]) { |
115 |
case Buffer.TYPE_BYTE: |
116 |
return (m_Buffer.getElemByte(newy, newx, iBand) & 0xff); |
117 |
case Buffer.TYPE_SHORT: |
118 |
return m_Buffer.getElemShort(newy, newx, iBand);
|
119 |
case Buffer.TYPE_INT: |
120 |
return m_Buffer.getElemInt(newy, newx, iBand);
|
121 |
case Buffer.TYPE_FLOAT: |
122 |
return m_Buffer.getElemFloat(newy, newx, iBand);
|
123 |
case Buffer.TYPE_DOUBLE: |
124 |
default:
|
125 |
return m_Buffer.getElemDouble(newy, newx, iBand);
|
126 |
} |
127 |
} else {
|
128 |
return getNoDataValue();
|
129 |
} |
130 |
} |
131 |
|
132 |
public int getBandsCount() { |
133 |
return getDataStore().getBandCount();
|
134 |
} |
135 |
|
136 |
public String getName() { |
137 |
if(lyr != null) { |
138 |
return lyr.getName();
|
139 |
} |
140 |
if(getDataStore() != null) { |
141 |
String uri = getDataStore().getName();
|
142 |
if(uri.contains(File.separator)) { |
143 |
int index = uri.lastIndexOf(File.separator); |
144 |
return uri.substring(index + 1); |
145 |
} |
146 |
return uri;
|
147 |
} |
148 |
return null; |
149 |
} |
150 |
|
151 |
public void postProcess() { |
152 |
|
153 |
} |
154 |
|
155 |
public void open() { |
156 |
if(getDataStore() == null) |
157 |
setBaseDataObject(fName); |
158 |
RasterQuery query = RasterLocator.getManager().createQuery(); |
159 |
query.setAllDrawableBands(); |
160 |
try {
|
161 |
query.setAreaOfInterest(); |
162 |
query.setReadOnly(true);
|
163 |
m_Buffer = getDataStore().query(query); |
164 |
} catch (QueryException e) {
|
165 |
Sextante.addErrorToLog(e); |
166 |
} catch (ProcessInterruptedException e) {
|
167 |
Sextante.addErrorToLog(e); |
168 |
} |
169 |
} |
170 |
|
171 |
public void close() { |
172 |
if(m_Buffer != null) |
173 |
m_Buffer.dispose(); |
174 |
} |
175 |
|
176 |
public Rectangle2D getFullExtent() { |
177 |
return getDataStore().getExtent().toRectangle2D();
|
178 |
} |
179 |
|
180 |
public AnalysisExtent getLayerGridExtent() {
|
181 |
if(layerExtent == null) { |
182 |
try {
|
183 |
layerExtent = new AnalysisExtent();
|
184 |
layerExtent.setCellSize(getDataStore().getCellSize()); |
185 |
Extent bbox = getDataStore().getExtent(); |
186 |
layerExtent.setXRange(bbox.getMin().getX(), bbox.getMax().getX(), true);
|
187 |
layerExtent.setYRange(bbox.getMin().getY(), bbox.getMax().getY(), true);
|
188 |
} catch (final Exception e) { |
189 |
return null; |
190 |
} |
191 |
} |
192 |
return layerExtent;
|
193 |
} |
194 |
|
195 |
public void setWindowExtent(final AnalysisExtent extent) { |
196 |
super.setWindowExtent(extent);
|
197 |
|
198 |
RasterManager rManager = RasterLocator.getManager(); |
199 |
RasterQuery query = rManager.createQuery(); |
200 |
query.setReadOnly(true);
|
201 |
int[] bands = new int[getDataStore().getBandCount()]; |
202 |
for (int i = 0; i < getDataStore().getBandCount(); i++) { |
203 |
bands[i] = i; |
204 |
} |
205 |
query.setDrawableBands(bands); |
206 |
Extent ext = RasterLocator.getManager().getDataStructFactory().createExtent( |
207 |
extent.getXMin(), |
208 |
extent.getYMax(), |
209 |
extent.getXMax(), |
210 |
extent.getYMin()); |
211 |
query.setAreaOfInterest(ext, extent.getNX(), extent.getNY()); |
212 |
Buffer entireBuf = m_Buffer;
|
213 |
try {
|
214 |
m_Buffer = getDataStore().query(query); |
215 |
//Calculamos la traslaci?n respecto a la ventana recortada pq Sextante pedir?
|
216 |
//p?xeles en relaci?n a la imagen completa
|
217 |
Extent bbox = getDataStore().getExtent(); |
218 |
double distx = extent.getXMin() - bbox.getMin().getX();
|
219 |
double disty = bbox.getMax().getY() - extent.getYMax();
|
220 |
xTranslate = (int)Math.round((distx * extent.getNX()) / (extent.getXMax() - extent.getXMin())); |
221 |
yTranslate = (int)Math.round((disty * extent.getNY()) / (extent.getYMax() - extent.getYMin())); |
222 |
} catch (QueryException e) {
|
223 |
m_Buffer = entireBuf; |
224 |
} catch (ProcessInterruptedException e) {
|
225 |
} |
226 |
} |
227 |
|
228 |
public double getLayerCellSize() { |
229 |
return getDataStore().getCellSize();
|
230 |
} |
231 |
|
232 |
public Object getCRS() { |
233 |
return getDataStore().getProjection();
|
234 |
} |
235 |
|
236 |
public void setName(final String name) { |
237 |
lyr.setName(name); |
238 |
} |
239 |
|
240 |
public void free() { |
241 |
if(m_Buffer != null) |
242 |
m_Buffer.dispose(); |
243 |
try {
|
244 |
getDataStore().close(); |
245 |
} catch (CloseException e) {
|
246 |
Sextante.addErrorToLog(e); |
247 |
} |
248 |
} |
249 |
|
250 |
public Object getBaseDataObject() { |
251 |
return lyr;
|
252 |
} |
253 |
|
254 |
public void setBaseDataObject(FLyrRaster lyr) { |
255 |
this.lyr = lyr;
|
256 |
} |
257 |
|
258 |
public void setBaseDataObject(RasterDataStore store) { |
259 |
DefaultFLyrRaster lyr = new DefaultFLyrRaster();
|
260 |
try {
|
261 |
lyr.setDataStore(store); |
262 |
} catch (LoadLayerException e) {
|
263 |
Sextante.addErrorToLog(e); |
264 |
return;
|
265 |
} |
266 |
this.lyr = lyr;
|
267 |
} |
268 |
|
269 |
public void setBaseDataObject(String fileName) { |
270 |
DataManager dataManager = DALLocator.getDataManager(); |
271 |
try {
|
272 |
RasterFileStoreParameters params = |
273 |
(RasterFileStoreParameters)dataManager.createStoreParameters("Gdal Store");
|
274 |
params.setURI(new File(fileName).toURI()); |
275 |
RasterDataStore dataStore = (RasterDataStore)dataManager.openStore(params.getDataStoreName(), params); |
276 |
setBaseDataObject(dataStore); |
277 |
} catch (ValidateDataParametersException e) {
|
278 |
Sextante.addErrorToLog(e); |
279 |
} catch (InitializeException e) {
|
280 |
Sextante.addErrorToLog(e); |
281 |
} catch (ProviderNotRegisteredException e) {
|
282 |
Sextante.addErrorToLog(e); |
283 |
} |
284 |
} |
285 |
|
286 |
|
287 |
|
288 |
public IOutputChannel getOutputChannel() {
|
289 |
return new IOutputChannel() { |
290 |
public String getAsCommandLineParameter() { |
291 |
return lyr.getName();
|
292 |
} |
293 |
}; |
294 |
} |
295 |
|
296 |
} |