Code cleanup
This commit is contained in:
@@ -11,12 +11,12 @@ using UnityEngine.Networking;
|
||||
public class MapRenderer : MonoBehaviour
|
||||
{
|
||||
[Header("Overpass settings")]
|
||||
public string overpassUrl = "https://mapz.honzuvkod.dev/api/interpreter";
|
||||
public const string overpassUrl = "https://mapz.honzuvkod.dev/api/interpreter";
|
||||
public float queryRadiusMeters = 200f; // radius around lat/lon to query
|
||||
|
||||
[Header("Location (lat, lon)")]
|
||||
public double latitude; // example Prague
|
||||
public double longitude;
|
||||
private double latitude = 50.7727878;
|
||||
private double longitude = 15.0718625;
|
||||
|
||||
[Header("Building settings")]
|
||||
public Material buildingMaterial;
|
||||
@@ -31,35 +31,27 @@ public class MapRenderer : MonoBehaviour
|
||||
public float metersPerUnit = 1f; // scale: 1 unit = 1 meter
|
||||
public bool autoStart = true;
|
||||
|
||||
// Internal storage
|
||||
[Header("Storage")]
|
||||
Dictionary<long, Vector2> nodes = new Dictionary<long, Vector2>(); // id -> latlon
|
||||
List<Way> parsedWays = new List<Way>();
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (autoStart)
|
||||
StartCoroutine(GenerateForLocation(latitude, longitude));
|
||||
if (autoStart) { StartCoroutine(RenderMap()); }
|
||||
}
|
||||
|
||||
// Public entry for other scripts
|
||||
public void StartGenerating(double lat, double lon)
|
||||
public void StartGenerating()
|
||||
{
|
||||
latitude = lat; longitude = lon;
|
||||
StartCoroutine(GenerateForLocation(lat, lon));
|
||||
StartCoroutine(RenderMap());
|
||||
}
|
||||
|
||||
IEnumerator GenerateForLocation(double lat, double lon)
|
||||
IEnumerator RenderMap()
|
||||
{
|
||||
ClearChildren();
|
||||
|
||||
// compute bbox from radius
|
||||
float degPerMeter = 1f / 111000f; // approximate
|
||||
double delta = queryRadiusMeters * degPerMeter;
|
||||
double south = lat - delta;
|
||||
double north = lat + delta;
|
||||
double west = lon - delta;
|
||||
double east = lon + delta;
|
||||
//TODO: GPS update
|
||||
|
||||
string q = $"[out:xml][timeout:25];(way[\"building\"]({south.ToString().Replace(",",".")},{west.ToString().Replace(",", ".")},{north.ToString().Replace(",", ".")},{east.ToString().Replace(",", ".")});way[\"highway\"]({south.ToString().Replace(",", ".")},{west.ToString().Replace(",", ".")},{north.ToString().Replace(",", ".")},{east.ToString().Replace(",", ".")}););(._;>;);out body;";
|
||||
string q = $"[out:xml][timeout:90];(way[\"building\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")});way[\"highway\"](around:{queryRadiusMeters.ToString().Replace(",", ".")},{latitude.ToString().Replace(",", ".")},{longitude.ToString().Replace(",", ".")}););(._;>;);out body;";
|
||||
|
||||
WWWForm form = new WWWForm();
|
||||
form.AddField("data", q);
|
||||
@@ -78,14 +70,12 @@ public class MapRenderer : MonoBehaviour
|
||||
string xml = www.downloadHandler.text;
|
||||
ParseOverpassXml(xml);
|
||||
|
||||
// create separate GameObjects for buildings and roads
|
||||
GameObject buildingsRoot = new GameObject("Buildings");
|
||||
buildingsRoot.transform.parent = this.transform;
|
||||
|
||||
GameObject roadsRoot = new GameObject("Roads");
|
||||
roadsRoot.transform.parent = this.transform;
|
||||
|
||||
// iterate parsed ways
|
||||
foreach (var w in parsedWays)
|
||||
{
|
||||
if (w.tags.ContainsKey("building"))
|
||||
@@ -106,7 +96,6 @@ public class MapRenderer : MonoBehaviour
|
||||
|
||||
void ClearChildren()
|
||||
{
|
||||
// remove existing generated children
|
||||
List<GameObject> toDestroy = new List<GameObject>();
|
||||
foreach (Transform t in transform)
|
||||
toDestroy.Add(t.gameObject);
|
||||
@@ -122,7 +111,7 @@ public class MapRenderer : MonoBehaviour
|
||||
public Dictionary<string, string> tags = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
List<Way> parsedWays = new List<Way>();
|
||||
|
||||
|
||||
void ParseOverpassXml(string xmlText)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user