Skip to content

Commit

Permalink
bbox calculation by crs. will be refactored later on but works for now
Browse files Browse the repository at this point in the history
  • Loading branch information
bozmir committed Dec 12, 2024
1 parent 380580b commit e22a318
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
24 changes: 15 additions & 9 deletions Assets/Scripts/CartesianTiles/WFSGeoJSONTileDataLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Netherlands3D.Twin.Layers;
using System;
using Netherlands3D.Coordinates;
using KindMen.Uxios;

namespace Netherlands3D.CartesianTiles
{
Expand Down Expand Up @@ -129,16 +130,21 @@ private Tile CreateNewTile(Vector2Int tileKey)
return tile;
}

private Twin.Wms.BoundingBox DetermineBoundingBox(TileChange tileChange, Twin.Wms.MapFilters mapFilters)
private Twin.Wms.BoundingBox DetermineBoundingBox(TileChange tileChange, string spatialReference, out string spatialReferenceCode)
{
var bottomLeft = new Coordinate(CoordinateSystem.RD, tileChange.X, tileChange.Y, 0);
var topRight = new Coordinate(CoordinateSystem.RD, tileChange.X + tileSize, tileChange.Y + tileSize, 0);

var splitReferenceCode = mapFilters.spatialReference.Split(':');
string coordinateSystemAsString = splitReferenceCode[0].ToLower() == "epsg"
? splitReferenceCode[^1]
: DefaultEpsgCoordinateSystem;
string coordinateSystemAsString = DefaultEpsgCoordinateSystem;
var splitReferenceCode = spatialReference.Split(':');
for (int i = 0; i < splitReferenceCode.Length - 1; i++)
if (splitReferenceCode[i].ToLower() == "epsg")
{
coordinateSystemAsString = splitReferenceCode[^1];
break;
}

spatialReferenceCode = coordinateSystemAsString;
CoordinateSystems.FindCoordinateSystem(coordinateSystemAsString, out var foundCoordinateSystem);

var boundingBox = new Twin.Wms.BoundingBox(bottomLeft, topRight);
Expand All @@ -149,10 +155,10 @@ private Twin.Wms.BoundingBox DetermineBoundingBox(TileChange tileChange, Twin.Wm

private IEnumerator DownloadGeoJSON(TileChange tileChange, Tile tile, System.Action<TileChange> callback = null)
{
var mapData = Twin.Wms.MapFilters.FromUrl(new Uri(wfsUrl));
var boundingBox = DetermineBoundingBox(tileChange, mapData);
string url = wfsUrl.Replace("{0}", boundingBox.ToString());

var queryParameters = QueryString.Decode(new Uri(wfsUrl).Query);
string spatialReference = queryParameters["srsname"];
var boundingBox = DetermineBoundingBox(tileChange, spatialReference, out string code);
string url = wfsUrl.Replace("{0}", boundingBox.ToString() + "," + code);

//var bboxValue = $"{tileChange.X},{tileChange.Y},{(tileChange.X + tileSize)},{(tileChange.Y + tileSize)}";
//string url = WfsUrl.Replace("{0}", bboxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ public void Execute(LocalFile localFile)
//Create a folder layer
foreach (var featureType in featureTypes)
{
string crs = featureType.DefaultCRS;
Debug.Log("Adding WFS layer for featureType: " + featureType);
AddWFSLayer(featureType.Name, sourceUrl, wfsFolder, featureType.Title);
AddWFSLayer(featureType.Name, sourceUrl, crs, wfsFolder, featureType.Title);
}

wfs = null;
Expand All @@ -97,11 +98,13 @@ public void Execute(LocalFile localFile)
new Uri(sourceUrl).TryParseQueryString(queryParameters);
var featureType = queryParameters.Get(ParameterNameOfTypeNameBasedOnVersion());


if (string.IsNullOrEmpty(featureType) == false)
{
string crs = queryParameters["srsname"];
// 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);
AddWFSLayer(featureType, sourceUrl, crs, wfsFolder, featureType);
}

wfs = null;
Expand All @@ -113,10 +116,10 @@ public void Execute(LocalFile localFile)
}
}

private void AddWFSLayer(string featureType, string sourceUrl, FolderLayer folderLayer, string title)
private void AddWFSLayer(string featureType, string sourceUrl, string crsType, FolderLayer folderLayer, string title)
{
// Create a GetFeature URL for the specific featureType
UriBuilder uriBuilder = CreateLayerUri(featureType, sourceUrl);
UriBuilder uriBuilder = CreateLayerUri(featureType, sourceUrl, crsType);
var getFeatureUrl = uriBuilder.Uri.ToString();

Debug.Log($"Adding WFS layer '{featureType}' with url '{getFeatureUrl}'");
Expand All @@ -139,7 +142,7 @@ private void AddWFSLayer(string featureType, string sourceUrl, FolderLayer folde
symbolizer?.SetStrokeColor(randomLayerColor);
}

private UriBuilder CreateLayerUri(string featureType, string sourceUrl)
private UriBuilder CreateLayerUri(string featureType, string sourceUrl, string crs)
{
// Start by removing any query parameters we want to inject
var uriBuilder = new UriBuilder(sourceUrl);
Expand Down Expand Up @@ -167,8 +170,8 @@ private UriBuilder CreateLayerUri(string featureType, string sourceUrl)
!string.IsNullOrEmpty(geoJsonOutputFormatString) ? geoJsonOutputFormatString : "application/json"
);
}
uriBuilder.SetQueryParameter("srsname", crs);
uriBuilder.SetQueryParameter("bbox", "{0}"); // Bbox value is injected by CartesianTileWFSLayer

return uriBuilder;
}

Expand Down

0 comments on commit e22a318

Please sign in to comment.