maps.chandlerswift.com/layers/fcc/towers/process.py

89 lines
3 KiB
Python
Executable file

#!/usr/bin/env nix-shell
#! nix-shell -i python3 -p python3
import csv
import io
import json
import zipfile
fieldnames = {
"CO": ("Record Type", "Content Indicator", "File Number", "Registration Number", "Unique System Identifier",
"Coordinate Type", "Latitude Degrees", "Latitude Minutes", "Latitude Seconds", "Latitude Direction",
"Latitude_Total_Seconds", "Longitude Degrees", "Longitude Minutes", "Longitude Seconds",
"Longitude Direction", "Longitude Total Seconds", "Array Tower Position", "Array Total Tower"),
}
filenames = ["CO", "EN", "HS", "RA", "RE", "SC"]
files = {}
with zipfile.ZipFile("r_tower.zip", "r") as z:
for filename in filenames:
with z.open(f"{filename}.dat", "r") as f, io.TextIOWrapper( # Avoids `_csv.Error: iterator should return strings, not bytes (the file should be opened in text mode)`
f, encoding="utf-8", newline="", errors="replace"
) as text_f:
contents = []
# csv expects text rows; TextIOWrapper decodes bytes from the zip member
reader = csv.DictReader(text_f, delimiter="|", fieldnames=fieldnames.get(filename))
for row in reader:
contents.append(row)
files[filename] = contents
# CO data example:
#{
# 'Record Type': 'CO',
# 'Content Indicator': 'REG',
# 'File Number': 'A0016772',
# 'Registration Number': '1014003',
# 'Unique System Identifier': '99845',
# 'Coordinate Type': 'T',
# 'Latitude Degrees': '18',
# 'Latitude Minutes': '26',
# 'Latitude Seconds': '18.0',
# 'Latitude Direction': 'N',
# 'Latitude_Total_Seconds': '66378.0',
# 'Longitude Degrees': '66',
# 'Longitude Minutes': '29',
# 'Longitude Seconds': '53.0',
# 'Longitude Direction': 'W',
# 'Longitude Total Seconds': '239393.0',
# 'Array Tower Position': '',
# 'Array Total Tower': ''
# }
towers = []
for w in files["CO"]:
if w['Longitude Degrees'] == '' or w['Latitude Degrees'] == '':
print(f"Skipping tower with missing coordinates: {w}")
continue
towers.append({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
(float(w['Longitude Degrees']) +
float(w['Longitude Minutes']) / 60 +
float(w['Longitude Seconds']) / 3600) *
(-1 if w['Longitude Direction'] == 'W' else 1),
(float(w['Latitude Degrees']) +
float(w['Latitude Minutes']) / 60 +
float(w['Latitude Seconds']) / 3600) *
(1 if w['Latitude Direction'] == 'N' else -1),
], # yes, [lon, lat] since it's [x, y]
},
"properties": {
"File Number": w['File Number'],
"Registration Number": w['Registration Number'],
"Unique System Identifier": w['Unique System Identifier'],
},
})
print(f"""{len(towers)} towers found""")
geojson = {
"type": "FeatureCollection",
"features": towers,
}
with open("data.geojson", "w") as f:
f.write(json.dumps(geojson))