oxjs/source/Ox.Geo/Ox.Geo.js

227 lines
8.7 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
Ox.load.Geo = function(options, callback) {
Ox.getJSON(Ox.PATH + 'Ox.Geo/json/Ox.Geo.json', function(data) {
2011-05-09 08:54:52 +00:00
//@ Constants ----------------------------------------------------------
/*@
Ox.COUNTRIES <[o]> Array of countries
2011-05-11 16:33:19 +00:00
A list of independent (or de-facto independent) countries and
dependencies (see
<a href="http://en.wikipedia.org/wiki/List_of_sovereign_states">Wikipedia</a>),
other entities with
<a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">ISO 3166-1 alpha-2</a>
2011-11-23 14:53:17 +00:00
country codes, and former countries according to
<a href="http://en.wikipedia.org/wiki/ISO_3166-3">ISO 3166-3</a>
2011-05-11 16:33:19 +00:00
and <a href="http://www.imdb.com/country/">IMDb</a>.
2011-05-09 08:54:52 +00:00
area <n> Area of the country in square meters
2011-05-11 16:33:19 +00:00
code <s> ISO 3166-1 alpha-2 country code
2011-11-23 14:53:17 +00:00
created <o> FIXME: incomplete data
2011-05-23 19:38:52 +00:00
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
2011-11-23 14:53:17 +00:00
dissolved <o> Present if the country was dissolves
country <s|[s]> Succeeding country or countries
date <s> Date of dissolution
type <s> "joined", "merged", "renamed" or "split"
2011-05-09 08:54:52 +00:00
east <n> Longitude of eastern boundary in deg
2011-05-23 19:38:52 +00:00
googleName <s|u> Google Maps country name
imdbName <s|u> IMDb country name
languages <[s]> Array of languages that are spoken in this country more than in any other
2011-05-09 08:54:52 +00:00
lat <n> Latitude of the center in deg
lng <n> Longitude of the center in deg
name <s> Name
north <n> Latitude of northern boundary in deg
other <b> True if the country is an "other entity" (EU, FX, UK)
south <n> Latitude of southern boundary in deg
2011-11-23 14:53:17 +00:00
wikipediaName <s> Wikipedia country name (http://en.wikipedia.org/wiki/$wikipediaName)
2011-05-09 08:54:52 +00:00
west <n> Longitude of western boundary in deg
<script>
Ox.test.array = [
2011-11-23 14:53:17 +00:00
// Current independent countries
2011-05-09 08:54:52 +00:00
Ox.COUNTRIES.filter(function(c) {
2011-11-23 14:53:17 +00:00
return !c.dissolved && !c.dependency && !c.disputed
2011-05-09 08:54:52 +00:00
}).length,
2011-05-23 19:38:52 +00:00
// Current dependent countries
2011-05-09 08:54:52 +00:00
Ox.COUNTRIES.filter(function(c) {
2011-11-23 14:53:17 +00:00
return !c.dissolved && c.dependency
2011-05-23 19:38:52 +00:00
}).length,
// Current disputed countries
Ox.COUNTRIES.filter(function(c) {
2011-11-23 14:53:17 +00:00
return !c.dissolved && c.disputed
2011-05-09 08:54:52 +00:00
}).length,
2011-11-23 14:53:17 +00:00
// Dissolved independent countries
2011-05-09 08:54:52 +00:00
Ox.COUNTRIES.filter(function(c) {
2011-11-23 14:53:17 +00:00
return c.dissolved && !c.dependency && !c.disputed
2011-05-23 19:38:52 +00:00
}).length,
2011-11-23 14:53:17 +00:00
// Dissolved dependent countries
2011-05-23 19:38:52 +00:00
Ox.COUNTRIES.filter(function(c) {
2011-11-23 14:53:17 +00:00
return c.dissolved && c.dependency
2011-05-09 08:54:52 +00:00
}).length,
2011-11-23 14:53:17 +00:00
// Dissolved disputed countries
2011-05-09 08:54:52 +00:00
Ox.COUNTRIES.filter(function(c) {
2011-11-23 14:53:17 +00:00
return c.dissolved && c.disputed
}).length
2011-05-09 08:54:52 +00:00
];
</script>
> Ox.COUNTRIES.length
2011-11-23 14:53:17 +00:00
319
2011-05-09 08:54:52 +00:00
> Ox.sum(Ox.test.array)
2011-11-23 14:53:17 +00:00
319
2011-05-09 08:54:52 +00:00
> Ox.test.array
2011-11-23 14:53:17 +00:00
[202, 77, 6, 22, 11, 1]
2011-05-09 08:54:52 +00:00
@*/
2011-05-23 19:38:52 +00:00
Ox.COUNTRIES = data;
2012-03-26 19:26:28 +00:00
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]
};
2011-05-09 08:54:52 +00:00
//@ Functions ----------------------------------------------------------
/*@
Ox.getCountryByCode <f> Returns a country object for a given country code
(code) -> <o> Country object
code <s> 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;
};
2011-05-22 17:12:21 +00:00
/*@
Ox.getCountryByGeoname <f> Returns a country object for a given geoname
(name) -> <o> Country object
name <s> Geoname
> Ox.getCountryByGeoname('Los Angeles, California, United States').code
'US'
2011-11-23 14:53:17 +00:00
> Ox.getCountryByGeoname('The Bottom, Saba, Bonaire, Sint Eustatius and Saba').code
2011-05-22 17:12:21 +00:00
'BQ'
@*/
Ox.getCountryByGeoname = function(geoname) {
2011-05-24 06:15:44 +00:00
// fixme: UAE correction doesn't belong here, fix in map
geoname = (geoname || '').replace(' - United Arab Emirates', ', United Arab Emirates')
2011-05-22 17:12:21 +00:00
return Ox.getCountryByName(
geoname.split(', ').pop()
2011-11-30 14:28:24 +00:00
.replace('Sint Eustatius and Saba', 'Bonaire, Sint Eustatius and Saba')
.replace('Ascension and Tristan da Cunha', 'Saint Helena, Ascension and Tristan da Cunha')
2011-05-22 17:12:21 +00:00
);
}
2011-05-09 08:54:52 +00:00
/*@
Ox.getCountryByName <f> Returns a country object for a given country name
(name) -> <o> Country object
name <s> Country name
> Ox.getCountryByName('United States').code
'US'
2011-05-23 19:38:52 +00:00
> Ox.getCountryByName('USA').code
'US'
2011-05-09 08:54:52 +00:00
@*/
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.getImageByGeoname <f> Returns an image URL for a given geoname
(type, size, geoname) -> <s> Image URL
type <s> Image type ('flag' or 'icon')
size <n> Image width (32, 256, 2048 (flag), 16, 256, 1024 (icon))
geoname <s> Geoname
@*/
2011-11-23 14:53:17 +00:00
Ox.getFlagByGeoname = function(geoname, size) {
2011-05-24 16:22:37 +00:00
var country = Ox.getCountryByGeoname(geoname),
2011-11-23 14:53:17 +00:00
code = country ? country.flag || country.code : 'NTHH';
return Ox.PATH + 'Ox.Geo/png/flags/' + size + '/' + code + '.png';
2011-05-24 12:50:16 +00:00
};
/*@
Ox.getImageByLanguage <f> Returns an image URL for a given language
(type, size, geoname) -> <s> Image URL
type <s> Image type ('flag' or 'icon')
size <n> Image width (32, 256, 2048 (flag), 16, 256, 1024 (icon))
language <s> Language
@*/
2011-11-23 14:53:17 +00:00
Ox.getFlagByLanguage = function(language, size) {
2011-09-09 16:37:21 +00:00
language = language
.replace(' languages', '')
.replace(' Sign Language', '');
2011-11-23 15:25:07 +00:00
var country, code;
2011-09-09 16:37:21 +00:00
Ox.COUNTRIES.forEach(function(c) {
2011-11-23 15:25:07 +00:00
if (c.languages && c.languages.indexOf(language) > -1) {
2011-09-09 16:37:21 +00:00
country = c;
return false;
}
});
2011-11-23 14:53:17 +00:00
code = country ? country.flag || country.code : 'NTHH';
return Ox.PATH + 'Ox.Geo/png/flags/' + size + '/' + code + '.png';
2011-09-09 16:37:21 +00:00
};
Ox.getGeoColor = function(str) {
2012-03-26 19:26:28 +00:00
return Ox.GEO_COLORS[str] || [128, 128, 128];
};
callback(true);
});
}