/// <summary> /// Creates the WFS XML string that can be used in a HTTP Post request. /// </summary> /// <param name="requestUrl">The request URL.</param> /// <param name="boundingBox">The bounding box.</param> /// <returns>A string containing WFS XML-text.</returns> private static string CreateWfsGetFeatureXmlString(string requestUrl, WfsBoundingBox boundingBox) { NameValueCollection nameValueCollection; StringBuilder sb; const string getFeatureStart = "<wfs:GetFeature xmlns:wfs=\"http://www.opengis.net/wfs\" service=\"WFS\" version=\"[Version]\" outputFormat=\"[OutputFormat]\" xsi:schemaLocation=\"http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"; const string getFeatureEnd = "</wfs:GetFeature>"; const string queryStart = "<wfs:Query typeName=\"[TypeName]\" srsName=\"[SrsName]\">"; const string queryEnd = "</wfs:Query>"; nameValueCollection = HttpUtility.ParseQueryString(requestUrl); string version = nameValueCollection["version"]; string typeName = nameValueCollection["typename"]; string srsName = nameValueCollection["srsName"]; string filter = nameValueCollection["filter"]; // Get geometry name WFSDescribeFeatureType describeFeatureType = GetWFSDescribeFeatureType(requestUrl, typeName); string geometryName = describeFeatureType.GeometryField.Name; string strFilterXml = CreateWfsXmlFilterString(filter, geometryName, boundingBox); sb = new StringBuilder(); sb.Append(getFeatureStart.Replace("[Version]", version).Replace("[OutputFormat]", "json")); sb.Append(queryStart.Replace("[TypeName]", typeName).Replace("[SrsName]", srsName)); sb.Append(strFilterXml); sb.Append(queryEnd); sb.Append(getFeatureEnd); string str = sb.ToString(); return(str); }
/// <summary> /// Makes a WFS DescribeFeatureType Request and returns a WFSDescribeFeature. /// </summary> /// <param name="url">The WFS server URL.</param> /// <param name="typeName">The feature layer name including namespace</param> /// <param name="version">The WFS version.</param> /// <returns></returns> public static WFSDescribeFeatureType GetWFSDescribeFeatureType(string url, string typeName, WFSVersion version) { string requestUrl = CreateDescribeFeatureTypeRequestUrl(url, typeName, version); using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; string strXml = wc.DownloadString(requestUrl); WFSDescribeFeatureType result = ParseDescribeFeatureType(strXml, version); result.Name = new WfsTypeName(typeName); return(result); //return ParseDescribeFeatureType(strXml, version); } }
/// <summary> /// Makes a GetCapabilities request and a DescribeFeature request /// The feature type that we get from DescribeFeature is added to /// the WfsCapabilities object. /// </summary> /// <param name="url">The WFS Server URL.</param> /// <param name="typeName">The feature layer name including namespace.</param> /// <returns></returns> public static WFSCapabilities GetWFSCapabilitiesAndMergeDescribeFeatureType(string url, string typeName) { WFSCapabilities wfsCapabilities = GetWFSCapabilities(url); WFSDescribeFeatureType describeFeatureType = GetWFSDescribeFeatureType(url, typeName); foreach (var featureType in wfsCapabilities.FeatureTypes) { if (featureType.Name.FullName == describeFeatureType.Name.FullName) { featureType.DescribeFeatureType = describeFeatureType; } } return(wfsCapabilities); }
public WfsLayerSetting CreateNewWfsLayer(string name, string filter, string serverUrl, string typeName, string color, bool useBbox) { var wfsLayer = new WfsLayerSetting(); wfsLayer.Filter = filter; wfsLayer.Name = name; wfsLayer.ServerUrl = serverUrl; wfsLayer.TypeName = typeName; WFSDescribeFeatureType wfsDescribeFeatureType = WFSManager.GetWFSDescribeFeatureType(serverUrl, typeName); wfsLayer.GeometryName = wfsDescribeFeatureType.GeometryField.Name; wfsLayer.GeometryType = wfsDescribeFeatureType.GeometryType; wfsLayer.Color = color; wfsLayer.UseSpatialFilterExtentAsBoundingBox = useBbox; return(wfsLayer); }
/// <summary> /// Parses WFS response XML into a Dictionary where the key is the full name of the feature /// and the value is its corresponding WFSDescribeFeature /// </summary> /// <param name="strXml">WFS response XML.</param> /// <returns></returns> public Dictionary <string, WFSDescribeFeatureType> ParseDescribeFeatureTypes(string strXml) { var dicDescribeFeatures = new Dictionary <string, WFSDescribeFeatureType>(); try { var doc = new XmlDocument(); doc.XmlResolver = null; doc.LoadXml(strXml); if (doc.DocumentElement == null) { return(dicDescribeFeatures); } var nsmgr = new XmlNamespaceManager(doc.NameTable); foreach (XmlNode nodes in doc.DocumentElement.Attributes) { if (nodes.Prefix == "xmlns") { nsmgr.AddNamespace(nodes.Name, nodes.NamespaceURI); } } XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("xsd:complexType"); if (nodeList.Count == 0) { nodeList = doc.DocumentElement.GetElementsByTagName("complexType"); } foreach (XmlElement node in nodeList) { XmlElement xname = node.NextSibling as XmlElement; WFSDescribeFeatureType wfsDescribeFeatureType = ParseDescribeFeature(node, xname); if (!dicDescribeFeatures.ContainsKey(wfsDescribeFeatureType.Name.FullName)) { dicDescribeFeatures.Add(wfsDescribeFeatureType.Name.FullName, wfsDescribeFeatureType); } } } catch (Exception) { return(dicDescribeFeatures); } return(dicDescribeFeatures); }
/// <summary> /// Parses XML objects into a WFSDescribeFeature object /// </summary> /// <param name="xtype">Describes the feature fields.</param> /// <param name="xname">Describes the feature name.</param> /// <returns></returns> private WFSDescribeFeatureType ParseDescribeFeature(XmlElement xtype, XmlElement xname) { if (xtype == null || xname == null) { return(null); } var wfsDescribeFeature = new WFSDescribeFeatureType(); wfsDescribeFeature.Fields = new List <Field>(); try { var xcomplexContent = xtype["xsd:complexContent"] ?? xtype["complexContent"]; var xextension = xcomplexContent["xsd:extension"] == null ? xcomplexContent["extension"] : xcomplexContent["xsd:extension"]; var xsequence = xextension["xsd:sequence"] == null ? xextension["sequence"] : xextension["xsd:sequence"]; if (xsequence != null) { foreach (XmlNode ele in xsequence) { var field = new Field(); field.Name = ele.Attributes["name"].Value.ToString(CultureInfo.InvariantCulture); if (ele.Attributes["type"] == null) { field.DataType = "Object"; } else { field.DataType = ele.Attributes["type"].Value.ToString(CultureInfo.InvariantCulture); } wfsDescribeFeature.Fields.Add(field); } } // name and namespace string name = xname.Attributes["name"].Value.ToString(); string typeName = xname.Attributes["type"].Value.ToString(); wfsDescribeFeature.Name = new WfsTypeName(typeName); wfsDescribeFeature.Name.Name = name; foreach (Field field in wfsDescribeFeature.Fields) { if (field.DataType.StartsWith("gml:")) { wfsDescribeFeature.GeometryField = field; if (field.DataType == "gml:PointPropertyType" || field.DataType == "gml:MultiPointPropertyType" || field.DataType == "gml:MultiPointType") { wfsDescribeFeature.GeometryType = GeometryType.Point; } else if (field.DataType == "gml:MultiLineStringPropertyType" || field.DataType == "gml:MultiLineStringType") { wfsDescribeFeature.GeometryType = GeometryType.Line; } else if (field.DataType == "gml:MultiSurfacePropertyType" || field.DataType == "gml:MultiSurfaceType" || field.DataType == "gml:MultiPolygonPropertyType" || field.DataType == "gml:MultiPolygonType") { wfsDescribeFeature.GeometryType = GeometryType.Polygon; } else { wfsDescribeFeature.GeometryType = GeometryType.Geometry; } } } return(wfsDescribeFeature); } catch (Exception) { return(null); } }