Revision 950 org.gvsig.raster.postgis/trunk/org.gvsig.raster.postgis/org.gvsig.raster.postgis.io/src/main/java/org/gvsig/raster/postgis/io/PostGISRasterProvider.java
PostGISRasterProvider.java | ||
---|---|---|
22 | 22 |
package org.gvsig.raster.postgis.io; |
23 | 23 |
|
24 | 24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.Point2D; |
|
26 |
import java.awt.geom.Rectangle2D; |
|
27 | 25 |
import java.util.ArrayList; |
28 | 26 |
import java.util.Collections; |
29 | 27 |
import java.util.Iterator; |
30 | 28 |
import java.util.List; |
31 | 29 |
|
32 | 30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
|
33 | 32 |
import org.gvsig.fmap.dal.DataStore; |
34 | 33 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
35 | 34 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
36 | 35 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
36 |
import org.gvsig.fmap.dal.coverage.datastruct.DatasetBand; |
|
37 | 37 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
38 |
import org.gvsig.fmap.dal.coverage.exception.BandNotFoundInListException; |
|
38 | 39 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
39 | 40 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
40 | 41 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
42 |
import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
|
43 |
import org.gvsig.fmap.dal.exception.DataEvaluatorException; |
|
41 | 44 |
import org.gvsig.fmap.dal.exception.DataException; |
45 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
46 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
|
47 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
42 | 48 |
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
43 | 49 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
44 | 50 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
45 | 51 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
46 |
import org.gvsig.jgdal.GdalException; |
|
47 | 52 |
import org.gvsig.metadata.MetadataLocator; |
48 | 53 |
import org.gvsig.raster.cache.tile.provider.TileListener; |
54 |
import org.gvsig.raster.gdal.io.GdalDataParameters; |
|
49 | 55 |
import org.gvsig.raster.gdal.io.GdalNative; |
50 | 56 |
import org.gvsig.raster.gdal.io.GdalProvider; |
51 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
|
57 |
import org.gvsig.raster.impl.datastruct.BandListImpl; |
|
58 |
import org.gvsig.raster.impl.datastruct.DatasetBandImpl; |
|
59 |
import org.gvsig.raster.impl.provider.DefaultRasterProvider; |
|
52 | 60 |
import org.gvsig.raster.impl.store.AbstractRasterDataParameters; |
53 | 61 |
import org.gvsig.raster.impl.store.DefaultStoreFactory; |
54 | 62 |
import org.gvsig.tools.ToolsLocator; |
... | ... | |
59 | 67 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
60 | 68 |
*/ |
61 | 69 |
public class PostGISRasterProvider extends GdalProvider { |
62 |
public static String NAME = "PostGIS Raster Store"; |
|
63 |
public static String DESCRIPTION = "PostGIS Raster file"; |
|
64 |
public static final String METADATA_DEFINITION_NAME = "PostGISRasterStore"; |
|
65 |
private static final String OVERVIEW_PREFIX = "o_"; |
|
70 |
public static String NAME = "PostGIS Raster Store";
|
|
71 |
public static String DESCRIPTION = "PostGIS Raster file";
|
|
72 |
public static final String METADATA_DEFINITION_NAME = "PostGISRasterStore";
|
|
73 |
private static final String OVERVIEW_PREFIX = "o_";
|
|
66 | 74 |
|
67 |
private DBServerExplorer dbServerExplorer = null; |
|
68 |
private DBStoreParameters dbParameters = null; |
|
69 |
private ArrayList<Overview> overviews = new ArrayList<Overview>(); |
|
75 |
private DBServerExplorer dbServerExplorer = null; |
|
76 |
private DBStoreParameters dbParameters = null; |
|
77 |
private ArrayList<Overview> overviews = null; |
|
78 |
private DataManager dataManager = DALLocator.getDataManager(); |
|
79 |
private DefaultRasterProvider selectedProvider = null; |
|
80 |
private Overview mainOverview = null; |
|
70 | 81 |
|
71 | 82 |
class Overview implements Comparable<Overview> { |
72 |
public int width = 0; |
|
73 |
public int height = 0; |
|
74 |
public int factor = 0; |
|
75 |
public double pxSizeOverview = 0; |
|
76 |
public DBStoreParameters param = null;
|
|
83 |
public int width = 0;
|
|
84 |
public int height = 0;
|
|
85 |
public int factor = 0;
|
|
86 |
public double pxSizeOverview = 0;
|
|
87 |
public DefaultRasterProvider provider = null;
|
|
77 | 88 |
|
78 | 89 |
public Overview() { |
79 | 90 |
|
80 | 91 |
} |
81 | 92 |
|
82 |
public Overview(int f, int w, int h, DBStoreParameters p) {
|
|
93 |
public Overview(int f, int w, int h, PostGISRasterDataParameters p) throws NotSupportedExtensionException {
|
|
83 | 94 |
this.width = w; |
84 | 95 |
this.height = h; |
85 | 96 |
this.factor = f; |
86 |
this.param = p; |
|
97 |
if(p != null) |
|
98 |
createProvider(p); |
|
87 | 99 |
this.pxSizeOverview = getExtent().width() / (double)w; |
88 | 100 |
} |
89 | 101 |
|
102 |
public void createProvider(PostGISRasterDataParameters p) throws NotSupportedExtensionException { |
|
103 |
GdalDataParameters gdalParams = new GdalDataParameters(); |
|
104 |
gdalParams.setURI(p.getURI()); |
|
105 |
this.provider = new GdalProvider(gdalParams, storeServices); |
|
106 |
this.width = (int)provider.getWidth(); |
|
107 |
this.height = (int)provider.getHeight(); |
|
108 |
} |
|
109 |
|
|
90 | 110 |
public int compareTo(Overview o) { |
91 | 111 |
if(factor < o.factor) |
92 | 112 |
return -1; |
... | ... | |
137 | 157 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException { |
138 | 158 |
try { |
139 | 159 |
setParam(storeServices, params); |
140 |
file = new GdalNative(translateFileName(params.getURI())); |
|
160 |
mainOverview = new Overview(); |
|
161 |
mainOverview.createProvider((PostGISRasterDataParameters)param); |
|
162 |
mainOverview.factor = 1; |
|
163 |
file = ((GdalProvider)mainOverview.provider).getNative(); |
|
141 | 164 |
setColorInterpretation(file.getColorInterpretation()); |
142 | 165 |
setColorTable(file.getColorTable()); |
143 | 166 |
noData = file.getNoDataValue(); |
... | ... | |
162 | 185 |
//Object obj = p.getDynValue(PostGISRasterDataParameters.FIELD_DBPARAMS); |
163 | 186 |
dbParameters = (DBStoreParameters)p.getDynValue(PostGISRasterDataParameters.FIELD_DBPARAMS); |
164 | 187 |
dbServerExplorer = (DBServerExplorer)p.getDynValue(PostGISRasterDataParameters.FIELD_DBEXPLORER); |
165 |
try { |
|
166 |
readOverviews(); |
|
167 |
} catch (DataException e) { |
|
168 |
throw new NotSupportedExtensionException("Overviews can't be read", e); |
|
169 |
} |
|
170 | 188 |
} |
171 | 189 |
|
172 | 190 |
/** |
... | ... | |
176 | 194 |
* |
177 | 195 |
* For that reason the overview object contains the DataParameter which correspond |
178 | 196 |
* to each table. |
179 |
*
|
|
197 |
* @throws DataException
|
|
180 | 198 |
* @throws DataException |
181 | 199 |
*/ |
182 |
private void readOverviews() throws DataException { |
|
183 |
overviews.clear(); |
|
184 |
overviews.add(new Overview(1, (int)getWidth(), (int)getHeight(), (DBStoreParameters)param)); |
|
185 |
|
|
186 |
String table = dbParameters.getTable(); |
|
187 |
List parameters = dbServerExplorer.list(); |
|
200 |
@SuppressWarnings("unchecked") |
|
201 |
private void readOverviews() throws DataException { |
|
202 |
if(overviews == null) { |
|
203 |
overviews = new ArrayList<Overview>(); |
|
204 |
String mainHost = ((PostGISRasterDataParameters)param).getURI(); |
|
188 | 205 |
|
189 |
Iterator iter = parameters.iterator(); |
|
190 |
DBStoreParameters param = null; |
|
191 |
while (iter.hasNext()) { |
|
192 |
DBStoreParameters p = (DBStoreParameters) iter.next(); |
|
193 |
String tname = p.getTable(); |
|
194 |
if(tname.endsWith(table) && tname.startsWith(OVERVIEW_PREFIX)) { |
|
195 |
String factor = tname.substring(tname.indexOf('_') + 1); |
|
196 |
factor = factor.substring(0, factor.indexOf('_')); |
|
206 |
String mainTable = dbParameters.getTable(); |
|
207 |
List parameters = dbServerExplorer.list(); |
|
208 |
|
|
209 |
int mainFactor = 1; |
|
210 |
|
|
211 |
if(isOverview(mainTable, mainTable)) { |
|
212 |
mainFactor = getFactor(mainTable); |
|
213 |
} |
|
214 |
mainOverview.pxSizeOverview = getExtent().width() / (double)mainOverview.width; |
|
215 |
overviews.add(mainOverview); |
|
216 |
|
|
217 |
Iterator iter = parameters.iterator(); |
|
218 |
while (iter.hasNext()) { |
|
219 |
DBStoreParameters p = (DBStoreParameters) iter.next(); |
|
220 |
String tname = p.getTable(); |
|
221 |
|
|
197 | 222 |
try { |
198 |
Overview o = new Overview(); |
|
199 |
o.factor = new Integer(factor); |
|
200 |
o.width = (int)(getWidth() / o.factor); |
|
201 |
o.height = (int)(getHeight() / o.factor); |
|
202 |
o.pxSizeOverview = getExtent().width() / (double)o.width; |
|
203 |
o.param = p; |
|
204 |
overviews.add(o); |
|
223 |
if(isOverview(mainTable, tname)) { |
|
224 |
Overview o = new Overview(); |
|
225 |
o.factor = (getFactor(tname) / mainFactor); |
|
226 |
o.width = (int)(getWidth() / o.factor); |
|
227 |
o.height = (int)(getHeight() / o.factor); |
|
228 |
o.pxSizeOverview = getExtent().width() / (double)o.width; |
|
229 |
o.createProvider(createParameters(mainHost, mainTable, tname)); |
|
230 |
overviews.add(o); |
|
231 |
} |
|
205 | 232 |
} catch (NumberFormatException e) { |
233 |
} catch (PostGISRasterCoreException e) { |
|
234 |
} catch (ValidateDataParametersException e) { |
|
235 |
} catch (NotSupportedExtensionException e) { |
|
206 | 236 |
} |
207 | 237 |
} |
238 |
Collections.sort(overviews); |
|
208 | 239 |
} |
209 |
Collections.sort(overviews); |
|
210 | 240 |
} |
211 | 241 |
|
212 | 242 |
/** |
243 |
* Returns true if tname is an overview of mainTable |
|
244 |
* @param mainTable |
|
245 |
* @param tname |
|
246 |
* @return |
|
247 |
*/ |
|
248 |
private boolean isOverview(String mainTable, String tname) throws NumberFormatException { |
|
249 |
if(mainTable.compareTo(tname) != 0) { |
|
250 |
return ((tname.endsWith(mainTable) && tname.startsWith(OVERVIEW_PREFIX)) || |
|
251 |
(tname.startsWith(OVERVIEW_PREFIX) && mainTable.startsWith(OVERVIEW_PREFIX) && getFactor(tname) > getFactor(mainTable))); |
|
252 |
} |
|
253 |
return false; |
|
254 |
} |
|
255 |
|
|
256 |
/** |
|
257 |
* Gets the overview factor number reading the name of the table |
|
258 |
* @param tname |
|
259 |
* @return |
|
260 |
* @throws NumberFormatException |
|
261 |
*/ |
|
262 |
private int getFactor(String tname) throws NumberFormatException { |
|
263 |
String factor = tname.substring(tname.indexOf('_') + 1); |
|
264 |
factor = factor.substring(0, factor.indexOf('_')); |
|
265 |
return new Integer(factor); |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Creates the list of parameters |
|
270 |
* @param mainHost |
|
271 |
* @param mainTable |
|
272 |
* @param tableName |
|
273 |
* @return |
|
274 |
* @throws PostGISRasterCoreException |
|
275 |
* @throws PostGISRasterCoreException |
|
276 |
* @throws ProviderNotRegisteredException |
|
277 |
* @throws InitializeException |
|
278 |
* @throws ValidateDataParametersException |
|
279 |
*/ |
|
280 |
@SuppressWarnings("deprecation") |
|
281 |
private PostGISRasterDataParameters createParameters(String mainHost, String mainTable, String tableName) throws PostGISRasterCoreException, ValidateDataParametersException, InitializeException, ProviderNotRegisteredException { |
|
282 |
String newHost = mainHost.replaceFirst(mainTable, tableName); |
|
283 |
|
|
284 |
PostGISRasterServerExplorerParameters explorerParams = (PostGISRasterServerExplorerParameters) dataManager.createServerExplorerParameters(PostGISRasterServerExplorer.NAME); |
|
285 |
explorerParams.setHost(newHost); |
|
286 |
PostGISRasterServerExplorer explorer = (PostGISRasterServerExplorer) dataManager.createServerExplorer(explorerParams); |
|
287 |
RasterDataParameters storeParameters = (RasterDataParameters)explorer.getStoreParameters(); |
|
288 |
storeParameters.setURI(newHost); |
|
289 |
return (PostGISRasterDataParameters)storeParameters; |
|
290 |
} |
|
291 |
|
|
292 |
/** |
|
213 | 293 |
* Selects the right overview. The right overview means the first |
214 | 294 |
* one with a pixel size lesser or equals than the original. |
215 | 295 |
* @param pixels |
216 | 296 |
* @param mts |
217 | 297 |
*/ |
218 | 298 |
private void overviewSelector(int pixels, double mts) { |
219 |
this.param = overviews.get(0).param; |
|
299 |
this.selectedProvider = overviews.get(overviews.size() - 1).provider; |
|
300 |
System.out.println("*************" + overviews.get(overviews.size() - 1).pxSizeOverview + "**************"); |
|
220 | 301 |
double pxSizeRequest = mts / (double)pixels; |
221 | 302 |
for (int i = overviews.size() - 1; i > 0; i--) { |
222 | 303 |
Overview ovb = overviews.get(i); |
223 | 304 |
if(pxSizeRequest <= ovb.pxSizeOverview) { |
224 |
this.param = ovb.param; |
|
305 |
this.selectedProvider = ovb.provider; |
|
306 |
System.out.println(ovb.pxSizeOverview); |
|
225 | 307 |
} |
226 | 308 |
} |
227 |
|
|
309 |
System.out.println("***************************"); |
|
228 | 310 |
} |
229 | 311 |
|
230 | 312 |
/* |
... | ... | |
260 | 342 |
*/ |
261 | 343 |
public Buffer getWindow(Extent extent, |
262 | 344 |
int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException { |
263 |
return super.getWindow(extent, bufWidth, bufHeight, bandList, rasterBuf, adjustToExtent); |
|
345 |
try { |
|
346 |
readOverviews(); |
|
347 |
} catch (DataException e) { |
|
348 |
throw new RasterDriverException("Overviews can't be read", e); |
|
349 |
} |
|
350 |
overviewSelector(bufWidth, extent.width()); |
|
351 |
//Creamos un BandList con todas las bandas del fichero |
|
352 |
BandList bl = new BandListImpl(); |
|
353 |
for(int i = 0; i < getBandCount(); i++) { |
|
354 |
try { |
|
355 |
DatasetBand band = new DatasetBandImpl(selectedProvider.getURIOfFirstProvider(), i, getDataType()[i], getBandCount()); |
|
356 |
bl.addBand(band, i); |
|
357 |
} catch(BandNotFoundInListException e) { |
|
358 |
//No a?adimos la banda |
|
359 |
} |
|
360 |
} |
|
361 |
bl.setDrawableBands(bandList.getDrawableBands()); |
|
362 |
|
|
363 |
return selectedProvider.getWindow(extent, bufWidth, bufHeight, bl, rasterBuf, adjustToExtent); |
|
264 | 364 |
} |
265 | 365 |
|
266 | 366 |
/* |
Also available in: Unified diff