line-json-formatter
v1.2.0
Published
A weirdly specific tool designed to help me build my app.
Maintainers
Readme
Line JSON Formatter
What is this?
It's a weird tool that I made to help me with a specific task while creating my app, STB Logger. I'll use it to convert and format multiple GPX files that I created. They represent every path between two public transport stops in Bucharest. This tool helps me convert them into GeoJSON, add properties (lines, id, length) and save them all in a MySQL database.
How does it work?
LineJSONFormatter
It uses a database with all the stops in Bucharest to find the closest stops to the start and end of each path and saves the ids in the json's properties.
The user is asked whether they would like to use automatic detection of the lines that use the paths. They can also choose to input them manually.
The user is also prompted to choose which paths are in which direction.
Path length is calculated using a npm module.
SubwayJSONFormatter
It takes a GeoJSON file with paths and stops corresponding to subway lines as input. I created it so that it uses a format I already have. The program uses the coordinates of the stops from the GeoJSON file, but uses the ids of stops in the stopJSON file. In order to avoid duplicates, stops whose ids have been used are deleted from the stopJSON file.
The paths are processed in the same way as the LineJSONFormatter function does. The only difference is the format of the input.
ReplaceLineIDs
It takes an ordinary JSON file as input and replaces the values of the indicated field with values from another JSON file.
Why?
I thought that instead of doing this task manually, it's faster to make a script. This way, I also practice JavaScript.
Installation and usage
Install from npm
npm intall --save line-json-formatterLink to npm module:
Use in a node.js script
import { LineJSONFormatter, SubwayJSONFormatter } from "./app.js";
const formattedJSON = await LineJSONFormatter("input/", "stops.json");
const formattedSubwayJSON = await SubwayJSONFormatter("input/Metrou 2.geojson", "stb-stops.json", true);
function removeRange(line) {
if(line === undefined) return false;
else return RegExp("4[0-9][0-9]B*").test(line)
}
const replacedJSON = ReplaceLineIDs('stop_times_4.json', 'line', 'routes_2.json', (line) => { return removeRange(line) });Documentation
The function LineJSONFormatter has 2 parameters:
| Parameter | Type | Description |
| ------------ | -------- | --------------- |
| inputPath | String | The path where input gpx files are situated |
| stopJSONFile | String | The JSON file that contains formatted stops |
| linesJSONFile | String | The JSON file that contains information about other lines' paths |
The function SubwayJSONFormatter has 4 parameters:
| Parameter | Type | Required | Description | |----------------|----------|--------------------------------|----------------------------------------------------------------------| | inputFile | String | Yes | The file that contains the paths to be formatted | | stopJSONFile | String | Yes | The JSON file that contains formatted stops | | modifyDb | Boolean | No, default value is true | Whether the function should add paths to database or not | | returnStopJSON | Boolean | No, default value is false | Whether the function should return the modified stopJSON file or not |
The function ReplaceLineIDs has 4 parameters:
| Parameter | Type | Required | Description | |----------------| -------- |--------------------------------|------------------------------------------------------------------------------------------| | inputFile | String | Yes | The JSON file that has the values that are to be replaced | | fieldToReplace | String | Yes | The field within the input JSON that is to be replaced | | linesFile | String | Yes | The JSON file that has the replacement lines | | removeRange | Boolean | No, default value is false | Function that takes a line as input and returns whether the line shoud be removed or not |
The module also requires 3 environmental variables. You must define these in your .env file! | Variable name | Description | | ----------------- | --------------- | | LJF_DB_USER | The MySQL database user | | LJF_DB_PASSWORD | The database password | | LJF_DB_HOST | The ip address where the database is hosted (if hosted locally, should be 127.0.0.1) |
Stop JSON format
The stop JSON must contain an array of objects, each with the following properties: id, name, latitude, longitude.
Example:
[
{
"id": 3654,
"name": "1 Mai",
"latitude": 44.419407,
"longitude": 26.047092
},
{
"id": 8682,
"name": "1 Martie",
"latitude": 44.422333,
"longitude": 25.878494
}
}LinesJSON format
The linesJSON file must contain an array of objects, each with the following properties: line, path_direction, startId. They represent each stop that every line stops at.
Example:
[
{"line":5,"path_direction":1,"startId":12067,"path_order":0},
{"line":5,"path_direction":1,"startId":3617,"path_order":1}
]Subway GeoJSON format
The subway GeoJSON file has to be of type FeatureCollection and has to only contain features of types Point and LineString.
Points must have the following properties:
- name - String - The name of the stop.
- line - String - Stores the lines that stop there. Lines must be separated by "/".
LineStrings must have the following properties:
- line - String - Stores the lines that stop there. Lines must be separated by "/".
- end1 - Int - One of the stops that the path connects.
- end2 - Int - The other stop.
Features may have other properties besides the required ones.
Example:
{
"features": [
{
"type": "Feature",
"properties": {
"id": 49,
"name": "1 Mai",
"line": "M4"
},
"geometry": {
"coordinates": [
26.050627,
44.470534,
91.580843
],
"type": "Point"
},
"id": "0598478d0b02151caba0b3d7813a2df9"
},
{
"type": "Feature",
"properties": {
"line": "M1",
"end1": 8,
"end2": 9
},
"geometry": {
"coordinates": [
[
26.068367,
44.450738,
81.30971
],
[
26.067905,
44.451043,
81.163368
]
],
"type": "LineString"
},
"id": "07c9e42a96d13b687ed7b3d69647570c"
}
]
}Lines file format
The lines file has to be a JSON object containing objects with the following fields:
- from - The value to be replaced
- to - The replacement value
AI Usage
I used AI to help me with the closest distance algorithm.
Demo videos
LineJSONFormatter (New)
Screencast_20260419_163509.webm
LineJSONFormatter (Old)
Screencast_20260322_232538.webm
SubwayJSONFormatter
Screencast_20260329_213430.webm
ReplaceLineIDs
