chmodseven

GeoJson polygon converter

Apr 26th, 2021 (edited)
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.IO;
  4. using CityGen3D;
  5. using GeoJSON.Net;
  6. using GeoJSON.Net.Feature;
  7. using GeoJSON.Net.Geometry;
  8. using Newtonsoft.Json;
  9. using UnityEditor;
  10. using UnityEngine;
  11. using Feature = GeoJSON.Net.Feature.Feature;
  12.  
  13. // ReSharper disable InvertIf
  14.  
  15. public class TestGeoJson : MonoBehaviour
  16. {
  17.     public string jsonFilename = "test.geojson";
  18.  
  19.     public static void ProcessGeoJson (string path)
  20.     {
  21.         string json = File.ReadAllText (path);
  22.         FeatureCollection jsonObject = JsonConvert.DeserializeObject<FeatureCollection> (json);
  23.  
  24.         GeoCoord origin = Map.Instance.data.GetOrigin ();
  25.         List<Vector3> buildingPoints = new List<Vector3> ();
  26.         foreach (Feature feature in jsonObject.Features)
  27.         {
  28.             buildingPoints.Clear ();
  29.             string buildingId = string.Empty;
  30.             if (feature.Properties.TryGetValue ("uuid", out object buildingIdProperty))
  31.             {
  32.                 buildingId = (string) buildingIdProperty;
  33.             }
  34.             IGeometryObject geometry = feature.Geometry;
  35.             if (geometry.Type == GeoJSONObjectType.Polygon)
  36.             {
  37.                 Polygon polygon = (Polygon) geometry;
  38.                 ReadOnlyCollection<LineString> coordinates = polygon.Coordinates;
  39.                 if (coordinates != null && coordinates.Count != 0)
  40.                 {
  41.                     foreach (LineString coordinate in coordinates)
  42.                     {
  43.                         ReadOnlyCollection<IPosition> points = coordinate.Coordinates;
  44.                         if (points != null && points.Count != 0)
  45.                         {
  46.                             foreach (IPosition point in points)
  47.                             {
  48.                                 double latitude = point.Latitude;
  49.                                 double longitude = point.Longitude;
  50.                                 GeoCoord geoPos = new GeoCoord (latitude, longitude);
  51.                                 MapCoord mapPos = geoPos.GetMapCoord (origin);
  52.                                 buildingPoints.Add (mapPos.GetPosition ());
  53.                             }
  54.                         }
  55.                     }
  56.  
  57.                     GameObject building = Map.Instance.mapBuildings.Create (0, buildingPoints);
  58.                     if (!string.IsNullOrEmpty (buildingId))
  59.                     {
  60.                         building.name += " " + buildingId;
  61.                     }
  62.                     if (building != null)
  63.                     {
  64.                         building.GetComponent<MapBuilding> ().RefreshPivot ();
  65.                         building.GetComponent<MapBuilding> ().BuildMap ();
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. [CustomEditor (typeof (TestGeoJson))]
  74. public class EditorCreateBuilding : Editor
  75. {
  76.     public override void OnInspectorGUI ()
  77.     {
  78.         DrawDefaultInspector ();
  79.         EditorGUILayout.Separator ();
  80.         TestGeoJson script = (TestGeoJson) target;
  81.         if (GUILayout.Button ("Process GeoJson File"))
  82.         {
  83.             string path = Path.Combine (Application.dataPath, "Data", script.jsonFilename);
  84.             TestGeoJson.ProcessGeoJson (path);
  85.         }
  86.     }
  87. }
Add Comment
Please, Sign In to add comment