gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.gdal / org.gvsig.raster.gdal.provider / src / main / java / org / gvsig / raster / gdal / provider / RasterGdalPostgisStoreProvider.java @ 8697
History | View | Annotate | Download (10.4 KB)
1 | 6335 | dmartinezizquierdo | package org.gvsig.raster.gdal.provider; |
---|---|---|---|
2 | |||
3 | import java.util.Arrays; |
||
4 | import java.util.Vector; |
||
5 | |||
6 | import org.apache.commons.lang3.StringUtils; |
||
7 | import org.cresques.cts.IProjection; |
||
8 | import org.gdal.gdal.Driver; |
||
9 | import org.gdal.gdal.GCP; |
||
10 | import org.gdal.gdal.gdal; |
||
11 | import org.gdal.gdalconst.gdalconstConstants; |
||
12 | import org.gvsig.fmap.dal.DALLocator; |
||
13 | import org.gvsig.fmap.dal.DataManager; |
||
14 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
15 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
16 | import org.gvsig.fmap.dal.exception.DataException; |
||
17 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
18 | import org.gvsig.fmap.dal.exception.OpenException; |
||
19 | import org.gvsig.fmap.dal.exception.ParameterMissingException; |
||
20 | import org.gvsig.fmap.dal.exception.ReadException; |
||
21 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
22 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
23 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
24 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
25 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
26 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
27 | import org.gvsig.fmap.geom.Geometry; |
||
28 | import org.gvsig.fmap.geom.GeometryLocator; |
||
29 | import org.gvsig.tools.dynobject.DynObject; |
||
30 | |||
31 | //FIXME: THIS PROVIDER IS NOT IN USE. IT WAS IMPLEMENTED AS AN EXAMPLE AS HOW IT SHOULD BE
|
||
32 | //IT NEEDED THIS DEPENDENCY WITH POSTGRES PLUGIN, OR FIX HOW TO MANAGE THE RESOURCE
|
||
33 | //<dependency>
|
||
34 | //<groupId>org.gvsig</groupId>
|
||
35 | //<artifactId>org.gvsig.postgresql.provider</artifactId>
|
||
36 | //</dependency>
|
||
37 | |||
38 | /**
|
||
39 | * Provider for Postgis through GDAL
|
||
40 | * @author dmartinezizquierdo
|
||
41 | *
|
||
42 | */
|
||
43 | public class RasterGdalPostgisStoreProvider |
||
44 | extends AbstractRasterGdalStoreProvider {
|
||
45 | |||
46 | protected RasterGdalPostgisStoreProvider(
|
||
47 | RasterGdalStoreParameters params, |
||
48 | DataStoreProviderServices storeServices, DynObject metadata) |
||
49 | throws InitializeException {
|
||
50 | super(params, storeServices, metadata);
|
||
51 | } |
||
52 | |||
53 | /**
|
||
54 | * Constructor
|
||
55 | * @param params
|
||
56 | * @param storeServices
|
||
57 | * @throws InitializeException
|
||
58 | */
|
||
59 | public RasterGdalPostgisStoreProvider(RasterGdalStoreParameters params,
|
||
60 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
61 | super(params, storeServices);
|
||
62 | } |
||
63 | |||
64 | @Override
|
||
65 | protected void init(DataStoreParameters params, |
||
66 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
67 | super.init(params, storeServices);
|
||
68 | String connectionString=getConnectionString();
|
||
69 | if (StringUtils.isEmpty(connectionString)) {
|
||
70 | throw new InitializeException( |
||
71 | new NullPointerException("Raster GDAL connection string is not valid")); |
||
72 | } |
||
73 | |||
74 | // FIXME: HOW TO CREATE A SQL RESOURCE (IT NEEDS NOW A DEPENDENCY WITH THE PLUGIN)
|
||
75 | // resource = this.createResource(PostgreSQLResource.NAME,
|
||
76 | // null);
|
||
77 | // if (resource instanceof PostgreSQLResource){
|
||
78 | // PostgreSQLResource sqlresource = (PostgreSQLResource)resource;
|
||
79 | // PostgreSQLResourceParameters sqlParams = (PostgreSQLResourceParameters) sqlresource.getParameters();
|
||
80 | // try {
|
||
81 | // sqlParams.setHost(getHost());
|
||
82 | // sqlParams.setPort(getPort());
|
||
83 | // sqlParams.setDBName(getDBName());
|
||
84 | // sqlParams.setUser(getUser());
|
||
85 | // sqlParams.setPassword(getPassword());
|
||
86 | // sqlParams.setUseSSL(false);
|
||
87 | // this.projection = getCRS();
|
||
88 | // } catch (ParameterMissingException e) {
|
||
89 | // throw new InitializeException("PostgreSQLResource", e);
|
||
90 | // }
|
||
91 | // }
|
||
92 | |||
93 | resource.addConsumer(this);
|
||
94 | |||
95 | } |
||
96 | |||
97 | |||
98 | protected RasterGdalPostgisStoreProvider(
|
||
99 | NewRasterGdalStoreParameters params, Driver gdalDriver) {
|
||
100 | super(params, gdalDriver);
|
||
101 | } |
||
102 | |||
103 | @Override
|
||
104 | public Object getSourceId() { |
||
105 | return getShortConnectionString();
|
||
106 | } |
||
107 | |||
108 | @Override
|
||
109 | public String getName() { |
||
110 | return getShortConnectionString();
|
||
111 | } |
||
112 | |||
113 | @Override
|
||
114 | public String getFullName() { |
||
115 | return getShortConnectionString();
|
||
116 | } |
||
117 | |||
118 | @Override
|
||
119 | public DataServerExplorer getExplorer()
|
||
120 | throws ReadException, ValidateDataParametersException {
|
||
121 | DataManager manager = DALLocator.getDataManager(); |
||
122 | FilesystemServerExplorerParameters params; |
||
123 | try {
|
||
124 | params = (FilesystemServerExplorerParameters) manager |
||
125 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
126 | //params.setRoot(this.getRasterGdalParameters().getFile().getParent());
|
||
127 | return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
||
128 | } catch (DataException e) {
|
||
129 | throw new ReadException(this.getProviderName(), e); |
||
130 | } catch (ValidateDataParametersException e) {
|
||
131 | throw new ReadException(this.getProviderName(), e); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | protected void openEver() throws OpenException { |
||
136 | try {
|
||
137 | getResource().execute(new ResourceAction() {
|
||
138 | |||
139 | public Object run() throws Exception { |
||
140 | |||
141 | gdalDataSet = gdal.Open( |
||
142 | getConnectionString(), |
||
143 | gdalconstConstants.GA_ReadOnly); |
||
144 | |||
145 | |||
146 | resource.notifyOpen(); |
||
147 | |||
148 | envelope = createEnvelope(getGeoTransform()); |
||
149 | |||
150 | if (envelope == null) { |
||
151 | // If couldn't be possible to create an envelope,
|
||
152 | // this is created at 0,0 coordinates.
|
||
153 | envelope = GeometryLocator.getGeometryManager() |
||
154 | .createEnvelope(0, 0, gdalDataSet.getRasterXSize(), |
||
155 | gdalDataSet.getRasterYSize(), |
||
156 | Geometry.SUBTYPES.GEOM2D); |
||
157 | } |
||
158 | |||
159 | resource.notifyClose(); |
||
160 | |||
161 | resource.setData(gdalDataSet); |
||
162 | |||
163 | return null; |
||
164 | } |
||
165 | |||
166 | }); |
||
167 | } catch (Exception e) { |
||
168 | this.gdalDataSet = null; |
||
169 | try {
|
||
170 | throw new OpenException(resource.getName(), e); |
||
171 | } catch (AccessResourceException e1) {
|
||
172 | throw new OpenException(getProviderName(), e); |
||
173 | } |
||
174 | } finally {
|
||
175 | this.taskStatus.remove();
|
||
176 | } |
||
177 | } |
||
178 | |||
179 | private double[] getGeoTransform(){ |
||
180 | double[] geotransform = null; |
||
181 | double[] defaultGeotransform = |
||
182 | { 0.0, 1.0, 0.0, 0.0, 0.0, -1.0 }; |
||
183 | boolean geoTransformCreated=false; |
||
184 | // Try to create an Envelope through geolocalization
|
||
185 | // methods
|
||
186 | |||
187 | if (!geoTransformCreated) {
|
||
188 | // Try to get a geotransform from the file
|
||
189 | geotransform = gdalDataSet.GetGeoTransform(); |
||
190 | if (Arrays.equals(defaultGeotransform, geotransform) ){ |
||
191 | geoTransformCreated=false;
|
||
192 | } |
||
193 | } |
||
194 | |||
195 | // Try to get a geotransform from GCPs in the file
|
||
196 | if (!geoTransformCreated) {
|
||
197 | Vector vectorGCPs = gdalDataSet.GetGCPs();
|
||
198 | GCP[] gcps = new GCP[gdalDataSet.GetGCPCount()]; |
||
199 | for (int i = 0; i < gdalDataSet |
||
200 | .GetGCPCount(); i++) { |
||
201 | gcps[i] = (GCP) vectorGCPs.get(i); |
||
202 | } |
||
203 | double[] gcpsGeotransform = new double[6]; |
||
204 | int isOK =
|
||
205 | gdal.GCPsToGeoTransform(gcps, gcpsGeotransform); |
||
206 | if (isOK != 0) { |
||
207 | geotransform = gcpsGeotransform; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | return geotransform;
|
||
212 | } |
||
213 | |||
214 | private String getConnectionString(){ |
||
215 | try {
|
||
216 | return "PG:host='"+getHost()+ |
||
217 | "' port='"+getPort()+
|
||
218 | "' dbname='"+getDBName()+
|
||
219 | "' user='"+getUser()+
|
||
220 | "' password='"+getPassword()+
|
||
221 | "' schema='"+getSchema()+
|
||
222 | "' table='"+getTable()+"'"; |
||
223 | } catch (ParameterMissingException e) {
|
||
224 | return null; |
||
225 | } |
||
226 | } |
||
227 | |||
228 | private String getShortConnectionString(){ |
||
229 | try {
|
||
230 | return "PG:host='"+getHost()+ |
||
231 | "' port='"+getPort()+
|
||
232 | "' dbname='"+getDBName()+
|
||
233 | "' user='"+getUser()+
|
||
234 | "' password='***********"+
|
||
235 | "' schema='"+getSchema()+
|
||
236 | "' table='"+getTable()+"'"; |
||
237 | } catch (ParameterMissingException e) {
|
||
238 | return null; |
||
239 | } |
||
240 | } |
||
241 | |||
242 | private String getHost() throws ParameterMissingException{ |
||
243 | if (getParameters().hasDynValue("host")){ |
||
244 | return (String)getParameters().getDynValue("host"); |
||
245 | }else {
|
||
246 | throw new ParameterMissingException("host"); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | private String getDBName() throws ParameterMissingException{ |
||
251 | if (getParameters().hasDynValue("dbname")){ |
||
252 | return (String)getParameters().getDynValue("dbname"); |
||
253 | }else {
|
||
254 | throw new ParameterMissingException("dbname"); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | private IProjection getCRS() throws ParameterMissingException{ |
||
259 | if (getParameters().hasDynValue("crs")){ |
||
260 | return (IProjection)getParameters().getDynValue("crs"); |
||
261 | }else {
|
||
262 | throw new ParameterMissingException("crs"); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | private String getTable() throws ParameterMissingException{ |
||
267 | if (getParameters().hasDynValue("table")){ |
||
268 | return (String)getParameters().getDynValue("table"); |
||
269 | }else {
|
||
270 | throw new ParameterMissingException("table"); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | private String getSchema() throws ParameterMissingException{ |
||
275 | if (getParameters().hasDynValue("schema")){ |
||
276 | return (String)getParameters().getDynValue("schema"); |
||
277 | }else {
|
||
278 | throw new ParameterMissingException("schema"); |
||
279 | } |
||
280 | } |
||
281 | |||
282 | private String getUser() throws ParameterMissingException{ |
||
283 | if (getParameters().hasDynValue("user")){ |
||
284 | return (String)getParameters().getDynValue("user"); |
||
285 | }else {
|
||
286 | throw new ParameterMissingException("user"); |
||
287 | } |
||
288 | } |
||
289 | |||
290 | private String getPassword() throws ParameterMissingException{ |
||
291 | if (getParameters().hasDynValue("password")){ |
||
292 | return (String)getParameters().getDynValue("password"); |
||
293 | }else {
|
||
294 | throw new ParameterMissingException("password"); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | private int getPort() throws ParameterMissingException{ |
||
299 | if (getParameters().hasDynValue("port")){ |
||
300 | return (int)getParameters().getDynValue("port"); |
||
301 | }else {
|
||
302 | throw new ParameterMissingException("port"); |
||
303 | } |
||
304 | } |
||
305 | } |