// Use this for initialization void Start() { nodes = new Dictionary <ulong, OsmNode> (); ways = new List <OsmWay> (); var txtAsset = Resources.Load <TextAsset> (resourceFile); XmlDocument doc = new XmlDocument(); doc.LoadXml(txtAsset.text); SetBounds(doc.SelectSingleNode("/osm/bounds")); GetNodes(doc.SelectNodes("/osm/node")); GetWays(doc.SelectNodes("/osm/way")); float minx = (float)MercatorProjection.lonToX(bounds.MinLon); float maxx = (float)MercatorProjection.lonToX(bounds.MaxLon); float miny = (float)MercatorProjection.latToY(bounds.MinLat); float maxy = (float)MercatorProjection.latToY(bounds.MaxLat); groundPlane.transform.localScale = new Vector3((maxx - minx) / 2, 1, (maxy - miny) / 2); IsReady = true; }
/// <summary> /// Updates the models position using GPS coordinates. /// Also creates smooth walking animation between the old model /// position and new model position. /// </summary> void Update() { x = (float)MercatorProjection.lonToX(Gps.instance.longitude); y = (float)MercatorProjection.latToY(Gps.instance.latitude); newPos = new Vector3(x, 0f, y); newPos = newPos - map.bounds.centre; float t = Mathf.SmoothStep(0f, 1f, Mathf.PingPong(1 / 25f, 1f)); //Linearly adjusts rotation and position. transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newPos), 0.15f); transform.position = Vector3.Lerp(transform.position, newPos, t); float distance = Vector3.Distance(transform.position, newPos); if (distance < 2) { anim.SetFloat("MoveSpeed", Mathf.SmoothStep(v, 0f, time)); } else { v = Mathf.SmoothStep(0f, 1f, time); anim.SetFloat("MoveSpeed", v); } time += 0.5f * Time.deltaTime; if (time > 1.0f) { time = 1.0f; } }
/// <summary> /// Spawns new enemies when they are sent from the server. /// </summary> /// <param name="enemies">List of type Enemy of all the enemies to be spawned.</param> void Spawn(List <Enemy> enemies) { //Only execute in the Map scene. if (SceneManager.GetActiveScene().name == "Map Reader Test") { //Destroy any current spawned enemies for (int i = enemyParent.transform.childCount; i > 0; i--) { Destroy(enemyParent.transform.GetChild(i - 1).gameObject); } //Instantiate each new enemy. foreach (Enemy e in enemies) { float x = (float)MercatorProjection.lonToX(e.lon); float y = (float)MercatorProjection.latToY(e.lat); Vector3 position = new Vector3(x, 0, y); position = position - map.bounds.centre; if (e.name == "wizard") { var newWizard = Instantiate(wizard, position, transform.rotation); newWizard.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f); newWizard.transform.SetParent(enemyParent.transform); } else if (e.name == "skeleton") { var newSkeleton = Instantiate(skeleton, position, transform.rotation); newSkeleton.transform.SetParent(enemyParent.transform); } } } }
public Vector2 GetUnityMapCenter() { Vector2 gpsMapCenter = GetGPSMapCenter(); double x = MercatorProjection.lonToX(gpsMapCenter.x); double y = MercatorProjection.latToY(gpsMapCenter.y); return(new Vector2((float)x, (float)y)); }
public OsmNode(XmlNode node) { ID = GetAttribute <ulong>("id", node.Attributes); Latitude = GetAttribute <float>("lat", node.Attributes); Longitude = GetAttribute <float>("lon", node.Attributes); X = (float)MercatorProjection.lonToX(Longitude); Y = (float)MercatorProjection.latToY(Latitude); }
public OsmNode(XmlNode node) // 读每个node 的id, lat and lon; { ID = GetAttribute <ulong>("id", node.Attributes); Latitude = GetAttribute <float>("lat", node.Attributes);// lat 和 lon 是在球面的,现在要转换成平面 Longitude = GetAttribute <float>("lon", node.Attributes); X = (float)MercatorProjection.lonToX(Longitude); Y = (float)MercatorProjection.latToY(Latitude); }
/// <summary> /// Creates an OsmNode with id, latitude and longitude from the given node. /// </summary> /// <param name="node">The from which the values are extracted.</param> public OsmNode(XmlNode node) { id = GetAttribute <ulong>("id", node.Attributes); latitude = GetAttribute <float>("lat", node.Attributes); longitude = GetAttribute <float>("lon", node.Attributes); x = (float)MercatorProjection.lonToX(longitude); y = (float)MercatorProjection.latToY(latitude); }
/// <summary> /// Constructor /// </summary> public NodeFactory(XmlNode node) { id = GetAttributes <ulong>("id", node.Attributes); // visible = GetAttributes<bool>("visible", node.Attributes); lat = GetAttributes <float>("lat", node.Attributes); lon = GetAttributes <float>("lon", node.Attributes); // Calculate x and y coordinates x = (float)MercatorProjection.lonToX(lon); y = (float)MercatorProjection.latToY(lat); }
/// <summary> /// Constructor. /// </summary> /// <param name="node">Xml node</param> public OsmNode(XmlNode node) { // Get the attribute values ID = GetAttribute <ulong>("id", node.Attributes); Latitude = GetAttribute <float>("lat", node.Attributes); Longitude = GetAttribute <float>("lon", node.Attributes); // Calculate the position in Unity units X = (float)MercatorProjection.lonToX(Longitude); Y = (float)MercatorProjection.latToY(Latitude); }
public void CalculateVertices(BoundingBox box) { LeftBottom = new Vector3((float)(MercatorProjection.lonToX(box.West)), 0, (float)(MercatorProjection.latToY(box.South))); LeftTop = new Vector3((float)(MercatorProjection.lonToX(box.West)), 0, (float)(MercatorProjection.latToY(box.North))); RightBottom = new Vector3((float)(MercatorProjection.lonToX(box.East)), 0, (float)(MercatorProjection.latToY(box.South))); RightTop = new Vector3((float)(MercatorProjection.lonToX(box.East)), 0, (float)(MercatorProjection.latToY(box.North))); }
public OsmBounds(XmlNode node) { MinLat = GetAttribute <float>("minlat", node.Attributes); MaxLat = GetAttribute <float>("maxlat", node.Attributes); MinLon = GetAttribute <float>("minlon", node.Attributes); MaxLon = GetAttribute <float>("maxlon", node.Attributes); float x = (float)(MercatorProjection.lonToX(MaxLon) + MercatorProjection.lonToX(MinLon)) / 2; float y = (float)(MercatorProjection.latToY(MaxLat) + MercatorProjection.latToY(MinLat)) / 2; Centre = new Vector3(x, 0, y); }
/// <summary> /// Constructs the OSMNode based on the attributes in the given XmlNode /// </summary> /// <param name="xmlNode">XmlNode with construction attributes</param> public OSMNode(XmlNode xmlNode, Vector3 mapCenter) { // OSM attributes ID = GetAttribute <ulong>("id", xmlNode.Attributes); Latitude = GetAttribute <float>("lat", xmlNode.Attributes); Longitude = GetAttribute <float>("lon", xmlNode.Attributes); // Convertion of OSM position attributes to unity positions XCoord = (float)MercatorProjection.lonToX(Longitude); YCoord = (float)MercatorProjection.latToY(Latitude); WorldCoord = new Vector3(XCoord - mapCenter.x, -mapCenter.y, YCoord - mapCenter.z); }
/// <summary> /// Sets the model to the clients GPS location. /// </summary> private void Start() { anim = gameObject.transform.GetChild(0).GetComponent <Animator>(); anim.SetBool("Grounded", true); map = GameObject.Find("Map").GetComponent <MapReader>(); x = (float)MercatorProjection.lonToX(Gps.instance.longitude); y = (float)MercatorProjection.latToY(Gps.instance.latitude); newPos = new Vector3(x, 0f, y); transform.position = newPos - map.bounds.centre; }
public Vector2 GetGPSAsUnityPosition(Vector2 gpsLocation) { double posX = MercatorProjection.lonToX(gpsLocation.x); double posY = MercatorProjection.latToY(gpsLocation.y); Vector2 mapCenter = GetUnityMapCenter(); posX -= mapCenter.x; posX /= GetZoom(); posY -= mapCenter.y; posY /= GetZoom(); return(new Vector2((float)posY, (float)posX)); }
/// <summary> /// Construct the Bounds based on the attributes given in the XmlNode /// </summary> /// <param name="xmlNode">XmlNode with construction attributes</param> public OSMBounds(XmlNode xmlNode) { // Read the OSM position attributes MinLatitude = GetAttribute <float>("minlat", xmlNode.Attributes); MaxLatitude = GetAttribute <float>("maxlat", xmlNode.Attributes); MinLongitude = GetAttribute <float>("minlon", xmlNode.Attributes); MaxLongitude = GetAttribute <float>("maxlon", xmlNode.Attributes); // Convert OSM position attributes to Unity positions float latitudeAsY = (float)((MercatorProjection.latToY(MinLatitude) + MercatorProjection.latToY(MaxLatitude)) / 2); float longitudeAsX = (float)((MercatorProjection.lonToX(MinLongitude) + MercatorProjection.lonToX(MaxLongitude)) / 2); Center = new Vector3(longitudeAsX, 0, latitudeAsY); }
/// <summary> /// Constructor. /// </summary> /// <param name="node">Xml node</param> public OsmBounds(XmlNode node) { // Get the values from the node MinLat = GetAttribute <float>("minlat", node.Attributes); MaxLat = GetAttribute <float>("maxlat", node.Attributes); MinLon = GetAttribute <float>("minlon", node.Attributes); MaxLon = GetAttribute <float>("maxlon", node.Attributes); // Create the centre location float x = (float)((MercatorProjection.lonToX(MaxLon) + MercatorProjection.lonToX(MinLon)) / 2); float y = (float)((MercatorProjection.latToY(MaxLat) + MercatorProjection.latToY(MinLat)) / 2); Centre = new Vector3(x, 0, y); }
//<bounds minlat="59.3356100" minlon="18.0864400" maxlat="59.3394800" maxlon="18.0984100"/> public OsmBounds(XmlNode node) { MinLat = GetAttribute <float>("minlat", node.Attributes); MaxLat = GetAttribute <float>("maxlat", node.Attributes); MinLon = GetAttribute <float>("minlon", node.Attributes); MaxLon = GetAttribute <float>("maxlon", node.Attributes); //calculate the center float x = (float)((MercatorProjection.lonToX(MaxLon) + MercatorProjection.lonToX(MinLon)) / 2); float y = (float)((MercatorProjection.latToY(MaxLat) + MercatorProjection.latToY(MinLat)) / 2); Centre = new Vector3(x, 0, y); //这个center point 可以设到(0,0,0),让我们所有的点都与之相关联 }
/// <summary> /// Constructor /// </summary> /// <param name="xmlNode">XML node</param> public BoundsFactory(XmlNode xmlNode) { // Get the values from the node minlat = GetAttributes <float>("minlat", xmlNode.Attributes); minlon = GetAttributes <float>("minlon", xmlNode.Attributes); maxlat = GetAttributes <float>("maxlat", xmlNode.Attributes); maxlon = GetAttributes <float>("maxlon", xmlNode.Attributes); // Calculate x and y coordinates y = (float)((MercatorProjection.latToY(minlat) + MercatorProjection.latToY(maxlat)) / 2); x = (float)((MercatorProjection.lonToX(minlon) + MercatorProjection.lonToX(maxlon)) / 2); CalculateCenter(x, y); }
public HighwayNode(XmlNode xmlNode, Vector3 MapCenter) { // OSM attributes ID = XMLHelper.GetAttribute <ulong>("id", xmlNode.Attributes); float Latitude = XMLHelper.GetAttribute <float>("lat", xmlNode.Attributes); float Longitude = XMLHelper.GetAttribute <float>("lon", xmlNode.Attributes); // Convertion of OSM position attributes to unity positions float XCoord = (float)MercatorProjection.lonToX(Longitude); float YCoord = (float)MercatorProjection.latToY(Latitude); Position = new Vector3(XCoord, 0.0f, YCoord) - MapCenter; IsParkingLotEntry = false; Neighbours = new List <HighwayNode>(); }
void Update() { if (Input.location.status == LocationServiceStatus.Running) { LocationInfo li; li = Input.location.lastData; double delta = MercatorProjection.latToY(li.latitude) - MercatorProjection.latToY(lat); transform.position += transform.forward * (float)delta * Time.deltaTime; delta = MercatorProjection.lonToX(li.longitude) - MercatorProjection.lonToX(lon); transform.position += transform.right * (float)delta * Time.deltaTime; lat = li.latitude; lon = li.longitude; } }
public OsmBound(XmlNode node) { minlat = GetAttribute <float>("minlat", node.Attributes); minlon = GetAttribute <float>("minlon", node.Attributes); maxlat = GetAttribute <float>("maxlat", node.Attributes); maxlon = GetAttribute <float>("maxlon", node.Attributes); float x = (float)((MercatorProjection.lonToX(maxlon) + MercatorProjection.lonToX(minlon))) / 2; float y = (float)((MercatorProjection.latToY(maxlat) + MercatorProjection.latToY(minlat))) / 2; float width = (float)((MercatorProjection.lonToX(maxlon) - MercatorProjection.lonToX(minlon))); float length = (float)((MercatorProjection.latToY(maxlat) - MercatorProjection.latToY(minlat))); size = new Vector3(width * 2, 600, length * 2); centre = new Vector3(x, 0, y); }
/// <summary> /// Constructor /// </summary> /// <param name="node">XML node</param> public OsmNode(XmlNode node) { TransportLines = new List <string>(); ID = GetAttribute <ulong>("id", node.Attributes); Latitude = GetFloat("lat", node.Attributes); Longitude = GetFloat("lon", node.Attributes); // Longitude and Latitude are being converted to Unity coordinates using the // Mercator projection. X = (float)MercatorProjection.lonToX(Longitude); Y = (float)MercatorProjection.latToY(Latitude); findName(node); }
// Use this for initialization void Start() { //TextAsset mapFileAsset = Resources.Load<TextAsset>(mapFile.name); // init data structures MapInfo.Nodes = new Dictionary <ulong, OSMNode>(); MapInfo.Unknown = new List <OSMWay>(); MapInfo.Roads = new List <OSMWay>(); MapInfo.Buildings = new List <OSMWay>(); MapInfo.ParkSpaces = new List <OSMWay>(); NavInfo.Nodes = new Dictionary <ulong, HighwayNode>(); // load xml map file XmlDocument xmlMap = new XmlDocument(); xmlMap.LoadXml(MapFile.text); // load different xmlNode types SetBounds(xmlMap.SelectSingleNode("/osm/bounds")); GetNodes(xmlMap.SelectNodes("/osm/node")); GetWays(xmlMap.SelectNodes("/osm/way")); // Connect and spawn Debug Displays ConnectHighwayNodes(); MarkParkingEntries(); NavDebugDisplay.SpawnNavigationNodes(NavInfo); NavDebugDisplay.SpawnParkingLotMarkers(MapInfo); NavDebugDisplay.SpawnRoads(MapInfo); NavDebugDisplay.SpawnBuildings(MapInfo); // read and convert map boundaries float minx = (float)MercatorProjection.lonToX(MapInfo.Bounds.MinLongitude); float maxx = (float)MercatorProjection.lonToX(MapInfo.Bounds.MaxLongitude); float miny = (float)MercatorProjection.latToY(MapInfo.Bounds.MinLatitude); float maxy = (float)MercatorProjection.latToY(MapInfo.Bounds.MaxLatitude); // scale the groundplane to cover the whole map area GroundPlane.transform.localScale = new Vector3((maxx - minx) / 2, 1, (maxy - miny) / 2); // signalize that the reading of the map file is done IsReady = true; }
IEnumerator MapAPICall() { float latHeight = 0.010f; float lonWidth = 0.025f; float lonMin = currentLon - lonWidth / 2, lonMax = currentLon + lonWidth / 2, latMin = currentLat - latHeight / 2, latMax = currentLat + latHeight / 2; // setting class variables for future use minX = (float)(MercatorProjection.lonToX(lonMin) - MercatorProjection.lonToX(currentLon)); maxX = (float)(MercatorProjection.lonToX(lonMax) - MercatorProjection.lonToX(currentLon)); minY = (float)(MercatorProjection.latToY(latMin) - MercatorProjection.latToY(currentLat)); maxY = (float)(MercatorProjection.latToY(latMax) - MercatorProjection.latToY(currentLat)); // longitude is "x", latitude is "y" // url bounds in form: longitude min, latitude min, longitude max, latitude max string URL = "https://overpass-api.de/api/map?bbox=" + lonMin.ToString() + "," + latMin.ToString() + "," + lonMax.ToString() + "," + latMax.ToString(); UnityWebRequest uwr = UnityWebRequest.Get(URL); String filePath = Application.persistentDataPath + "/data1.txt"; // this needs to change if (File.Exists(filePath)) { Debug.Log("File already exists! Deleting it now."); File.Delete(filePath); //map.ReadInput(filePath); //yield break; } uwr.downloadHandler = new DownloadHandlerFile(filePath); yield return(uwr.SendWebRequest()); if (uwr.isHttpError || uwr.isNetworkError) { Debug.Log(uwr.error); yield break; } else { Debug.Log("Saved data to: " + filePath); map.ReadInput(filePath); yield break; } }
private void GetOsmBoundsCentre(XmlNode node) { var MaxLat = XmlAttributeParser .GetAttribute <float>("maxlat", node.Attributes); var MinLat = XmlAttributeParser .GetAttribute <float>("minlat", node.Attributes); var MinLon = XmlAttributeParser .GetAttribute <float>("minlon", node.Attributes); var MaxLon = XmlAttributeParser .GetAttribute <float>("maxlon", node.Attributes); // Create the centre location of OSM data float x = (float)((MercatorProjection .lonToX(MaxLon) + MercatorProjection.lonToX(MinLon)) / 2); float z = (float)((MercatorProjection .latToY(MaxLat) + MercatorProjection.latToY(MinLat)) / 2); BoundsCentre = new Vector3(x, 0, z); }
public void buildNodePositionWithOffset(Vector3 offset) { float Latitude = XmlAttributeParser .GetAttribute <float>("lat", nodeData.Attributes); float Longitude = XmlAttributeParser .GetAttribute <float>("lon", nodeData.Attributes); // Debug.Log(Latitude + " " + Longitude); float X = (float)MercatorProjection .lonToX(Longitude) - offset.x; float Z = (float)MercatorProjection .latToY(Latitude) - offset.z; float Y = 0; node.setNodePosition(new Vector3(X, Y, Z)); // Debug.Log(new Vector3(X, Y, Z)); }
/// <summary> /// Load the OpenMap data resource file. /// </summary> /// <param name="resourceFile">Path to the resource file. The file must exist.</param> public void Read(string resourceFile) { nodes = new Dictionary <ulong, OsmNode>(); ways = new List <OsmWay>(); var xmlText = File.ReadAllText(resourceFile); XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlText); SetBounds(doc.SelectSingleNode("/osm/bounds")); GetNodes(doc.SelectNodes("/osm/node")); GetWays(doc.SelectNodes("/osm/way")); float minx = (float)MercatorProjection.lonToX(bounds.MinLon); float maxx = (float)MercatorProjection.lonToX(bounds.MaxLon); float miny = (float)MercatorProjection.latToY(bounds.MinLat); float maxy = (float)MercatorProjection.latToY(bounds.MaxLat); }
public MapNode(XmlNode node, OSMBounds bounds) { latitude = GetAttribute <float>("lat", node.Attributes); id = GetAttribute <long>("id", node.Attributes); longitude = GetAttribute <float>("lon", node.Attributes); X = MercatorProjection.lonToX(longitude); Y = MercatorProjection.latToY(latitude); XmlNodeList tags = node.SelectNodes("tag"); foreach (XmlNode tag in tags) { string key = GetAttribute <string>("k", tag.Attributes); if (key == "natural") { isTree = true; } } }
public OSMNode(XmlNode node) { ID = GetAttribute <ulong>("id", node.Attributes); Latitude = GetAttribute <float>("lat", node.Attributes); Longitude = GetAttribute <float>("lon", node.Attributes); //string Val = GetAttribute<string>("v", node.Attributes); // Transfer this for waypoints? X = (float)MercatorProjection.lonToX(Longitude); Y = (float)MercatorProjection.latToY(Latitude); XmlNodeList tags = node.SelectNodes("tag"); foreach (XmlNode t in tags) { string val = GetAttribute <string>("v", t.Attributes); //Debug.Log(val); if (val == "traffic_signals") { IsStreetLight = true; } } }
/// <summary> /// Constructor /// </summary> /// <param name="node">XML node parameter</param> public OsmBounds(XmlNode node) { MinLat = GetFloat("minlat", node.Attributes); MaxLat = GetFloat("maxlat", node.Attributes); MinLon = GetFloat("minlon", node.Attributes); MaxLon = GetFloat("maxlon", node.Attributes); // Longitude and Latitude are being converted to Unity coordinates using the Mercator-Projection. float x = (float)((MercatorProjection.lonToX(MaxLon) + MercatorProjection.lonToX(MinLon)) / 2); float y = (float)((MercatorProjection.latToY(MaxLat) + MercatorProjection.latToY(MinLat)) / 2); // Reference for manually generated objects position. Centre = new Vector3(x, 0, y); string x_2 = ((MaxLat + MinLat) / 2).ToString(CultureInfo.InvariantCulture); string y_2 = ((MaxLon + MinLon) / 2).ToString(CultureInfo.InvariantCulture); // Reference for Mapbox objects position. BuildingCentre = x_2 + ", " + y_2; // Reference for Mapbox objects scale. SetMapboxFactor(MaxLon, MinLon, MaxLat, MinLat); }