How getting coordinates of geagraphical locations?

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

 Risposta accettata

Geoff Hayes
Geoff Hayes il 7 Ott 2014
Modificato: Geoff Hayes il 8 Ott 2014
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.

6 Commenti

that's really what I was looking for
Thank you so much
Hi Jeff,
the function seems to work for few places: like it doesn't work in the following istances:
[lat,lon] = getLatLonViaYql('Oslo','NO')
[lat,lon] = getLatLonViaYql('Sofia','BG')
[lat,lon] = getLatLonViaYql('Berlin','DE')
in all of these lats and lons are zero.
What am I doing wrong-?
Thanks
*EDIT
*
I solved it: in that cases length(posData) is upper than 2 because two entries were found from the xmldata.
Thanks
Hi Pietro - you are not doing anything wrong, it is just that the YQL query is returning at least two records for Oslo (I didn't check the other two, but they are most likely suffering from the same problem). You will need to adjust the where clause to include an additional condition
yqlQuery = sprintf(['select centroid.latitude,centroid.longitude ' ...
'from geo.places ' ...
'where text="%s" and country.code="%s" and ' ...
'placeTypeName.code="7"'], ...
city, countryCode);
Note the line and placeTypeName.code="7". This code corresponds to "Town". In the case of Oslo, there were two records returned - one with this code of 7, and the other without. The London and Rome examples both returned one record with the "Town" code too. I updated the code with the extra condition and re-ran it for Rome, London, Oslo, Sofia, and Berlin. It worked for all three.
Thanks again. You're help has been very important
Glad it worked out!
See my post below: the query on the yahoo site does not work anymore...
Any idea ?

Accedi per commentare.

Più risposte (2)

Do you have the mapping toolbox? If so, the inputm function lets you click on a map to get geographic coordinates.
If it's just a few locations you need, there's always Wikipedia or this site.
As a matter of style, I strongly recommend against ever using bubble plots. Circles are two dimensional, but your variable, let's call it population, is one dimensional. As Edward Tufte points out, you should never let the dimensions of the display exceed the dimensions of the data. This is particularly important in the case of circles, because our eyes do not scale the size of circles linearly.
A question I always have when I see bubble plots: is the radius of the circle scaling with the data, or is the area of the circle scaling with the data? That is rarely clear. Even if you are explicit about which scaling you use, the way we perceive the size of a circle is not linear with radius or area.
Inevitably, if you place a giant circle over London to show its big population, some of that circle will overlap outlying towns that are not represented by that circle, and that just does not make any sense at all. It would be more insightful to plot population density with a color map or even contours. That way you'd preserve the nuance of the original data instead of distilling everything down and saying to your viewer, "look, this circle is bigger than this other circle". Just a personal preference, but I say leave bubble plots to the click bait sites.

7 Commenti

Hi Chad,
thanks for you helpful reply. You're on right about the bouble plot, I'll do a map color. About clicking to the map is not practical because I have around 200 locations to click on. Any other way to get the coordinates of a location?
Thanks
Regards
Pietro
See Geoff's solution. It's pretty darn cool.
Chad,
how would you plot a colormap plot? I just need a cicles with the same size, colored according to their values?
Thanks
Ah, if all the circles are the same size that's fine. I thought you'd be scaling the size of the circles relative to some variable. No color map or heat map necessary. :)
Hi, I am bringing back the post from the dead, because the https://developer.yahoo.com/yql/ is ritered. Any other solutions?
Hi pietro,
it is clear that there is a huge problem if the website does not exist anymore.
Did you find a solution?
Hi Sebastien,
unfortunately, no solution yet. :(

Accedi per commentare.

Hi all,
the query.yahooapis.com site does not work anymore since few days.
Do you have an idea to bypass this yahoo queries?
Best regards.
Capture.PNG

2 Commenti

It seems there is geo place name data that can be downloaded here, but not sure how to use with Matlab.
that is good resource. You can access to website to the API. I am not an expert on this.

Accedi per commentare.

Categorie

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by