Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the Title of a WFS and its Feature Types in the layerpanel inste… #282

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Netherlands3D.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Xml.Serialization;
using Netherlands3D.Twin.Layers;
using UnityEngine.Networking;

Expand Down Expand Up @@ -61,7 +62,7 @@ public bool Supports(LocalFile localFile)
public void Execute(LocalFile localFile)
{
var sourceUrl = localFile.SourceUrl;
var wfsFolder = new FolderLayer(sourceUrl);
var wfsFolder = new FolderLayer(!string.IsNullOrEmpty(wfs.GetTitle()) ? wfs.GetTitle() : sourceUrl);

switch (wfs.requestType)
{
Expand All @@ -73,7 +74,7 @@ public void Execute(LocalFile localFile)
foreach (var featureType in featureTypes)
{
Debug.Log("Adding WFS layer for featureType: " + featureType);
AddWFSLayer(featureType, sourceUrl, wfsFolder);
AddWFSLayer(featureType.Name, sourceUrl, wfsFolder, featureType.Title);
}

wfs = null;
Expand All @@ -87,7 +88,9 @@ public void Execute(LocalFile localFile)

if (string.IsNullOrEmpty(featureType) == false)
{
AddWFSLayer(featureType, sourceUrl, wfsFolder);
// Can't deduct a human-readable title at the moment, we should add that we always query for the
// capabilities; this also helps with things like outputFormat and CRS
AddWFSLayer(featureType, sourceUrl, wfsFolder, featureType);
}

wfs = null;
Expand All @@ -99,7 +102,7 @@ public void Execute(LocalFile localFile)
}
}

private void AddWFSLayer(string featureType, string sourceUrl, FolderLayer folderLayer)
private void AddWFSLayer(string featureType, string sourceUrl, FolderLayer folderLayer, string title)
{
// Create a GetFeature URL for the specific featureType
UriBuilder uriBuilder = CreateLayerUri(featureType, sourceUrl);
Expand All @@ -110,7 +113,7 @@ private void AddWFSLayer(string featureType, string sourceUrl, FolderLayer folde
//Spawn a new WFS GeoJSON layer
WFSGeoJsonLayerGameObject newLayer = Instantiate(layerPrefab);
newLayer.LayerData.SetParent(folderLayer);
newLayer.Name = featureType;
newLayer.Name = title;
newLayer.SetURL(getFeatureUrl);
}

Expand Down Expand Up @@ -152,6 +155,17 @@ private string ParameterNameOfTypeNameBasedOnVersion()
return wfsVersion == "1.1.0" ? "typeName" : "typeNames";
}

[Serializable]
public class FeatureType
{
public string Name;
public string Title;
public string Abstract;
public string DefaultCRS;
public string[] OtherCRS;
public string MetadataURL;
}

private class GeoJSONWFS
{
private readonly string sourceUrl;
Expand Down Expand Up @@ -256,27 +270,59 @@ public string GetWFSVersionFromBody()
return "";
}

public IEnumerable<string> GetFeatureTypes()
public string GetTitle()
{
if(xmlDocument == null)
ParseBodyAsXML();

var title = xmlDocument?
.DocumentElement?
.SelectSingleNode("//*[local-name()='ServiceIdentification']/*[local-name()='Title']", namespaceManager);

return title != null ? title.InnerText : "";
}

public IEnumerable<FeatureType> GetFeatureTypes()
{
if(xmlDocument == null)
ParseBodyAsXML();

var featureTypeListNodeInRoot = xmlDocument?.DocumentElement?
.SelectSingleNode("//*[local-name()='FeatureTypeList']", namespaceManager);
var featureTypeChildNodes = featureTypeListNodeInRoot?.ChildNodes;
var featureTypes = new List<string>();
var featureTypes = new List<FeatureType>();
if (featureTypeChildNodes == null)
{
Debug.LogWarning("No feature types were found in WFS' GetCapabilities response");
return featureTypes;
}

var wfsVersion = GetWFSVersion();
string namespaceVersion = wfsVersion switch
{
"1.1.0" => "1.1.0",
"2.0.0" => "2.0",
_ => null
};

// Unsupported version
if (namespaceVersion == null) return featureTypes;

XmlSerializer serializer = new XmlSerializer(
typeof(FeatureType),
new XmlRootAttribute("FeatureType")
{
Namespace = "http://www.opengis.net/wfs/" + namespaceVersion
}
);
foreach (XmlNode featureTypeNode in featureTypeChildNodes)
{
var featureTypeName = featureTypeNode?.SelectSingleNode(".//*[local-name()='Name']", namespaceManager)?.InnerText;
if (string.IsNullOrEmpty(featureTypeName)) continue;

featureTypes.Add(featureTypeName);
using XmlNodeReader reader = new XmlNodeReader(featureTypeNode);

FeatureType featureType = serializer.Deserialize(reader) as FeatureType;
if (featureType == null) continue;

featureTypes.Add(featureType);
}

return featureTypes;
Expand Down
Loading