1
0
Fork 0
forked from 0x2620/oxjs

geo/map bugfixes

This commit is contained in:
rolux 2011-11-24 19:38:10 +01:00
commit dfd2787438
14 changed files with 892 additions and 809 deletions

View file

@ -1,44 +1,46 @@
Ox.load('UI', function() {
Ox.getJSONC('../jsonc/countries.jsonc', function(geo) {
var $map = $('<img>')
.attr({
id: 'map',
src: '../png/map.png'
}),
$bar = Ox.Bar({size: 24}),
$status = $('<div>')
.css({margin: '5px 0 0 8px'})
.appendTo($bar)
$panel = Ox.SplitPanel({
elements: [
{
element: $map
},
{
element: $bar,
size: 24
}
],
orientation: 'vertical'
})
.appendTo(Ox.UI.$body),
errors = [],
geocoder = new google.maps.Geocoder(),
json = [],
timeout = 2000;
Ox.getJSONC('../jsonc/countries.jsonc', function(data) {
Ox.getJSON('../json/countries.json', function(countries) {
var length = countries.length;
callGetData();
function callGetData() {
getData(countries.shift(), function(country) {
var $map = $('<img>')
.attr({
id: 'map',
src: '../png/map.png'
}),
$bar = Ox.Bar({size: 24}),
$status = $('<div>')
.css({margin: '5px 0 0 8px'})
.appendTo($bar),
$panel = Ox.SplitPanel({
elements: [
{
element: $map
},
{
element: $bar,
size: 24
}
],
orientation: 'vertical'
})
.appendTo(Ox.UI.$body),
errors = [],
geocoder = new google.maps.Geocoder(),
json = [],
length = countries.length,
timeout = 2000;
getNextCountry();
function getNextCountry() {
getCountryData(countries.shift(), function(country) {
addFlag(country);
json.push(country);
$status.html(json.length + '/' + length + ' ' + country.name);
if (countries.length) {
setTimeout(callGetData, timeout);
setTimeout(getNextCountry, timeout);
} else {
var $dialog = Ox.Dialog({
buttons: [
@ -58,200 +60,179 @@ Ox.load('UI', function() {
WebkitUserSelect: 'text'
})
.html(
'<code><pre>' + JSON.stringify(json, null, 4) + '</pre></code>'
'<code><pre>' + JSON.stringify(
errors.length ? {errors: errors, data: json} : json,
null, 4) + '</pre></code>'
),
height: window.innerHeight * 0.9 - 48,
title: 'Ox.Geo',
width: window.innerWidth * 0.9
}).open();
Ox.print('Errors:', errors);
$status.html(
errors.length
? 'Done, see console for errors.'
: 'Done, no errors.'
);
$status.html('');
}
});
}
});
function addFlag(country) {
var $div,
center = Ox.getXYByLatLng({lat: country.lat, lng: country.lng}),
crossesDateline = country.west > country.east,
height = $map.height(),
northEast = Ox.getXYByLatLng({lat: country.north, lng: country.east}),
southWest = Ox.getXYByLatLng({lat: country.south, lng: country.west}),
width = $map.width();
if (crossesDateline) {
$div = [
$('<div>')
.addClass('rect')
.css({
left: '-16px',
top: (height * northEast.y) + 'px',
right: (width - width * northEast.x) + 'px',
bottom: (height - height * southWest.y) + 'px',
})
.hide()
.appendTo(Ox.UI.$body),
$('<div>')
.addClass('rect')
.css({
left: (width * southWest.x) + 'px',
top: (height * northEast.y) + 'px',
right: '-16px',
bottom: (height - height * southWest.y) + 'px',
})
.hide()
.appendTo(Ox.UI.$body),
];
} else {
$div = [
$('<div>')
.addClass('rect')
.css({
left: ($map.width() * southWest.x) + 'px',
top: ($map.height() * northEast.y) + 'px',
right: ($map.width() - $map.width() * northEast.x) + 'px',
bottom: ($map.height() - $map.height() * southWest.y) + 'px',
})
.hide()
.appendTo(Ox.UI.$body)
];
function addFlag(country) {
var $div,
center = Ox.getXYByLatLng({lat: country.lat, lng: country.lng}),
crossesDateline = country.west > country.east,
height = $map.height(),
northEast = Ox.getXYByLatLng({lat: country.north, lng: country.east}),
southWest = Ox.getXYByLatLng({lat: country.south, lng: country.west}),
width = $map.width();
if (crossesDateline) {
$div = [
$('<div>')
.addClass('rect')
.css({
left: '-16px',
top: (height * northEast.y) + 'px',
right: (width - width * northEast.x) + 'px',
bottom: (height - height * southWest.y) + 'px',
})
.hide()
.appendTo(Ox.UI.$body),
$('<div>')
.addClass('rect')
.css({
left: (width * southWest.x) + 'px',
top: (height * northEast.y) + 'px',
right: '-16px',
bottom: (height - height * southWest.y) + 'px',
})
.hide()
.appendTo(Ox.UI.$body)
];
} else {
$div = [
$('<div>')
.addClass('rect')
.css({
left: ($map.width() * southWest.x) + 'px',
top: ($map.height() * northEast.y) + 'px',
right: ($map.width() - $map.width() * northEast.x) + 'px',
bottom: ($map.height() - $map.height() * southWest.y) + 'px',
})
.hide()
.appendTo(Ox.UI.$body)
];
}
$('<img>')
.attr({
src: '../png/icons/16/' + country.code + '.png',
title: country.name
})
.addClass('flag')
.css({
left: (center.x * 100) + '%',
top: (center.y * 100) + '%'
})
.mouseenter(function() {
$(this).css({
zIndex: Ox.uid()
});
$.each($div, function() {
$(this).show();
});
})
.mouseleave(function() {
$.each($div, function() {
$(this).hide();
});
})
.appendTo(Ox.UI.$body);
}
$('<img>')
.attr({
src: '../png/icons/16/' + country.code + '.png',
title: country.name + ' ((' + country.south + ", " + country.west + "), (" + country.north + ", " + country.east + "))"
})
.addClass('flag')
.css({
left: (center.x * 100) + '%',
top: (center.y * 100) + '%'
})
.mouseenter(function() {
$(this).css({
zIndex: Ox.uid()
});
$.each($div, function() {
$(this).show();
});
})
.mouseleave(function() {
$.each($div, function() {
$(this).hide();
});
})
.appendTo(Ox.UI.$body);
}
function geocode(address, country, callback) {
var bounds;
if (!Ox.isUndefined(country.north)) {
bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(country.south, country.west),
new google.maps.LatLng(country.north, country.east)
);
callback({
bounds: bounds,
location: bounds.getCenter()
});
} else {
geocoder.geocode({
language: 'en',
address: address
}, function(results, status) {
if (results && results.length) {
var result = results[0];
callback({
bounds: result.geometry.bounds || result.geometry.viewport,
location: result.geometry.location
})
} else {
errors.push([address, status])
callback(null);
}
});
}
}
function getData(country, callback) {
if (country.lat) {
callback(country);
return;
}
var addresses = geo.geocode[country.name] || [country.name],
length = addresses.length,
union;
getImageURLs(country, function(imageURLs) {
// this might be too much data
/*
Ox.extend(country, {
imageURLs: imageURLs
});
*/
callGeocode();
});
function callGeocode() {
geocode(addresses.shift(), country, function(data) {
var center, lat, lng,
northEast, southWest,
east, north, south, west;
if (data) {
union = !union ? data.bounds : union.union(data.bounds);
if (addresses.length == 0) {
if (length == 1) {
lat = data.location.lat();
lng = data.location.lng();
} else {
center = union.getCenter();
lat = center.lat();
lng = center.lng();
}
northEast = union.getNorthEast();
southWest = union.getSouthWest();
east = northEast.lng();
north = northEast.lat();
south = southWest.lat();
west = southWest.lng();
callback($.extend(country, {
area: Ox.getArea(
{lat: south, lng: west},
{lat: north, lng: east}
),
east: toFixed(east),
lat: toFixed(lat),
lng: toFixed(lng),
north: toFixed(north),
south: toFixed(south),
west: toFixed(west)
}));
function geocode(geoname, callback) {
var bounds, location = data.location[geoname];
if (location) {
bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(location.south, location.west),
new google.maps.LatLng(location.north, location.east)
);
callback({
bounds: bounds,
location: bounds.getCenter()
});
} else {
geocoder.geocode({
language: 'en',
address: geoname
}, function(results, status) {
if (results && results.length) {
var result = results[0];
callback({
bounds: result.geometry.bounds || result.geometry.viewport,
location: result.geometry.location
})
} else {
errors.push({geoname: geoname, status: status});
Ox.print('remove this print statement', errors)
callback(null);
}
} else {
callback(country);
}
});
if (addresses.length) {
setTimeout(callGeocode, timeout);
});
}
}
}
function getImageURLs(country, callback) {
var image = new Image();
image.onload = function() {
callback({
icon16: Ox.canvas(image).canvas.toDataURL()
});
};
image.src = '../png/icons/16/' + country.code + '.png';
}
function getCountryData(country, callback) {
function toFixed(num) {
return parseFloat(num.toFixed(8));
}
var geonames = Ox.clone(data.geocode[country.name]) || [country.name],
length = geonames.length,
union;
getNextGeoname();
function getNextGeoname() {
geocode(geonames.shift(), function(geodata) {
var center, lat, lng,
northEast, southWest,
east, north, south, west;
if (geodata) {
union = !union ? geodata.bounds : union.union(geodata.bounds);
}
if (geonames.length) {
setTimeout(getNextGeoname, timeout);
} else {
if (union) {
if (length == 1) {
lat = geodata.location.lat();
lng = geodata.location.lng();
} else {
center = union.getCenter();
lat = center.lat();
lng = center.lng();
}
northEast = union.getNorthEast();
southWest = union.getSouthWest();
east = northEast.lng();
north = northEast.lat();
south = southWest.lat();
west = southWest.lng();
country = Ox.extend(country, {
area: Ox.getArea(
{lat: south, lng: west},
{lat: north, lng: east}
),
east: toFixed(east),
lat: toFixed(lat),
lng: toFixed(lng),
north: toFixed(north),
south: toFixed(south),
west: toFixed(west)
});
}
callback(country);
}
});
}
}
function toFixed(num) {
return parseFloat(num.toFixed(10));
}
});
});

View file

@ -380,27 +380,26 @@
],
"geocode": {
// construction of countries unknown to the google maps geocoder
"Abkhazia": ["Abkhazia, Georgia"],
"Akrotiri and Dhekelia": ["Akrotiri, Cyprus", "Dhekelia, Cyprus"],
"Bonaire, Saint Eustatius and Saba": ["Bonaire", "Saba, Netherlands Antilles", "Saint Eustatius"],
"Bonaire, Sint Eustatius and Saba": ["Bonaire", "Saba", "Sint Eustatius"],
"Byelorussian Soviet Socialist Republic": ["Belarus"],
"Canary Islands": ["Ferro, Canary Islands", "Isla Alegranza, Canary Islands"],
"Canton and Enderbury Islands": ["Canton Island", "Enderbury Island"],
"Ceuta and Melilla": ["Ceuta", "Melilla"],
"Curaçao": ["Banda Abou, Curaçao"],
"Czechoslovakia": ["Czech Republic", "Slovakia"],
"East Germany": [
"Mecklenburg Vorpommern", "Saxony", "Thuringia"
],
"East Germany": ["Mecklenburg Vorpommern", "Saxony", "Thuringia"],
"Dahomey": ["Benin"],
"European Union": ["Europe"],
"European Union": ["Cyprus", "Finland", "Greece", "Portugal"],
"French Afar and Issas": ["Djibouti"],
// see http://en.wikipedia.org/wiki/French_Southern_and_Antarctic_Lands
// and http://en.wikipedia.org/wiki/Scattered_Islands_in_the_Indian_Ocean
"French Southern and Antarctic Territories": ["Adélie Land", "Bassas da India", "Glorioso Islands", "Île Kerguelen"],
"French Southern Territories": ["Bassas da India", "Glorioso Islands", "Île Kerguelen"],
"French Southern and Antarctic Territories": ["Adélie Land", "Bassas da India", "Glorioso Islands"],
"French Southern Territories": ["Amsterdam Island", "Bassas da India", "Glorioso Islands", "Kerguelen Island"],
"Georgia": ["Georgia, Asia"],
// see http://en.wikipedia.org/wiki/Gilbert_Islands
"Gilbert and Ellice Islands": ["Arorae, Kiribati", "Butaritari, Kiribati", "Makin, Kiribati", "Tuvalu"],
"Jamaica": ["Clarendon Parish, Jamaica", "St. James Parish, Jamaica", "St. Thomas Parish, Jamaica", "Westmoreland Parish, Jamaica"], // in case results are us-biased
// "Jamaica": ["Clarendon Parish, Jamaica", "St. James Parish, Jamaica", "St. Thomas Parish, Jamaica", "Westmoreland Parish, Jamaica"], // in case results are us-biased
"Jammu and Kashmir": ["Jammu and Kashmir, India"],
"Johnston Island": ["Johnston Atoll"],
"Korea": ["North Korea", "South Korea"],
@ -409,7 +408,7 @@
"Neutral Zone": ["39944, Saudi Arabia", "76611, Saudi Arabia"],
"New Hebrides": ["Vanuatu"],
"North Vietnam": ["Ha Giang Province, Vietnam", "Lai Chau Province, Vietnam", "Thua Thien-Hue Province, Vietnam"],
"North Yemen": ["Al Jawf, Yemen", "Hajjah, Yemen", "Ta'izz, Yemen"],
"North Yemen": ["Al Jawf, Yemen", "Hajjah, Yemen", "Ta'izz Province, Yemen"],
"Northern Cyprus": ["Karpass, Cyprus", "Kokkina, Cyprus", "Lympia, Cyprus"],
"Pacific Islands": ["Marshall Islands", "Micronesia", "Northern Mariana Islands", "Palau"],
// "Palestine": ["71, Israel", "El-arish Rafah, Egypt"],
@ -419,21 +418,21 @@
"San Marino": ["San Marino, Europe"], // in case results are us-biased
"Serbia and Montenegro": ["Montenegro", "Serbia"],
"Siam": ["Thailand"],
"Sint Maarten": ["Sint Maarten, Netherlands Antilles"],
"Sint Maarten": ["Great Bay, Sint Maarten", "Oyster Pond, Sint Maarten", "Simpson Bay Lagoon, Sint Maarten"],
"South Ossetia": ["Shida Kartli"],
// see http://en.wikipedia.org/wiki/Sudan#States_and_regions
// "South Sudan": [
// "Central Equatoria, Sudan", "Eastern Equatoria, Sudan", "Jonglei, Sudan", "Lakes, Sudan", "Northern Bahr el Ghazal, Sudan",
// "Upper Nile, Sudan", "Unity, Sudan", "Warrap, Sudan", "Western Bahr el Ghazal, Sudan", "Western Equatoria, Sudan"
// ],
"South Vietnam": ["Ca Mau Province, Vietnam", "Da Nang Province, Vietnam", "Kn Giang Province, Vietnam"],
"South Vietnam": ["Ca Mau Province, Vietnam", "Da Nang Province, Vietnam", "Khanh Hoa Province, Vietnam", "Kien Giang Province, Vietnam"],
"South Yemen": ["Al-Mahrah, Yemen", "Lahij, Yemen"],
"Soviet Union": [
"Armenia", "Azerbaijan", "Belarus", "Estonia", "Georgia, Europe",
"Latvia", "Lithuania", "Kazakhstan", "Kyrgyzstan", "Moldova",
"Russia", "Tajikistan", "Turkmenistan", "Ukraine", "Uzbekistan"
],
"United Kingdom": ["England", "Northern Ireland", "Scotland", "Wales, United Kingdom"],
"United Kingdom": ["England", "Northern Ireland", "Scotland"],
"United States Minor Outlying Islands": [
"Baker Island, UM", "Howland Island", "Jarvis Island", "Johnston Atoll", "Kingman Reef",
"Midway Islands", "Palmyra Atoll", "Wake Island"
@ -442,9 +441,7 @@
"United States Miscellaneous Pacific Islands": [
"Baker Island, UM", "Howland Island", "Jarvis Island", "Kingman Reef", "Palmyra Atoll"
],
"UK": ["England", "Northern Ireland", "Scotland", "Wales, United Kingdom"],
"Upper Volta": ["Burkina Faso"],
"Wake Island": ["Wake Atoll"],
"Wales": ["Wales, United Kingdom"],
"West Germany": ["Schleswig Holstein", "Northrhine Westphalia", "Bavaria, Germany"],
"Yugoslavia": [

View file

@ -53,17 +53,14 @@ def get_countries():
html = read_wikipedia_url('ISO 3166-3')
matches = re.compile('<td id="([A-Z]{4})">.*?<a href="/wiki/(.*?)".*?>', re.DOTALL).findall(html)
countries += map(lambda x: parse(x), matches)
print sorted(map(lambda x: x['name'], countries))
# ISO 3166-1 alpha-2
html = fix(read_wikipedia_url('ISO 3166-1 alpha-2'))
matches = re.compile('<tt>([A-Z]{2})</tt></td>\n<td><a href="/wiki/(.*?)"', re.DOTALL).findall(html)
countries += filter(lambda x: not exists(x), map(lambda x: parse(x), matches))
print sorted(map(lambda x: x['name'], countries))
# List of sovereign states
html = read_wikipedia_url('List of sovereign states')
matches = re.compile('>&#160;</span><a href="/wiki/(.*?)"', re.DOTALL).findall(html)
countries += filter(lambda x: not exists(x), map(lambda x: parse(x), matches))
print sorted(map(lambda x: x['name'], countries))
'''
for year in range(1970, 2020, 10):
html = read_wikipedia_url('List of sovereign states in the %ds' % year)
@ -119,7 +116,7 @@ def get_country_data(country):
for c, d in DATA['dependencies'].iteritems():
c = c.split(', ')
if name in c:
country['dependecies'] = d if not 'dependencies' in country else country['dependencies'] + d
country['dependencies'] = d if not 'dependencies' in country else country['dependencies'] + d
elif name in d:
country['dependency'] = c if not 'dependency' in country else country['dependency'] + c
# disputes
@ -152,10 +149,6 @@ def get_country_data(country):
country['languages'] = [language]
else:
country['languages'].append(language)
# location
if name in DATA['location']:
for key, value in DATA['location'][name].iteritems():
country[key] = value
return country
def get_flag(id):

View file

@ -1,6 +1,7 @@
import Image
import json
import os
import sys
svg_path = '../svg/icons/'
png_path = '../png/icons/4096/'
@ -21,16 +22,17 @@ for file in os.listdir(svg_path):
image = image.resize((size, size), Image.ANTIALIAS)
image.save(png_file.replace('/4096/', '/%d/' % size))
for file in os.listdir('../png/flags/'):
if file[-4:] == '.png':
country = file[:-4]
png_file = png_path + country + '.png'
print png_file
image = Image.open(png_file)
# include 4096 to overwrite manually generated image
for size in [4096, 1024, 256, 64, 16]:
image = image.resize((size, size), Image.ANTIALIAS)
image.save(png_file.replace('/4096/', '/%d/' % size))
if sys.argv[-1] == '-png':
for file in os.listdir('../png/flags/'):
if file[-4:] == '.png':
country = file[:-4]
png_file = png_path + country + '.png'
print png_file
image = Image.open(png_file)
# include 4096 to overwrite manually generated image
for size in [4096, 1024, 256, 64, 16]:
image = image.resize((size, size), Image.ANTIALIAS)
image.save(png_file.replace('/4096/', '/%d/' % size))
image = Image.new('RGB', (1152,1152))
f = open('../json/countries.json')