Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extWFS2 / src / com / iver / cit / gvsig / fmap / drivers / geotools / WFSGeotoolsDriver.java @ 5291

History | View | Annotate | Download (6.63 KB)

1
package com.iver.cit.gvsig.fmap.drivers.geotools;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.io.IOException;
5
import java.net.ConnectException;
6
import java.net.MalformedURLException;
7
import java.net.URL;
8
import java.util.ArrayList;
9
import java.util.HashMap;
10
import java.util.Hashtable;
11
import java.util.Map;
12

    
13
import org.geotools.data.DefaultQuery;
14
import org.geotools.data.FeatureReader;
15
import org.geotools.data.Query;
16
import org.geotools.data.Transaction;
17
import org.geotools.data.wfs.WFSDataStoreFactory;
18
import org.geotools.feature.AttributeType;
19
import org.gvsig.remoteClient.RemoteClient;
20
import org.gvsig.remoteClient.wfs.WFSStatus;
21
import org.xml.sax.SAXException;
22

    
23
import com.iver.cit.gvsig.fmap.DriverException;
24
import com.iver.cit.gvsig.fmap.core.IFeature;
25
import com.iver.cit.gvsig.fmap.drivers.DataStore;
26

    
27

    
28
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
29
 *
30
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
31
 *
32
 * This program is free software; you can redistribute it and/or
33
 * modify it under the terms of the GNU General Public License
34
 * as published by the Free Software Foundation; either version 2
35
 * of the License, or (at your option) any later version.
36
 *
37
 * This program is distributed in the hope that it will be useful,
38
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
40
 * GNU General Public License for more details.
41
 *
42
 * You should have received a copy of the GNU General Public License
43
 * along with this program; if not, write to the Free Software
44
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
45
 *
46
 * For more information, contact:
47
 *
48
 *  Generalitat Valenciana
49
 *   Conselleria d'Infraestructures i Transport
50
 *   Av. Blasco Ib??ez, 50
51
 *   46010 VALENCIA
52
 *   SPAIN
53
 *
54
 *      +34 963862235
55
 *   gvsig@gva.es
56
 *      www.gvsig.gva.es
57
 *
58
 *    or
59
 *
60
 *   IVER T.I. S.A
61
 *   Salamanca 50
62
 *   46005 Valencia
63
 *   Spain
64
 *
65
 *   +34 963163400
66
 *   dac@iver.es
67
 */
68
/* CVS MESSAGES:
69
 *
70
 * $Id: WFSGeotoolsDriver.java 5291 2006-05-19 12:48:21Z jorpiell $
71
 * $Log$
72
 * Revision 1.1  2006-05-19 12:48:21  jorpiell
73
 * Driver que implementa la accesibilidad a geotools
74
 *
75
 *
76
 */
77
/**
78
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
79
 */
80
public class WFSGeotoolsDriver extends RemoteClient{
81
        private DataStore dataStore = null;
82
        private ArrayList features = new ArrayList();
83
        private Rectangle2D fullExtent = null;
84
        private Hashtable hashRelate = new Hashtable();
85
        private WFSStatus currentStatus = null;
86
        private int numReg = -1;
87
        private int numField = -1;
88
        private AttributeType[] attributes;
89
        private WFSFeaturesIterator iterator = null;
90
        
91
        public WFSGeotoolsDriver(String host) throws ConnectException{
92
                this.setHost(host);
93
                if (!connect()){
94
                        throw new ConnectException();        
95
                }                
96
        }
97
        
98
        /**
99
         * The GetFeature operation allows retrieval of features from a 
100
         * web feature service. A GetFeature request is processed by
101
         * a WFS and when the value of the outputFormat attribute is 
102
         * set to text/gml a GML instance document, containing the 
103
         * result set, is returned to the client.
104
         * @param status
105
         * WFS client status. Contains all the information to create
106
         * the query
107
         * @return Object
108
         * GML File
109
         */
110
        public Object getFeature(WFSStatus status){
111
                try { 
112
                        if (status != null){
113
                                this.currentStatus = status;
114
                        }
115
                        Query query = new DefaultQuery(currentStatus.getFeatureName());
116
                        FeatureReader featureReader;
117
                        featureReader = dataStore.getFeatureReader(query,Transaction.AUTO_COMMIT);
118
                        if (numField == -1) {
119
                                numField = featureReader.getFeatureType().getAttributeCount();
120
                                attributes = featureReader.getFeatureType().getAttributeTypes();
121
                        }
122
                        iterator = new WFSFeaturesIterator(featureReader);
123
                        return iterator;
124
                } catch (IOException e) {
125
                        // TODO Auto-generated catch block
126
                        e.printStackTrace();
127
                } 
128
                return null;
129
        }
130
        
131
        /**
132
         * Sets the retrieved features
133
         * @param transformation
134
         * @throws DriverException 
135
         * @throws DriverException 
136
         */
137
        public void setFeatures(WFSFeaturesIterator iterator) {
138
                int index = 0;
139
                while (iterator.hasNext()){
140
                        IFeature feature;
141
                        try {
142
                                feature = iterator.next();
143
                                features.add(feature);
144
                                Rectangle2D rect = feature.getGeometry().getBounds2D();
145

    
146
                                if (fullExtent == null) {
147
                                        fullExtent = new Rectangle2D.Double(rect.getMinX(),
148
                                                        rect.getMinY(), rect.getWidth(), rect.getHeight());
149
                                } else {
150
                                        fullExtent.add(new Rectangle2D.Double(rect.getMinX(),
151
                                                        rect.getMinY(), rect.getWidth(), rect.getHeight()));
152
                                }
153

    
154
                                hashRelate.put(feature.getID(), new Integer(index));
155
                        } catch (DriverException e) {
156
                                // TODO Auto-generated catch block
157
                                e.printStackTrace();
158
                        }                        
159
                        index++;
160
                }
161
                numReg = index;
162
        }
163

    
164
        /*
165
         *  (non-Javadoc)
166
         * @see org.gvsig.remoteClient.RemoteClient#connect()
167
         */
168
        public boolean connect() {
169
                URL url;
170
                try {
171
                        url = new URL(getCapabilitiesRequest());
172
                        Map m = new HashMap();
173
                        m.put(WFSDataStoreFactory.URL.key,url);
174
                        m.put(WFSDataStoreFactory.TIMEOUT.key,new Integer(1000000));
175
                        m.put(WFSDataStoreFactory.PROTOCOL.key,Boolean.TRUE);
176
                        //dataStore = (new WFSDataStoreFactory()).createNewDataStore(m);
177
                        dataStore = new DataStore(url, new Boolean(true), "", "", 1000000, 100);
178
                        return true;
179
                } catch (MalformedURLException e) {
180
                        // TODO Auto-generated catch block
181
                        e.printStackTrace();
182
                } catch (IOException e) {
183
                        // TODO Auto-generated catch block
184
                        e.printStackTrace();
185
                } catch (SAXException e) {
186
                        // TODO Auto-generated catch block
187
                        e.printStackTrace();
188
                } 
189
                return false;         
190
        }
191

    
192
        /**
193
         * @return the host name
194
         */
195
    public String getCapabilitiesRequest() {        
196
            StringBuffer req = new StringBuffer();
197
            req.append(super.getHost()).append("?").append("REQUEST=GetCapabilities&SERVICE=WFS&");
198
            req.append("&EXCEPTIONS=XML");
199
            return req.toString();        
200
    } 
201

    
202
        /*
203
         *  (non-Javadoc)
204
         * @see org.gvsig.remoteClient.RemoteClient#close()
205
         */
206
        public void close() {
207
                // TODO Auto-generated method stub
208
                
209
        }
210

    
211
        /**
212
         * @return Returns the features.
213
         */
214
        public ArrayList getFeatures() {
215
                return features;
216
        }
217

    
218
        /**
219
         * @return Returns the hashRelate.
220
         */
221
        public Hashtable getHashRelate() {
222
                return hashRelate;
223
        }
224

    
225
        /**
226
         * @return Returns the fullExtent.
227
         */
228
        public Rectangle2D getFullExtent() {
229
                return fullExtent;
230
        }
231

    
232
        /**
233
         * @return Returns the numReg.
234
         */
235
        public int getNumReg() {
236
                return numReg;
237
        }
238

    
239
        /**
240
         * @return Returns the attributes.
241
         */
242
        public AttributeType[] getAttributes() {
243
                return attributes;
244
        }
245

    
246
        /**
247
         * @return Returns the numField.
248
         */
249
        public int getNumField() {
250
                return numField;
251
        }
252

    
253
        /**
254
         * @return Returns the iterator.
255
         */
256
        public WFSFeaturesIterator getIterator() {
257
                return (WFSFeaturesIterator)getFeature(null);
258
        }
259
}