require "csv" # curl -o data/ourairports.csv http://ourairports.com/data/airports.csv AIRPORT_DATA_FILE = "data/ourairports.csv" AIRPORT_RUST_FILE = "src/airport.rs" f = File.open(AIRPORT_RUST_FILE, "w") f.write("use crate::Airport;\r\n\n") f.write("pub const AIRPORTS: [Airport; NUM_AIRPORTS] = [\r\n") number_of_airports = 0 CSV.foreach(AIRPORT_DATA_FILE, headers: true) do |row| next if row["iata_code"].nil? next if row["iata_code"].length != 3 next if row["type"] != "large_airport" && row["type"] != "medium_airport" name = row["name"].gsub('"', '\"') latitude = row["latitude_deg"] == "0" ? 0.0 : row["latitude_deg"] longitude = row["longitude_deg"] == "0" ? 0.0 : row["longitude_deg"] altitude = row["elevation_ft"].nil? ? 0 : row["elevation_ft"] f.write("\tAirport{\ id: #{row["id"]}, \ name: \"#{name}\", \ city: \"#{row["municipality"]}\", \ country: \"country\", \ continent: \"#{row["continent"]}\", \ iata: \"#{row["iata_code"]}\", \ icao: \"#{row["gps_code"]}\", \ latitude: #{latitude}, \ longitude: #{longitude}, \ altitude: #{altitude}, \ },\r\n") number_of_airports = number_of_airports + 1 end f.write("];\n") f.close generated_code = File.read(AIRPORT_RUST_FILE) s = generated_code.gsub("NUM_AIRPORTS", number_of_airports.to_s) File.write(AIRPORT_RUST_FILE, s)