gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / DatasetBandImpl.java @ 4439
History | View | Annotate | Download (9.95 KB)
1 | 2443 | nbrodin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
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 | */
|
||
22 | package org.gvsig.raster.impl.datastruct; |
||
23 | |||
24 | import java.io.File; |
||
25 | import java.net.URI; |
||
26 | import java.net.URISyntaxException; |
||
27 | import java.util.ArrayList; |
||
28 | import java.util.List; |
||
29 | |||
30 | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
||
31 | import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand; |
||
32 | import org.gvsig.tools.ToolsLocator; |
||
33 | import org.gvsig.tools.dynobject.DynStruct; |
||
34 | import org.gvsig.tools.persistence.PersistenceManager; |
||
35 | import org.gvsig.tools.persistence.PersistentState; |
||
36 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
37 | import org.slf4j.Logger; |
||
38 | import org.slf4j.LoggerFactory; |
||
39 | |||
40 | |||
41 | /**
|
||
42 | * Clase que representa a una banda de un fichero.
|
||
43 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
44 | */
|
||
45 | public class DatasetBandImpl implements DatasetBand { |
||
46 | public static final String PERSISTENT_NAME = "DatasetBand_Persistent"; |
||
47 | public static final String PERSISTENT_DESCRIPTION = "DatasetBand Persistent"; |
||
48 | 4181 | fdiaz | |
49 | 2623 | nbrodin | private final Logger logger = LoggerFactory.getLogger(DatasetBandImpl.class); |
50 | 2443 | nbrodin | //Nombre del fichero al que pertenece la banda
|
51 | private String fileName = ""; |
||
52 | //Lista de nombre de otros ficheros que forman la banda. Esto es util para CompositeDataset que est? compuesto por un mosaico de ficheros.
|
||
53 | 2623 | nbrodin | private List<String> additionalName = new ArrayList<String>(); |
54 | 2443 | nbrodin | //Posici?n en el fichero de la banda
|
55 | private int position = -1; |
||
56 | //Tipo de dato de la banda
|
||
57 | private int dataType = Buffer.TYPE_BYTE; |
||
58 | //lista de bandas del buffer donde se dibuja esta banda de imagen. Cada elemento
|
||
59 | //del vector es un n?mero que corresponde con el n?mero de banda del buffer donde se escribe esta
|
||
60 | private int[] rasterBufBandToDrawList = null; |
||
61 | //
|
||
62 | private int nBandsInThisProvider = 0; |
||
63 | 4181 | fdiaz | |
64 | 4439 | fdiaz | /**
|
65 | * Constructor
|
||
66 | */
|
||
67 | 2443 | nbrodin | public DatasetBandImpl() {
|
68 | |||
69 | } |
||
70 | 4181 | fdiaz | |
71 | 2443 | nbrodin | /**
|
72 | * Creates a new <code>DatasetBand</code>
|
||
73 | * @param fileName
|
||
74 | * File name
|
||
75 | * @param position
|
||
76 | * Position of this band inside the provider
|
||
77 | * @param dataType
|
||
78 | * Data type
|
||
79 | * @param nBands
|
||
80 | * Number of bands of the provider
|
||
81 | */
|
||
82 | public DatasetBandImpl(String fileName, int position, int dataType, int nBands){ |
||
83 | setFileName(fileName); |
||
84 | setPosition(position); |
||
85 | setDataType(dataType); |
||
86 | nBandsInThisProvider = nBands; |
||
87 | } |
||
88 | 4181 | fdiaz | |
89 | 4200 | fdiaz | /**
|
90 | * Creates a new <code>DatasetBand</code>
|
||
91 | * @param uri
|
||
92 | * URI
|
||
93 | * @param position
|
||
94 | * Position of this band inside the provider
|
||
95 | * @param dataType
|
||
96 | * Data type
|
||
97 | * @param nBands
|
||
98 | * Number of bands of the provider
|
||
99 | */
|
||
100 | public DatasetBandImpl(URI uri, int position, int dataType, int nBands){ |
||
101 | setFileName(uri.getPath()); |
||
102 | setPosition(position); |
||
103 | setDataType(dataType); |
||
104 | nBandsInThisProvider = nBands; |
||
105 | } |
||
106 | |||
107 | 2443 | nbrodin | /*
|
108 | * (non-Javadoc)
|
||
109 | * @see java.lang.Object#clone()
|
||
110 | */
|
||
111 | public Object clone() { |
||
112 | DatasetBandImpl result = new DatasetBandImpl(fileName, position, dataType, nBandsInThisProvider);
|
||
113 | if(rasterBufBandToDrawList != null) { |
||
114 | int[] drawBands = new int[rasterBufBandToDrawList.length]; |
||
115 | 4181 | fdiaz | for (int i = 0; i < rasterBufBandToDrawList.length; i++) |
116 | 2443 | nbrodin | drawBands[i] = rasterBufBandToDrawList[i]; |
117 | result.rasterBufBandToDrawList = drawBands; |
||
118 | } |
||
119 | return result;
|
||
120 | } |
||
121 | 4181 | fdiaz | |
122 | 2443 | nbrodin | /**
|
123 | * Resetea la asignaci?n de dibujado de las bandas de la imagen
|
||
124 | * sobre el DataImage cuando se hace un update para esta banda.
|
||
125 | */
|
||
126 | public void clearDrawableBands(){ |
||
127 | rasterBufBandToDrawList = null;
|
||
128 | } |
||
129 | |||
130 | //******************************
|
||
131 | //Setters and Getters
|
||
132 | //******************************
|
||
133 | 4181 | fdiaz | |
134 | 2443 | nbrodin | /**
|
135 | * Obtiene las banda del RasterBuf sobre la que se pinta
|
||
136 | * este objeto banda
|
||
137 | * @return bandas del RasterBuf
|
||
138 | */
|
||
139 | public int[] getBufferBandListToDraw() { |
||
140 | return rasterBufBandToDrawList;
|
||
141 | } |
||
142 | 4181 | fdiaz | |
143 | 2443 | nbrodin | /**
|
144 | * Obtiene las banda del RasterBuf sobre la que se pinta
|
||
145 | * este objeto banda
|
||
146 | * @return bandas del RasterBuf
|
||
147 | */
|
||
148 | public int[] getLocalBufferBandListToDraw() { |
||
149 | return rasterBufBandToDrawList;
|
||
150 | } |
||
151 | 4181 | fdiaz | |
152 | 2443 | nbrodin | /**
|
153 | * Dice si la banda se est? dibujando en el buffers de salida.
|
||
154 | * @return true si la banda se est? dibujando y false si no se est? haciendo
|
||
155 | */
|
||
156 | public boolean isDrawing(){ |
||
157 | if(this.rasterBufBandToDrawList == null) |
||
158 | return false; |
||
159 | return true; |
||
160 | } |
||
161 | 4181 | fdiaz | |
162 | 2443 | nbrodin | /**
|
163 | * Adds a band of the output buffer where the input band is drawn. <code>rasterBufBandToDraw</code>
|
||
164 | * is the number of band of the output buffer. This number is added to the list of numbers.
|
||
165 | * @param rasterBufBandToDraw number of band of the <code>Buffer</code>
|
||
166 | */
|
||
167 | public void addPositionToDrawInBuffer(int rasterBufBandToDraw) { |
||
168 | if (rasterBufBandToDrawList == null) { |
||
169 | rasterBufBandToDrawList = new int[1]; |
||
170 | rasterBufBandToDrawList[0] = rasterBufBandToDraw;
|
||
171 | } else {
|
||
172 | int[] auxList = new int[rasterBufBandToDrawList.length + 1]; |
||
173 | for (int i = 0; i < rasterBufBandToDrawList.length; i++) |
||
174 | auxList[i] = rasterBufBandToDrawList[i]; |
||
175 | auxList[rasterBufBandToDrawList.length] = rasterBufBandToDraw; |
||
176 | rasterBufBandToDrawList = auxList; |
||
177 | } |
||
178 | } |
||
179 | 4181 | fdiaz | |
180 | 2443 | nbrodin | /**
|
181 | * Obtiene el nombre del fichero al que pertenece la banda
|
||
182 | 4181 | fdiaz | * @return String con el nombre del fichero al
|
183 | 2443 | nbrodin | * que pertenece la banda
|
184 | */
|
||
185 | public String getFileName() { |
||
186 | return fileName;
|
||
187 | } |
||
188 | |||
189 | /**
|
||
190 | * Asigna el nombre del fichero al que pertenece la banda
|
||
191 | 4181 | fdiaz | * @param fileName String con el nombre del fichero al
|
192 | 2443 | nbrodin | * que pertenece la banda
|
193 | */
|
||
194 | public void setFileName(String fileName) { |
||
195 | this.fileName = fileName;
|
||
196 | } |
||
197 | |||
198 | /**
|
||
199 | * Obtiene la posici?n de la banda en el fichero
|
||
200 | * @return entero con la posici?n de la banda en el fichero
|
||
201 | */
|
||
202 | public int getPosition() { |
||
203 | return position;
|
||
204 | } |
||
205 | |||
206 | /**
|
||
207 | * Asigna la posici?n de la banda en el fichero
|
||
208 | * @param position Posici?n de la banda en el fichero
|
||
209 | */
|
||
210 | public void setPosition(int position) { |
||
211 | this.position = position;
|
||
212 | } |
||
213 | |||
214 | /**
|
||
215 | * Obtiene el tipo de dato de la banda
|
||
216 | 4181 | fdiaz | * @return entero con el tipo de dato
|
217 | 2443 | nbrodin | */
|
218 | public int getDataType() { |
||
219 | return dataType;
|
||
220 | } |
||
221 | |||
222 | /**
|
||
223 | * Asigna el tipo de dato de la banda
|
||
224 | * @param datatype entero con el tipo de dato de la banda
|
||
225 | */
|
||
226 | public void setDataType(int dataType) { |
||
227 | this.dataType = dataType;
|
||
228 | } |
||
229 | 4181 | fdiaz | |
230 | 2443 | nbrodin | /**
|
231 | * Asigna un nombre de fichero adicional al principal. Esto es util para
|
||
232 | * mosaicos de raster donde una banda est? compuesta por multiples ficheros.
|
||
233 | */
|
||
234 | public void setAdditionalName(String fileName) { |
||
235 | additionalName.add(fileName); |
||
236 | } |
||
237 | 4181 | fdiaz | |
238 | 2443 | nbrodin | /**
|
239 | * Obtiene la lista de nombres de fichero adicionales.
|
||
240 | * @return String[]
|
||
241 | */
|
||
242 | public String[] getAdditionalName() { |
||
243 | return (String[]) additionalName.toArray(); |
||
244 | } |
||
245 | |||
246 | @SuppressWarnings("unchecked") |
||
247 | public void loadFromState(PersistentState state) |
||
248 | throws PersistenceException {
|
||
249 | List<String> aux = state.getList("additionalName"); |
||
250 | this.additionalName = new ArrayList<String>(); |
||
251 | this.additionalName.addAll(aux);
|
||
252 | 4181 | fdiaz | |
253 | 2443 | nbrodin | try {
|
254 | URI uri = state.getURI("fileName"); |
||
255 | if(uri != null) { |
||
256 | 4439 | fdiaz | fileName = uri.toString(); |
257 | if (uri.getScheme() != null && "FILE".equalsIgnoreCase(uri.getScheme())) { |
||
258 | File file = new File(uri); |
||
259 | if (file.isFile() && file.exists()) {
|
||
260 | fileName = file.getAbsolutePath(); |
||
261 | } else {
|
||
262 | fileName = uri.getPath(); |
||
263 | } |
||
264 | } |
||
265 | 2443 | nbrodin | } |
266 | } catch(ClassCastException e) { |
||
267 | fileName = state.getString("fileName");
|
||
268 | } |
||
269 | |||
270 | this.position = state.getInt("position"); |
||
271 | this.dataType = state.getInt("dataType"); |
||
272 | this.nBandsInThisProvider = state.getInt("nBandsInThisProvider"); |
||
273 | this.rasterBufBandToDrawList = (int[])state.getIntArray("rasterBufBandToDrawList"); |
||
274 | } |
||
275 | |||
276 | public void saveToState(PersistentState state) throws PersistenceException { |
||
277 | try {
|
||
278 | File file = new File(fileName); |
||
279 | 4181 | fdiaz | if(file.exists())
|
280 | 2443 | nbrodin | state.set("fileName", file.toURI());
|
281 | else
|
||
282 | state.set("fileName", new URI(fileName)); |
||
283 | } catch (URISyntaxException e) { |
||
284 | logger.debug("Error saving the URI " + fileName);
|
||
285 | 4181 | fdiaz | //Es mejor no propagar la excepci?n.
|
286 | 2443 | nbrodin | //Mejor que solo afecte al fichero que a todo el proyecto
|
287 | //throw new PersistenceException(e);
|
||
288 | } |
||
289 | 4181 | fdiaz | state.set("position", position);
|
290 | state.set("dataType", dataType);
|
||
291 | state.set("nBandsInThisProvider", nBandsInThisProvider);
|
||
292 | state.set("additionalName", additionalName);
|
||
293 | state.set("rasterBufBandToDrawList", rasterBufBandToDrawList);
|
||
294 | } |
||
295 | |||
296 | 4439 | fdiaz | /**
|
297 | * Register persistence
|
||
298 | */
|
||
299 | 2443 | nbrodin | public static void registerPersistence() { |
300 | PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
||
301 | DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
||
302 | if( definition == null ) { |
||
303 | definition = manager.addDefinition( |
||
304 | DatasetBandImpl.class, |
||
305 | PERSISTENT_NAME, |
||
306 | PERSISTENT_DESCRIPTION, |
||
307 | 4181 | fdiaz | null,
|
308 | 2443 | nbrodin | null
|
309 | ); |
||
310 | 4181 | fdiaz | |
311 | 2443 | nbrodin | definition.addDynFieldURI("fileName").setMandatory(false); |
312 | definition.addDynFieldInt("position").setMandatory(false); |
||
313 | definition.addDynFieldList("additionalName").setClassOfItems(String.class).setMandatory(false); |
||
314 | definition.addDynFieldList("rasterBufBandToDrawList").setClassOfItems(int.class).setMandatory(false); |
||
315 | definition.addDynFieldInt("dataType").setMandatory(false); |
||
316 | definition.addDynFieldInt("nBandsInThisProvider").setMandatory(false); |
||
317 | } |
||
318 | } |
||
319 | } |