Skip to content

Commit

Permalink
Merge pull request #749 from sweiland-openrails/map_in_browser
Browse files Browse the repository at this point in the history
OpenRailway Map
  • Loading branch information
cjakeman committed Jan 10, 2023
2 parents 084d232 + db5764c commit d2e6821
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 4 deletions.
22 changes: 20 additions & 2 deletions Source/Orts.Simulation/Common/WorldLatLon.cs
Expand Up @@ -45,8 +45,8 @@ public class WorldLatLon

// The upper left corner of the Goode projection is ul_x,ul_y
// The bottom right corner of the Goode projection is -ul_x,-ul_y
int ul_x = -20015000; // -180 deg in Goode projection
int ul_y = 8673000; // +90 deg lat in Goode projection
int ul_x = -20013965; // -180 deg in Goode projection
int ul_y = 8674008; // +90 deg lat in Goode projection

// Offsets to convert Goode raster coordinates to MSTS world tile coordinates
int wt_ew_offset = -16385;
Expand Down Expand Up @@ -287,4 +287,22 @@ static double Adjust_Lon(double value)
}

}

/// <summary>
/// Class to store the latitude and longitude values of a point on the map
/// </summary>
public class LatLon
{
private readonly float _lat;
private readonly float _lon;

public LatLon(float lat, float lon)
{
this._lat = lat;
this._lon = lon;
}

public float Lat => _lat;
public float Lon => _lon;
}
}
20 changes: 20 additions & 0 deletions Source/Orts.Simulation/Simulation/RollingStocks/TrainCar.cs
Expand Up @@ -34,6 +34,7 @@
//#define DEBUG_BRAKE_SLIDE

using Microsoft.Xna.Framework;
using Orts.Common;
using Orts.Formats.Msts;
using Orts.Parsers.Msts;
using Orts.Simulation.AIs;
Expand Down Expand Up @@ -3348,6 +3349,25 @@ public virtual void UpdateHeatLoss()

TotalCarCompartmentHeatLossW = HeatLossTransmissionW + HeatLossInfiltrationW + HeatLossVentilationW;
}

/// <summary>
/// Determine latitude/longitude position of the current TrainCar
/// </summary>
public LatLon GetLatLon()
{
double lat = 0;
double lon = 0;

var playerLocation = WorldPosition.WorldLocation;

new WorldLatLon().ConvertWTC(playerLocation.TileX, playerLocation.TileZ, playerLocation.Location, ref lat, ref lon);

LatLon latLon = new LatLon(
MathHelper.ToDegrees((float)lat),
MathHelper.ToDegrees((float)lon));

return (latLon);
}
}

public class WheelAxle : IComparer<WheelAxle>
Expand Down
58 changes: 58 additions & 0 deletions Source/RunActivity/Viewer3D/WebServices/Web/Map/index.html
@@ -0,0 +1,58 @@
<!DOCTYPE HTML>
<!--
COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014 by the Open Rails project.
This file is part of Open Rails.
Open Rails is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Open Rails is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
-->
<html lang="en">
<head>
<title>OR: Map</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.2/dist/leaflet.css"
integrity="sha256-sA+zWATbFveLLNqWO2gtiw3HL/lh1giY/Inf1BJ0z14="
crossorigin="">

<script src="https://unpkg.com/jquery@3.6.0/dist/jquery.min.js"
integrity="sha384-vtXRMe3mGCbOeY7l30aIg8H9p3GdeSe4IFlP6G8JMa7o7lXvnz3GFKzPxzJdPfGK"
crossorigin="anonymous"></script>

<script src="https://unpkg.com/leaflet@1.9.2/dist/leaflet.js"
integrity="sha256-o9N1jGDZrf5tS+Ft4gbIK7mYMipq9lqpVJ91xHSyKhg="
crossorigin=""></script>

<style type="text/css">
html, body {
height: 99%;
width: 99%;
margin: 10px;
padding: 0;
}

#map {
height: 99%;
min-height: 100%;
}
</style>
</head>

<body onload="setInterval (ApiMap, 500)">
<div id="map"></div>
<script src="index.js"></script>
</body>
</html>

79 changes: 79 additions & 0 deletions Source/RunActivity/Viewer3D/WebServices/Web/Map/index.js
@@ -0,0 +1,79 @@
// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
//

const hr = new XMLHttpRequest;
const httpCodeSuccess = 200;
const xmlHttpRequestCodeDone = 4;

var locomotiveMarker;
var map;
var latLonPrev = [0, 0];

function MapInit(latLon) {

map = L.map('map').setView(latLon, 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: 'Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

L.tileLayer('https://{s}.tiles.openrailwaymap.org/standard/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: ' | Map style: &copy; <a href="https://www.OpenRailwayMap.org">OpenRailwayMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
}).addTo(map);
}

function ApiMap() {

hr.open("GET", "/API/MAP/", true);
hr.send();
hr.onreadystatechange = function () {
if (this.readyState == xmlHttpRequestCodeDone && this.status == httpCodeSuccess) {

let latLonObj = JSON.parse(hr.responseText);

if (latLonObj != null) {

let latLon = [latLonObj.Lat, latLonObj.Lon];

if (typeof locomotiveMarker == 'undefined') {
// init
MapInit(latLon);
locomotiveMarker = L.marker(
latLon,
{ icon: myIcon }
).addTo(map);
} else {
if ((latLon[0] != latLonPrev[0]) || (latLon[1] != latLonPrev[1])) {
// changed
map.panTo(latLon);
locomotiveMarker.setLatLng(latLon).update();
}
}
latLonPrev[0] = latLon[0];
latLonPrev[1] = latLon[1];
}
}
}
}

var myIcon = L.icon({
iconUrl: 'locomotive.png',
iconSize: [29, 24],
iconAnchor: [9, 21],
})
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Source/RunActivity/Viewer3D/WebServices/Web/index.html
Expand Up @@ -37,6 +37,7 @@ <h1>Open Rails - Web Interface</h1>
<li><a href="/HUD/index.html">Head Up Display (Alt+F5)</a></li>
<li><a href="/CabControls/index.html">Cab Controls</a></li>
<li><a href="/Time/index.html">Time</a></li>
<li><a href="/Map/index.html">Map</a></li>
<li><a href="/APISample/index.html">API Sample</a></li>
</ul>
</body>
Expand Down
11 changes: 9 additions & 2 deletions Source/RunActivity/Viewer3D/WebServices/WebServer.cs
Expand Up @@ -26,8 +26,10 @@
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Orts.Common;
using Orts.Simulation.Physics;
using Orts.Viewer3D.RollingStock;
using ORTS.Common;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -112,13 +114,13 @@ internal class ORTSApiController : WebApiController
/// The Viewer to serve train data from.
/// </summary>
private readonly Viewer Viewer;
protected WorldLocation cameraLocation = new WorldLocation();

public ORTSApiController(Viewer viewer)
{
Viewer = viewer;
}


#region /API/APISAMPLE
public struct Embedded
{
Expand Down Expand Up @@ -254,5 +256,10 @@ IEnumerable<string> GetValues()
[Route(HttpVerbs.Get, "/TIME")]
public double Time() => Viewer.Simulator.ClockTime;
#endregion

#region /API/MAP
[Route(HttpVerbs.Get, "/MAP")]
public LatLon LatLon() => Viewer.Simulator.PlayerLocomotive.GetLatLon();
#endregion
}
}
}

0 comments on commit d2e6821

Please sign in to comment.