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 @ 2623
History | View | Annotate | Download (9.29 KB)
1 |
/* 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 |
|
49 |
private final Logger logger = LoggerFactory.getLogger(DatasetBandImpl.class); |
50 |
//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 |
private List<String> additionalName = new ArrayList<String>(); |
54 |
//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 |
|
64 |
public DatasetBandImpl() {
|
65 |
|
66 |
} |
67 |
|
68 |
/**
|
69 |
* Creates a new <code>DatasetBand</code>
|
70 |
* @param fileName
|
71 |
* File name
|
72 |
* @param position
|
73 |
* Position of this band inside the provider
|
74 |
* @param dataType
|
75 |
* Data type
|
76 |
* @param nBands
|
77 |
* Number of bands of the provider
|
78 |
*/
|
79 |
public DatasetBandImpl(String fileName, int position, int dataType, int nBands){ |
80 |
setFileName(fileName); |
81 |
setPosition(position); |
82 |
setDataType(dataType); |
83 |
nBandsInThisProvider = nBands; |
84 |
} |
85 |
|
86 |
/*
|
87 |
* (non-Javadoc)
|
88 |
* @see java.lang.Object#clone()
|
89 |
*/
|
90 |
public Object clone() { |
91 |
DatasetBandImpl result = new DatasetBandImpl(fileName, position, dataType, nBandsInThisProvider);
|
92 |
if(rasterBufBandToDrawList != null) { |
93 |
int[] drawBands = new int[rasterBufBandToDrawList.length]; |
94 |
for (int i = 0; i < rasterBufBandToDrawList.length; i++) |
95 |
drawBands[i] = rasterBufBandToDrawList[i]; |
96 |
result.rasterBufBandToDrawList = drawBands; |
97 |
} |
98 |
return result;
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Resetea la asignaci?n de dibujado de las bandas de la imagen
|
103 |
* sobre el DataImage cuando se hace un update para esta banda.
|
104 |
*/
|
105 |
public void clearDrawableBands(){ |
106 |
rasterBufBandToDrawList = null;
|
107 |
} |
108 |
|
109 |
//******************************
|
110 |
//Setters and Getters
|
111 |
//******************************
|
112 |
|
113 |
/**
|
114 |
* Obtiene las banda del RasterBuf sobre la que se pinta
|
115 |
* este objeto banda
|
116 |
* @return bandas del RasterBuf
|
117 |
*/
|
118 |
public int[] getBufferBandListToDraw() { |
119 |
return rasterBufBandToDrawList;
|
120 |
} |
121 |
|
122 |
/**
|
123 |
* Obtiene las banda del RasterBuf sobre la que se pinta
|
124 |
* este objeto banda
|
125 |
* @return bandas del RasterBuf
|
126 |
*/
|
127 |
public int[] getLocalBufferBandListToDraw() { |
128 |
return rasterBufBandToDrawList;
|
129 |
} |
130 |
|
131 |
/**
|
132 |
* Dice si la banda se est? dibujando en el buffers de salida.
|
133 |
* @return true si la banda se est? dibujando y false si no se est? haciendo
|
134 |
*/
|
135 |
public boolean isDrawing(){ |
136 |
if(this.rasterBufBandToDrawList == null) |
137 |
return false; |
138 |
return true; |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Adds a band of the output buffer where the input band is drawn. <code>rasterBufBandToDraw</code>
|
143 |
* is the number of band of the output buffer. This number is added to the list of numbers.
|
144 |
* @param rasterBufBandToDraw number of band of the <code>Buffer</code>
|
145 |
*/
|
146 |
public void addPositionToDrawInBuffer(int rasterBufBandToDraw) { |
147 |
if (rasterBufBandToDrawList == null) { |
148 |
rasterBufBandToDrawList = new int[1]; |
149 |
rasterBufBandToDrawList[0] = rasterBufBandToDraw;
|
150 |
} else {
|
151 |
int[] auxList = new int[rasterBufBandToDrawList.length + 1]; |
152 |
for (int i = 0; i < rasterBufBandToDrawList.length; i++) |
153 |
auxList[i] = rasterBufBandToDrawList[i]; |
154 |
auxList[rasterBufBandToDrawList.length] = rasterBufBandToDraw; |
155 |
rasterBufBandToDrawList = auxList; |
156 |
} |
157 |
} |
158 |
|
159 |
/**
|
160 |
* Obtiene el nombre del fichero al que pertenece la banda
|
161 |
* @return String con el nombre del fichero al
|
162 |
* que pertenece la banda
|
163 |
*/
|
164 |
public String getFileName() { |
165 |
return fileName;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* Asigna el nombre del fichero al que pertenece la banda
|
170 |
* @param fileName String con el nombre del fichero al
|
171 |
* que pertenece la banda
|
172 |
*/
|
173 |
public void setFileName(String fileName) { |
174 |
this.fileName = fileName;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* Obtiene la posici?n de la banda en el fichero
|
179 |
* @return entero con la posici?n de la banda en el fichero
|
180 |
*/
|
181 |
public int getPosition() { |
182 |
return position;
|
183 |
} |
184 |
|
185 |
/**
|
186 |
* Asigna la posici?n de la banda en el fichero
|
187 |
* @param position Posici?n de la banda en el fichero
|
188 |
*/
|
189 |
public void setPosition(int position) { |
190 |
this.position = position;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Obtiene el tipo de dato de la banda
|
195 |
* @return entero con el tipo de dato
|
196 |
*/
|
197 |
public int getDataType() { |
198 |
return dataType;
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Asigna el tipo de dato de la banda
|
203 |
* @param datatype entero con el tipo de dato de la banda
|
204 |
*/
|
205 |
public void setDataType(int dataType) { |
206 |
this.dataType = dataType;
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* Asigna un nombre de fichero adicional al principal. Esto es util para
|
211 |
* mosaicos de raster donde una banda est? compuesta por multiples ficheros.
|
212 |
*/
|
213 |
public void setAdditionalName(String fileName) { |
214 |
additionalName.add(fileName); |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Obtiene la lista de nombres de fichero adicionales.
|
219 |
* @return String[]
|
220 |
*/
|
221 |
public String[] getAdditionalName() { |
222 |
return (String[]) additionalName.toArray(); |
223 |
} |
224 |
|
225 |
@SuppressWarnings("unchecked") |
226 |
public void loadFromState(PersistentState state) |
227 |
throws PersistenceException {
|
228 |
List<String> aux = state.getList("additionalName"); |
229 |
this.additionalName = new ArrayList<String>(); |
230 |
this.additionalName.addAll(aux);
|
231 |
|
232 |
try {
|
233 |
URI uri = state.getURI("fileName"); |
234 |
if(uri != null) { |
235 |
File file = new File(uri.toString()); |
236 |
if(file.isFile() && file.exists())
|
237 |
fileName = file.getAbsolutePath(); |
238 |
else {
|
239 |
fileName = uri.toString(); |
240 |
if(uri.getScheme() == null || "file".equalsIgnoreCase(uri.getScheme())) |
241 |
fileName = uri.getPath(); |
242 |
} |
243 |
} |
244 |
} catch(ClassCastException e) { |
245 |
fileName = state.getString("fileName");
|
246 |
} |
247 |
|
248 |
this.position = state.getInt("position"); |
249 |
this.dataType = state.getInt("dataType"); |
250 |
this.nBandsInThisProvider = state.getInt("nBandsInThisProvider"); |
251 |
this.rasterBufBandToDrawList = (int[])state.getIntArray("rasterBufBandToDrawList"); |
252 |
} |
253 |
|
254 |
public void saveToState(PersistentState state) throws PersistenceException { |
255 |
try {
|
256 |
File file = new File(fileName); |
257 |
if(file.exists())
|
258 |
state.set("fileName", file.toURI());
|
259 |
else
|
260 |
state.set("fileName", new URI(fileName)); |
261 |
} catch (URISyntaxException e) { |
262 |
logger.debug("Error saving the URI " + fileName);
|
263 |
//Es mejor no propagar la excepci?n.
|
264 |
//Mejor que solo afecte al fichero que a todo el proyecto
|
265 |
//throw new PersistenceException(e);
|
266 |
} |
267 |
state.set("position", position);
|
268 |
state.set("dataType", dataType);
|
269 |
state.set("nBandsInThisProvider", nBandsInThisProvider);
|
270 |
state.set("additionalName", additionalName);
|
271 |
state.set("rasterBufBandToDrawList", rasterBufBandToDrawList);
|
272 |
} |
273 |
|
274 |
public static void registerPersistence() { |
275 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
276 |
DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
277 |
if( definition == null ) { |
278 |
definition = manager.addDefinition( |
279 |
DatasetBandImpl.class, |
280 |
PERSISTENT_NAME, |
281 |
PERSISTENT_DESCRIPTION, |
282 |
null,
|
283 |
null
|
284 |
); |
285 |
|
286 |
definition.addDynFieldURI("fileName").setMandatory(false); |
287 |
definition.addDynFieldInt("position").setMandatory(false); |
288 |
definition.addDynFieldList("additionalName").setClassOfItems(String.class).setMandatory(false); |
289 |
definition.addDynFieldList("rasterBufBandToDrawList").setClassOfItems(int.class).setMandatory(false); |
290 |
definition.addDynFieldInt("dataType").setMandatory(false); |
291 |
definition.addDynFieldInt("nBandsInThisProvider").setMandatory(false); |
292 |
} |
293 |
} |
294 |
} |