Skip to content

Instantly share code, notes, and snippets.

@AndrewTheTM
Created May 21, 2024 13:22
Show Gist options
  • Save AndrewTheTM/e76c63925eb4efaac6af090a2359282a to your computer and use it in GitHub Desktop.
Save AndrewTheTM/e76c63925eb4efaac6af090a2359282a to your computer and use it in GitHub Desktop.
This is a Python script that can convert Bentley/Cube Voyager PT line input files to a shapefile.
import pandas as pd
import numpy as np
import re
import geopandas as gpd
from shapely.geometry import Point, LineString
def read_card(input_file, group_id, key_id, nxy_table):
with open(input_file, 'r') as fr:
lines = fr.readlines()
lines = [line.rstrip('\n') for line in lines]
lines = [line for line in lines if line[0] != ';']
lines = ''.join(lines)
lines = lines.split(group_id)
lines = [dict(re.findall(r'(\S+)\s*\=\s*(.*?)\s*(?=\S+\s*\=|$)', line)) for line in lines]
out_lines = []
for line in lines:
if 'NAME' in line.keys():
x = {}
x['route_id'] = line['NAME']
for k, v in line.items():
if not k in ['NAME', 'N']:
x[k] = v.replace('"',"").replace(',','')
coords = nxy_table.loc[[abs(int(n)) for n in line['N'].replace('\n','').replace(' ', '').split(',')]]
geom = LineString([tuple(x) for x in coords.to_numpy()])
x['geometry'] = geom
out_lines.append(x)
return gpd.GeoDataFrame(out_lines)
transit_lines = read_card(r'path\to\transit.lin', 'LINE', 'NAME', nodes[['N', 'X', 'Y']].set_index('N'))
transit_lines.to_file('trn_routes.shp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment