Skip to content

Commit

Permalink
Merge pull request #67 from linkedconnections/development
Browse files Browse the repository at this point in the history
v2.0.4
  • Loading branch information
julianrojas87 authored Sep 27, 2022
2 parents 8379e40 + a531a8d commit 658f694
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 223 deletions.
10 changes: 3 additions & 7 deletions bin/gtfsrt2lc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,16 @@ if (!program.static) {
process.exit();
}

if (!program.urisTemplate) {
console.error('Please provide path to a template file');
console.error("GTFS-RT to linked connections converter use --help to discover how to use it");
process.exit();
}

if (!program.store) {
program.store = 'MemStore';
}

// Load URIs template
let template = null;
try {
template = JSON.parse(fs.readFileSync(program.urisTemplate, 'utf8'));
if (program.urisTemplate) {
template = JSON.parse(fs.readFileSync(program.urisTemplate, 'utf8'));
}
} catch (err) {
console.error('Please provide a valid path to a template file');
console.error("GTFS-RT to linked connections converter use --help to discover how to use it");
Expand Down
10 changes: 6 additions & 4 deletions lib/Connections2CSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ class Connections2CSV extends Transform {
this._headerStreamed = false;
}

_transform(lc, encoding, done) {
_transform(conn, encoding, done) {
if (!this.headerStreamed) {
this.headerStreamed = true;
done(null, '"id","type",departureStop","departureTime","departureDelay",arrivalStop","arrivalTime","arrivalDelay","direction",trip","route"\n');
done(null, '"type",departureStop","departureTime","departureDelay",arrivalStop","arrivalTime","arrivalDelay","headsign",trip","route"\n');
} else {
let csv = lc['@id'] + ',' + 'http://semweb.mmlab.be/ns/linkedconnections#' + lc['@type'] + ',' + lc['departureStop'] + ',' + lc['departureTime'] + ',' + lc['departureDelay'] + ',' + lc['arrivalStop']
+ ',' + lc['arrivalTime'] + ',' + lc['arrivalDelay'] + ',' + lc['direction'] + ',' + lc['trip'] + ',' + lc['route'] + '\n';
const csv = conn['type'] + ',' + conn['departureStop']['stop_name'] + ','
+ conn['departureTime'].toISOString() + ',' + conn['departureDelay'] + ',' + conn['arrivalStop']['stop_name'] + ','
+ conn['arrivalTime'].toISOString() + ',' + conn['arrivalDelay'] + ',' + conn['headsign'] + ','
+ conn['trip']['trip_id'] + ',' + conn['route']['route_long_name'] + '\n';
done(null, csv);
}
}
Expand Down
58 changes: 41 additions & 17 deletions lib/Connections2JSONLD.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { Transform } = require('stream');
const uri_templates = require('uri-templates');
const Utils = require('./Utils');

class Connections2JSONLD extends Transform {
constructor(streamContext) {
constructor(streamContext, templates) {
super({ objectMode: true });
this._streamContext = streamContext || false;
this._contextStreamed = false;
this._templates = templates;
this._context = {
"@context": {
"xsd": "http://www.w3.org/2001/XMLSchema#",
Expand Down Expand Up @@ -50,29 +53,46 @@ class Connections2JSONLD extends Transform {
}
}

_transform(lc, encoding, done) {
_transform(conn, encoding, done) {
if (this._streamContext && !this.contextStreamed) {
this.contextStreamed = true;
this.push(this.context);
}

let temp = {
"@id": lc['@id'],
"@type": lc['@type'],
"departureStop": lc['departureStop'],
"arrivalStop": lc['arrivalStop'],
"departureTime": lc['departureTime'],
"arrivalTime": lc['arrivalTime'],
"departureDelay": lc['departureDelay'],
"arrivalDelay": lc['arrivalDelay'],
"direction": lc['direction'],
"gtfs:trip": lc['trip'],
"gtfs:route": lc['route'],
"gtfs:pickupType": lc['gtfs:pickupType'],
"gtfs:dropOffType": lc['gtfs:dropOffType']
// Predefined URI templates
const stopTemplate = uri_templates(this.templates['stop']);
const routeTemplate = uri_templates(this.templates['route']);
const tripTemplate = uri_templates(this.templates['trip']);
const connectionTemplate = uri_templates(this.templates['connection']);

// Resolve values for URIs
const departureStopURI = Utils.resolveURI(stopTemplate, conn, this.templates['resolve'], "departureStop");
const arrivalStopURI = Utils.resolveURI(stopTemplate, conn, this.templates['resolve'], "arrivalStop");
const routeURI = Utils.resolveURI(routeTemplate, conn, this.templates['resolve']);
const tripURI = Utils.resolveURI(tripTemplate, conn, this.templates['resolve']);
const connectionURI = Utils.resolveURI(connectionTemplate, conn, this.templates['resolve']);
// Determine Pick Up & Drop Off types
const pickupType = Utils.resolveScheduleRelationship(conn['pickup_type']);
const dropOffType = Utils.resolveScheduleRelationship(conn['drop_off_type']);

// Build LC
const lc = {
"@id": connectionURI,
"@type": conn.type,
"departureStop": departureStopURI,
"arrivalStop": arrivalStopURI,
"departureTime": conn.departureTime.toISOString(),
"arrivalTime": conn.arrivalTime.toISOString(),
"departureDelay": conn.departureDelay,
"arrivalDelay": conn.arrivalDelay,
"gtfs:trip": tripURI,
"gtfs:route": routeURI,
"direction": conn.trip['trip_headsign'],
"gtfs:pickupType": pickupType,
"gtfs:dropOffType": dropOffType
}

done(null, temp);
done(null, lc);
}

get contextStreamed() {
Expand All @@ -86,6 +106,10 @@ class Connections2JSONLD extends Transform {
get context() {
return this._context;
}

get templates() {
return this._templates;
}
}

module.exports = Connections2JSONLD;
99 changes: 58 additions & 41 deletions lib/Connections2Triples.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,110 @@ const { Transform } = require('stream');
const N3 = require('n3');
const { DataFactory } = N3;
const { namedNode, literal, quad } = DataFactory;
const uri_templates = require('uri-templates');
const Utils = require('./Utils');

class Connections2Triples extends Transform {
constructor() {
constructor(templates) {
super({ objectMode: true });
this._templates = templates;
}

_transform(lc, encoding, done) {
if (lc['@type'] === 'Connection') {
_transform(conn, encoding, done) {
// Predefined URI templates
const stopTemplate = uri_templates(this.templates['stop']);
const routeTemplate = uri_templates(this.templates['route']);
const tripTemplate = uri_templates(this.templates['trip']);
const connectionTemplate = uri_templates(this.templates['connection']);

// Resolve values for URIs
const departureStopURI = Utils.resolveURI(stopTemplate, conn, this.templates['resolve'], "departureStop");
const arrivalStopURI = Utils.resolveURI(stopTemplate, conn, this.templates['resolve'], "arrivalStop");
const routeURI = Utils.resolveURI(routeTemplate, conn, this.templates['resolve']);
const tripURI = Utils.resolveURI(tripTemplate, conn, this.templates['resolve']);
const connectionURI = Utils.resolveURI(connectionTemplate, conn, this.templates['resolve']);
// Determine Pick Up & Drop Off types
const pickupType = Utils.resolveScheduleRelationship(conn['pickup_type']);
const dropOffType = Utils.resolveScheduleRelationship(conn['drop_off_type']);

if (conn['type'] === 'Connection') {
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#Connection')));
} else {
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#CancelledConnection')));
}

this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#departureStop'),
namedNode(lc['departureStop'])));
namedNode(departureStopURI)));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#arrivalStop'),
namedNode(lc['arrivalStop'])));
namedNode(arrivalStopURI)));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#departureTime'),
literal(lc['departureTime'], namedNode('http://www.w3.org/2001/XMLSchema#dateTime'))));
literal(conn['departureTime'].toISOString(), namedNode('http://www.w3.org/2001/XMLSchema#dateTime'))));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#arrivalTime'),
literal(lc['arrivalTime'], namedNode('http://www.w3.org/2001/XMLSchema#dateTime'))));
literal(conn['arrivalTime'].toISOString(), namedNode('http://www.w3.org/2001/XMLSchema#dateTime'))));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#departureDelay'),
literal(lc['departureDelay'], namedNode('http://www.w3.org/2001/XMLSchema#integer'))));
literal(conn['departureDelay'], namedNode('http://www.w3.org/2001/XMLSchema#integer'))));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://semweb.mmlab.be/ns/linkedconnections#arrivalDelay'),
literal(lc['arrivalDelay'], namedNode('http://www.w3.org/2001/XMLSchema#integer'))));
literal(conn['arrivalDelay'], namedNode('http://www.w3.org/2001/XMLSchema#integer'))));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://vocab.gtfs.org/terms#headsign'),
literal(lc['direction'], namedNode('http://www.w3.org/2001/XMLSchema#string'))));
literal(conn['headsign'], namedNode('http://www.w3.org/2001/XMLSchema#string'))));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://vocab.gtfs.org/terms#trip'),
namedNode(lc['trip'])));
namedNode(tripURI)));
this.push(
quad(
namedNode(lc['@id']),
namedNode(connectionURI),
namedNode('http://vocab.gtfs.org/terms#route'),
namedNode(lc['route'])));

if (lc['gtfs:dropOffType'] && lc['gtfs:dropOffType'] !== null) {
this.push(
quad(
namedNode(lc['@id']),
namedNode('http://vocab.gtfs.org/terms#dropOffType'),
namedNode(lc['gtfs:dropOffType'])
));
}

if (lc['gtfs:pickupType'] && lc['gtfs:pickupType'] !== null) {
this.push(
quad(
namedNode(lc['@id']),
namedNode('http://vocab.gtfs.org/terms#pickupType'),
namedNode(lc['gtfs:pickupType'])
));
}
namedNode(routeURI)));
this.push(
quad(
namedNode(connectionURI),
namedNode('http://vocab.gtfs.org/terms#dropOffType'),
namedNode(dropOffType)
));
this.push(
quad(
namedNode(connectionURI),
namedNode('http://vocab.gtfs.org/terms#pickupType'),
namedNode(pickupType)
));

done(null);
}

get templates() {
return this._templates;
}
}

module.exports = Connections2Triples;
Loading

0 comments on commit 658f694

Please sign in to comment.