gvsig-raster / org.gvsig.raster.postgis / trunk / org.gvsig.raster.postgis / org.gvsig.raster.postgis.io / src / main / java / org / gvsig / raster / postgis / io / PostGISRasterProvider.java @ 938
History | View | Annotate | Download (11.5 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.postgis.io; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.Point2D; |
26 |
import java.awt.geom.Rectangle2D; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Collections; |
29 |
import java.util.Iterator; |
30 |
import java.util.List; |
31 |
|
32 |
import org.gvsig.fmap.dal.DALLocator; |
33 |
import org.gvsig.fmap.dal.DataStore; |
34 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
35 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
36 |
import org.gvsig.fmap.dal.coverage.datastruct.BandList; |
37 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
38 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
39 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
40 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
41 |
import org.gvsig.fmap.dal.exception.DataException; |
42 |
import org.gvsig.fmap.dal.serverexplorer.db.DBServerExplorer; |
43 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
44 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
45 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
46 |
import org.gvsig.jgdal.GdalException; |
47 |
import org.gvsig.metadata.MetadataLocator; |
48 |
import org.gvsig.raster.cache.tile.provider.TileListener; |
49 |
import org.gvsig.raster.gdal.io.GdalNative; |
50 |
import org.gvsig.raster.gdal.io.GdalProvider; |
51 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
52 |
import org.gvsig.raster.impl.store.AbstractRasterDataParameters; |
53 |
import org.gvsig.raster.impl.store.DefaultStoreFactory; |
54 |
import org.gvsig.tools.ToolsLocator; |
55 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
56 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
57 |
/**
|
58 |
* This class represents the data access just for PostGIS raster databases.
|
59 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
60 |
*/
|
61 |
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_"; |
66 |
|
67 |
private DBServerExplorer dbServerExplorer = null; |
68 |
private DBStoreParameters dbParameters = null; |
69 |
private ArrayList<Overview> overviews = new ArrayList<Overview>(); |
70 |
|
71 |
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; |
77 |
|
78 |
public Overview() {
|
79 |
|
80 |
} |
81 |
|
82 |
public Overview(int f, int w, int h, DBStoreParameters p) { |
83 |
this.width = w;
|
84 |
this.height = h;
|
85 |
this.factor = f;
|
86 |
this.param = p;
|
87 |
this.pxSizeOverview = getExtent().width() / (double)w; |
88 |
} |
89 |
|
90 |
public int compareTo(Overview o) { |
91 |
if(factor < o.factor)
|
92 |
return -1; |
93 |
if(factor > o.factor)
|
94 |
return 1; |
95 |
return 0; |
96 |
} |
97 |
} |
98 |
|
99 |
public static void register() { |
100 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
101 |
ExtensionPoint point = extensionPoints.get("DefaultDriver");
|
102 |
point.append("reader", PostGISRasterProvider.NAME, PostGISRasterProvider.class);
|
103 |
|
104 |
RasterLocator.getManager().registerFileProvidersTiled(PostGISRasterProvider.class); |
105 |
|
106 |
DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator.getDataManager(); |
107 |
if (dataman != null && !dataman.getStoreProviders().contains(NAME)) { |
108 |
dataman.registerStoreProvider(NAME, |
109 |
PostGISRasterProvider.class, PostGISRasterDataParameters.class); |
110 |
} |
111 |
|
112 |
if (!dataman.getExplorerProviders().contains(PostGISRasterProvider.NAME)) {
|
113 |
dataman.registerExplorerProvider(PostGISRasterServerExplorer.NAME, PostGISRasterServerExplorer.class, PostGISRasterServerExplorerParameters.class); |
114 |
} |
115 |
dataman.registerStoreFactory(NAME, DefaultStoreFactory.class); |
116 |
} |
117 |
|
118 |
public PostGISRasterProvider() {
|
119 |
|
120 |
} |
121 |
|
122 |
public PostGISRasterProvider (PostGISRasterDataParameters params,
|
123 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
124 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
125 |
.createDynObject( |
126 |
MetadataLocator.getMetadataManager().getDefinition( |
127 |
DataStore.METADATA_DEFINITION_NAME))); |
128 |
init(params, storeServices); |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Creates data base references and loads structures with the information and metadata
|
133 |
* @param params load parameters
|
134 |
* @throws NotSupportedExtensionException
|
135 |
*/
|
136 |
public void init (AbstractRasterDataParameters params, |
137 |
DataStoreProviderServices storeServices) throws NotSupportedExtensionException {
|
138 |
try {
|
139 |
setParam(storeServices, params); |
140 |
file = new GdalNative(translateFileName(params.getURI()));
|
141 |
setColorInterpretation(file.getColorInterpretation()); |
142 |
setColorTable(file.getColorTable()); |
143 |
noData = file.getNoDataValue(); |
144 |
wktProjection = file.getProjectionRef(); |
145 |
//CrsWkt crs = new CrsWkt(wktProjection);
|
146 |
//IProjection proj = CRSFactory.getCRS("EPSG:23030");
|
147 |
ownTransformation = file.getOwnTransformation(); |
148 |
externalTransformation = (AffineTransform)ownTransformation.clone();
|
149 |
bandCount = file.getRasterCount(); |
150 |
load(); |
151 |
} catch (Exception e) { |
152 |
throw new NotSupportedExtensionException("Gdal library can't be initialized with this PostGIS raster parameters", e); |
153 |
} |
154 |
|
155 |
//Obtenemos el tipo de dato de gdal y lo convertimos el de RasterBuf
|
156 |
int[] dt = new int[file.getDataType().length]; |
157 |
for (int i = 0; i < dt.length; i++) |
158 |
dt[i] = rasterUtil.getRasterBufTypeFromGdalType(file.getDataType()[i]); |
159 |
setDataType(dt); |
160 |
|
161 |
PostGISRasterDataParameters p = (PostGISRasterDataParameters)params; |
162 |
//Object obj = p.getDynValue(PostGISRasterDataParameters.FIELD_DBPARAMS);
|
163 |
dbParameters = (DBStoreParameters)p.getDynValue(PostGISRasterDataParameters.FIELD_DBPARAMS); |
164 |
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 |
} |
171 |
|
172 |
/**
|
173 |
* Reads overviews from database. The overviews in PostGIS raster are loaded
|
174 |
* in separated tables. The name of those tables is the same that the original table
|
175 |
* with a overview prefix. Now this prefix is "o_X_" where the X is a factor scale.
|
176 |
*
|
177 |
* For that reason the overview object contains the DataParameter which correspond
|
178 |
* to each table.
|
179 |
*
|
180 |
* @throws DataException
|
181 |
*/
|
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();
|
188 |
|
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('_')); |
197 |
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); |
205 |
} catch (NumberFormatException e) { |
206 |
} |
207 |
} |
208 |
} |
209 |
Collections.sort(overviews);
|
210 |
} |
211 |
|
212 |
/**
|
213 |
* Selects the right overview. The right overview means the first
|
214 |
* one with a pixel size lesser or equals than the original.
|
215 |
* @param pixels
|
216 |
* @param mts
|
217 |
*/
|
218 |
private void overviewSelector(int pixels, double mts) { |
219 |
this.param = overviews.get(0).param; |
220 |
double pxSizeRequest = mts / (double)pixels; |
221 |
for (int i = overviews.size() - 1; i > 0; i--) { |
222 |
Overview ovb = overviews.get(i); |
223 |
if(pxSizeRequest <= ovb.pxSizeOverview) {
|
224 |
this.param = ovb.param;
|
225 |
} |
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
/*
|
231 |
* (non-Javadoc)
|
232 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.raster.cache.tile.provider.TileListener)
|
233 |
*/
|
234 |
public void getWindow(Extent ex, int bufWidth, int bufHeight, |
235 |
BandList bandList, TileListener listener) throws ProcessInterruptedException, RasterDriverException {
|
236 |
super.getWindow(ex, bufWidth, bufHeight, bandList, listener);
|
237 |
} |
238 |
|
239 |
/*
|
240 |
* (non-Javadoc)
|
241 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(org.gvsig.fmap.dal.coverage.datastruct.Extent, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
|
242 |
*/
|
243 |
public Buffer getWindow(Extent ex, BandList bandList, Buffer rasterBuf) |
244 |
throws ProcessInterruptedException, RasterDriverException {
|
245 |
return super.getWindow(ex, bandList, rasterBuf); |
246 |
} |
247 |
|
248 |
/*
|
249 |
* (non-Javadoc)
|
250 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
|
251 |
*/
|
252 |
public Buffer getWindow(double ulx, double uly, double w, double h, |
253 |
BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException { |
254 |
return super.getWindow(ulx, uly, w, h, bandList, rasterBuf, adjustToExtent); |
255 |
} |
256 |
|
257 |
/*
|
258 |
* (non-Javadoc)
|
259 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(double, double, double, double, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer, boolean)
|
260 |
*/
|
261 |
public Buffer getWindow(Extent extent, |
262 |
int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf, boolean adjustToExtent) throws ProcessInterruptedException, RasterDriverException { |
263 |
return super.getWindow(extent, bufWidth, bufHeight, bandList, rasterBuf, adjustToExtent); |
264 |
} |
265 |
|
266 |
/*
|
267 |
* (non-Javadoc)
|
268 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
|
269 |
*/
|
270 |
public Buffer getWindow(int x, int y, |
271 |
BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException { |
272 |
return super.getWindow(x, y, bandList, rasterBuf); |
273 |
} |
274 |
|
275 |
/*
|
276 |
* (non-Javadoc)
|
277 |
* @see org.gvsig.raster.impl.provider.DefaultRasterProvider#getWindowRaster(int, int, int, int, int, int, org.gvsig.fmap.dal.coverage.datastruct.BandList, org.gvsig.fmap.dal.coverage.dataset.Buffer)
|
278 |
*/
|
279 |
public Buffer getWindow(int x, int y, int w, int h, |
280 |
int bufWidth, int bufHeight, BandList bandList, Buffer rasterBuf) throws ProcessInterruptedException, RasterDriverException { |
281 |
return super.getWindow(x, y, w, h, bufWidth, bufHeight, bandList, rasterBuf); |
282 |
} |
283 |
|
284 |
|
285 |
} |