'use strict';
Ox.load.Geo = function(options, callback) {
Ox.getJSON(Ox.PATH + 'Ox.Geo/json/Ox.Geo.json', function(data) {
//@ Constants ----------------------------------------------------------
/*@
Ox.COUNTRIES <[o]> Array of countries
A list of independent (or de-facto independent) countries and
dependencies (see
Wikipedia),
other entities with
ISO 3166-1 alpha-2
country codes, and former countries according to
ISO 3166-3
and IMDb.
area Area of the country in square meters
code ISO 3166-1 alpha-2 country code
created FIXME: incomplete data
dependencies <[s]> Array of dependencies of the country
dependency <[s]> Array of countries the country is a dependency of
disputed <[s]> Array of countries the country is disputed by
disputes <[s]> Array of countries the country disputes
dissolved Present if the country was dissolves
country Succeeding country or countries
date Date of dissolution
type "joined", "merged", "renamed" or "split"
east Longitude of eastern boundary in deg
googleName Google Maps country name
imdbName IMDb country name
languages <[s]> Array of languages that are spoken in this country more than in any other
lat Latitude of the center in deg
lng Longitude of the center in deg
name Name
north Latitude of northern boundary in deg
other True if the country is an "other entity" (EU, FX, UK)
south Latitude of southern boundary in deg
wikipediaName Wikipedia country name (http://en.wikipedia.org/wiki/$wikipediaName)
west Longitude of western boundary in deg
> Ox.COUNTRIES.length
319
> Ox.sum(Ox.test.array)
319
> Ox.test.array
[202, 77, 6, 22, 11, 1]
@*/
Ox.COUNTRIES = data;
Ox.GEO_COLORS = {
'North America': [0, 0, 255],
'Northern America': [0, 0, 255],
'South America': [0, 255, 0],
'Southern America': [0, 255, 0],
'Caribbean': [192, 255, 192],
'Central America': [0, 128, 0],
'Europe': [255, 255, 0],
'Western Europe': [255, 255, 0],
'Northern Europe': [255, 255, 192],
'Southern Europe': [128, 128, 0],
'Eastern Europe': [255, 192, 0],
'Africa': [255, 0, 255],
'Northern Africa': [255, 0, 255],
'Southern Africa': [255, 128, 255],
'Middle Africa': [128, 0, 128],
'Western Africa': [128, 0, 255],
'Eastern Africa': [255, 0, 128],
'Asia': [255, 0, 0],
'Eastern Asia': [255, 0, 0],
'South-Eastern Asia': [255, 128, 128],
'Southern Asia': [128, 0, 0],
'Western Asia': [255, 128, 0],
'Central Asia': [128, 64, 0],
'Oceania': [0, 255, 255],
'Australia and New Zealand': [0, 255, 255],
'Micronesia': [192, 255, 255],
'Melanesia': [0, 128, 128],
'Polynesia': [128, 128, 255],
'Antarctica': [128, 128, 128]
};
//@ Functions ----------------------------------------------------------
/*@
Ox.getCountryByCode Returns a country object for a given country code
(code) -> Country object
code ISO 3166 country code
> Ox.getCountryByCode('US').name
'United States'
@*/
Ox.getCountryByCode = function(code) {
var country;
code = code.toUpperCase();
Ox.forEach(Ox.COUNTRIES, function(c) {
if (c.code == code) {
country = c;
return false;
}
});
return country;
};
/*@
Ox.getCountryByGeoname Returns a country object for a given geoname
(name) -> Country object
name Geoname
> Ox.getCountryByGeoname('Los Angeles, California, United States').code
'US'
> Ox.getCountryByGeoname('The Bottom, Saba, Bonaire, Sint Eustatius and Saba').code
'BQ'
@*/
Ox.getCountryByGeoname = function(geoname) {
// fixme: UAE correction doesn't belong here, fix in map
geoname = (geoname || '').replace(' - United Arab Emirates', ', United Arab Emirates')
return Ox.getCountryByName(
geoname.split(', ').pop()
.replace('Sint Eustatius and Saba', 'Bonaire, Sint Eustatius and Saba')
.replace('Ascension and Tristan da Cunha', 'Saint Helena, Ascension and Tristan da Cunha')
);
}
/*@
Ox.getCountryByName Returns a country object for a given country name
(name) -> Country object
name Country name
> Ox.getCountryByName('United States').code
'US'
> Ox.getCountryByName('USA').code
'US'
@*/
Ox.getCountryByName = function(name) {
var country;
Ox.forEach(Ox.COUNTRIES, function(c) {
if (name == c.name || name == c.googleName || name == c.imdbName) {
country = c;
return false;
}
});
return country;
};
Ox.getFlagByCountryCode = function(code, size) {
size = size || 16;
return Ox.PATH + 'Ox.Geo/png/flags/' + size + '/' + code + '.png';
};
/*@
Ox.getImageByGeoname Returns an image URL for a given geoname
(type, size, geoname) -> Image URL
type Image type ('flag' or 'icon')
size Image width (32, 256, 2048 (flag), 16, 256, 1024 (icon))
geoname Geoname
@*/
Ox.getFlagByGeoname = function(geoname, size) {
var country = Ox.getCountryByGeoname(geoname),
code = country ? country.code : 'NTHH';
size = size || 16;
return Ox.PATH + 'Ox.Geo/png/flags/' + size + '/' + code + '.png';
};
/*@
Ox.getImageByLanguage Returns an image URL for a given language
(type, size, geoname) -> Image URL
type Image type ('flag' or 'icon')
size Image width (32, 256, 2048 (flag), 16, 256, 1024 (icon))
language Language
@*/
Ox.getFlagByLanguage = function(language, size) {
language = language
.replace(' languages', '')
.replace(' Sign Language', '');
var country, code;
Ox.COUNTRIES.forEach(function(c) {
if (c.languages && c.languages.indexOf(language) > -1) {
country = c;
return false;
}
});
code = country ? country.flag || country.code : 'NTHH';
return Ox.PATH + 'Ox.Geo/png/flags/' + size + '/' + code + '.png';
};
Ox.getGeoColor = function(str) {
return Ox.GEO_COLORS[str] || [128, 128, 128];
};
callback(true);
});
}