Skip to content

Commit

Permalink
Load project boundaries from GeoJSON
Browse files Browse the repository at this point in the history
The Dossier contains a reference to a GeoJSON file that can be loaded, and for which we emit an event once it has done so. This event is used to center the camera on the returned feature collection's center.
  • Loading branch information
mvriel committed Jul 17, 2023
1 parent a7da8d8 commit 8ae9c1c
Show file tree
Hide file tree
Showing 16 changed files with 324 additions and 44 deletions.
81 changes: 42 additions & 39 deletions Assets/Scenes/Main.unity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion Assets/Scripts/Configuration/Indicators/DossierLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Netherlands3D.Indicators;
using Netherlands3D.Indicators.Dossier;
using UnityEngine;
using UnityEngine.Events;

namespace Netherlands3D.Twin.Configuration.Indicators
{
Expand All @@ -12,16 +12,28 @@ public class DossierLoader : MonoBehaviour
private void OnEnable()
{
configuration.OnDossierLoadingStart.AddListener(OnDossierStartLoading);
dossier.onSelectedVariant.AddListener(OnSelectedVariant);
}

private void OnDisable()
{
dossier.onSelectedVariant.RemoveListener(OnSelectedVariant);
configuration.OnDossierLoadingStart.RemoveListener(OnDossierStartLoading);
}

private void OnDossierStartLoading(string dossierId)
{
StartCoroutine(dossier.Open(dossierId));
}

private void OnSelectedVariant(Variant? variant)
{
if (variant.HasValue == false)
{
return;
}

StartCoroutine(dossier.LoadProjectAreaGeometry(variant.Value));
}
}
}
8 changes: 8 additions & 0 deletions Packages/eu.netherlands3d.geojson/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Packages/eu.netherlands3d.geojson/Runtime/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using GeoJSON.Net.Feature;
using Netherlands3D.Coordinates;
using UnityEngine;
using UnityEngine.Events;

namespace Netherlands.GeoJSON
{
/// <summary>
/// Positions a game object to match the center of the given feature collection.
///
/// This component assumes that the attached mesh/sprite equals 1 square unity unit/meter; similar to a regular
/// 3D Plane.
/// </summary>
public class CenterOnFeatureCollection : MonoBehaviour
{
[SerializeField] private GameObject targetGameObject;

public UnityEvent<Vector3> onSetPosition = new();

private void Awake()
{
if (targetGameObject == null)
{
targetGameObject = gameObject;
}
}

public void CenterOn(FeatureCollection featureCollection)
{
double[] boundingBox = featureCollection.BoundingBoxes ?? featureCollection.DerivedBoundingBoxes();
int epsgId = featureCollection.EPSGId();

Coordinate topLeft = CoordinateConverter.ConvertTo(
new Coordinate(epsgId, boundingBox[0], boundingBox[1], 0),
CoordinateSystem.Unity
);
Coordinate bottomRight = CoordinateConverter.ConvertTo(
new Coordinate(epsgId, boundingBox[2], boundingBox[3], 0),
CoordinateSystem.Unity
);
Coordinate center = CoordinateConverter.ConvertTo(
new Coordinate(
epsgId,
(boundingBox[0] + boundingBox[2]) / 2d,
(boundingBox[1] + boundingBox[3]) / 2d,
0
),
CoordinateSystem.Unity
);

Vector3 position = new Vector3(
center.ToVector3().x,
transform.position.y,
center.ToVector3().z
);

transform.position = position;
onSetPosition.Invoke(position);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ae9c1c

Please sign in to comment.