Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
arindam1993 committed Jul 25, 2024
1 parent e3d59cc commit 47fcfd5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
container: 'map', // container id
style: 'https://api.maptiler.com/maps/basic-v2/style.json?key=WT4VsZ6xeluYL5zzsbwL', // style URL
center: [0, 0], // starting position [lng, lat]
zoom: 1 // starting zoom
zoom: 1, // starting zoom
showTileBoundaries: true,
});

map.showTileBoundaries = true;

map.on('load', () => {
map.addSource('routes-tiles', {
type: 'vector',
Expand Down
22 changes: 5 additions & 17 deletions src/generateLocalTransitBounds.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
const { createReadStream, createWriteStream } = require('node:fs');
const { writeFile, mkdtemp, rm } = require('node:fs/promises');
const { createReadStream } = require('node:fs');
const { writeFile, mkdtemp } = require('node:fs/promises');
const turfConvex = require('@turf/convex').default;
const turfBuffer = require('@turf/buffer');
const turfCenterOfMass = require('@turf/center-of-mass').default;
const bbox = require('@turf/bbox').default;
const unzipper = require("unzipper");
const { resolve, join, basename} = require("path");
const { resolve, join } = require("path");
const { tmpdir } = require('node:os');
const { filterRouteIds, filterTripIds, getInterestingStopIds, getInterestingStopsAsGeoJsonPoints } = require('./gtfs-helpers');
const { filterRouteIds, filterTripIds, getInterestingStopIds, getInterestingStopsAsGeoJsonPoints, unzipGtfs } = require('./gtfs-helpers');

const requiredGTFSFiles = new Set(['routes.txt', 'trips.txt', 'stop_times.txt', 'stops.txt']);
const ENV_FILTERED_AGENCY_IDS = process.env.FILTERED_AGENCY_IDS || '';
const ENV_MANUALLY_FILTERED_ROUTE_IDS = process.env.MANUALLY_FILTERED_ROUTE_IDS || '';

async function unzip(src, dest) {
const zip = createReadStream(src).pipe(unzipper.Parse({forceStream: true}));
for await (const entry of zip) {
const fileName = basename(entry.path);
if (requiredGTFSFiles.has(fileName)) {
entry.pipe(createWriteStream(join(dest, fileName)));
} else {
entry.autodrain();
}
}
}

(async () => {
// computes a polygon to define the "transit service area"

Expand All @@ -39,7 +27,7 @@ async function unzip(src, dest) {
const gtfsOutputPath = await mkdtemp(join(tmpdir(), 'gtfs-'));

// decompress GTFS zip
await unzip(gtfsFilePath, gtfsOutputPath);
await unzipGtfs(gtfsFilePath, gtfsOutputPath, requiredGTFSFiles);

// Bay Area: we want to filter out stops only served by ACE and Capitol
// Corridor JPA since they go far outside the area we have local transit for
Expand Down
32 changes: 24 additions & 8 deletions src/generateVectorTiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const fs = require('fs');
const geojson2mvt = require('geojson2mvt');
const bbox = require('@turf/bbox').default;
const turf = require('@turf/helpers');
const { tmpdir } = require('node:os');
const { unzipGtfs } = require('./gtfs-helpers');

const agencies = [
{
Expand Down Expand Up @@ -43,14 +45,14 @@ function flattenStopRoutes(geojson) {
const lineClippingDict = {};

(async () => {
const gtfsToGeoJSON = await import('gtfs-to-geojson');
// const gtfsToGeoJSON = await import('gtfs-to-geojson');

await gtfsToGeoJSON.default({
agencies,
outputType: 'agency',
outputFormat: 'lines-and-stops',
ignoreDuplicates: true,
});
// await gtfsToGeoJSON.default({
// agencies,
// outputType: 'agency',
// outputFormat: 'lines-and-stops',
// ignoreDuplicates: true,
// });

console.log('geojson generated');

Expand All @@ -77,5 +79,19 @@ const lineClippingDict = {};
};

// /tiles output director gets generated at cwd
geojson2mvt(geojson, options);
// geojson2mvt(geojson, options);

// /dicts
const rootDir = path.join(process.cwd(), '/dicts/');
const linesStringsPath = path.join(rootDir, 'routeIdLineStringDict.json');
const stopIdLocationsPath = path.join(rootDir, 'stopIdPointsDict.json');

const gtfsOutputPath = await mkdtemp(path.join(tmpdir(), 'gtfs-'));
await unzipGtfs(agencies[0].path, gtfsOutputPath, new Set(['stop_times.txt']));

if(!fs.existsSync(rootDir)){
fs.mkdirSync(rootDir);
}
fs.writeFileSync(linesStringsPath, JSON.stringify(routeIdLineStringDict, null, 2), {encoding: 'utf-8'});
fs.writeFileSync(stopIdLocationsPath, JSON.stringify(stopIdPointDict, null, 2), {encoding: 'utf-8'});
})();
16 changes: 16 additions & 0 deletions src/gtfs-helpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { parse } = require('csv-parse');
const { createReadStream, createWriteStream } = require('node:fs');
const unzipper = require('unzipper');
const { join, basename} = require('path');

async function filterRouteIds(filteredAgencyIds, manuallyFilteredRouteIds, gtfsReadableStream) {
const filteredRouteIds = new Set(manuallyFilteredRouteIds);
Expand Down Expand Up @@ -104,9 +107,22 @@ async function getInterestingStopsAsGeoJsonPoints(interestingStopIds, gtfsReadab
return stops;
}

async function unzipGtfs(src, dest, requiredGTFSFiles) {
const zip = createReadStream(src).pipe(unzipper.Parse({forceStream: true}));
for await (const entry of zip) {
const fileName = basename(entry.path);
if (requiredGTFSFiles.has(fileName)) {
entry.pipe(createWriteStream(join(dest, fileName)));
} else {
entry.autodrain();
}
}
}

module.exports = {
filterRouteIds,
filterTripIds,
getInterestingStopIds,
getInterestingStopsAsGeoJsonPoints,
unzipGtfs,
};

0 comments on commit 47fcfd5

Please sign in to comment.