Last active
February 13, 2024 14:11
-
-
Save Tristramg/0b2fa8359b40900664edfbd5568143b9 to your computer and use it in GitHub Desktop.
locinfra: extraction rk
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 csv | |
import os | |
class Row: | |
def __init__(self, row): | |
self.srv = f"{row['LIGNE']}-{row['TRONCON']}-{row['VOIE']}" | |
self.longitude = float(row['LONGITUDE']) | |
self.latitude = float(row['LATITUDE']) | |
self.pk = float(row['PK']) | |
def overlaps(self, other): | |
return (int(self.pk / 1000) != int(other.pk / 1000)) | |
def interpolate(self, other): | |
rk = int(max(self.pk, other.pk) / 1000) * 1000 | |
if other.pk == self.pk: | |
return rk, other.longitude, other.latitude | |
r = (rk - self.pk) / (other.pk - self.pk) | |
longitude = self.longitude + (other.longitude - self.longitude) * r | |
latitude = self.latitude + (other.latitude - self.latitude) * r | |
return rk, longitude, latitude | |
def parse_file(file): | |
with open(file) as csvfile: | |
reader = csv.DictReader(csvfile, delimiter=';') | |
rows = [Row(row) for row in reader if row['PK'] != ''] | |
for i in range(1, len(rows)): | |
if rows[i-1].overlaps(rows[i]): | |
rk, longitude, latitude = rows[i-1].interpolate(rows[i]) | |
print(f"{rows[i-1].srv};{rk};{longitude};{latitude}") | |
def main(): | |
base_path = 'Extraction LOCINFRA' | |
print("srv_id;rk;longitude;latitude") | |
for file in os.listdir(base_path): | |
parse_file(os.path.join(base_path, file)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment