block by zanarmstrong b7381e04dcded29b2b6f

City latitude and longitude lookup tool (worldwide)

Full Screen

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
	width: 800px;
}

#cityList {
	width: 800px;
}

#errorCities {
	color: red;
}

#multipleCities {
	color: blue;
}

</style>
<body>
<h4>This tool converts a comma separted list of cities into json formatted text with that city, country, and latitude and longitude based on the location of that city's airport. The source data is from <a href="//www.partow.net/miscellaneous/airportdatabase/index.html">here</a></h4>
<p>Type or copy/paste city names below, as shown in grey, and press "enter"</p>
<input type="text" name="cityList" id="cityList" placeholder="San Francisco, Chicago, Paris">
<p id="errors"></p>
<p id="multiples"></p>
<pre id="outputJSON"></pre>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script src="lookup.js"></script>
</body>

dataHeader.txt

ICAO,IATA,AirportName,City,Country,LatDeg,LatMin,LatSec,LatDirection,LongDeg,LongMin,LongSec,LongDirection,Altitude

lookup.js

chosenCities = [];

d3.json("cities.json", function(error, latLonList){

	d3.select("#cityList").on("change", function() {
		var errors = [];
		var output = [];
    var multiples = [];
  		chosenCities = this.value.split(',');
  		for(var i = 0; i < chosenCities.length; i++){
  			var k = latLonList[chosenCities[i].toUpperCase().trim()];
  			if(typeof(k) == "undefined"){
          errors.push(chosenCities[i].trim());
        } else {
          if (k.length > 1) {
            multiples.push(chosenCities[i].trim());
          }
          for (var z = 0; z < k.length; z++){
           m = k[z];
           output.push({city: chosenCities[i].trim(), country: m[0], location: {latitude: m[1], longitude: m[2]}});
          }
  			}
  		}

    outputJSON("outputJSON", JSON.stringify(output, undefined, 2));

    postWarnings(errors, "errors", "errorCities", "couldn't be found in the dataset. Try an alternate spelling and the commonly used name in English. If that fails, look it up <a href='http://www.partow.net/miscellaneous/airportdatabase/index.html'>in the original file</a> or on GoogleMaps <a href='http://universimmedia.pagesperso-orange.fr/geo/loc.htm'>here</a>");
    postWarnings(multiples, "multiples", "multipleCities", "listed more than once because there is more than one city with this name or because the city has more than one airport.");
	});

})

function outputJSON(element, text) {
    document.getElementById(element).innerHTML = text;
}

function postWarnings(cities, element, id, text){
      if(cities.length > 0){
        var cityListText = "";
        for(j = 0; j < cities.length; j++){
          if(cities.length == 1){
            cityListText = cities[j];
          } else if (cities.length == 2){
            cityListText = cities[0] + " and " + cities[1];
          } else if(j < cities.length - 1){
            cityListText = cityListText + " " + cities[j] + ",";
          } else {
            cityListText = cityListText + " and " + cities[j];
        }
      }
      document.getElementById(element).innerHTML = "<span id='" + id + "'>" + cityListText + "</span>: " + text; 
    }
}

readme.txt

This example is designed to make it easy to submit cities and get a js object in return with the relevant latitude and longitude. 

Cities should be submitted into the text box separated by commas.  For example: "San Francisco, Seattle, Kansas City, Chicago, Boston, Paris"

----- Information about data source

                       The Global Airport Database
                         Release Version 0.0.1

Author: Arash Partow 2003
Copyright notice:
Free use of The Global Airport Database is permitted under the guidelines
and in accordance with the most current version of the Common Public License.
http://www.opensource.org/licenses/cpl.php


Introduction


The Global Airport Database is a FREE online downloadable database of aiports
big and small from around the world. The database is presented in a simple
token seperated format.


For more information please visit:

http://www.partow.net/miscellaneous/airportdatabase/index.html

reformat.py

#Reformatting List of Lat/Long

import csv
import json

def parseRow(row):
    temp = [row[4],
            float(row[5]) + (float(row[6]) / 60) + float(row[7]) / 3600, 
            float(row[9]) + (float(row[10]) / 60) + float(row[11]) / 3600]
    if row[8] == "S":
        temp[1] = (-1) * temp[1]
    if row[12] != "E":
        temp[2] = (-1) * temp[2]
    return (temp)



def readList(filename):
    cityDict = {};
    with open(filename, newline='', encoding='utf-8') as a_file:
        locationReader = csv.reader(a_file, delimiter=",")
        for row in locationReader:
            if row[8] != "U":
                if(row[3] in cityDict):
                    cityDict[row[3]].append(parseRow(row))
                else: 
                    cityDict[row[3]] = [parseRow(row)]
    return (cityDict)

# start doing stuff here

cities = readList('GlobalAirportDatabase.txt')
print(json.dumps(cities))

f = open("cities.json", 'w')
json.dump(cities, f)