Created
May 21, 2024 13:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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