Statistics
| Revision:

svn-gvsig-desktop / branches / Fmap_GisPlanet / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / wfs / WFSDriver.java @ 1919

History | View | Annotate | Download (11 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.fmap.drivers.wfs;
42

    
43
import java.awt.geom.Rectangle2D;
44
import java.io.IOException;
45
import java.net.MalformedURLException;
46
import java.net.ProtocolException;
47
import java.net.URL;
48
import java.sql.ResultSet;
49
import java.sql.SQLException;
50
import java.sql.Statement;
51
import java.sql.Types;
52
import java.util.ArrayList;
53
import java.util.HashMap;
54
import java.util.Hashtable;
55
import java.util.Map;
56
import java.util.NoSuchElementException;
57

    
58
import org.geotools.data.DataStore;
59
import org.geotools.data.DefaultFIDReader;
60
import org.geotools.data.DefaultQuery;
61
import org.geotools.data.FeatureReader;
62
import org.geotools.data.FeatureSource;
63
import org.geotools.data.Query;
64
import org.geotools.data.Transaction;
65
import org.geotools.data.ows.BoundingBox;
66
import org.geotools.data.wfs.WFSDataStoreFactory;
67
import org.geotools.factory.FactoryConfigurationError;
68
import org.geotools.feature.AttributeType;
69
import org.geotools.feature.DefaultFeature;
70
import org.geotools.feature.Feature;
71
import org.geotools.feature.IllegalAttributeException;
72
import org.geotools.filter.BBoxExpressionImpl;
73
import org.geotools.filter.FidFilter;
74
import org.geotools.filter.FidFilterImpl;
75
import org.geotools.filter.Filter;
76
import org.geotools.filter.FilterFactory;
77
import org.geotools.filter.FilterFactoryImpl;
78
import org.geotools.filter.IllegalFilterException;
79
import org.postgis.PGbox3d;
80

    
81
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
82
import com.hardcode.gdbms.engine.values.StringValue;
83
import com.hardcode.gdbms.engine.values.Value;
84
import com.hardcode.gdbms.engine.values.ValueFactory;
85
import com.iver.cit.gvsig.fmap.DriverException;
86
import com.iver.cit.gvsig.fmap.core.FShape;
87
import com.iver.cit.gvsig.fmap.core.IFeature;
88
import com.iver.cit.gvsig.fmap.core.IGeometry;
89
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
90
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
91
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
92
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
93
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
94
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
95
import com.iver.cit.gvsig.fmap.drivers.jdbc.mysql.MySqlFeatureIterator;
96
import com.vividsolutions.jts.geom.Envelope;
97

    
98
public class WFSDriver implements IWFSDriver,ObjectDriver {
99
        private Rectangle2D fullExtent=null;
100
        private Rectangle2D extent=null;
101
        private DataStore ds;
102
        //private FeatureReader fr;
103
        private String urlS="http://localhost:8080/geoserver/wfs";
104
        private String versionS="version=1.0.0";
105
        private String requestS="request=getcapabilities";
106
        private String serviceS="service=wfs";
107
        private String userS="admin";
108
        private String pwdS="geoserver";
109
        private String[] layersS;
110
        private boolean protocolS=true;
111
        private int numFeaturesS=100;
112
        private int timeoutS=10000;
113
        private Hashtable hashRelate;
114
        ///private Hashtable indexFID;
115
        protected int idFID_FieldName;
116
        private int numReg=-1;
117
        private int numField=-1;
118
        private AttributeType[] attributes;
119
        //private FeatureReader fr;
120
        ///private ArrayList typeNames=new ArrayList();
121
        private ArrayList features=new ArrayList();
122
        public Rectangle2D getFullExtent() {
123
                return fullExtent;
124
        }
125

    
126
        public int getShapeType() {
127
                return FShape.MULTI;
128
        }
129
        private Filter createFidFilter(String[] fids) {
130
                 FidFilter filter = FilterFactory.createFilterFactory().createFidFilter();
131
                for (int i = 0; i < fids.length; i++) {
132
                    filter.addFid(fids[i]);
133
                }
134
        return filter;
135
    }
136

    
137
        public IGeometry getShape(int index) {
138
                return ((IFeature)features.get(index)).getGeometry();
139
        }
140

    
141
        public URL getHost() {
142
                try {
143
                        return new URL(urlS+"?"+versionS+"&"+requestS+"&"+serviceS);
144
                } catch (MalformedURLException e) {
145
                        e.printStackTrace();
146
                }
147
                return null;
148
        }
149

    
150
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException {
151
                WFSFeatureIterator geomIterator = null;
152
                /*if (r!=null && extent.contains(r)){
153
                        //extent=(Rectangle2D)r.clone();
154
                        return new WFSFeatureIterator(features);
155
                }
156
                */
157
               // ArrayList shapes=new ArrayList();   
158
                /*        BBoxExpressionImpl RectangleFilter=null;
159
                        try {
160
                                RectangleFilter = (BBoxExpressionImpl)FilterFactoryImpl.createFilterFactory().createBBoxExpression(new Envelope(fullExtent.getX(),fullExtent.getY(),fullExtent.getMaxX(),fullExtent.getMaxY()));
161
                        } catch (IllegalFilterException e2) {
162
                                e2.printStackTrace();
163
                        } catch (FactoryConfigurationError e2) {
164
                                e2.printStackTrace();
165
                        }
166
                        */
167
                           Query query = new DefaultQuery(layersS[0]);
168
                           FeatureReader fr=null;
169
                                try {
170
                                        fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
171
                                        if (numField ==-1){
172
                                        numField=fr.getFeatureType().getAttributeCount();
173
                                        attributes=fr.getFeatureType().getAttributeTypes();
174
                                        }
175
                                } catch (IOException e) {
176
                                        e.printStackTrace();
177
                                }
178
                                
179
                                geomIterator=new WFSFeatureIterator(fr);
180
                           
181
                           
182
                        /*   ResultSet rs = st.executeQuery(sql);
183
                    geomIterator = new MySqlFeatureIterator(rs);
184
                } catch (SQLException e) {
185
                    e.printStackTrace();
186
                    throw new com.iver.cit.gvsig.fmap.DriverException(e);
187
                }
188
                  */  
189
                return geomIterator;
190
        }
191

    
192
        public void close() {
193
                
194
        }
195

    
196
        public void open() throws DriverException {
197
        }
198

    
199
        public int getRowIndexByFID(IFeature FID) {
200
                int resul;
201
            Integer rowIndex = (Integer) hashRelate.get(FID.getID());
202
                resul = rowIndex.intValue();
203
                return resul;
204
        }
205

    
206
        public int getShapeCount() throws IOException {
207
                return numReg;
208
        }
209

    
210
        public DriverAttributes getDriverAttributes() {
211
                return null;
212
        }
213

    
214
        public String getName() {
215
                return "WFS Driver";
216
        }
217
        /**
218
     * Recorre el recordset creando una tabla Hash que usaremos para 
219
     * relacionar el n?mero de un registro con su identificador ?nico.
220
     * Debe ser llamado en el setData justo despu?s de crear el recorset
221
     * principal
222
     */
223
    protected void doRelateID_FID()
224
    {
225
                if (ds==null){
226
                        URL url=null;
227
                        try {
228
                                url = new URL(urlS+"?"+versionS+"&"+requestS+"&"+serviceS);
229
                        } catch (MalformedURLException e) {
230
                                e.printStackTrace();
231
                        }
232
                        //int timeout=10000;
233
                        //boolean protocol=true;
234
                        
235
         Map m = new HashMap();
236
         m.put(WFSDataStoreFactory.URL.key,url);
237
         m.put(WFSDataStoreFactory.TIMEOUT.key,new Integer(timeoutS));
238
         m.put(WFSDataStoreFactory.PROTOCOL.key,new Boolean(protocolS));
239
                 m.put(WFSDataStoreFactory.PASSWORD.key,pwdS);
240
                 m.put(WFSDataStoreFactory.USERNAME.key,userS);
241
                 m.put(WFSDataStoreFactory.BUFFER_SIZE.key,new Integer(numFeaturesS));
242
        try {
243
                        ds = (new WFSDataStoreFactory()).createNewDataStore(m);
244
                } catch (IOException e) {
245
                        e.printStackTrace();
246
                }
247
                /*
248
                String[] names=null;
249
                try {
250
                        names = ds.getTypeNames();
251
                } catch (IOException e1) {
252
                        e1.printStackTrace();
253
                }
254
                System.out.println("Capas :");
255
       
256
                for (int i=0;i<names.length;i++){
257
                        System.out.println("Capa "+i+" : "+names[i]);
258
                        //Se selecciona la capa que se quiere abrir
259
                        //if (true)//si esta seleccionada en la lista o arbol que se muestre en el wizard
260
                        //typeNames.add(names[i]);
261
                }
262
                */
263
        }
264
                
265
        hashRelate = new Hashtable();
266
                ///indexFID = new Hashtable();
267
                Query query = new DefaultQuery(layersS[0]);
268
                //FeatureReader fr=null;
269
                
270
                        //fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
271
                        
272
                        int index = 0;
273
            IFeatureIterator iter=null;
274
                        try {
275
                                iter = getFeatureIterator(null,null);
276
                        } catch (DriverException e) {
277
                                e.printStackTrace();
278
                        }
279
                        
280
                        try {
281
                                while(iter.hasNext()){
282
                                        IFeature feature=iter.next();
283
                                        features.add(feature);
284
                                        Rectangle2D rect=feature.getGeometry().getBounds2D();
285
                                        if (fullExtent==null){
286
                                                fullExtent=new Rectangle2D.Double(rect.getMinX(),rect.getMinY(),rect.getWidth(),rect.getHeight());
287
                                        }else{
288
                                                fullExtent.add(new Rectangle2D.Double(rect.getMinX(),rect.getMinY(),rect.getWidth(),rect.getHeight()));
289
                                        }
290
                                        hashRelate.put(feature.getID(), new Integer(index));
291
                                        ///indexFID.put(new Integer(index),feature.getID());
292
                                        index++;
293
                                }
294
                        } catch (SQLException e) {
295
                                e.printStackTrace();
296
                        }
297
                        numReg = index;
298
                        extent=fullExtent;
299
                                 
300
                        
301
    }
302

    
303
        public void setData(String url,String user,String pwd,String[] layers,boolean protocol,int numFeatures,int timeout,int id_FID_field) {
304
                idFID_FieldName=id_FID_field;
305
                urlS=url;
306
                userS=user;
307
                pwdS=pwd;
308
                layersS=layers;
309
                protocolS=protocol;
310
                numFeaturesS=numFeatures;
311
                timeoutS=timeout;
312
                doRelateID_FID();
313
        }
314
        
315
        public Value getFieldValue(long arg0, int arg1) throws com.hardcode.gdbms.engine.data.driver.DriverException {
316
                
317
                /*try {
318
                        ds.getTypeNames();
319
                } catch (IOException e1) {
320
                        e1.printStackTrace();
321
                }*/
322
                /*System.out.println("Filter");
323
                Filter fidFilter=createFilter(new String[]{(String)indexFID.get(new Integer((int)arg0))});
324
                System.out.println("Query");
325
                Query query = new DefaultQuery((String)typeNames.get(0),fidFilter);
326
                FeatureReader fr=null;
327
                try {
328
                        System.out.println("getFeatureReader");
329
                        fr = ds.getFeatureReader(query,Transaction.AUTO_COMMIT);
330
                } catch (IOException e) {
331
                        e.printStackTrace();
332
                }
333
                        try {
334
                                return getValue(fr,arg1);
335
                        } catch (NoSuchElementException e) {
336
                                e.printStackTrace();
337
                        } catch (IOException e) {
338
                                e.printStackTrace();
339
                        } catch (IllegalAttributeException e) {
340
                                e.printStackTrace();
341
                        }
342
                        */
343
                return ((IFeature)features.get((int)arg0)).getAttribute(arg1+1);
344
        }
345

    
346
        public int getFieldCount() throws com.hardcode.gdbms.engine.data.driver.DriverException {
347
                return numField-1;
348
        }
349

    
350
        public String getFieldName(int arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException {
351
                return attributes[arg0].getName();                
352
        }
353

    
354
        public long getRowCount() throws com.hardcode.gdbms.engine.data.driver.DriverException {
355
                return numReg;
356
        }
357

    
358
        public int getFieldType(int arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException {
359
                attributes[arg0].getType();
360
                return FIELD_TYPE_STRING;//fr.getFeatureType().getAttributeType(arg0).getType();
361
        }
362
        
363
}