Revision 1919

View differences:

branches/Fmap_GisPlanet/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/wfs/WFSException.java
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

  
44
public class WFSException extends Exception {
45

  
46
	/**
47
	 * 
48
	 */
49
	public WFSException() {
50
		super();
51

  
52
	}
53
	/**
54
	 * @param message
55
	 */
56
	public WFSException(String message) {
57
		super(message);
58

  
59
	}
60
	/**
61
	 * @param message
62
	 * @param cause
63
	 */
64
	public WFSException(String message, Throwable cause) {
65
		super(message, cause);
66

  
67
	}
68
	/**
69
	 * @param cause
70
	 */
71
	public WFSException(Throwable cause) {
72
		super(cause);
73

  
74
	}
75
}
branches/Fmap_GisPlanet/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/wfs/WFSDriver.java
40 40
 */
41 41
package com.iver.cit.gvsig.fmap.drivers.wfs;
42 42

  
43
import java.awt.geom.Rectangle2D;
43 44
import java.io.IOException;
45
import java.net.MalformedURLException;
44 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;
45 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;
46 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;
47 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;
48 97

  
49
public interface WFSDriver extends  VectorialDriver {
50
    void describeFeatureType() throws IOException, WFSException, ProtocolException;
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
	}
51 125

  
52
    IGeometry[] getFeature() throws IOException, WFSException, ProtocolException;
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
    }
53 136

  
54
    void lockFeature() throws IOException, WFSException, ProtocolException;
137
	public IGeometry getShape(int index) {
138
		return ((IFeature)features.get(index)).getGeometry();
139
	}
55 140

  
56
    void transaction() throws IOException, WFSException, ProtocolException;
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
	
57 363
}
branches/Fmap_GisPlanet/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/wfs/WFSFeatureIterator.java
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.io.IOException;
44
import java.sql.SQLException;
45
import java.util.ArrayList;
46
import java.util.Date;
47
import java.util.NoSuchElementException;
48

  
49
import org.geotools.data.FeatureReader;
50
import org.geotools.feature.DefaultFeature;
51
import org.geotools.feature.Feature;
52
import org.geotools.feature.IllegalAttributeException;
53

  
54
import com.hardcode.gdbms.engine.values.Value;
55
import com.hardcode.gdbms.engine.values.ValueFactory;
56
import com.iver.cit.gvsig.fmap.core.IFeature;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
59
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
60
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
61

  
62
public class WFSFeatureIterator implements IFeatureIterator{
63
	//private IFeature[] features=null;
64
	private FeatureReader featureReader;
65
	//private ArrayList features;
66
	//private int index=-1;
67
	//private int pos=0;
68
	public WFSFeatureIterator(FeatureReader fr){
69
		featureReader=fr;
70
	}
71
	/*public WFSFeatureIterator(ArrayList l){
72
		features=l;
73
	}*/
74
	public boolean hasNext(){
75
		/*if (features !=null){
76
			if (index<features.size()-1)
77
				return true;
78
			else{
79
				index=-1;
80
				features=null;
81
				return false;
82
			}
83
		}*/
84
		try {
85
			if (featureReader.hasNext()){
86
				return true;
87
			}else{
88
				featureReader.close();
89
				
90
				return false;
91
			}
92
		} catch (IOException e) {
93
			e.printStackTrace();
94
		}
95
		return false;
96
	}
97

  
98
	public IFeature next() throws SQLException {
99
	/*	if (features !=null){
100
			return (IFeature)features.get(++index);
101
		}*/
102
		Feature feature=null;
103
		try {
104
			feature = featureReader.next();
105
		} catch (NoSuchElementException e) {
106
			e.printStackTrace();
107
		} catch (IOException e) {
108
			e.printStackTrace();
109
		} catch (IllegalAttributeException e) {
110
			e.printStackTrace();
111
		}
112
		Object[] attr=null;
113
		attr=feature.getAttributes(attr);
114
		Value[] values=getValues(attr);
115
		
116
		IGeometry g=ShapeFactory.createGeometry(FConverter.jts_to_java2d(feature.getDefaultGeometry()));
117
		return new com.iver.cit.gvsig.fmap.core.DefaultFeature(g,values,feature.getID());
118
	}
119
	private Value[] getValues(Object[] attr) {
120
		Value[] values=new Value[attr.length];
121
		for (int i=1;i<attr.length;i++){
122
			if (attr[i]!=null){
123
				if (attr[i] instanceof Double){
124
					values[i]=ValueFactory.createValue(((Double)attr[i]).doubleValue());
125
				}else if (attr[i] instanceof String){
126
					values[i]=ValueFactory.createValue(String.valueOf(attr[i]));
127
				}else if (attr[i] instanceof Long){
128
					values[i]=ValueFactory.createValue(((Long)attr[i]).longValue());
129
				}else if (attr[i] instanceof Integer){
130
					values[i]=ValueFactory.createValue(((Integer)attr[i]).intValue());
131
				}else if (attr[i] instanceof Float){
132
					values[i]=ValueFactory.createValue(((Float)attr[i]).floatValue());
133
				}else if (attr[i] instanceof Short){
134
					values[i]=ValueFactory.createValue(((Short)attr[i]).shortValue());
135
				}else if (attr[i] instanceof Boolean){
136
					values[i]=ValueFactory.createValue(((Boolean)attr[i]).booleanValue());
137
				}else if (attr[i] instanceof Date){
138
					values[i]=ValueFactory.createValue(((Date)attr[i]));
139
				}
140
			//	System.out.println("class = "+attr[i].getClass());
141
					
142
			}else{
143
				values[i]=ValueFactory.createValue("");
144
			}
145
		}
146
		return values;
147
	}
148
	
149
}
0 150

  

Also available in: Unified diff