MATLAB: How getting coordinates of geagraphical locations

bubble plotsMapping Toolbox

Hi,
I have the following struct array.
data=struct('Position',[],'Value',[])
data(1).Position='London';
data(1).Value=20;
data(2).Position='Rome';
data(2).Value=100;
What I need is a bubble plot of values field with a map in the background and the bubble must be located where is the geographical location. I know this function http://www.mathworks.com/matlabcentral/fileexchange/27627-plot-google-map but I don't know how to get the coordinates of the locations. Does anyone have an idea?
Thanks
Cheers
Pietro

Best Answer

Pietro - if you know the country code for the city, then you could try the following function which makes use of the Yahoo GeoPlanet Web Service, GeoPlanet YQL Tables.
% Uses YQL to query the geo.places database for the latitude and longitude
% of a city, given the city name and country code.
function [latDegs, lonDegs] = getLatLonViaYql(city,countryCode)
% build the YQL query
yqlQuery = sprintf(['select centroid.latitude,centroid.longitude ' ...
'from geo.places ' ...
'where text="%s" and country.code="%s" and ' ...
'placeTypeName.code="7"'], ...
city, countryCode);
% build the URL
yqlUrl = ['http://query.yahooapis.com/v1/public/yql?q=' yqlQuery];
% replace blank spaces with %20
yqlUrl = strrep(yqlUrl,' ','%20');
% retrieve the data
xmlData = urlread(yqlUrl);
% get the latitude and longitude (here we assume that the latitude is
% within <latitude>XYZ</latitude> and the longitude within
% <longitude>UVW</longitude> tags)
posData = regexp(xmlData,'>[0-9.-]+<','match');
% if exactly two tags match the above regexp then parse the latitude
% and longitude data
if length(posData)==2
latDegs = str2double(char(posData{1}(2:end-1)));
lonDegs = str2double(char(posData{2}(2:end-1)));
else
latDegs = 0;
lonDegs = 0;
end
end
The latitude and longitude positions can then be obtained as
[latLondon,lonLondon] = getLatLonViaYql('London','GB')
latLondon =
51.507702
lonLondon =
-0.12797
[latRome,lonRome] = getLatLonViaYql('Rome','IT')
latRome =
41.898399
lonRome =
12.4956
In the YQL query, we grab the "centroid latitude and longitude of a "bounding box" that surrounds the city. (This box is defined by south-west and north-east positions.) Whether this is accurate enough for your application remains to be seen. But constructing two vectors as
lat = [latLondon ; latRome];
lon = [lonLondon ; lonRome];
and executing
plot(lon,lat,'.r','MarkerSize',20)
plot_google_map
puts the red markers at the positions of London and Rome on the map.
EDIT
Updated the above code to include an additional where condition/clause to ensure that the record returned corresponds to a "Town". Pietro observed that at least two records were being returned for cities like Oslo, Berlin, and Sofia.
Related Question