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 / RasterGdalWMSStoreProvider.java @ 8697
History | View | Annotate | Download (6.98 KB)
1 | 6335 | dmartinezizquierdo | package org.gvsig.raster.gdal.provider; |
---|---|---|---|
2 | |||
3 | import java.io.File; |
||
4 | import java.net.URL; |
||
5 | import java.util.Arrays; |
||
6 | import java.util.Vector; |
||
7 | |||
8 | import org.cresques.cts.IProjection; |
||
9 | import org.gdal.gdal.Driver; |
||
10 | import org.gdal.gdal.GCP; |
||
11 | import org.gdal.gdal.gdal; |
||
12 | import org.gdal.gdalconst.gdalconstConstants; |
||
13 | import org.gvsig.fmap.dal.DALLocator; |
||
14 | import org.gvsig.fmap.dal.DataManager; |
||
15 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
16 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
17 | import org.gvsig.fmap.dal.exception.DataException; |
||
18 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
19 | import org.gvsig.fmap.dal.exception.OpenException; |
||
20 | import org.gvsig.fmap.dal.exception.ParameterMissingException; |
||
21 | import org.gvsig.fmap.dal.exception.ReadException; |
||
22 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
23 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
24 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
25 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
26 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
27 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
28 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
29 | import org.gvsig.fmap.geom.Geometry; |
||
30 | import org.gvsig.fmap.geom.GeometryLocator; |
||
31 | import org.gvsig.tools.dynobject.DynObject; |
||
32 | |||
33 | //FIXME: THIS PROVIDER IS NOT IN USE. IT WAS IMPLEMENTED AS AN EXAMPLE AS HOW IT SHOULD BE
|
||
34 | /**
|
||
35 | * Provider for WMS through GDAL
|
||
36 | * @author dmartinezizquierdo
|
||
37 | *
|
||
38 | */
|
||
39 | public class RasterGdalWMSStoreProvider |
||
40 | extends AbstractRasterGdalStoreProvider {
|
||
41 | private File xmlFile; |
||
42 | |||
43 | protected RasterGdalWMSStoreProvider(
|
||
44 | RasterGdalStoreParameters params, |
||
45 | DataStoreProviderServices storeServices, DynObject metadata) |
||
46 | throws InitializeException {
|
||
47 | super(params, storeServices, metadata);
|
||
48 | } |
||
49 | |||
50 | /**
|
||
51 | * Constructor
|
||
52 | * @param params
|
||
53 | * @param storeServices
|
||
54 | * @throws InitializeException
|
||
55 | */
|
||
56 | public RasterGdalWMSStoreProvider(RasterGdalStoreParameters params,
|
||
57 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
58 | super(params, storeServices);
|
||
59 | } |
||
60 | |||
61 | protected RasterGdalWMSStoreProvider(
|
||
62 | NewRasterGdalStoreParameters params, Driver gdalDriver) {
|
||
63 | super(params, gdalDriver);
|
||
64 | } |
||
65 | |||
66 | @Override
|
||
67 | protected void init(DataStoreParameters params, |
||
68 | DataStoreProviderServices storeServices) throws InitializeException {
|
||
69 | super.init(params, storeServices);
|
||
70 | xmlFile=createXMLFile(); |
||
71 | if (xmlFile==null) { |
||
72 | throw new InitializeException( |
||
73 | new NullPointerException("Raster GDAL can't create WMS xml file")); |
||
74 | } |
||
75 | |||
76 | //FIXME: THIS PROBABLY SHOULD BE FIXED
|
||
77 | resource = this.createResource(
|
||
78 | FileResource.NAME, |
||
79 | new Object[] { xmlFile.getAbsolutePath() } |
||
80 | ); |
||
81 | resource.addConsumer(this);
|
||
82 | |||
83 | } |
||
84 | |||
85 | |||
86 | @Override
|
||
87 | public Object getSourceId() { |
||
88 | return xmlFile;
|
||
89 | } |
||
90 | |||
91 | @Override
|
||
92 | public String getName() { |
||
93 | return xmlFile.getName();
|
||
94 | } |
||
95 | |||
96 | @Override
|
||
97 | public String getFullName() { |
||
98 | return xmlFile.getAbsolutePath();
|
||
99 | } |
||
100 | |||
101 | @Override
|
||
102 | public DataServerExplorer getExplorer()
|
||
103 | throws ReadException, ValidateDataParametersException {
|
||
104 | DataManager manager = DALLocator.getDataManager(); |
||
105 | FilesystemServerExplorerParameters params; |
||
106 | try {
|
||
107 | params = (FilesystemServerExplorerParameters) manager |
||
108 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
109 | return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
||
110 | } catch (DataException e) {
|
||
111 | throw new ReadException(this.getProviderName(), e); |
||
112 | } catch (ValidateDataParametersException e) {
|
||
113 | throw new ReadException(this.getProviderName(), e); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | protected void openEver() throws OpenException { |
||
118 | try {
|
||
119 | getResource().execute(new ResourceAction() {
|
||
120 | |||
121 | public Object run() throws Exception { |
||
122 | |||
123 | gdalDataSet = gdal.Open( |
||
124 | xmlFile.getAbsolutePath(), |
||
125 | gdalconstConstants.GA_ReadOnly); |
||
126 | gdal.GetLastErrorMsg(); |
||
127 | |||
128 | resource.notifyOpen(); |
||
129 | |||
130 | envelope = createEnvelope(getGeoTransform()); |
||
131 | |||
132 | if (envelope == null) { |
||
133 | // If couldn't be possible to create an envelope,
|
||
134 | // this is created at 0,0 coordinates.
|
||
135 | envelope = GeometryLocator.getGeometryManager() |
||
136 | .createEnvelope(0, 0, gdalDataSet.getRasterXSize(), |
||
137 | gdalDataSet.getRasterYSize(), |
||
138 | Geometry.SUBTYPES.GEOM2D); |
||
139 | } |
||
140 | |||
141 | resource.notifyClose(); |
||
142 | |||
143 | resource.setData(gdalDataSet); |
||
144 | |||
145 | return null; |
||
146 | } |
||
147 | |||
148 | }); |
||
149 | } catch (Exception e) { |
||
150 | this.gdalDataSet = null; |
||
151 | try {
|
||
152 | throw new OpenException(resource.getName(), e); |
||
153 | } catch (AccessResourceException e1) {
|
||
154 | throw new OpenException(getProviderName(), e); |
||
155 | } |
||
156 | } finally {
|
||
157 | this.taskStatus.remove();
|
||
158 | } |
||
159 | } |
||
160 | |||
161 | private double[] getGeoTransform(){ |
||
162 | double[] geotransform = null; |
||
163 | double[] defaultGeotransform = |
||
164 | { 0.0, 1.0, 0.0, 0.0, 0.0, -1.0 }; |
||
165 | boolean geoTransformCreated=false; |
||
166 | // Try to create an Envelope through geolocalization
|
||
167 | // methods
|
||
168 | |||
169 | if (!geoTransformCreated) {
|
||
170 | // Try to get a geotransform from the file
|
||
171 | geotransform = gdalDataSet.GetGeoTransform(); |
||
172 | if (Arrays.equals(defaultGeotransform, geotransform) ){ |
||
173 | geoTransformCreated=false;
|
||
174 | } |
||
175 | } |
||
176 | |||
177 | // Try to get a geotransform from GCPs in the file
|
||
178 | if (!geoTransformCreated) {
|
||
179 | Vector vectorGCPs = gdalDataSet.GetGCPs();
|
||
180 | GCP[] gcps = new GCP[gdalDataSet.GetGCPCount()]; |
||
181 | for (int i = 0; i < gdalDataSet |
||
182 | .GetGCPCount(); i++) { |
||
183 | gcps[i] = (GCP) vectorGCPs.get(i); |
||
184 | } |
||
185 | double[] gcpsGeotransform = new double[6]; |
||
186 | int isOK =
|
||
187 | gdal.GCPsToGeoTransform(gcps, gcpsGeotransform); |
||
188 | if (isOK != 0) { |
||
189 | geotransform = gcpsGeotransform; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | return geotransform;
|
||
194 | } |
||
195 | |||
196 | private File createXMLFile(){ |
||
197 | //FIXME: Archivo adjunto de prueba. copiar a la ruta que se desee para probar
|
||
198 | URL res = getClass().getResource("/wms_gdal_input_data.xml"); |
||
199 | File xmlFile=new File(res.getFile()); |
||
200 | return xmlFile;
|
||
201 | } |
||
202 | |||
203 | private IProjection getCRS() throws ParameterMissingException{ |
||
204 | if (getParameters().hasDynValue("crs")){ |
||
205 | return (IProjection)getParameters().getDynValue("crs"); |
||
206 | }else {
|
||
207 | throw new ParameterMissingException("crs"); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | |||
212 | } |