root / org.gvsig.wfs.app / trunk / org.gvsig.wfs.app / org.gvsig.wfs.app.mainplugin / src / main / java / org / gvsig / fmap / dal / store / wfs / WFSStoreProvider.java @ 101
History | View | Annotate | Download (25.9 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 {Iver T.I.} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.wfs; |
29 |
|
30 |
import java.io.File; |
31 |
import java.io.IOException; |
32 |
import java.util.ArrayList; |
33 |
import java.util.Iterator; |
34 |
import java.util.List; |
35 |
|
36 |
import org.cresques.cts.ICoordTrans; |
37 |
import org.cresques.cts.IProjection; |
38 |
import org.gvsig.fmap.crs.CRSFactory; |
39 |
import org.gvsig.fmap.dal.DALLocator; |
40 |
import org.gvsig.fmap.dal.DataManager; |
41 |
import org.gvsig.fmap.dal.DataServerExplorer; |
42 |
import org.gvsig.fmap.dal.DataStoreParameters; |
43 |
import org.gvsig.fmap.dal.DataTypes; |
44 |
import org.gvsig.fmap.dal.exception.ConnectionServerException; |
45 |
import org.gvsig.fmap.dal.exception.DataException; |
46 |
import org.gvsig.fmap.dal.exception.InitializeException; |
47 |
import org.gvsig.fmap.dal.exception.OpenException; |
48 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
49 |
import org.gvsig.fmap.dal.exception.ReadException; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
51 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
52 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
53 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
54 |
import org.gvsig.fmap.dal.feature.FeatureType; |
55 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
56 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
57 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
58 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
59 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
60 |
import org.gvsig.fmap.dal.serverexplorer.wfs.WFSServerExplorer; |
61 |
import org.gvsig.fmap.dal.serverexplorer.wfs.WFSServerExplorerParameters; |
62 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
63 |
import org.gvsig.fmap.dal.store.gpe.GPEStoreProvider; |
64 |
import org.gvsig.fmap.geom.Geometry; |
65 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
66 |
import org.gvsig.fmap.geom.GeometryLocator; |
67 |
import org.gvsig.fmap.geom.GeometryManager; |
68 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
69 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
70 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
71 |
import org.gvsig.fmap.geom.primitive.Envelope; |
72 |
import org.gvsig.gpe.lib.api.exceptions.WriterHandlerCreationException; |
73 |
import org.gvsig.metadata.MetadataLocator; |
74 |
import org.gvsig.metadata.MetadataManager; |
75 |
import org.gvsig.metadata.exceptions.MetadataException; |
76 |
import org.gvsig.remoteclient.ogc.OGCClientOperation; |
77 |
import org.gvsig.remoteclient.utils.BoundaryBox; |
78 |
import org.gvsig.remoteclient.wfs.WFSClient; |
79 |
import org.gvsig.remoteclient.wfs.WFSFeature; |
80 |
import org.gvsig.remoteclient.wfs.WFSFeatureField; |
81 |
import org.gvsig.remoteclient.wfs.WFSStatus; |
82 |
import org.gvsig.remoteclient.wfs.exceptions.WFSException; |
83 |
import org.gvsig.remoteclient.wfs.filters.filterencoding.FilterEncoding; |
84 |
import org.gvsig.remoteclient.wfs.request.WFSGetFeatureRequestInformation; |
85 |
import org.gvsig.remoteclient.wfs.request.WFSTransactionRequestInformation; |
86 |
import org.gvsig.tools.ToolsLocator; |
87 |
import org.gvsig.tools.dynobject.DynStruct; |
88 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
89 |
import org.gvsig.tools.evaluator.EvaluatorFieldValueMatch; |
90 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
91 |
import org.gvsig.xmlschema.lib.api.XMLSchemaLocator; |
92 |
import org.gvsig.xmlschema.lib.api.XMLSchemaManager; |
93 |
import org.gvsig.xmlschema.lib.api.som.IXSGeometryTypeDefinition; |
94 |
import org.gvsig.xmlschema.lib.api.som.IXSTypeDefinition; |
95 |
import org.slf4j.Logger; |
96 |
import org.slf4j.LoggerFactory; |
97 |
|
98 |
|
99 |
/**
|
100 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
101 |
*/
|
102 |
public class WFSStoreProvider extends AbstractFeatureStoreProvider { |
103 |
private static final Logger logger = LoggerFactory.getLogger(WFSStoreProvider.class); |
104 |
public static String NAME = "WFSStore"; |
105 |
public static String DESCRIPTION = "WFS store to load WFS resources"; |
106 |
|
107 |
private static final MetadataManager metadataManager = MetadataLocator.getMetadataManager(); |
108 |
private static final String METADATA_DEFINITION_NAME = "WFSStore"; |
109 |
private static final String METADATA_DEFINITION_DESCRIPTION = "WFSStore metadata definition"; |
110 |
|
111 |
// private IProjection projection;
|
112 |
private long featureCount = -1; |
113 |
private boolean isKnownEnvelope = false; |
114 |
|
115 |
/**
|
116 |
* If the user has selected the filter by area option
|
117 |
*/
|
118 |
private boolean hasFilterByArea = false; |
119 |
|
120 |
/**
|
121 |
* GML provider used by delegation to read the GML files
|
122 |
*/
|
123 |
private GPEStoreProvider storeProviderDelegate = null; |
124 |
|
125 |
private static final DataManager dataManager = DALLocator.getDataManager(); |
126 |
private static final XMLSchemaManager xmlSchemaManager = XMLSchemaLocator.getXMLSchemaManager(); |
127 |
|
128 |
boolean isFilterByAreaSupported = true; |
129 |
|
130 |
//WFS Parameters
|
131 |
private WFSClient wfsClient;
|
132 |
private WFSStatus wfsStatus;
|
133 |
|
134 |
public WFSStoreProvider(DataStoreParameters params,
|
135 |
DataStoreProviderServices storeServices) |
136 |
throws InitializeException {
|
137 |
super(params, storeServices,
|
138 |
ToolsLocator.getDynObjectManager().createDynObject(metadataManager.getDefinition(METADATA_DEFINITION_NAME))); |
139 |
initParams(); |
140 |
try {
|
141 |
initFeatureType(); |
142 |
} catch (WFSException e) {
|
143 |
throw new InitializeException(e); |
144 |
} catch (ReadException e) {
|
145 |
throw new InitializeException(e); |
146 |
} |
147 |
} |
148 |
|
149 |
private void initParams() throws InitializeException{ |
150 |
try {
|
151 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
152 |
try {
|
153 |
if (wfsParameters.getVersion() == null){ |
154 |
wfsClient = new WFSClient(wfsParameters.getUrl(), wfsParameters.getIgnoreCache());
|
155 |
wfsParameters.setVersion(wfsClient.getVersion()); |
156 |
}else{
|
157 |
wfsClient = new WFSClient(wfsParameters.getUrl(), wfsParameters.getVersion(), wfsParameters.getIgnoreCache());
|
158 |
} |
159 |
} catch (IOException e) { |
160 |
throw new InitializeException(e); |
161 |
} |
162 |
wfsStatus = new WFSStatus( wfsParameters.getFeatureType(),
|
163 |
wfsParameters.getFeatureNamespace()); |
164 |
wfsStatus.setNamespacePrefix(wfsParameters.getFeaturePrefix()); |
165 |
wfsStatus.setFields(wfsParameters.getFields()); |
166 |
|
167 |
//Select the filter by area
|
168 |
wfsStatus.setFilterByArea(wfsParameters.getFilterByAreaGeometry(), wfsParameters.getFilterByAreaAttribute(), |
169 |
wfsParameters.getFilterByAreaCrs(), wfsParameters.getFilterByAreaOperation()); |
170 |
this.hasFilterByArea = (wfsStatus.getFilterByArea() != null); |
171 |
|
172 |
//Select the filter by attribute
|
173 |
wfsStatus.setFilterByAttribute(wfsParameters.getFilterEncodingByAttribute()); |
174 |
|
175 |
String _srs = wfsParameters.getFilterByAreaCrs();
|
176 |
wfsStatus.setSrs(_srs); |
177 |
|
178 |
wfsStatus.setTimeout(wfsParameters.getTimeOut()); |
179 |
wfsStatus.setMaxFeatures(wfsParameters.getMaxFeatures()); |
180 |
wfsStatus.setUserName(wfsParameters.getUser()); |
181 |
wfsStatus.setPassword(wfsParameters.getPassword()); |
182 |
|
183 |
//Setting the envelope for the layer
|
184 |
wfsClient.getCapabilities(wfsStatus, false, null); |
185 |
|
186 |
Envelope envelope = calculateFullEnvelope(); |
187 |
if (envelope != null){ |
188 |
this.setDynValue("Envelope", envelope); |
189 |
isKnownEnvelope = true;
|
190 |
} |
191 |
} catch (WFSException e) {
|
192 |
throw new InitializeException("Impossible to retrieve the file", e); |
193 |
} |
194 |
} |
195 |
|
196 |
private WFSFeature getSelectedWFSFeature() throws ReadException, WFSException{ |
197 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
198 |
|
199 |
return ((WFSServerExplorer)getExplorer()).getFeatureInfo(wfsParameters.getFeatureNamespace(),
|
200 |
wfsParameters.getFeatureType()); |
201 |
} |
202 |
|
203 |
private void initFeatureType() throws WFSException, ReadException{ |
204 |
WFSFeature feature = getSelectedWFSFeature(); |
205 |
|
206 |
if (feature == null){ |
207 |
//TODO read from GML
|
208 |
throw new WFSException("It is not possible to parse the schema"); |
209 |
} |
210 |
|
211 |
IProjection ipro = null;
|
212 |
try {
|
213 |
ipro = CRSFactory.getCRS(wfsStatus.getSrs()); |
214 |
this.setDynValue("CRS", ipro); |
215 |
} catch (Exception ex) { |
216 |
logger.info("Error: did not set CRS in store provider.", ex);
|
217 |
} |
218 |
|
219 |
EditableFeatureType featureType = getStoreServices().createFeatureType(); |
220 |
|
221 |
for (int i=0 ; i<feature.getFieldSize() ; i++){ |
222 |
WFSFeatureField featureField = feature.getFieldAt(i); |
223 |
int dataType = DataTypes.STRING;
|
224 |
String typeName = featureField.getType();
|
225 |
EditableFeatureAttributeDescriptor attributeDescriptor = null;
|
226 |
|
227 |
IXSTypeDefinition typeDefinition = null;
|
228 |
|
229 |
if (typeName != null){ |
230 |
typeDefinition = xmlSchemaManager.getTypeDefinition(typeName); |
231 |
} |
232 |
|
233 |
if (typeDefinition != null){ |
234 |
if (typeDefinition.getDataType() != null){ |
235 |
if (typeDefinition.getDataType().getType() == DataTypes.GEOMETRY){
|
236 |
dataType = typeDefinition.getDataType().getType(); |
237 |
} |
238 |
} |
239 |
attributeDescriptor = featureType.add(featureField.getName(), dataType); |
240 |
if (typeDefinition instanceof IXSGeometryTypeDefinition){ |
241 |
|
242 |
if (ipro != null) { |
243 |
attributeDescriptor.setSRS(ipro); |
244 |
} else {
|
245 |
if (feature.getSrs().size() > 0){ |
246 |
attributeDescriptor.setSRS(CRSFactory.getCRS((String)feature.getSrs().get(0))); |
247 |
} else {
|
248 |
logger.info("Unable to set CRS in feature type.");
|
249 |
} |
250 |
} |
251 |
|
252 |
|
253 |
attributeDescriptor.setGeometryType(((IXSGeometryTypeDefinition)typeDefinition).getGeometryType()); |
254 |
attributeDescriptor.setGeometrySubType(SUBTYPES.GEOM3D); |
255 |
featureType.setDefaultGeometryAttributeName(featureField.getName()); |
256 |
} |
257 |
}else{
|
258 |
attributeDescriptor = featureType.add(typeName, dataType); |
259 |
} |
260 |
// length, relevant for all (strings) except geometry
|
261 |
attributeDescriptor.setSize(128);
|
262 |
} |
263 |
featureType.setHasOID(true);
|
264 |
|
265 |
FeatureType defaultType = featureType.getNotEditableCopy(); |
266 |
List types = new ArrayList(1); |
267 |
types.add(defaultType); |
268 |
this.getStoreServices().setFeatureTypes(types, defaultType);
|
269 |
} |
270 |
|
271 |
private Envelope calculateFullEnvelope() {
|
272 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
273 |
|
274 |
try{
|
275 |
WFSFeature feature = getSelectedWFSFeature(); |
276 |
if (feature != null){ |
277 |
String _srs = wfsStatus.getSrs();
|
278 |
if (_srs != null) { |
279 |
|
280 |
BoundaryBox boundaryBox = feature.getBbox(_srs); |
281 |
//The projection is found
|
282 |
if (boundaryBox != null){ |
283 |
return geometryManager.createEnvelope(boundaryBox.getXmin(),
|
284 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
285 |
} |
286 |
//Check if there is a latlon envelope
|
287 |
boundaryBox = feature.getLatLonBbox(); |
288 |
if (boundaryBox != null){ |
289 |
Envelope envelope = geometryManager.createEnvelope(boundaryBox.getXmin(), |
290 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
291 |
if (!"EPSG:4326".equals(_srs)){ |
292 |
IProjection sourceProjection = CRSFactory.getCRS("EPSG:4326");
|
293 |
IProjection destinationProjection = CRSFactory.getCRS(_srs); |
294 |
if (destinationProjection != null){ |
295 |
ICoordTrans coordTrans = sourceProjection.getCT(destinationProjection); |
296 |
return envelope.convert(coordTrans);
|
297 |
} |
298 |
} |
299 |
} |
300 |
}else{
|
301 |
//If there is not a SRS is thrown a request for the first 500 features
|
302 |
int odlMaxFeatures = wfsStatus.getMaxFeatures();
|
303 |
wfsStatus.setMaxFeatures(500);
|
304 |
createSet(null, null); |
305 |
return storeProviderDelegate.getEnvelope();
|
306 |
} |
307 |
} |
308 |
}catch(CreateEnvelopeException e){
|
309 |
logger.error("Impossible to create an envelope", e);
|
310 |
} catch (ReadException e) {
|
311 |
logger.error("Impossible to create an envelope", e);
|
312 |
} catch (WFSException e) {
|
313 |
logger.error("Impossible to create an envelope", e);
|
314 |
} catch (DataException e) {
|
315 |
logger.error("Impossible to create an envelope", e);
|
316 |
} |
317 |
return null; |
318 |
} |
319 |
|
320 |
private WFSStoreParameters getWFSParameters() {
|
321 |
return (WFSStoreParameters) getParameters();
|
322 |
} |
323 |
|
324 |
/* (non-Javadoc)
|
325 |
* @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#open()
|
326 |
*/
|
327 |
public void open() throws OpenException { |
328 |
try {
|
329 |
List featureTypes = this.getFeatureStore().getFeatureTypes(); |
330 |
for (int i=0 ; i<featureTypes.size() ; i++){ |
331 |
FeatureType featureType = (FeatureType)featureTypes.get(i); |
332 |
} |
333 |
} catch (DataException e) {
|
334 |
throw new OpenException("Reading the geometry type", e); |
335 |
} |
336 |
} |
337 |
|
338 |
protected static void registerMetadataDefinition() throws MetadataException { |
339 |
if (metadataManager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
340 |
DynStruct metadataDefinition = metadataManager.addDefinition( |
341 |
METADATA_DEFINITION_NAME, METADATA_DEFINITION_DESCRIPTION); |
342 |
|
343 |
metadataDefinition.addDynFieldObject("Envelope").setClassOfValue(Envelope.class).setMandatory(false); |
344 |
metadataDefinition.addDynFieldObject("CRS").setClassOfValue(IProjection.class).setMandatory(true); |
345 |
|
346 |
metadataDefinition.extend(metadataManager |
347 |
.getDefinition(FeatureStore.METADATA_DEFINITION_NAME)); |
348 |
} |
349 |
} |
350 |
|
351 |
/* (non-Javadoc)
|
352 |
* @see org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#getExplorer()
|
353 |
*/
|
354 |
public DataServerExplorer getExplorer() throws ReadException { |
355 |
DataManager manager = DALLocator.getDataManager(); |
356 |
WFSServerExplorerParameters params; |
357 |
try {
|
358 |
params = (WFSServerExplorerParameters) manager |
359 |
.createServerExplorerParameters(WFSServerExplorer.NAME); |
360 |
params.setUrl(wfsClient.getHost()); |
361 |
return manager.openServerExplorer(WFSServerExplorer.NAME,params);
|
362 |
} catch (Exception e) { |
363 |
throw new ReadException(this.getName(), e); |
364 |
} |
365 |
} |
366 |
|
367 |
public String getProviderName() { |
368 |
return NAME;
|
369 |
} |
370 |
|
371 |
public FeatureSetProvider createSet(FeatureQuery query)
|
372 |
throws DataException {
|
373 |
return createSet(query, null); |
374 |
} |
375 |
|
376 |
public FeatureSetProvider createSet(FeatureQuery query,
|
377 |
FeatureType featureType) throws DataException {
|
378 |
if ((featureCount == -1) || |
379 |
(storeProviderDelegate == null) ||
|
380 |
(featureCount >= wfsStatus.getMaxFeatures())){ |
381 |
|
382 |
File file = null; |
383 |
|
384 |
if (query == null){ |
385 |
file = executeQuery(); |
386 |
}else{
|
387 |
//If it is possible to execute a spatial query...
|
388 |
if ((query.getFilter() != null) && (!hasFilterByArea)){ |
389 |
file = executeSpatialQuery(query); |
390 |
}else{
|
391 |
file = executeQuery(); |
392 |
} |
393 |
|
394 |
} |
395 |
storeProviderDelegate = createProvider(file); |
396 |
} |
397 |
|
398 |
return storeProviderDelegate.createSet(query, featureType);
|
399 |
} |
400 |
|
401 |
/**
|
402 |
* It creates the provider that is used to retrieve features from a file.
|
403 |
* @param file
|
404 |
* the file that contains the downloaded file
|
405 |
* @return
|
406 |
* @throws InitializeException
|
407 |
* @throws ProviderNotRegisteredException
|
408 |
*/
|
409 |
private GPEStoreProvider createProvider(File file) throws InitializeException, ProviderNotRegisteredException{ |
410 |
return new GPEStoreProvider(createStoreParameters(file), getStoreServices()); |
411 |
} |
412 |
|
413 |
private DataStoreParameters createStoreParameters(File file) throws InitializeException, ProviderNotRegisteredException{ |
414 |
DataStoreParameters parameters = dataManager.createStoreParameters(GPEStoreProvider.NAME); |
415 |
|
416 |
parameters.setDynValue("File", file.getAbsolutePath());
|
417 |
parameters.setDynValue("Envelope", this.getDynValue("Envelope")); |
418 |
parameters.setDynValue("useAxisOrderYX", this.getWFSParameters().getUseAxisOrderYX()); |
419 |
return parameters;
|
420 |
} |
421 |
|
422 |
/**
|
423 |
* It executes a wfs Query without spatial filter
|
424 |
* @throws DataException
|
425 |
*/
|
426 |
private File executeQuery() throws DataException{ |
427 |
try{
|
428 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
429 |
if (!hasFilterByArea){
|
430 |
wfsStatus.removeFilterByArea(); |
431 |
} |
432 |
return wfsClient.getFeature(wfsStatus, getWFSParameters().getIgnoreCache(), null); |
433 |
} catch (WFSException e) {
|
434 |
throw new InitializeException("Impossible to retrieve the file", e); |
435 |
} |
436 |
} |
437 |
|
438 |
/**
|
439 |
* Executes a new query sending the FilterEncoding request to the server
|
440 |
* @param query
|
441 |
* The Query to send
|
442 |
* @throws InitializeException
|
443 |
* If there is a problem with the request or the parsing process
|
444 |
*/
|
445 |
private File executeSpatialQuery(FeatureQuery query) throws DataException{ |
446 |
EvaluatorFieldsInfo fieldsInfo = query.getFilter().getFieldsInfo(); |
447 |
String[] fields = fieldsInfo.getFieldNames(); |
448 |
for (int i=0 ; i<fields.length ; i++){ |
449 |
EvaluatorFieldValue[] evaluatorFieldValues = fieldsInfo.getFieldValues(fields[i]);
|
450 |
for (int j=0 ; j<evaluatorFieldValues.length ; j++){ |
451 |
if (EvaluatorFieldValue.MATCH == evaluatorFieldValues[j].getType()){
|
452 |
EvaluatorFieldValueMatch evaluatorFieldValueMatch = (EvaluatorFieldValueMatch)evaluatorFieldValues[j]; |
453 |
Object area = evaluatorFieldValueMatch.getValue();
|
454 |
if (area instanceof Envelope){ |
455 |
return executeSpatialQueryFromEnvelope(evaluatorFieldValueMatch.getFieldName(),
|
456 |
(Envelope)area); |
457 |
}else if (area instanceof Geometry){ |
458 |
return executeSpatialQueryFromGeometry(evaluatorFieldValueMatch.getFieldName(),
|
459 |
(Geometry)area); |
460 |
} |
461 |
} |
462 |
} |
463 |
} |
464 |
return null; |
465 |
} |
466 |
|
467 |
private File executeSpatialQueryFromGeometry(String the_geom, Geometry geometry) throws DataException{ |
468 |
wfsStatus.setFilterByArea(geometry, the_geom, wfsStatus.getSrs(), |
469 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
470 |
return executeSpatialQuery();
|
471 |
} |
472 |
|
473 |
private File executeSpatialQueryFromEnvelope(String the_geom, Envelope envelope) throws DataException{ |
474 |
wfsStatus.setFilterByArea(envelope, the_geom, wfsStatus.getSrs(), |
475 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
476 |
return executeSpatialQuery();
|
477 |
} |
478 |
|
479 |
private File executeSpatialQuery() throws DataException{ |
480 |
try {
|
481 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
482 |
//If it is not possible to calculate the
|
483 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
484 |
return wfsClient.getFeature(wfsStatus, getWFSParameters().getIgnoreCache(), null); |
485 |
} catch (WFSException e) {
|
486 |
throw new InitializeException("Impossible to retrieve the file", e); |
487 |
} |
488 |
} |
489 |
|
490 |
public Object getSourceId() { |
491 |
StringBuffer sourceID = new StringBuffer(getWFSParameters().getUrl()); |
492 |
sourceID.append("_" + getWFSParameters().getFeatureType());
|
493 |
int hash = sourceID.hashCode();
|
494 |
if (sourceID.length() > 60){ |
495 |
return sourceID.substring(0, 60) + "_" + hash; |
496 |
}else{
|
497 |
return sourceID.toString() + "_" + hash; |
498 |
} |
499 |
} |
500 |
|
501 |
public Object createNewOID() { |
502 |
try {
|
503 |
return String.valueOf(getFeatureCount()+1); |
504 |
} catch (DataException e) {
|
505 |
return String.valueOf(Math.random()); |
506 |
} |
507 |
} |
508 |
|
509 |
public long getFeatureCount() throws DataException { |
510 |
if (featureCount == -1){ |
511 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_HITS); |
512 |
try {
|
513 |
//The filter encoding is better supported using POST
|
514 |
if (hasFilterByArea){
|
515 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
516 |
}else{
|
517 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_GET); |
518 |
} |
519 |
File file = wfsClient.getFeature(wfsStatus, getWFSParameters().getIgnoreCache(), null); |
520 |
featureCount = ((WFSGetFeatureRequestInformation)wfsClient.getLastWfsRequestInformation()).getNumberOfFeatures(); |
521 |
//If the service doesn't support RESULTTYPE_HITS, parse the file and get the FeatureCount
|
522 |
if (featureCount == -1){ |
523 |
storeProviderDelegate = createProvider(file); |
524 |
featureCount = storeProviderDelegate.getFeatureCount(); |
525 |
} |
526 |
} catch (WFSException e) {
|
527 |
throw new ConnectionServerException("Error calculating the feature count", e); |
528 |
} |
529 |
} |
530 |
return featureCount;
|
531 |
} |
532 |
|
533 |
public int getOIDType() { |
534 |
return DataTypes.STRING;
|
535 |
} |
536 |
|
537 |
public ResourceProvider getResource() {
|
538 |
return storeProviderDelegate.getResource();
|
539 |
} |
540 |
|
541 |
protected FeatureProvider internalGetFeatureProviderByReference(
|
542 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
543 |
throws DataException {
|
544 |
return storeProviderDelegate.internalGetFeatureProviderByReference(reference, featureType);
|
545 |
} |
546 |
|
547 |
public String getFullName() { |
548 |
return getWFSParameters().getUrl() + "_" + getWFSParameters().getFeatureType(); |
549 |
} |
550 |
|
551 |
public String getName() { |
552 |
return getWFSParameters().getFeatureType();
|
553 |
} |
554 |
|
555 |
public boolean isKnownEnvelope(){ |
556 |
return isKnownEnvelope;
|
557 |
} |
558 |
|
559 |
public boolean hasRetrievedFeaturesLimit(){ |
560 |
return true; |
561 |
} |
562 |
|
563 |
public int getRetrievedFeaturesLimit(){ |
564 |
//WFS 1.0.0 can not know where is the limit
|
565 |
if (getWFSParameters().getVersion().equals("1.0.0")){ |
566 |
return 0; |
567 |
}else{
|
568 |
return wfsClient.getServiceInformation().getMaxFeatures();
|
569 |
} |
570 |
} |
571 |
|
572 |
public void performChanges(Iterator deleteds, Iterator inserteds, |
573 |
Iterator updateds, Iterator featureTypesChanged) |
574 |
throws DataException {
|
575 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
576 |
WFSTTransactionBuilder transactionBuilder = new WFSTTransactionBuilder(wfsStatus, wfsClient);
|
577 |
|
578 |
//Create the Query
|
579 |
try {
|
580 |
//Add new features
|
581 |
while (inserteds.hasNext()){
|
582 |
transactionBuilder.addInsertFeature((FeatureProvider)inserteds.next()); |
583 |
} |
584 |
//Add deleted features
|
585 |
while (deleteds.hasNext()){
|
586 |
transactionBuilder.addDeleteFeature( |
587 |
internalGetFeatureProviderByReference((FeatureReferenceProviderServices)deleteds.next(), null).getOID().toString());;
|
588 |
} |
589 |
//Add updated features
|
590 |
while (updateds.hasNext()){
|
591 |
transactionBuilder.addUpdateFeature((FeatureProvider)updateds.next()); |
592 |
} |
593 |
} catch (WriterHandlerCreationException e) {
|
594 |
throw new WFSTTRansactionException(e); |
595 |
} catch (GeometryOperationNotSupportedException e) {
|
596 |
throw new WFSTTRansactionException(e); |
597 |
} catch (GeometryOperationException e) {
|
598 |
throw new WFSTTRansactionException(e); |
599 |
} |
600 |
|
601 |
//Send the query to the server
|
602 |
try {
|
603 |
wfsClient.transaction(wfsStatus, true, null); |
604 |
WFSTransactionRequestInformation transactionRequestInformation = (WFSTransactionRequestInformation)wfsClient.getLastWfsRequestInformation(); |
605 |
if (transactionRequestInformation.getStatus() == WFSTransactionRequestInformation.STATUS_FAILED){
|
606 |
throw new WFSTTRansactionException(transactionRequestInformation.getMessage()); |
607 |
} |
608 |
} catch (WFSException e) {
|
609 |
throw new WFSTTRansactionException(e); |
610 |
} |
611 |
} |
612 |
|
613 |
public boolean allowWrite() { |
614 |
return wfsClient.getServiceInformation().isOperationSupported("Transaction"); |
615 |
} |
616 |
|
617 |
public Envelope getEnvelope() throws DataException { |
618 |
return (Envelope) this.getDynValue("Envelope"); |
619 |
} |
620 |
|
621 |
public FeatureProvider createFeatureProvider(FeatureType type)
|
622 |
throws DataException {
|
623 |
return super.createFeatureProvider(type); |
624 |
} |
625 |
|
626 |
|
627 |
} |
628 |
|