Revision 34026 branches/v2_0_0_prep/extensions/extWFS2/src/org/gvsig/fmap/dal/store/wfs/WFSStoreProvider.java
WFSStoreProvider.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package org.gvsig.fmap.dal.store.wfs; |
29 | 29 |
|
30 |
import java.io.File; |
|
30 | 31 |
import java.io.IOException; |
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Iterator; |
|
31 | 34 |
import java.util.List; |
32 |
import java.util.Vector; |
|
33 | 35 |
|
34 | 36 |
import org.cresques.cts.ICoordTrans; |
35 | 37 |
import org.cresques.cts.IProjection; |
38 |
import org.slf4j.Logger; |
|
39 |
import org.slf4j.LoggerFactory; |
|
40 |
|
|
36 | 41 |
import org.gvsig.fmap.crs.CRSFactory; |
37 | 42 |
import org.gvsig.fmap.dal.DALLocator; |
38 | 43 |
import org.gvsig.fmap.dal.DataManager; |
39 | 44 |
import org.gvsig.fmap.dal.DataServerExplorer; |
40 | 45 |
import org.gvsig.fmap.dal.DataStoreParameters; |
46 |
import org.gvsig.fmap.dal.DataTypes; |
|
47 |
import org.gvsig.fmap.dal.exception.ConnectionServerException; |
|
41 | 48 |
import org.gvsig.fmap.dal.exception.DataException; |
42 | 49 |
import org.gvsig.fmap.dal.exception.InitializeException; |
43 | 50 |
import org.gvsig.fmap.dal.exception.OpenException; |
51 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
|
44 | 52 |
import org.gvsig.fmap.dal.exception.ReadException; |
53 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
54 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
45 | 55 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
46 | 56 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 | 57 |
import org.gvsig.fmap.dal.feature.FeatureType; |
58 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
|
59 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
60 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
48 | 61 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
49 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
|
|
62 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
|
|
50 | 63 |
import org.gvsig.fmap.dal.serverexplorer.wfs.WFSServerExplorer; |
51 | 64 |
import org.gvsig.fmap.dal.serverexplorer.wfs.WFSServerExplorerParameters; |
52 | 65 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
53 | 66 |
import org.gvsig.fmap.dal.store.gpe.GPEStoreProvider; |
54 | 67 |
import org.gvsig.fmap.geom.Geometry; |
68 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
55 | 69 |
import org.gvsig.fmap.geom.GeometryLocator; |
56 | 70 |
import org.gvsig.fmap.geom.GeometryManager; |
57 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
58 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
|
59 | 71 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
60 | 72 |
import org.gvsig.fmap.geom.primitive.Envelope; |
61 | 73 |
import org.gvsig.metadata.MetadataLocator; |
... | ... | |
65 | 77 |
import org.gvsig.remoteclient.utils.BoundaryBox; |
66 | 78 |
import org.gvsig.remoteclient.wfs.WFSClient; |
67 | 79 |
import org.gvsig.remoteclient.wfs.WFSFeature; |
80 |
import org.gvsig.remoteclient.wfs.WFSFeatureField; |
|
68 | 81 |
import org.gvsig.remoteclient.wfs.WFSStatus; |
69 | 82 |
import org.gvsig.remoteclient.wfs.exceptions.WFSException; |
70 | 83 |
import org.gvsig.remoteclient.wfs.filters.filterencoding.FilterEncoding; |
71 |
import org.gvsig.remoteclient.wfs.schema.XMLElement;
|
|
84 |
import org.gvsig.remoteclient.wfs.request.WFSGetFeatureRequestInformation;
|
|
72 | 85 |
import org.gvsig.tools.ToolsLocator; |
73 |
import org.gvsig.tools.dynobject.DynClass;
|
|
86 |
import org.gvsig.tools.dataTypes.DataTypesManager;
|
|
74 | 87 |
import org.gvsig.tools.dynobject.DynStruct; |
75 | 88 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
76 | 89 |
import org.gvsig.tools.evaluator.EvaluatorFieldValueMatch; |
77 | 90 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
78 |
import org.slf4j.Logger; |
|
79 |
import org.slf4j.LoggerFactory; |
|
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; |
|
80 | 95 |
|
81 | 96 |
|
82 | 97 |
/** |
83 | 98 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a> |
84 | 99 |
*/ |
85 |
public class WFSStoreProvider extends GPEStoreProvider implements |
|
86 |
ResourceConsumer { |
|
87 |
private static final Logger logger = LoggerFactory.getLogger(WFSStoreProvider.class); |
|
88 |
public static String NAME = "WFSStore"; |
|
89 |
public static String DESCRIPTION = "WFS store to load WFS resources"; |
|
90 |
private static final String METADATA_DEFINITION_NAME = "WFSStore"; |
|
91 |
private static final String METADATA_DEFINITION_DESCRIPTION = "WFSStore metadata definition"; |
|
92 |
private IProjection projection; |
|
100 |
public class WFSStoreProvider extends AbstractFeatureStoreProvider { |
|
101 |
private static final Logger logger = LoggerFactory.getLogger(WFSStoreProvider.class); |
|
102 |
public static String NAME = "WFSStore"; |
|
103 |
public static String DESCRIPTION = "WFS store to load WFS resources"; |
|
104 |
|
|
105 |
private static final MetadataManager metadataManager = MetadataLocator.getMetadataManager(); |
|
106 |
private static final String METADATA_DEFINITION_NAME = "WFSStore"; |
|
107 |
private static final String METADATA_DEFINITION_DESCRIPTION = "WFSStore metadata definition"; |
|
108 |
|
|
109 |
private IProjection projection; |
|
110 |
private int featureCount = -1; |
|
111 |
private boolean isKnownEnvelope = false; |
|
93 | 112 |
|
94 |
boolean isFilterByAreaSupported = true; |
|
113 |
/** |
|
114 |
* GML provider used by delegation to read the GML files |
|
115 |
*/ |
|
116 |
private GPEStoreProvider storeProviderDelegate = null; |
|
95 | 117 |
|
96 |
//WFS Parameters
|
|
97 |
private WFSClient wfsClient;
|
|
98 |
private WFSStatus wfsStatus;
|
|
118 |
private static final DataManager dataManager = DALLocator.getDataManager();
|
|
119 |
private static final DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
|
|
120 |
private static final XMLSchemaManager xmlSchemaManager = XMLSchemaLocator.getXMLSchemaManager();
|
|
99 | 121 |
|
100 |
public WFSStoreProvider(DataStoreParameters params, |
|
101 |
DataStoreProviderServices storeServices) |
|
102 |
throws InitializeException { |
|
103 |
super(params, storeServices);//, ToolsLocator.getDynObjectManager() |
|
104 |
//.createDynObject(DYNCLASS)); |
|
105 |
} |
|
122 |
boolean isFilterByAreaSupported = true; |
|
106 | 123 |
|
107 |
protected void retrieveFile() throws InitializeException{ |
|
108 |
try { |
|
109 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
|
110 |
try { |
|
111 |
if (wfsParameters.getVersion() == null){ |
|
112 |
wfsClient = new WFSClient(wfsParameters.getUrl()); |
|
113 |
wfsParameters.setVersion(wfsClient.getVersion()); |
|
114 |
}else{ |
|
115 |
wfsClient = new WFSClient(wfsParameters.getUrl(), wfsParameters.getVersion()); |
|
116 |
} |
|
117 |
} catch (IOException e) { |
|
118 |
throw new InitializeException(e); |
|
119 |
} |
|
120 |
wfsStatus = new WFSStatus( wfsParameters.getFeatureType(), |
|
121 |
wfsParameters.getFeaturePrefix()); |
|
122 |
wfsStatus.setNamespace(wfsParameters.getFeatureNamespace()); |
|
123 |
wfsStatus.setFields(wfsParameters.getFields()); |
|
124 |
//wfsStatus.setFilterQuery(wfsParameters.getFilterEncoding()); |
|
125 |
wfsStatus.setTimeout(wfsParameters.getTimeOut()); |
|
126 |
wfsStatus.setBuffer(wfsParameters.getMaxFeatures()); |
|
127 |
wfsStatus.setUserName(wfsParameters.getUser()); |
|
128 |
wfsStatus.setPassword(wfsParameters.getPassword()); |
|
124 |
//WFS Parameters |
|
125 |
private WFSClient wfsClient; |
|
126 |
private WFSStatus wfsStatus; |
|
129 | 127 |
|
130 |
//Setting the envelope for the layer |
|
131 |
wfsClient.getCapabilities(wfsStatus, false, null); |
|
128 |
public WFSStoreProvider(DataStoreParameters params, |
|
129 |
DataStoreProviderServices storeServices) |
|
130 |
throws InitializeException { |
|
131 |
super(params, storeServices, |
|
132 |
ToolsLocator.getDynObjectManager().createDynObject(metadataManager.getDefinition(METADATA_DEFINITION_NAME))); |
|
133 |
initParams(); |
|
134 |
try { |
|
135 |
initFeatureType(); |
|
136 |
} catch (WFSException e) { |
|
137 |
throw new InitializeException(e); |
|
138 |
} catch (ReadException e) { |
|
139 |
throw new InitializeException(e); |
|
140 |
} |
|
141 |
} |
|
132 | 142 |
|
133 |
Envelope envelope = calculateFullEnvelope(); |
|
134 |
if (envelope != null){ |
|
135 |
this.setDynValue("Envelope", envelope); |
|
136 |
} |
|
143 |
private void initParams() throws InitializeException{ |
|
144 |
try { |
|
145 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
|
146 |
try { |
|
147 |
if (wfsParameters.getVersion() == null){ |
|
148 |
wfsClient = new WFSClient(wfsParameters.getUrl()); |
|
149 |
wfsParameters.setVersion(wfsClient.getVersion()); |
|
150 |
}else{ |
|
151 |
wfsClient = new WFSClient(wfsParameters.getUrl(), wfsParameters.getVersion()); |
|
152 |
} |
|
153 |
} catch (IOException e) { |
|
154 |
throw new InitializeException(e); |
|
155 |
} |
|
156 |
wfsStatus = new WFSStatus( wfsParameters.getFeatureType(), |
|
157 |
wfsParameters.getFeatureNamespace()); |
|
158 |
wfsStatus.setNamespacePrefix(wfsParameters.getFeaturePrefix()); |
|
159 |
wfsStatus.setFields(wfsParameters.getFields()); |
|
160 |
//wfsStatus.setFilterQuery(wfsParameters.getFilterEncoding()); |
|
161 |
wfsStatus.setTimeout(wfsParameters.getTimeOut()); |
|
162 |
wfsStatus.setMaxFeatures(wfsParameters.getMaxFeatures()); |
|
163 |
wfsStatus.setUserName(wfsParameters.getUser()); |
|
164 |
wfsStatus.setPassword(wfsParameters.getPassword()); |
|
137 | 165 |
|
138 |
m_Fich = wfsClient.getFeature(wfsStatus, true, null); |
|
139 |
} catch (WFSException e) { |
|
140 |
throw new InitializeException("Impossible to retrieve the file", e); |
|
141 |
} |
|
142 |
} |
|
166 |
//Setting the envelope for the layer |
|
167 |
wfsClient.getCapabilities(wfsStatus, false, null); |
|
143 | 168 |
|
144 |
private Envelope calculateFullEnvelope() { |
|
145 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
146 |
|
|
147 |
Object obj = wfsClient.getFeatures().get(wfsStatus.getFeatureName()); |
|
148 |
try{ |
|
149 |
if (obj != null){ |
|
150 |
WFSFeature feature = (WFSFeature)obj; |
|
151 |
if (feature.getSrs().size() > 0){ |
|
152 |
String srs = (String)feature.getSrs().get(0); |
|
153 |
BoundaryBox boundaryBox = feature.getBbox("srs"); |
|
154 |
if (boundaryBox == null) |
|
155 |
{ |
|
156 |
boundaryBox = feature.getBbox(getEPSGCode("srs")); |
|
157 |
} |
|
158 |
//The projection is found |
|
159 |
if (boundaryBox != null){ |
|
160 |
return geometryManager.createEnvelope(boundaryBox.getXmin(), |
|
161 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
|
162 |
} |
|
163 |
//Check if there is a latlon envelope |
|
164 |
boundaryBox = feature.getLatLonBbox(); |
|
165 |
if (boundaryBox != null){ |
|
166 |
Envelope envelope = geometryManager.createEnvelope(boundaryBox.getXmin(), |
|
167 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
|
168 |
IProjection projection = CRSFactory.getCRS(getEPSGCode(srs)); |
|
169 |
if (projection != null){ |
|
170 |
ICoordTrans coordTrans = projection.getCT(CRSFactory.getCRS(getEPSGCode("EPSG:4326"))); |
|
171 |
return envelope.convert(coordTrans); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
176 |
}catch(CreateEnvelopeException e){ |
|
177 |
logger.error("Impossible to create an envelope", e); |
|
178 |
} |
|
179 |
return null; |
|
180 |
} |
|
169 |
Envelope envelope = calculateFullEnvelope(); |
|
170 |
if (envelope != null){ |
|
171 |
this.setDynValue("Envelope", envelope); |
|
172 |
isKnownEnvelope = true; |
|
173 |
} |
|
174 |
} catch (WFSException e) { |
|
175 |
throw new InitializeException("Impossible to retrieve the file", e); |
|
176 |
} |
|
177 |
} |
|
181 | 178 |
|
182 |
private WFSStoreParameters getWFSParameters() { |
|
183 |
return (WFSStoreParameters) getParameters(); |
|
184 |
} |
|
179 |
private WFSFeature getSelectedWFSFeature() throws ReadException, WFSException{ |
|
180 |
WFSStoreParameters wfsParameters = getWFSParameters(); |
|
185 | 181 |
|
186 |
/* (non-Javadoc) |
|
187 |
* @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#open() |
|
188 |
*/ |
|
189 |
public void open() throws OpenException { |
|
190 |
super.open(); |
|
191 |
try { |
|
192 |
List featureTypes = this.getFeatureStore().getFeatureTypes(); |
|
193 |
for (int i=0 ; i<featureTypes.size() ; i++){ |
|
194 |
FeatureType featureType = (FeatureType)featureTypes.get(i); |
|
182 |
return ((WFSServerExplorer)getExplorer()).getFeatureInfo(wfsParameters.getFeatureNamespace(), |
|
183 |
wfsParameters.getFeatureType()); |
|
184 |
} |
|
195 | 185 |
|
196 |
} |
|
197 |
} catch (DataException e) { |
|
198 |
throw new OpenException("Reading the geometry type", e); |
|
199 |
} |
|
200 |
} |
|
186 |
private void initFeatureType() throws WFSException, ReadException{ |
|
187 |
WFSFeature feature = getSelectedWFSFeature(); |
|
201 | 188 |
|
202 |
protected static void registerMetadataDefinition() throws MetadataException { |
|
203 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
|
204 |
if (manager.getDefinition(METADATA_DEFINITION_NAME) == null) { |
|
205 |
DynStruct metadataDefinition = manager.addDefinition( |
|
206 |
METADATA_DEFINITION_NAME, METADATA_DEFINITION_DESCRIPTION); |
|
207 |
metadataDefinition.extend(manager |
|
208 |
.getDefinition(FeatureStore.METADATA_DEFINITION_NAME)); |
|
209 |
} |
|
189 |
if (feature == null){ |
|
190 |
//TODO read from GML |
|
191 |
throw new WFSException("It is not possible to parse the schema"); |
|
192 |
} |
|
210 | 193 |
|
211 |
} |
|
194 |
projection = CRSFactory.getCRS(getEPSGCode(feature.getDefaultSRS())); |
|
195 |
this.setDynValue("CRS", projection); |
|
196 |
|
|
197 |
EditableFeatureType featureType = getStoreServices().createFeatureType(); |
|
198 |
|
|
199 |
for (int i=0 ; i<feature.getFieldSize() ; i++){ |
|
200 |
WFSFeatureField featureField = feature.getFieldAt(i); |
|
201 |
int dataType = DataTypes.OBJECT; |
|
202 |
String typeName = featureField.getType(); |
|
203 |
EditableFeatureAttributeDescriptor attributeDescriptor = null; |
|
212 | 204 |
|
213 |
/* (non-Javadoc) |
|
214 |
* @see org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#getExplorer() |
|
215 |
*/ |
|
216 |
public DataServerExplorer getExplorer() throws ReadException { |
|
217 |
DataManager manager = DALLocator.getDataManager(); |
|
218 |
WFSServerExplorerParameters params; |
|
219 |
try { |
|
220 |
params = (WFSServerExplorerParameters) manager |
|
221 |
.createServerExplorerParameters(WFSServerExplorer.NAME); |
|
222 |
params.setUrl(wfsClient.getHost()); |
|
223 |
return manager.openServerExplorer(WFSServerExplorer.NAME,params); |
|
224 |
} catch (Exception e) { |
|
225 |
throw new ReadException(this.getName(), e); |
|
226 |
} |
|
227 |
} |
|
205 |
IXSTypeDefinition typeDefinition = xmlSchemaManager.getTypeDefinition(typeName); |
|
206 |
if (typeDefinition != null){ |
|
207 |
if (typeDefinition.getDataType() != null){ |
|
208 |
dataType = typeDefinition.getDataType().getType(); |
|
209 |
} |
|
210 |
attributeDescriptor = featureType.add(featureField.getName(), dataType); |
|
211 |
if (typeDefinition instanceof IXSGeometryTypeDefinition){ |
|
212 |
if (feature.getSrs().size() > 0){ |
|
213 |
attributeDescriptor.setSRS(CRSFactory.getCRS(getEPSGCode((String)feature.getSrs().get(0)))); |
|
214 |
} |
|
215 |
attributeDescriptor.setGeometryType(((IXSGeometryTypeDefinition)typeDefinition).getGeometryType()); |
|
216 |
attributeDescriptor.setGeometrySubType(SUBTYPES.GEOM2D); |
|
217 |
featureType.setDefaultGeometryAttributeName(featureField.getName()); |
|
218 |
} |
|
219 |
}else{ |
|
220 |
attributeDescriptor = featureType.add(typeName, dataType); |
|
221 |
} |
|
222 |
} |
|
223 |
featureType.add("gid", DataTypes.LONG); |
|
224 |
featureType.setHasOID(true); |
|
225 |
|
|
226 |
FeatureType defaultType = featureType.getNotEditableCopy(); |
|
227 |
List types = new ArrayList(1); |
|
228 |
types.add(defaultType); |
|
229 |
this.getStoreServices().setFeatureTypes(types, defaultType); |
|
230 |
} |
|
228 | 231 |
|
232 |
private Envelope calculateFullEnvelope() { |
|
233 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
|
229 | 234 |
|
230 |
/* (non-Javadoc) |
|
231 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getName() |
|
232 |
*/ |
|
233 |
public String getName() { |
|
234 |
return NAME; |
|
235 |
} |
|
235 |
try{ |
|
236 |
WFSFeature feature = getSelectedWFSFeature(); |
|
237 |
if (feature != null){ |
|
238 |
if (feature.getSrs().size() > 0){ |
|
239 |
String srs = feature.getDefaultSRS(); |
|
240 |
BoundaryBox boundaryBox = feature.getBbox(srs); |
|
241 |
if (boundaryBox == null) |
|
242 |
{ |
|
243 |
boundaryBox = feature.getBbox(getEPSGCode(srs)); |
|
244 |
} |
|
245 |
//The projection is found |
|
246 |
if (boundaryBox != null){ |
|
247 |
return geometryManager.createEnvelope(boundaryBox.getXmin(), |
|
248 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
|
249 |
} |
|
250 |
//Check if there is a latlon envelope |
|
251 |
boundaryBox = feature.getLatLonBbox(); |
|
252 |
if (boundaryBox != null){ |
|
253 |
Envelope envelope = geometryManager.createEnvelope(boundaryBox.getXmin(), |
|
254 |
boundaryBox.getYmin(), boundaryBox.getXmax(), boundaryBox.getYmax(), SUBTYPES.GEOM2D); |
|
255 |
IProjection sourceProjection = CRSFactory.getCRS(getEPSGCode("EPSG:4326")); |
|
256 |
IProjection destinationProjection = CRSFactory.getCRS(getEPSGCode(srs)); |
|
257 |
if (destinationProjection != null){ |
|
258 |
ICoordTrans coordTrans = sourceProjection.getCT(destinationProjection); |
|
259 |
return envelope.convert(coordTrans); |
|
260 |
} |
|
261 |
} |
|
262 |
}else{ |
|
263 |
//If there is not a SRS is thrown a request for the first 500 features |
|
264 |
int odlMaxFeatures = wfsStatus.getMaxFeatures(); |
|
265 |
wfsStatus.setMaxFeatures(500); |
|
266 |
createSet(null, null); |
|
267 |
return storeProviderDelegate.getEnvelope(); |
|
268 |
} |
|
269 |
} |
|
270 |
}catch(CreateEnvelopeException e){ |
|
271 |
logger.error("Impossible to create an envelope", e); |
|
272 |
} catch (ReadException e) { |
|
273 |
logger.error("Impossible to create an envelope", e); |
|
274 |
} catch (WFSException e) { |
|
275 |
logger.error("Impossible to create an envelope", e); |
|
276 |
} catch (DataException e) { |
|
277 |
logger.error("Impossible to create an envelope", e); |
|
278 |
} |
|
279 |
return null; |
|
280 |
} |
|
236 | 281 |
|
237 |
/* (non-Javadoc) |
|
238 |
* @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#createSet(org.gvsig.fmap.dal.feature.FeatureQuery) |
|
239 |
*/ |
|
240 |
@Override |
|
241 |
public FeatureSetProvider createSet(FeatureQuery query) |
|
242 |
throws DataException { |
|
243 |
return createSet(query, null); |
|
244 |
} |
|
282 |
private WFSStoreParameters getWFSParameters() { |
|
283 |
return (WFSStoreParameters) getParameters(); |
|
284 |
} |
|
245 | 285 |
|
246 |
/* (non-Javadoc) |
|
247 |
* @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#createSet(org.gvsig.fmap.dal.feature.FeatureQuery, org.gvsig.fmap.dal.feature.FeatureType) |
|
248 |
*/ |
|
249 |
@Override |
|
250 |
public FeatureSetProvider createSet(FeatureQuery query, |
|
251 |
FeatureType featureType) throws DataException { |
|
252 |
//If it is possible to execute a spatial query... |
|
253 |
if (query.getFilter() != null){ |
|
254 |
executeSpatialQuery(query); |
|
255 |
}else{ |
|
256 |
executeQuery(); |
|
257 |
} |
|
258 |
isOpen = false; |
|
259 |
open(); |
|
260 |
return super.createSet(query, featureType); |
|
261 |
} |
|
286 |
/* (non-Javadoc) |
|
287 |
* @see org.gvsig.fmap.dal.store.gpe.GPEStoreProvider#open() |
|
288 |
*/ |
|
289 |
public void open() throws OpenException { |
|
290 |
try { |
|
291 |
List featureTypes = this.getFeatureStore().getFeatureTypes(); |
|
292 |
for (int i=0 ; i<featureTypes.size() ; i++){ |
|
293 |
FeatureType featureType = (FeatureType)featureTypes.get(i); |
|
294 |
} |
|
295 |
} catch (DataException e) { |
|
296 |
throw new OpenException("Reading the geometry type", e); |
|
297 |
} |
|
298 |
} |
|
262 | 299 |
|
263 |
/**
|
|
264 |
* It executes a wfs Query without spatial filter
|
|
265 |
* @throws DataException
|
|
266 |
*/
|
|
267 |
private void executeQuery() throws DataException{ |
|
268 |
try{
|
|
269 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS);
|
|
270 |
m_Fich = wfsClient.getFeature(wfsStatus, true, null); |
|
271 |
} catch (WFSException e) {
|
|
272 |
throw new InitializeException("Impossible to retrieve the file", e);
|
|
273 |
}
|
|
274 |
}
|
|
300 |
protected static void registerMetadataDefinition() throws MetadataException {
|
|
301 |
if (metadataManager.getDefinition(METADATA_DEFINITION_NAME) == null) {
|
|
302 |
DynStruct metadataDefinition = metadataManager.addDefinition(
|
|
303 |
METADATA_DEFINITION_NAME, METADATA_DEFINITION_DESCRIPTION);
|
|
304 |
|
|
305 |
metadataDefinition.addDynFieldObject("Envelope").setClassOfValue(Envelope.class).setMandatory(false);
|
|
306 |
metadataDefinition.addDynFieldObject("CRS").setClassOfValue(IProjection.class).setMandatory(true);
|
|
307 |
|
|
308 |
metadataDefinition.extend(metadataManager
|
|
309 |
.getDefinition(FeatureStore.METADATA_DEFINITION_NAME));
|
|
310 |
}
|
|
311 |
}
|
|
275 | 312 |
|
276 |
/** |
|
277 |
* Executes a new query sending the FilterEncoding request to the server |
|
278 |
* @param query |
|
279 |
* The Query to send |
|
280 |
* @throws InitializeException |
|
281 |
* If there is a problem with the request or the parsing process |
|
282 |
*/ |
|
283 |
private boolean executeSpatialQuery(FeatureQuery query) throws DataException{ |
|
284 |
EvaluatorFieldsInfo fieldsInfo = query.getFilter().getFieldsInfo(); |
|
285 |
String[] fields = fieldsInfo.getFieldNames(); |
|
286 |
for (int i=0 ; i<fields.length ; i++){ |
|
287 |
EvaluatorFieldValue[] evaluatorFieldValues = fieldsInfo.getFieldValues(fields[i]); |
|
288 |
for (int j=0 ; j<evaluatorFieldValues.length ; j++){ |
|
289 |
if (EvaluatorFieldValue.MATCH == evaluatorFieldValues[j].getType()){ |
|
290 |
EvaluatorFieldValueMatch evaluatorFieldValueMatch = (EvaluatorFieldValueMatch)evaluatorFieldValues[j]; |
|
291 |
Object area = evaluatorFieldValueMatch.getValue(); |
|
292 |
if (area instanceof Envelope){ |
|
293 |
executeSpatialQueryFromEnvelope(evaluatorFieldValueMatch.getFieldName(), |
|
294 |
(Envelope)area); |
|
295 |
return true; |
|
296 |
}else if (area instanceof Geometry){ |
|
297 |
executeSpatialQueryFromGeometry(evaluatorFieldValueMatch.getFieldName(), |
|
298 |
(Geometry)area); |
|
299 |
return true; |
|
300 |
} |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
return false; |
|
305 |
} |
|
313 |
/* (non-Javadoc) |
|
314 |
* @see org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider#getExplorer() |
|
315 |
*/ |
|
316 |
public DataServerExplorer getExplorer() throws ReadException { |
|
317 |
DataManager manager = DALLocator.getDataManager(); |
|
318 |
WFSServerExplorerParameters params; |
|
319 |
try { |
|
320 |
params = (WFSServerExplorerParameters) manager |
|
321 |
.createServerExplorerParameters(WFSServerExplorer.NAME); |
|
322 |
params.setUrl(wfsClient.getHost()); |
|
323 |
return manager.openServerExplorer(WFSServerExplorer.NAME,params); |
|
324 |
} catch (Exception e) { |
|
325 |
throw new ReadException(this.getName(), e); |
|
326 |
} |
|
327 |
} |
|
306 | 328 |
|
307 |
private void executeSpatialQueryFromGeometry(String the_geom, Geometry geometry) throws DataException{ |
|
308 |
wfsStatus.setFilterByArea(geometry, the_geom, projection.getAbrev(), |
|
309 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
|
310 |
executeSpatialQuery(); |
|
311 |
} |
|
312 | 329 |
|
313 |
private void executeSpatialQueryFromEnvelope(String the_geom, Envelope envelope) throws DataException{ |
|
314 |
wfsStatus.setFilterByArea(envelope, the_geom, projection.getAbrev(), |
|
315 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
|
316 |
executeSpatialQuery(); |
|
317 |
} |
|
330 |
/* (non-Javadoc) |
|
331 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider#getName() |
|
332 |
*/ |
|
333 |
public String getProviderName() { |
|
334 |
return NAME; |
|
335 |
} |
|
318 | 336 |
|
319 |
private void executeSpatialQuery() throws DataException{ |
|
320 |
try { |
|
321 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
|
322 |
//If it is not possible to calculate the |
|
323 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
|
324 |
m_Fich = wfsClient.getFeature(wfsStatus, true, null); |
|
325 |
} catch (WFSException e) { |
|
326 |
throw new InitializeException("Impossible to retrieve the file", e); |
|
327 |
} |
|
328 |
} |
|
337 |
public FeatureSetProvider createSet(FeatureQuery query) |
|
338 |
throws DataException { |
|
339 |
return createSet(query, null); |
|
340 |
} |
|
329 | 341 |
|
330 |
public Object getSourceId() { |
|
331 |
StringBuffer sourceID = new StringBuffer(getWFSParameters().getUrl()); |
|
332 |
sourceID.append("_" + getWFSParameters().getFeatureType()); |
|
333 |
int hash = sourceID.hashCode(); |
|
334 |
if (sourceID.length() > 60){ |
|
335 |
return sourceID.substring(0, 60) + "_" + hash; |
|
336 |
}else{ |
|
337 |
return sourceID.toString() + "_" + hash; |
|
338 |
} |
|
339 |
} |
|
342 |
public FeatureSetProvider createSet(FeatureQuery query, |
|
343 |
FeatureType featureType) throws DataException { |
|
344 |
File file = null; |
|
345 |
if (query == null){ |
|
346 |
file = executeQuery(); |
|
347 |
}else{ |
|
348 |
//If it is possible to execute a spatial query... |
|
349 |
if (query.getFilter() != null){ |
|
350 |
file = executeSpatialQuery(query); |
|
351 |
}else{ |
|
352 |
file = executeQuery(); |
|
353 |
} |
|
340 | 354 |
|
341 |
protected IProjection getSRS(){ |
|
342 |
if (projection == null){ |
|
343 |
try { |
|
344 |
wfsClient.getCapabilities(wfsStatus, false, null); |
|
345 |
wfsClient.describeFeatureType(wfsStatus, false, null); |
|
346 |
WFSFeature feature = (WFSFeature) wfsClient.getFeatures().get(wfsStatus.getFeatureName()); |
|
347 |
if (feature.getSrs().size() > 0){ |
|
348 |
projection = CRSFactory.getCRS(getEPSGCode((String)feature.getSrs().get(0))); |
|
349 |
} |
|
350 |
} catch (WFSException e) { |
|
351 |
logger.error("Error retrieving the feature type"); |
|
352 |
} |
|
353 |
} |
|
354 |
return projection; |
|
355 |
} |
|
355 |
} |
|
356 | 356 |
|
357 |
private String getEPSGCode(String srs){ |
|
358 |
if (srs == null){ |
|
359 |
return null; |
|
360 |
} |
|
361 |
if (srs.startsWith("urn:x-ogc:def:crs:")){ |
|
362 |
String newString = srs.substring(srs.lastIndexOf(":") + 1, srs.length()); |
|
363 |
if (srs.indexOf("EPSG") > 0){ |
|
364 |
if (newString.indexOf("EPSG") < 0){ |
|
365 |
newString = "EPSG:" + newString; |
|
366 |
} |
|
367 |
} |
|
368 |
return newString; |
|
369 |
} |
|
370 |
if (srs.toLowerCase().startsWith("crs:")){ |
|
371 |
return srs.substring(4, srs.length()); |
|
372 |
} |
|
373 |
return srs; |
|
374 |
} |
|
357 |
storeProviderDelegate = createProvider(file); |
|
358 |
return storeProviderDelegate.createSet(query, featureType); |
|
359 |
} |
|
375 | 360 |
|
376 |
protected int getGeometryType() { |
|
377 |
WFSFeature feature = (WFSFeature) wfsClient.getFeatures().get(wfsStatus.getFeatureName()); |
|
378 |
Vector field = feature.getFields(); |
|
379 |
if (field.size() > 0){ |
|
380 |
XMLElement rootElement = (XMLElement)field.get(0); |
|
381 |
Vector children = rootElement.getChildren(); |
|
382 |
for (int i=0 ; i<children.size() ; i++){ |
|
383 |
XMLElement child = (XMLElement)children.get(i); |
|
384 |
String type = child.getEntityType().getName(); |
|
385 |
if (type != null){ |
|
386 |
int index = type.indexOf(":"); |
|
387 |
if (index > -1){ |
|
388 |
type = type.substring(index+1); |
|
389 |
} |
|
390 |
Object obj = xmlSchemaManager.getApplicationGeometryTypeByFormat("gml", type); |
|
391 |
if (obj != null){ |
|
392 |
return (Integer)obj; |
|
393 |
} |
|
394 |
} |
|
395 |
} |
|
396 |
} |
|
397 |
return TYPES.GEOMETRY; |
|
398 |
} |
|
361 |
|
|
362 |
|
|
363 |
/** |
|
364 |
* It creates the provider that is used to retrieve features from a file. |
|
365 |
* @param file |
|
366 |
* the file that contains the downloaded file |
|
367 |
* @return |
|
368 |
* @throws InitializeException |
|
369 |
* @throws ProviderNotRegisteredException |
|
370 |
*/ |
|
371 |
private GPEStoreProvider createProvider(File file) throws InitializeException, ProviderNotRegisteredException{ |
|
372 |
return new GPEStoreProvider(createStoreParameters(file), getStoreServices()); |
|
373 |
} |
|
374 |
|
|
375 |
private DataStoreParameters createStoreParameters(File file) throws InitializeException, ProviderNotRegisteredException{ |
|
376 |
DataStoreParameters parameters = dataManager.createStoreParameters(GPEStoreProvider.NAME); |
|
377 |
|
|
378 |
parameters.setDynValue("File", file.getAbsolutePath()); |
|
379 |
parameters.setDynValue("Envelope", this.getDynValue("Envelope")); |
|
380 |
return parameters; |
|
381 |
} |
|
382 |
|
|
383 |
/** |
|
384 |
* It executes a wfs Query without spatial filter |
|
385 |
* @throws DataException |
|
386 |
*/ |
|
387 |
private File executeQuery() throws DataException{ |
|
388 |
try{ |
|
389 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
|
390 |
return wfsClient.getFeature(wfsStatus, true, null); |
|
391 |
} catch (WFSException e) { |
|
392 |
throw new InitializeException("Impossible to retrieve the file", e); |
|
393 |
} |
|
394 |
} |
|
395 |
|
|
396 |
/** |
|
397 |
* Executes a new query sending the FilterEncoding request to the server |
|
398 |
* @param query |
|
399 |
* The Query to send |
|
400 |
* @throws InitializeException |
|
401 |
* If there is a problem with the request or the parsing process |
|
402 |
*/ |
|
403 |
private File executeSpatialQuery(FeatureQuery query) throws DataException{ |
|
404 |
EvaluatorFieldsInfo fieldsInfo = query.getFilter().getFieldsInfo(); |
|
405 |
String[] fields = fieldsInfo.getFieldNames(); |
|
406 |
for (int i=0 ; i<fields.length ; i++){ |
|
407 |
EvaluatorFieldValue[] evaluatorFieldValues = fieldsInfo.getFieldValues(fields[i]); |
|
408 |
for (int j=0 ; j<evaluatorFieldValues.length ; j++){ |
|
409 |
if (EvaluatorFieldValue.MATCH == evaluatorFieldValues[j].getType()){ |
|
410 |
EvaluatorFieldValueMatch evaluatorFieldValueMatch = (EvaluatorFieldValueMatch)evaluatorFieldValues[j]; |
|
411 |
Object area = evaluatorFieldValueMatch.getValue(); |
|
412 |
if (area instanceof Envelope){ |
|
413 |
return executeSpatialQueryFromEnvelope(evaluatorFieldValueMatch.getFieldName(), |
|
414 |
(Envelope)area); |
|
415 |
}else if (area instanceof Geometry){ |
|
416 |
return executeSpatialQueryFromGeometry(evaluatorFieldValueMatch.getFieldName(), |
|
417 |
(Geometry)area); |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
} |
|
422 |
return null; |
|
423 |
} |
|
424 |
|
|
425 |
private File executeSpatialQueryFromGeometry(String the_geom, Geometry geometry) throws DataException{ |
|
426 |
wfsStatus.setFilterByArea(geometry, the_geom, projection.getAbrev(), |
|
427 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
|
428 |
return executeSpatialQuery(); |
|
429 |
} |
|
430 |
|
|
431 |
private File executeSpatialQueryFromEnvelope(String the_geom, Envelope envelope) throws DataException{ |
|
432 |
wfsStatus.setFilterByArea(envelope, the_geom, projection.getAbrev(), |
|
433 |
FilterEncoding.GEOMETRIC_OPERATOR_CONTAINS); |
|
434 |
return executeSpatialQuery(); |
|
435 |
} |
|
436 |
|
|
437 |
private File executeSpatialQuery() throws DataException{ |
|
438 |
try { |
|
439 |
wfsStatus.setProtocol(OGCClientOperation.PROTOCOL_POST); |
|
440 |
//If it is not possible to calculate the |
|
441 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_RESULTS); |
|
442 |
return wfsClient.getFeature(wfsStatus, true, null); |
|
443 |
} catch (WFSException e) { |
|
444 |
throw new InitializeException("Impossible to retrieve the file", e); |
|
445 |
} |
|
446 |
} |
|
447 |
|
|
448 |
public Object getSourceId() { |
|
449 |
StringBuffer sourceID = new StringBuffer(getWFSParameters().getUrl()); |
|
450 |
sourceID.append("_" + getWFSParameters().getFeatureType()); |
|
451 |
int hash = sourceID.hashCode(); |
|
452 |
if (sourceID.length() > 60){ |
|
453 |
return sourceID.substring(0, 60) + "_" + hash; |
|
454 |
}else{ |
|
455 |
return sourceID.toString() + "_" + hash; |
|
456 |
} |
|
457 |
} |
|
458 |
|
|
459 |
private String getEPSGCode(String srs){ |
|
460 |
if (srs == null){ |
|
461 |
return null; |
|
462 |
} |
|
463 |
if (srs.startsWith("urn:x-ogc:def:crs:")){ |
|
464 |
String newString = srs.substring(srs.lastIndexOf(":") + 1, srs.length()); |
|
465 |
if (srs.indexOf("EPSG") > 0){ |
|
466 |
if (newString.indexOf("EPSG") < 0){ |
|
467 |
newString = "EPSG:" + newString; |
|
468 |
} |
|
469 |
} |
|
470 |
return newString; |
|
471 |
} |
|
472 |
if (srs.toLowerCase().startsWith("crs:")){ |
|
473 |
return srs.substring(4, srs.length()); |
|
474 |
} |
|
475 |
return srs; |
|
476 |
} |
|
477 |
|
|
478 |
public Object createNewOID() { |
|
479 |
// TODO Auto-generated method stub |
|
480 |
return null; |
|
481 |
} |
|
482 |
|
|
483 |
public long getFeatureCount() throws DataException { |
|
484 |
if (featureCount == -1){ |
|
485 |
wfsStatus.setResultType(WFSStatus.RESULTYPE_HITS); |
|
486 |
try { |
|
487 |
wfsClient.getFeature(wfsStatus, true, null); |
|
488 |
featureCount = ((WFSGetFeatureRequestInformation)wfsClient.getLastWfsRequestInformation()).getNumberOfFeatures(); |
|
489 |
} catch (WFSException e) { |
|
490 |
throw new ConnectionServerException("Error calculating the feature count", e); |
|
491 |
} |
|
492 |
} |
|
493 |
return featureCount; |
|
494 |
} |
|
495 |
|
|
496 |
public int getOIDType() { |
|
497 |
// TODO Auto-generated method stub |
|
498 |
return 0; |
|
499 |
} |
|
500 |
|
|
501 |
public ResourceProvider getResource() { |
|
502 |
return storeProviderDelegate.getResource(); |
|
503 |
} |
|
504 |
|
|
505 |
protected FeatureProvider internalGetFeatureProviderByReference( |
|
506 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
|
507 |
throws DataException { |
|
508 |
return storeProviderDelegate.internalGetFeatureProviderByReference(reference, featureType); |
|
509 |
} |
|
510 |
|
|
511 |
public String getFullName() { |
|
512 |
return getWFSParameters().getUrl() + "_" + getWFSParameters().getFeatureType(); |
|
513 |
} |
|
514 |
|
|
515 |
public String getName() { |
|
516 |
return getFullName(); |
|
517 |
} |
|
518 |
|
|
519 |
public boolean isKnownEnvelope(){ |
|
520 |
return isKnownEnvelope; |
|
521 |
} |
|
522 |
|
|
523 |
public boolean hasRetrievedFeaturesLimit(){ |
|
524 |
return true; |
|
525 |
} |
|
526 |
|
|
527 |
public int getRetrievedFeaturesLimit(){ |
|
528 |
//WFS 1.0.0 can not know where is the limit |
|
529 |
if (getWFSParameters().getVersion().equals("1.0.0")){ |
|
530 |
return 0; |
|
531 |
}else{ |
|
532 |
return wfsClient.getServiceInformation().getMaxFeatures(); |
|
533 |
} |
|
534 |
} |
|
535 |
|
|
536 |
public void performChanges(Iterator deleteds, Iterator inserteds, |
|
537 |
Iterator updateds, Iterator featureTypesChanged) |
|
538 |
throws DataException { |
|
539 |
// FIXME exception |
|
540 |
throw new UnsupportedOperationException(); |
|
541 |
} |
|
542 |
|
|
543 |
public boolean allowWrite() { |
|
544 |
return wfsClient.getServiceInformation().isOperationSupported("Transaction"); |
|
545 |
} |
|
546 |
|
|
547 |
public Envelope getEnvelope() throws DataException { |
|
548 |
return (Envelope) this.getDynValue("Envelope"); |
|
549 |
} |
|
399 | 550 |
} |
400 | 551 |
|
Also available in: Unified diff