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 / FLyrRasterReadOnlyIRasterLayer.java @ 382
History | View | Annotate | Download (7.6 KB)
1 | 237 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | 245 | cordinyana | * Copyright (C) 2007-2012 gvSIG Association.
|
5 | 237 | cordinyana | *
|
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 | 245 | cordinyana | *
|
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 | 237 | cordinyana | */
|
24 | 220 | cordinyana | package org.gvsig.geoprocess.lib.sextante.dataObjects; |
25 | |||
26 | import java.awt.geom.Rectangle2D; |
||
27 | |||
28 | 382 | nbrodin | import org.gvsig.fmap.dal.coverage.RasterLocator; |
29 | import org.gvsig.fmap.dal.coverage.RasterManager; |
||
30 | 220 | cordinyana | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
31 | 382 | nbrodin | import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
32 | 220 | cordinyana | import org.gvsig.fmap.dal.coverage.datastruct.NoData; |
33 | import org.gvsig.fmap.dal.coverage.exception.GridException; |
||
34 | 382 | nbrodin | import org.gvsig.fmap.dal.coverage.exception.InvalidSetViewException; |
35 | import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
||
36 | import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
||
37 | 220 | cordinyana | import org.gvsig.fmap.dal.coverage.grid.Grid; |
38 | 382 | nbrodin | import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
39 | import org.gvsig.fmap.dal.coverage.store.RasterQuery; |
||
40 | 220 | cordinyana | import org.gvsig.raster.fmap.layers.FLyrRaster; |
41 | |||
42 | 382 | nbrodin | import es.unex.sextante.core.AnalysisExtent; |
43 | import es.unex.sextante.core.Sextante; |
||
44 | import es.unex.sextante.dataObjects.AbstractRasterLayer; |
||
45 | import es.unex.sextante.outputs.IOutputChannel; |
||
46 | |||
47 | 220 | cordinyana | /**
|
48 | * A wrapper for a gvSIG Raster layer. Allows only reading, but not writing to
|
||
49 | * it
|
||
50 | *
|
||
51 | * @author volaya, nacho brodin (nachobrodin@gmail.com)
|
||
52 | *
|
||
53 | */
|
||
54 | 225 | cordinyana | public class FLyrRasterReadOnlyIRasterLayer extends AbstractRasterLayer { |
55 | 220 | cordinyana | |
56 | 382 | nbrodin | private Grid m_LayerGrid = null; |
57 | private FLyrRaster m_Layer = null; |
||
58 | private Buffer m_Buffer = null; |
||
59 | private int m_iDataType = 0; |
||
60 | private double m_dNoDataValue = 0; |
||
61 | private int xTranslate = 0; |
||
62 | private int yTranslate = 0; |
||
63 | private AnalysisExtent layerExtent = null; |
||
64 | 220 | cordinyana | |
65 | public void create(final FLyrRaster obj) { |
||
66 | m_Layer = obj; |
||
67 | } |
||
68 | |||
69 | public int getDataType() { |
||
70 | return m_Layer.getDataStore().getDataType()[0]; |
||
71 | } |
||
72 | |||
73 | public void setCellValue(final int x, final int y, final int iBand, |
||
74 | final double dValue) { |
||
75 | } |
||
76 | |||
77 | public void setNoDataValue(final double dNoDataValue) { |
||
78 | m_Layer.getNoDataValue().setValue(Double.valueOf(dNoDataValue));
|
||
79 | } |
||
80 | |||
81 | @Override
|
||
82 | public void setNoData(final int x, final int y) { |
||
83 | |||
84 | } |
||
85 | |||
86 | public double getNoDataValue() { |
||
87 | return m_dNoDataValue;
|
||
88 | } |
||
89 | |||
90 | public double getCellValueInLayerCoords(final int x, final int y, |
||
91 | final int iBand) { |
||
92 | 382 | nbrodin | int newx = x - xTranslate;
|
93 | int newy = y - yTranslate;
|
||
94 | if (m_Buffer.isInside(newx, newy)) {
|
||
95 | 220 | cordinyana | switch (m_iDataType) {
|
96 | case Buffer.TYPE_BYTE: |
||
97 | 382 | nbrodin | return m_Buffer.getElemByte(newy, newx, iBand);
|
98 | 220 | cordinyana | case Buffer.TYPE_SHORT: |
99 | 382 | nbrodin | return m_Buffer.getElemShort(newy, newx, iBand);
|
100 | 220 | cordinyana | case Buffer.TYPE_INT: |
101 | 382 | nbrodin | return m_Buffer.getElemInt(newy, newx, iBand);
|
102 | 220 | cordinyana | case Buffer.TYPE_FLOAT: |
103 | 382 | nbrodin | return m_Buffer.getElemFloat(newy, newx, iBand);
|
104 | 220 | cordinyana | case Buffer.TYPE_DOUBLE: |
105 | default:
|
||
106 | 382 | nbrodin | return m_Buffer.getElemDouble(newy, newx, iBand);
|
107 | 220 | cordinyana | } |
108 | } else {
|
||
109 | return m_dNoDataValue;
|
||
110 | } |
||
111 | |||
112 | } |
||
113 | |||
114 | public int getBandsCount() { |
||
115 | return m_Layer.getDataStore().getBandCount();
|
||
116 | } |
||
117 | |||
118 | public String getName() { |
||
119 | return m_Layer.getName();
|
||
120 | } |
||
121 | |||
122 | public void postProcess() { |
||
123 | |||
124 | } |
||
125 | |||
126 | public void open() { |
||
127 | try {
|
||
128 | m_LayerGrid = m_Layer.getReadOnlyFullGrid(false);
|
||
129 | m_Buffer = m_LayerGrid.getRasterBuf(); |
||
130 | m_iDataType = m_LayerGrid.getDataType(); |
||
131 | NoData nodata = m_LayerGrid.getNoDataValue(); |
||
132 | 382 | nbrodin | m_dNoDataValue = nodata != null ?
|
133 | (nodata.getValue() != null ? nodata.getValue().doubleValue() : 0.0d) : |
||
134 | 0.0d;
|
||
135 | } catch (GridException e) {
|
||
136 | 220 | cordinyana | Sextante.addErrorToLog(e); |
137 | 382 | nbrodin | } catch (InterruptedException e) { |
138 | 220 | cordinyana | Sextante.addErrorToLog(e); |
139 | } |
||
140 | } |
||
141 | |||
142 | public void close() { |
||
143 | m_LayerGrid = null;
|
||
144 | } |
||
145 | |||
146 | public Rectangle2D getFullExtent() { |
||
147 | return m_Layer.getFullRasterExtent().toRectangle2D();
|
||
148 | } |
||
149 | |||
150 | public AnalysisExtent getLayerGridExtent() {
|
||
151 | 382 | nbrodin | if(layerExtent == null) { |
152 | try {
|
||
153 | layerExtent = new AnalysisExtent();
|
||
154 | layerExtent.setCellSize(m_Layer.getDataStore().getCellSize()); |
||
155 | layerExtent.setXRange(m_Layer.getMinX(), m_Layer.getMaxX(), true);
|
||
156 | layerExtent.setYRange(m_Layer.getMinY(), m_Layer.getMaxY(), true);
|
||
157 | } catch (final Exception e) { |
||
158 | return null; |
||
159 | } |
||
160 | } |
||
161 | return layerExtent;
|
||
162 | } |
||
163 | |||
164 | public void setWindowExtent(final AnalysisExtent extent) { |
||
165 | super.setWindowExtent(extent);
|
||
166 | 220 | cordinyana | |
167 | 382 | nbrodin | RasterManager rManager = RasterLocator.getManager(); |
168 | RasterDataStore store = m_Layer.getDataStore(); |
||
169 | RasterQuery query = rManager.createQuery(); |
||
170 | query.setReadOnly(true);
|
||
171 | int[] bands = new int[store.getBandCount()]; |
||
172 | for (int i = 0; i < store.getBandCount(); i++) { |
||
173 | bands[i] = i; |
||
174 | } |
||
175 | query.setDrawableBands(bands); |
||
176 | Extent ext = RasterLocator.getManager().getDataStructFactory().createExtent( |
||
177 | extent.getXMin(), |
||
178 | extent.getYMax(), |
||
179 | extent.getXMax(), |
||
180 | extent.getYMin()); |
||
181 | query.setAreaOfInterest(ext, extent.getNX(), extent.getNY()); |
||
182 | Buffer entireBuf = m_Buffer;
|
||
183 | 220 | cordinyana | try {
|
184 | 382 | nbrodin | m_Buffer = store.query(query); |
185 | //Calculamos la traslaci?n respecto a la ventana recortada pq Sextante pedir?
|
||
186 | //p?xeles en relaci?n a la imagen completa
|
||
187 | double distx = extent.getXMin() - m_Layer.getMinX();
|
||
188 | double disty = m_Layer.getMaxY() - extent.getYMax();
|
||
189 | xTranslate = (int)Math.round((distx * extent.getNX()) / (extent.getXMax() - extent.getXMin())); |
||
190 | yTranslate = (int)Math.round((disty * extent.getNY()) / (extent.getYMax() - extent.getYMin())); |
||
191 | } catch (RasterDriverException e) {
|
||
192 | m_Buffer = entireBuf; |
||
193 | } catch (ProcessInterruptedException e) {
|
||
194 | } catch (InvalidSetViewException e) {
|
||
195 | m_Buffer = entireBuf; |
||
196 | } |
||
197 | } |
||
198 | 220 | cordinyana | |
199 | public double getLayerCellSize() { |
||
200 | return m_Layer.getDataStore().getCellSize();
|
||
201 | } |
||
202 | |||
203 | public Object getCRS() { |
||
204 | return m_Layer.getProjection();
|
||
205 | } |
||
206 | |||
207 | public void setName(final String name) { |
||
208 | m_Layer.setName(name); |
||
209 | } |
||
210 | |||
211 | public void free() { |
||
212 | m_Layer.dispose(); |
||
213 | m_Layer = null;
|
||
214 | m_LayerGrid = null;
|
||
215 | } |
||
216 | |||
217 | public Object getBaseDataObject() { |
||
218 | return m_Layer;
|
||
219 | } |
||
220 | |||
221 | public IOutputChannel getOutputChannel() {
|
||
222 | return new IOutputChannel() { |
||
223 | public String getAsCommandLineParameter() { |
||
224 | return m_Layer.getName();
|
||
225 | } |
||
226 | }; |
||
227 | } |
||
228 | |||
229 | } |