gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
38 lines • 1.29 kB
JavaScript
import { parseCSVAsRecord } from '../../index.js';
/**
* @param input - the input string to parse from
* @returns - an array of Agencies
*/
export function parseGTFSShapes(input) {
const data = parseCSVAsRecord(input);
// 1) Group data by shape_id
const groups = {};
data.forEach((d) => {
const { shape_id, shape_pt_lat, shape_pt_lon, shape_pt_sequence } = d;
if (groups[shape_id] === undefined)
groups[shape_id] = [];
groups[shape_id].push({
id: shape_id,
lat: parseFloat(shape_pt_lat),
lon: parseFloat(shape_pt_lon),
sequence: parseInt(shape_pt_sequence, 10),
});
});
// 2) for each shape_id group, sort data by shape_pt_sequence geometry
const features = {};
for (const [shapeId, shapes] of Object.entries(groups)) {
shapes.sort((a, b) => a.sequence - b.sequence);
features[shapeId] = {
type: 'VectorFeature',
metadata: {},
geometry: {
type: 'LineString',
is3D: false,
coordinates: shapes.map((s) => ({ x: s.lon, y: s.lat })),
},
properties: { id: shapeId },
};
}
return features;
}
//# sourceMappingURL=shapes.js.map