Advertisement
chmodseven

Fetch OSM File for EasyRoads3D Import

Apr 20th, 2022
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.50 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using CityGen3D;
  5. using Unity.EditorCoroutines.Editor;
  6. using UnityEditor;
  7. using UnityEditor.SceneManagement;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. using UnityEngine.SceneManagement;
  11.  
  12. #if UNITY_EDITOR
  13.  
  14. public class FetchOsmFileForEasyRoads3DImportWizard : ScriptableWizard
  15. {
  16.     private const float WindowWidth = 500f;
  17.     private const float WindowHeight = 350f;
  18.  
  19.     [SerializeField] private string saveFileName;
  20.  
  21.     private GeoCoord _minBounds;
  22.     private GeoCoord _maxBounds;
  23.  
  24.     [MenuItem ("Tools/CityGen3D/Fetch OSM File for EasyRoads3D Import", priority = 20)]
  25.     private static void CreateWizard ()
  26.     {
  27.         DisplayWizard (
  28.             "Fetch OSM File for EasyRoads3D Import",
  29.             typeof (FetchOsmFileForEasyRoads3DImportWizard),
  30.             "Fetch OSM File");
  31.     }
  32.  
  33.     private void OnEnable ()
  34.     {
  35.         ConstrainWindowRectSize (WindowWidth, WindowHeight);
  36.  
  37.         Landscape [] landscapes = FindObjectsOfType<Landscape> ();
  38.         if (landscapes == null || landscapes.Length == 0)
  39.         {
  40.             helpString = "No CityGen3D Landscape objects in scene.";
  41.             throw new ArgumentException (helpString);
  42.         }
  43.  
  44.         // Map.Instance doesn't seem to cover bounds across multiple landscapes quite right, so aggregate these ourselves
  45.         _minBounds = new GeoCoord ();
  46.         _maxBounds = new GeoCoord ();
  47.         bool setFirst = true;
  48.         foreach (Landscape landscape in landscapes)
  49.         {
  50.             GeoCoord min = landscape.locationMin;
  51.             GeoCoord max = landscape.locationMax;
  52.             if (setFirst || min.latitude < _minBounds.latitude)
  53.             {
  54.                 _minBounds.latitude = min.latitude;
  55.             }
  56.             if (setFirst || min.longitude < _minBounds.longitude)
  57.             {
  58.                 _minBounds.longitude = min.longitude;
  59.             }
  60.             if (setFirst || max.latitude > _maxBounds.latitude)
  61.             {
  62.                 _maxBounds.latitude = max.latitude;
  63.             }
  64.             if (setFirst || max.longitude > _maxBounds.longitude)
  65.             {
  66.                 _maxBounds.longitude = max.longitude;
  67.             }
  68.             setFirst = false;
  69.         }
  70.  
  71.         if (string.IsNullOrEmpty (saveFileName))
  72.         {
  73.             saveFileName = SceneManager.GetActiveScene ().name + ".osm";
  74.         }
  75.     }
  76.  
  77.     private static FetchOsmFileForEasyRoads3DImportWizard GetWindowReference ()
  78.     {
  79.         // Get the window without changing the focus away from the current window
  80.         return (FetchOsmFileForEasyRoads3DImportWizard) GetWindow (
  81.             typeof (FetchOsmFileForEasyRoads3DImportWizard), false, "Fetch OSM File", false);
  82.     }
  83.  
  84.     private static void ConstrainWindowRectSize (float windowWidth, float windowHeight)
  85.     {
  86.         FetchOsmFileForEasyRoads3DImportWizard window = GetWindowReference ();
  87.  
  88.         window.minSize = new Vector2 (windowWidth, windowHeight);
  89.         window.maxSize = new Vector2 (windowWidth, windowHeight);
  90.     }
  91.  
  92.     protected override bool DrawWizardGUI ()
  93.     {
  94.         GUIStyle labelStyle = new (EditorStyles.label) {richText = true};
  95.         GUILayoutOption labelWidth = GUILayout.Width (600f);
  96.  
  97.         GUILayout.Label ("<b><i>USAGE</i></b>", labelStyle, labelWidth);
  98.         GUILayout.Space (10);
  99.         GUILayout.Label ("1. Run this wizard in a scene with CityGen3D terrain landscapes present", labelStyle, labelWidth);
  100.         GUILayout.Label ("2. Set appropriate filename (to save in Database folder) and click <b>Fetch OSM File</b>", labelStyle, labelWidth);
  101.         GUILayout.Label ("3. Create an EasyRoads3D Road Network object in the scene", labelStyle, labelWidth);
  102.         GUILayout.Label ("4. Go under the Road Network's <i>General Settings/Import Road Data</i> tab", labelStyle, labelWidth);
  103.         GUILayout.Label ("5. Leave <i>Global Terrain Coordinates</i> on defaults of 0", labelStyle, labelWidth);
  104.         GUILayout.Label ("6. Adjust any of the <i>OSM Import</i> dropdowns as needed", labelStyle, labelWidth);
  105.         GUILayout.Label ("7. Click <b>Open OSM File</b> and select the newly created file", labelStyle, labelWidth);
  106.         GUILayout.Space (20);
  107.         GUILayout.Label ("Landscape min: " + _minBounds, labelStyle, labelWidth);
  108.         GUILayout.Label ("Landscape max: " + _maxBounds, labelStyle, labelWidth);
  109.         GUILayout.Space (10);
  110.  
  111.         return base.DrawWizardGUI ();
  112.     }
  113.  
  114.     private void OnWizardCreate ()
  115.     {
  116.         if (_minBounds == null || _maxBounds == null)
  117.         {
  118.             helpString = "No CityGen3D Landscape objects in scene.";
  119.             throw new ArgumentException (helpString);
  120.         }
  121.  
  122.         if (string.IsNullOrEmpty (saveFileName))
  123.         {
  124.             helpString = "No save filename provided.";
  125.             throw new ArgumentException (helpString);
  126.         }
  127.  
  128.         EditorCoroutineUtility.StartCoroutine (DoProcessing (), this);
  129.     }
  130.  
  131.     private IEnumerator DoProcessing ()
  132.     {
  133.         EditorUtility.ClearProgressBar ();
  134.         EditorUtility.DisplayProgressBar ("Fetching OSM data", "Fetching OSM data", 0.1f);
  135.  
  136.         Map.Instance.data.UpdateQuery ();
  137.         UnityWebRequest www = UnityWebRequest.Get (Map.Instance.data.query);
  138.         yield return www.SendWebRequest ();
  139.         if (www.result == (UnityWebRequest.Result) 2 || www.result == (UnityWebRequest.Result) 3)
  140.         {
  141.             EditorUtility.ClearProgressBar ();
  142.             helpString = "Download failed.";
  143.             throw new ArgumentException (helpString);
  144.         }
  145.  
  146.         EditorUtility.DisplayProgressBar ("Processing OSM data", "Processing OSM data into EasyRoads3D compatible format", 0.5f);
  147.  
  148.         // Hack for adding bounds in, that query result doesn't include
  149.         string result = www.downloadHandler.text.Replace (
  150.             "<note>",
  151.             "<bounds minlat=\"" + _minBounds.latitude + "\" " +
  152.             "minlon=\"" + _minBounds.longitude + "\" " +
  153.             "maxlat=\"" + _maxBounds.latitude + "\" " +
  154.             "maxlon=\"" + _maxBounds.longitude + "\"/>\n<note>");
  155.  
  156.         EditorUtility.DisplayProgressBar ("Saving OSM data", "Saving OSM data", 0.9f);
  157.         string saveFilePath = Path.Combine (Application.dataPath, "Database", saveFileName);
  158.         File.WriteAllText (saveFilePath, result);
  159.         EditorUtility.ClearProgressBar ();
  160.  
  161.         Scene currentScene = SceneManager.GetActiveScene ();
  162.         EditorSceneManager.MarkSceneDirty (currentScene);
  163.     }
  164. }
  165.  
  166. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement