Skip to content

Commit

Permalink
Load easting / northings into the database and use them to produce co…
Browse files Browse the repository at this point in the history
…ordinates (#88)

* import easting and northing

* use easting and northing to generate coordinates in GTFS

---------

Co-authored-by: Michael Tsang <michael.tsang@jnction.co.uk>
  • Loading branch information
miklcct and Michael Tsang committed May 3, 2024
1 parent 8448624 commit 949ac4c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
4 changes: 4 additions & 0 deletions config/timetable/file/MSN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const physicalStation = new FixedWidthRecord(
"tiploc_code": new TextField(36, 7),
"crs_reference_code": new TextField(43, 3, true),
"crs_code": new TextField(49, 3, true),
// The actual easting is (easting - 10000) * 100
"easting": new IntField(52, 5, true),
// The actual northing is (northing - 60000) * 100
"northing": new IntField(58, 5, true),
"minimum_change_time": new IntField(63, 2, false, [])
},
["crs_code"]
Expand Down
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
},
"homepage": "https://github.com/open-track/dtd2mysql#readme",
"dependencies": {
"@types/proj4": "^2.5.5",
"adm-zip": "^0.5.10",
"byline": "^5.0.0",
"csv-write-stream": "^2.0.0",
"fs-extra": "^11.1.1",
"memoized-class-decorator": "^1.6.1",
"moment": "^2.29.4",
"mysql2": "^3.6.1",
"proj4": "^2.11.0",
"ssh2": "^1.14.0",
"stream-to-promise": "^3.0.0"
},
Expand Down
20 changes: 16 additions & 4 deletions src/gtfs/repository/CIFRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import * as proj4 from 'proj4';
import {DatabaseConnection} from "../../database/DatabaseConnection";
import {Transfer} from "../file/Transfer";
import {CRS, Stop} from "../file/Stop";
Expand All @@ -20,7 +21,9 @@ export class CIFRepository {
private readonly db: DatabaseConnection,
private readonly stream,
private readonly stationCoordinates: StationCoordinates
) {}
) {
proj4.defs('EPSG:27700', '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs');
}

/**
* Return the interchange time between each station
Expand All @@ -43,7 +46,7 @@ export class CIFRepository {
* Return all the stops with some configurable long/lat applied
*/
public async getStops(): Promise<Stop[]> {
const [results] = await this.db.query<Stop[]>(`
const [results] = await this.db.query<(Stop & {easting : number, northing : number})>(`
SELECT
crs_code AS stop_id,
tiploc_code AS stop_code,
Expand All @@ -56,13 +59,22 @@ export class CIFRepository {
NULL AS location_type,
NULL AS parent_station,
IF(POSITION("(CIE" IN station_name), "Europe/Dublin", "Europe/London") AS stop_timezone,
0 AS wheelchair_boarding
0 AS wheelchair_boarding,
easting,
northing
FROM physical_station WHERE crs_code IS NOT NULL
GROUP BY crs_code
`);

// overlay the long and latitude values from configuration
return results.map(stop => Object.assign(stop, this.stationCoordinates[stop.stop_id]));
return results.map(stop => {
const [stop_lon, stop_lat] = proj4('EPSG:27700', 'EPSG:4326', [(stop.easting - 10000) * 100, (stop.northing - 60000) * 100]);
stop.stop_lon = stop_lon;
stop.stop_lat = stop_lat;
delete stop.easting;
delete stop.northing;
return Object.assign(stop, this.stationCoordinates[stop.stop_id]);
});
}

/**
Expand Down

0 comments on commit 949ac4c

Please sign in to comment.