Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Paikkatietotyöpaja2011 kovanen
1. Presenting trains in real-time
in OpenLayers via GeoRSS
Janne Kovanen
Finnish Geodetic Institute
Department of Geoinformatics and Cartography
GI Norden June 8–9, 2011, Turku, Finland
2. GeoRSS
An geographical syntax in RSS feeds (XML)
The coordinate reference system is WGS84
A coordinate conversion to a plane coordinate
reference system is mostly required
Notice! In web browsers you typically see the
interpretation of a RSS feed reader
view-source:http://188.117.35.14/TrainRSS/
TrainService.svc/trainInfo?train=P704
Updating from an app requires a proxy
Several exists (web )
Workshop on Open Location Data 2011 Finnish Geodetic Institute 2
janne.kovanen@fgi.fi
3. GeoRSS and GeoRSS-Simple
For Atom 1.0, RSS 2.0 and RSS 1.0
GML content
<georss:where>
<gml:Point>
<gml:pos>45.256 -71.92</gml:pos>
</gml:Point>
</georss:where>
Too complex? Simpler alternative:
<georss:point>60.1 54.2</georss:point>
<georss:line>45.256 -110.45 46.46 -109.48 43.84 -109.86</
georss:line>
<georss:polygon>45.256 -110.45 46.46 -109.48 43.84 -109.86
45.256 -110.45</georss:polygon>
Workshop on Open Location Data 2011 Finnish Geodetic Institute 3
janne.kovanen@fgi.fi
4. OpenLayers and GeoRSS
OpenLayers.Layer.GeoRSS
Extends a layer for markers
As such OL downloads the feed only ones
Features are stored as OpenLayers.Feature
Does not contains methods for handling features!
OpenLayers.Format.GeoRSS
Format for reading GeoRSS
Only reads such as a title, description and geometry
Features are handled as OpenLayers.Feature.Vector
OpenLayers.LonLat vs. OpenLayers.Geometry
Workshop on Open Location Data 2011 Finnish Geodetic Institute 4
janne.kovanen@fgi.fi
5. “Trains on a map” interface
All trains
<rss version="2.0">
<channel xmlns:georss="http://www.georss.org/georss">
<title>Train Feed</title>
<description>All trains feed</description>
<lastBuildDate>Tue, 20 Sep 2011 15:00:35 +0300</lastBuildDate>
<category>Train System</category>
<item>
<guid isPermaLink="false">H355</guid>
<category>1</category><title>H 355</title>
<description>Summary</description>
<pubDate>Tue, 20 Sep 2011 15:00:35 +0300</pubDate>
<georss:point>60.6302800000001 26.8929000000001</georss:point>
<from>KTS</from><to>KV</to>
<status>1</status><dir>345</dir>
</item>
...
</channel>
</rss>
http://www.apps4finland.fi/fi/data/junat-kartalla-palvelun-rajapinta/
Workshop on Open Location Data 2011 Finnish Geodetic Institute 5
janne.kovanen@fgi.fi
6. Demo 1: Static one time feed
// Use some proxy. Add the IP address to the proxy file before this
OpenLayers.ProxyHost = “/cgi-bin/proxy.cgi?url=”;
// Initialize OpenLayers
function init() {
var map = new OpenLayers.Map('map');
// Create the background map layers etc (the other demo)
// Create the train layer
var newTrainLayer = new OpenLayers.Layer.GeoRSS(
“Train Feed”,
“http://188.117.35.14/TrainRSS/TrainService.svc/AllTrains”,
{projection: new OpenLayers.Projection(“EPSG:4326”)});
map.addLayer(newTrainLayer);
// Add controls etc.
}
Workshop on Open Location Data 2011 Finnish Geodetic Institute 6
janne.kovanen@fgi.fi
7. “Trains on a map” interface
Detailed information on trains
<rss version="2.0”>
<channel xmlns:georss="http://www.georss.org/georss">
<title>P 704</title>
<description>Train information feed for train P 704</description>
<lastBuildDate>Fri, 16 Sep 2011 13:57:05 +0300</lastBuildDate>
<category>1</category>
<georss:point>62.2989900000001 27.1673200000001</georss:point>
<speed>33</speed>
<heading>355</heading>
<startStation>OL</startStation>
<endStation>KUO</endStation>
<status>1</status>
<reasonCode/>
<lateness>0</lateness>
…
</channel>
</rss>
Workshop on Open Location Data 2011 Finnish Geodetic Institute 7
janne.kovanen@fgi.fi
8. “Trains on a map” interface
Detailed information on trains
<rss version="2.0”>
<channel xmlns:georss="http://www.georss.org/georss">
<title>P 704</title>
<description>Train information feed for train P 704</description>
<lastBuildDate>Fri, 16 Sep 2011 13:57:05 +0300</lastBuildDate>
<category>1</category>
<georss:point>62.2989900000001 27.1673200000001</georss:point>
<speed>33</speed>
<heading>355</heading>
<startStation>OL</startStation>
<endStation>KUO</endStation>
<status>1</status>
<reasonCode/>
<lateness>0</lateness>
…
</channel>
</rss>
Workshop on Open Location Data 2011 Finnish Geodetic Institute 8
janne.kovanen@fgi.fi
10. Demo 2: Make the trains move
// Add the trains layer in the init() through a function call
function init() {
// …
updateTrains();
}
function updateTrains() {
// Do not yet show the trains layer in the layer switcher
var newTrainLayer = new OpenLayers.Layer.GeoRSS(
“Train Feed”,
“http://188.117.35.14/TrainRSS/TrainService.svc/AllTrains”,
{projection: new OpenLayers.Projection(“EPSG:4326”),
displayInLayerSwitcher: false});
map.addLayer(newTrainLayer);
// Add a listener for loading
newTrainLayer.events.register("loadend", self,
function(e) {trainsLoaded(e)});
}
Workshop on Open Location Data 2011 Finnish Geodetic Institute 10
janne.kovanen@fgi.fi
11. … Demo 2: Make the trains move
// The update interval in milliseconds
var trainSetUpdateTimeout = 2000;
function trainsLoaded(event) {
if(event.object == null)
return;
if(trainsLayer != null && trainsLayer != event.object) {
map.removeLayer(trainsLayer, false);
}
trainsLayer = event.object;
// Now show the trains layer on the map and in the layer switcher
trainsLayer.setVisibility(true);
trainsLayer.displayInLayerSwitcher = true;
// Timed update
self.setTimeout('updateTrains()', trainSetUpdateTimeout);
}
Example 2
Workshop on Open Location Data 2011 Finnish Geodetic Institute 11
janne.kovanen@fgi.fi
12. Demo 3: Extending OpenLayers
var TrainRSS = OpenLayers.Class(OpenLayers.Format.XML, {
initialize: function(options) {
OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
},
…
}
var TrainFeature = OpenLayers.Class(OpenLayers.Feature, {
guid: "”, type: 0, speed: 0, heading: 0, status: 0,
initialize: function(layer, lonlat, data) {
OpenLayers.Feature.prototype.initialize.apply(this, [layer, lonlat, data]);
},
…
}
Workshop on Open Location Data 2011 Finnish Geodetic Institute 12
janne.kovanen@fgi.fi
13. … Demo 3: Extending OpenLayers
var TrainsRSS = OpenLayers.Class(OpenLayers.Format.GeoRSS, {
layer: null,
initialize: function(layer, options) {
OpenLayers.Format.GeoRSS.prototype.initialize.apply(this, [options]);
this.layer = layer;
},
…
},
var TrainsLayer = OpenLayers.Class(OpenLayers.Layer.GeoRSS, {
initialize: function(name, location, options) {
OpenLayers.Layer.GeoRSS.prototype.initialize.apply(this,
[name, location, options]);
},
…
},
Workshop on Open Location Data 2011 Finnish Geodetic Institute 13
janne.kovanen@fgi.fi
14. “Trains on a map” interface
Train station information
<rss version="2.0">
<channel xmlns:georss="http://www.georss.org/georss">
<title />
<description>Station information feed for station KML</description>
<lastBuildDate>Thu, 03 Feb 2011 17:18:42 +0200</lastBuildDate>
<category>Train System</category>
<item>
<guid isPermaLink="false">P274</guid>
<category>1</category>
<title>P 274</title>
<description>Summary</description>
<pubDate>Thu, 03 Feb 2011 17:18:42 +0200</pubDate>
<georss:point>0 0</georss:point>
…
</item>
…
</channel>
</rss>
http://www.apps4finland.fi/fi/data/junat-kartalla-palvelun-rajapinta/
Workshop on Open Location Data 2011 Finnish Geodetic Institute 14
janne.kovanen@fgi.fi