all-airports
Version:
Serves as an independent data scraping module, complete with ontology and full scraping ability for the airports of the world
168 lines (167 loc) • 10.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var funktologies_1 = require("funktologies");
var getUuid = require("uuid-by-string");
var constants_1 = require("../constants/constants");
var globalStore_1 = require("../constants/globalStore");
var country_code_lookup_tables_1 = require("../utils/country-code-lookup-tables");
var country_to_id_1 = require("../utils/country-to-id");
// Populate remaining airports from datahub list
function getAirportsFromDatahub() {
country_code_lookup_tables_1.airportDatahubList.forEach(function (ap) {
var countryISO = ap.iso_country;
var countryId = country_to_id_1.countryToId(country_code_lookup_tables_1.isoCodeToDataCode(countryISO));
// Deleted name means it's not a legit airport. Skip it.
if (ap.name.toLowerCase() === 'delete' || ap.name === 'marked as spam') {
return;
}
// If airport was built from previous files, only update potentially empty fields.
var existingId = ap.iata_code && constants_1.consts.ONTOLOGY.INST_AIRPORT + getUuid(ap.iata_code);
var existingAirport = globalStore_1.store.airports[existingId];
if (ap.iata_code && existingAirport) {
if (!existingAirport.datatypeProperties[constants_1.consts.ONTOLOGY.DT_ICAO_CODE] && ap.gps_code) {
globalStore_1.store.airports[existingId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_ICAO_CODE] = ap.gps_code;
}
if (!existingAirport.datatypeProperties[constants_1.consts.ONTOLOGY.DT_NAME] && ap.name) {
globalStore_1.store.airports[existingId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_NAME] = ap.name;
}
if (!existingAirport.datatypeProperties[constants_1.consts.ONTOLOGY.DT_REGION_ISO_CODE] && ap.iso_region) {
globalStore_1.store.airports[existingId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_REGION_ISO_CODE] = ap.iso_region;
}
if (ap.elevation_ft) {
makeElevation(existingAirport, ap);
}
if (ap.municipality) {
makeMunicipality(existingAirport, ap, globalStore_1.store.countries[countryId]);
}
return;
}
// Fetch or create airport entity
var airportId = constants_1.consts.ONTOLOGY.INST_AIRPORT + getUuid(ap.ident);
if (!airportId) {
return; // No ident, no id. No id, no airport.
}
var airportObjectProp = {};
if (!globalStore_1.store.airports[airportId]) {
airportObjectProp = funktologies_1.entityMaker(constants_1.consts.ONTOLOGY.HAS_AIRPORT, constants_1.consts.ONTOLOGY.ONT_AIRPORT, airportId, "" + (ap.name || 'N/A'));
globalStore_1.store.airports[airportId] = airportObjectProp[constants_1.consts.ONTOLOGY.HAS_AIRPORT];
}
else {
return; // If already in system, it already has this source's data.
}
if (ap.name) {
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_NAME] = ap.name;
}
if (ap.gps_code) {
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_ICAO_CODE] = ap.gps_code;
}
if (ap.iata_code) {
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_IATA_CODE] = ap.iata_code;
}
if (ap.iso_region) {
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_REGION_ISO_CODE] = ap.iso_region;
}
var coords = ap.coordinates && ap.coordinates.split(',').map(function (c) { return c.trim(); });
var lat;
var lng;
if (coords.length) {
lng = Number(coords[0]);
lat = Number(coords[1]);
}
if (lat && lng) {
var geoId = constants_1.consts.ONTOLOGY.INST_GEO_LOCATION + getUuid(ap.ident);
var objectProp = {};
if (globalStore_1.store.locations[geoId]) {
objectProp[constants_1.consts.ONTOLOGY.HAS_LOCATION] = globalStore_1.store.locations[geoId];
}
else {
objectProp = funktologies_1.entityMaker(constants_1.consts.ONTOLOGY.HAS_LOCATION, constants_1.consts.ONTOLOGY.ONT_GEO_LOCATION, geoId, "Geographic Location for " + (ap.name || 'N/A'));
globalStore_1.store.locations[geoId] = objectProp[constants_1.consts.ONTOLOGY.HAS_LOCATION];
globalStore_1.store.airports[airportId].objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_LOCATION, objectProp));
}
var locAttr = objectProp[constants_1.consts.ONTOLOGY.HAS_LOCATION];
var datatypeProp = {};
if (locAttr && locAttr.datatypeProperties) {
locAttr.datatypeProperties[constants_1.consts.WGS84_POS.LAT] = Number(lat);
locAttr.datatypeProperties[constants_1.consts.WGS84_POS.LONG] = Number(lng);
locAttr.datatypeProperties[constants_1.consts.WGS84_POS.LAT_LONG] = lat + ", " + lng;
}
else {
datatypeProp[constants_1.consts.WGS84_POS.LAT] = Number(lat);
datatypeProp[constants_1.consts.WGS84_POS.LONG] = Number(lng);
datatypeProp[constants_1.consts.WGS84_POS.LAT_LONG] = lat + ", " + lng;
locAttr.datatypeProperties = datatypeProp;
}
}
// Extract Elevation
if (ap.elevation_ft) {
makeElevation(globalStore_1.store.airports[airportId], ap);
}
// Associate Country
if (countryId) {
globalStore_1.store.airports[airportId].objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_COUNTRY, globalStore_1.store.countries, countryId));
globalStore_1.store.countries[countryId].objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_AIRPORT, airportObjectProp));
// Get relative size of airport (small, medium, large)
if (ap.type && ap.type.indexOf('_airport') > 0) {
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_RELATIVE_SIZE] = ap.type.split('_')[0];
}
// Get type of airport (heliport, military, seaplanes, closed)
if (ap.type) {
var airport = ap.type.split('_')[1];
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_TYPE] = airport ? airport : ap.type;
}
// Get status of airport (open, closed)
if (ap.type && ap.type.toLowerCase() === 'closed') {
globalStore_1.store.airports[airportId].datatypeProperties[constants_1.consts.ONTOLOGY.DT_STATUS] = 'Closed';
}
}
// Extract Municipality
if (ap.municipality) {
makeMunicipality(globalStore_1.store.airports[airportId], ap, globalStore_1.store.countries[countryId]);
}
});
}
exports.getAirportsFromDatahub = getAirportsFromDatahub;
function makeElevation(airport, apToScrape) {
var objectProperties = airport.objectProperties;
var map = funktologies_1.getRelation(objectProperties, constants_1.consts.ONTOLOGY.HAS_ELEVATION);
var eId = constants_1.consts.ONTOLOGY.INST_ELEVATION + getUuid(apToScrape.ident);
if (!map) {
var objectProp = {};
if (globalStore_1.store.elevations[eId]) {
objectProp[constants_1.consts.ONTOLOGY.HAS_ELEVATION] = globalStore_1.store.elevations[eId];
}
else {
objectProp = funktologies_1.entityMaker(constants_1.consts.ONTOLOGY.HAS_ELEVATION, constants_1.consts.ONTOLOGY.ONT_ELEVATION, eId, "Elevation for " + (airport.name || apToScrape.name || apToScrape.ident));
globalStore_1.store.elevations[eId] = objectProp[constants_1.consts.ONTOLOGY.HAS_ELEVATION];
}
map = objectProp[constants_1.consts.ONTOLOGY.HAS_ELEVATION];
airport.objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_ELEVATION, objectProp));
}
map.datatypeProperties[constants_1.consts.ONTOLOGY.DT_HIGHEST_POINT] = Number(apToScrape.elevation_ft);
map.datatypeProperties[constants_1.consts.ONTOLOGY.DT_HIGHEST_POINT_DESCRIPTION] = 'The highest point of the airport\'s useable runway system measured in feet above mean sea level';
map.datatypeProperties[constants_1.consts.ONTOLOGY.DT_UNIT] = 'ft';
}
function makeMunicipality(airport, apToScrape, country) {
var objectProperties = airport.objectProperties;
var map = funktologies_1.getRelation(objectProperties, constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY);
var mId = constants_1.consts.ONTOLOGY.INST_MUNICIPALITY + getUuid(apToScrape.ident);
if (!map) {
var objectProp = {};
if (globalStore_1.store.municipalities[mId]) {
objectProp[constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY] = globalStore_1.store.municipalities[mId];
}
else {
objectProp = funktologies_1.entityMaker(constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY, constants_1.consts.ONTOLOGY.ONT_MUNICIPALITY, mId, "Municipality of " + (airport.municipality || apToScrape.municipality));
globalStore_1.store.municipalities[mId] = objectProp[constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY];
}
map = objectProp[constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY];
map.objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_AIRPORT, globalStore_1.store.airports, airport['@id']));
airport.objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY, objectProp));
map.datatypeProperties[constants_1.consts.ONTOLOGY.DT_NAME] = apToScrape.municipality;
if (country) {
country.objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_MUNICIPALITY, objectProp));
map.objectProperties.push(funktologies_1.entityRefMaker(constants_1.consts.ONTOLOGY.HAS_COUNTRY, globalStore_1.store.countries, country['@id']));
}
}
}
;