-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeGeoJsons.js
More file actions
90 lines (78 loc) · 3.06 KB
/
makeGeoJsons.js
File metadata and controls
90 lines (78 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict'
console.time('Make JSONs')
const fs = require('fs')
const csv = require('csv-parse/lib/sync')
const write = require('./utils/write')
const turf = require('@turf/turf')
const make = require('./utils/make')
const path = './gtfs/'
const outPath = `./geojson`
if (!fs.statSync(outPath)) fs.mkdirSync(outPath)
const shapesFile = 'shapes.txt'
const routesFile = 'routes.txt'
const tripsFile = 'trips.txt'
/* read the csvs from fs */
let shapes = fs.readFileSync(path + shapesFile, 'utf8')
let routes = fs.readFileSync(path + routesFile, 'utf8')
let trips = fs.readFileSync(path + tripsFile, 'utf8')
/* turn csv string to js objects */
shapes = csv(shapes, {columns: true})
routes = csv(routes, {columns: true})
trips = csv(trips, {columns: true})
/* Maps */
let shapesMap = make.shapesMap(shapes)
let routesMap = make.routesMap(routes)
let tripsMap = make.tripsMap(trips)
let tripsFreqs = make.tripsFreqNest(trips)
let mostFreqTrips = make.mostFrequentTrips(tripsFreqs)
let routeShapes = new Map()
mostFreqTrips.forEach(function (route) {
route.values.forEach(function (service) {
service.values.forEach(function (trip) {
let identifier = trip.shape_id // route.route_id
if (!routeShapes.has(identifier)) {
let props = {}
Object.assign(props, routesMap.get(route.route_id))
let headsign = tripsMap.get(trip.shape_id).headsign
props.services = [[service.service_id, trip.direction_id, headsign]]
let geo = shapesMap.get(trip.shape_id)
routeShapes.set(identifier, {route: route.route_id, props: props, geo: geo})
} else {
let temp = routeShapes.get(identifier)
temp.props.services.push([service.service_id, trip.direction_id, tripsMap.get(trip.shape_id).headsign])
routeShapes.set(identifier, temp)
}
})
})
})
let routesTrips = new Map()
routeShapes.forEach(function (data, shapeid) {
let routeDirection = `${data.route}-${data.props.services[0][1]}`
let props = {
shortName: data.props.shortName,
longName: data.props.longName,
route: data.route,
headsign: data.props.services[0][2],
direction: data.props.services[0][1],
service_ids: data.props.services.map(d => d[0]),
busType: make.busType(data.props.shortName),
routeDirection: routeDirection
}
let geoJson = turf.lineString(data.geo, props)
if (props.shortName.includes('/')) { props.shortName = props.shortName.replace(/\//, '-') }
write(`${outPath}/${props.shortName}-${props.direction}.geo.json`, geoJson)
routesTrips.set(routeDirection, props.headsign)
})
let lines = make.lineList(routes, routesTrips)
write('./src/lines.json', lines)
console.timeEnd('Make JSONs')
// writeMapsToDisk()
function writeMapsToDisk () {
write('fooshapesMap.json', Array.from(shapesMap))
write('fooroutesMap.json', Array.from(routesMap))
write('footripsMap.json', Array.from(tripsMap))
write('footripsFreqs.json', Array.from(tripsFreqs))
write('foomostFreqTrips.json', Array.from(mostFreqTrips))
write('foorouteShapes.json', Array.from(routeShapes))
write('fooroutesTrips.json', Array.from(routesTrips))
}