Thursday 22 November 2018

Challenge: FAA Digital Obstacle File into CSV format

If we want to visualize or work DOF we need to import it into our GIS application. If we don't have a customized function that import DOF in dat format, probably the easiest way is to convert dat file into CSV file, simultaneously calculating coordinates in DD format.

First, let's define fields in our output CSV file:

cvs_field_names = ['oas_code', 'obs_number', 'verif_stat', 'country_id', 'state_id',
                       'city_name', 'lat_dms', 'lon_dms', 'obs_type', 'quantity', 'agl_height',
                       'ams_height', 'lighting', 'hor_acc', 'vert_acc', 'mar_indicator', 'faa_study_number',
                       'action', 'jdate', 'lat_dd', 'lon_dd']

Script will read line of input file, parse this line and assign values to appropriate fields of output csv file. So we need to open input file and 'open' output csv file:

with open(in_file, 'r') as dof_file:
        with open(output_file, 'w', newline='') as csv_file:

Next step is to create DictWriter from csv module and write header to output csv file:

writer = csv.DictWriter(csv_file, fieldnames=cvs_field_names, delimiter=',')
writer.writeheader()

Now it's time to read each line of DOF  file, parse it in order to get obstacle identifier, latitude, height above ground level, marking and lighting information and other attributes. Because first 4  lines of DOF file makes header, they not contain relevant information about obstacles as object, we skip them in following way:

line_nr = 0
for line in dof_file:
    try:
        line_nr += 1
        if line_nr < 5:  # Skip first 4 lines - header of DOF
            pass
        else:

Real work happens after 'else' statement: line of DOF file is parsed and written into output CSV file.

# Write parsed data to output file
writer.writerow({'oas_code': line[0:2],
                 'obs_number': line[3:9],
                 'verif_stat': line[10],
                 'country_id': line[12:14],
                 'state_id': line[15:17],
                 'city_name': line[18:34].rstrip(),
                 'lat_dms': line[35:47],
                 'lon_dms': line[48:61],
                 'obs_type': line[62:80].rstrip(),
                 'quantity': line[81],
                 'agl_height': line[83:88],
                 'ams_height': line[89:94],
                 'lighting': line[95],
                 'hor_acc': line[97],
                 'vert_acc': line[99],
                 'mar_indicator': line[101],
                 'faa_study_number': line[103:117].strip(),
                 'action': line[118],
                 'jdate': line[120:127],
                 'lat_dd': str(dms2dd(line[35:47])),
                 'lon_dd': str(dms2dd(line[48:61]))})

For the time being I don' handle any error, e. .g if line does not match the documentation of DOF file, or some error will arise during calculation DD format of latitude or longitude. In case of exeption simply do nothing:

except:
    continue

Script is available at: https://github.com/strpaw/python_examples/blob/master/faa_dof2csv.py






No comments:

Post a Comment