Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using CityGen3D;
- using GeoJSON.Net;
- using GeoJSON.Net.Feature;
- using GeoJSON.Net.Geometry;
- using Newtonsoft.Json;
- using UnityEditor;
- using UnityEngine;
- using Feature = GeoJSON.Net.Feature.Feature;
- // ReSharper disable InvertIf
- public class TestGeoJson : MonoBehaviour
- {
- public string jsonFilename = "test.geojson";
- public static void ProcessGeoJson (string path)
- {
- string json = File.ReadAllText (path);
- FeatureCollection jsonObject = JsonConvert.DeserializeObject<FeatureCollection> (json);
- GeoCoord origin = Map.Instance.data.GetOrigin ();
- List<Vector3> buildingPoints = new List<Vector3> ();
- foreach (Feature feature in jsonObject.Features)
- {
- buildingPoints.Clear ();
- string buildingId = string.Empty;
- if (feature.Properties.TryGetValue ("uuid", out object buildingIdProperty))
- {
- buildingId = (string) buildingIdProperty;
- }
- IGeometryObject geometry = feature.Geometry;
- if (geometry.Type == GeoJSONObjectType.Polygon)
- {
- Polygon polygon = (Polygon) geometry;
- ReadOnlyCollection<LineString> coordinates = polygon.Coordinates;
- if (coordinates != null && coordinates.Count != 0)
- {
- foreach (LineString coordinate in coordinates)
- {
- ReadOnlyCollection<IPosition> points = coordinate.Coordinates;
- if (points != null && points.Count != 0)
- {
- foreach (IPosition point in points)
- {
- double latitude = point.Latitude;
- double longitude = point.Longitude;
- GeoCoord geoPos = new GeoCoord (latitude, longitude);
- MapCoord mapPos = geoPos.GetMapCoord (origin);
- buildingPoints.Add (mapPos.GetPosition ());
- }
- }
- }
- GameObject building = Map.Instance.mapBuildings.Create (0, buildingPoints);
- if (!string.IsNullOrEmpty (buildingId))
- {
- building.name += " " + buildingId;
- }
- if (building != null)
- {
- building.GetComponent<MapBuilding> ().RefreshPivot ();
- building.GetComponent<MapBuilding> ().BuildMap ();
- }
- }
- }
- }
- }
- }
- [CustomEditor (typeof (TestGeoJson))]
- public class EditorCreateBuilding : Editor
- {
- public override void OnInspectorGUI ()
- {
- DrawDefaultInspector ();
- EditorGUILayout.Separator ();
- TestGeoJson script = (TestGeoJson) target;
- if (GUILayout.Button ("Process GeoJson File"))
- {
- string path = Path.Combine (Application.dataPath, "Data", script.jsonFilename);
- TestGeoJson.ProcessGeoJson (path);
- }
- }
- }
Add Comment
Please, Sign In to add comment