We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Sphere degree coordinates to 2D plane
Page Index Toggle Pages: 1
Sphere degree coordinates to 2D plane? (Read 1482 times)
Sphere degree coordinates to 2D plane?
Aug 15th, 2008, 7:56pm
 
Hi

I gathered 62 cities and their capitals in a colon separated text file.
I then ran them through this site http://www.gpsvisualizer.com/geocoder/
Great site, he has done some really useful API programming up against the Yahoo geocoder - thingy-ish.

This process gave me longitude and latitude for these major cities.
But in the format as they are placed on the sphere that is earth.
I would like to map them to a flat 2D plane, a picture of the earth.

I used the formula given in the "Visualizing Data" book by Ben Fry, to project the coordinates onto a flat surface. To my surprise this yielded the cities to be placed all wrong...

I then went to http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html which Ben mentions in the book as the source of this nifty piece of conversion math that places sphere projections on a plane.

As I understand it: how you define your standard parallel and starting points is quite important to get the right result.

So I have a picture of the world with greenwich meantime as the vertical center, how to get my coordinates mapped onto it?

Here's  my list, try going to the gpsvisualizer website, paste them in, press "Start Geocoding" and afterwards press "draw map". All the values fit in the google 2d map, so they must know the conversion :) It is quite fun, and one can give it the odd badly formatted address and it will still give a quite precise coordinate.

Argentina:Buenos Aires
Australia:Canberra
Austria:Vienna
Belgium:Brussels
Brazil:Brasília
Bulgaria:Sofia
Canada:Ottawa, Ontario
Cape Verde:Praia
Chile:Santiago
China:Beijing
Costa Rica:San José
Croatia:Zagreb
Czech Republic:Prague
Denmark:Copenhagen
Egypt:Cairo
Estonia:Tallinn
Faroe Islands:Torshavn
Finland:Helsinki
France:Paris
Germany:Berlin
United Kingdom:London
Greece:Athens
Hungary:Budapest
India:New Delhi
Iran:Tehran
Ireland:Dublin
Israel:Jerusalem
Italy:Rome
Jamaica:Kingston
Japan:Tokyo
Jordan:Amman
South Korea:Seoul
Lithuania:Vilnius
Luxembourg:Luxembourg
Malaysia:Kuala Lumpur
Mexico:Mexico City
Morocco:Rabat
Netherlands:Amsterdam
New Caledonia:Nouméa
New Zealand:Wellington
North Korea:Pyongyang
Norway:Oslo
Peru:Lima
Philippines:Manila
Poland:Warsaw
Portugal:Lisbon
Romania:Bucharest
Russia:Moscow
Slovakia:Bratislava
South Africa:Pretoria
Spain:Madrid
Sri Lanka:Colombo
Sweden:Stockholm
Switzerland:Bern
Taiwan:Taipei
Thailand:Bangkok
Dominican Republic:Santo Domingo
Turkey:Ankara
United Arab Emirates:Abu Dhabi
United States:Washington
Uruguay:Montevideo





Re: Sphere degree coordinates to 2D plane?
Reply #1 - Aug 15th, 2008, 10:27pm
 
Also check out p. 312 of the book which covers using the Google API to geocode, which will return lat/lon coordinates (as well as cleaned up names, and lots of other location info).

Once you have the lat/lon coordinates, you just have to find a proper projection. For instance, Albers works well for the U.S., and in particular a smaller area than the whole globe. For the rest of the world, you'll probably want something different, in particular if you want a whole globe.

The Wikipedia page on map projections gives more info:
http://en.wikipedia.org/wiki/Map_projection
You can use that for background on different types, then when you find a projection that's suitable for your purpose, use MathWorld to find the equation for it.

If you just want to plot 2D lat/lon coordinates, you can also start by not using a projection (y goes from -90 to 90, x from -180 to 180... just use the map() function), or using something simple like Mercator:
http://en.wikipedia.org/wiki/Mercator_projection
which is used by Google Maps and others.
Re: Sphere degree coordinates to 2D plane?
Reply #2 - Aug 15th, 2008, 10:46pm
 
Thanks Fry

Always good to have the author of the book one is currently reading , at hand 24/7 Smiley

I did the map (ping) of the coordinates, that was actually when it hit me that I forgot to take the sphere shape into account.

I will try to look at mercator, that seems to be a solution, especially if I were to redraw my map background from the perspective of a google map of the world.

The internet and crazy amounts of data and formats is taking it's toll on me tonight.
Re: Sphere degree coordinates to 2D plane?
Reply #3 - Aug 16th, 2008, 4:00am
 
I guess, thats what you are looking for. Ive found that a while ago and knew in would need it some day. Dont know who did that. Sorry, for not knowing the author. Like you see you enter the coordinates like the one from Berlin and it gets marked on the map. I uploaded the PGN-Map for you to
http://www.dec32.de/public/world_longlatwgs84.png

//code:
//........................................

WorldMap wmap;
Point[] cities;

void setup() {
 
 size(1000, 600);
 ellipseMode(CENTER);
 smooth();
 
 wmap = new WorldMap();
 cities = new Point[] {
   wmap.getPoint( 48.45,     2.35),  // Paris
   wmap.getPoint( -34.60, - 58.38),  // Buenos Aires
   wmap.getPoint(  17.96,  102.62),   // Vientiane
   wmap.getPoint(  52.31,  13.25)   // Berlin
 };
 
}

void draw() {
 
 wmap.drawBackground();
 
 fill(255, 150, 0);
 for (int i = 0; i < cities.length; i++) {
   ellipse(cities[i].x, cities[i].y, 14, 14);
 }
 
}

class WorldMap {
 
 int x, y, w, h;
 PImage raster;
 
 WorldMap() {
   this(0, 0, width, height);
 }
 
 WorldMap(int x, int y, int w, int h) {
   if (h >= w/2) {
     this.w = w;
     this.h = w/2;
     this.x = x;
     this.y = (h - this.h)/2;
   } else {
     this.h = h;
     this.w = 2*h;
     this.x = (w - this.w)/2;
     this.y = y;
   }
   raster = loadImage("world_longlatwgs84.png");
 }
 
 void drawBackground() {
   image(raster, x, y, w, h);
 }
 
 Point getPoint(float phi, float lambda) {
   return new Point(
     x + ((180+lambda)/360)*w,
     y + h - ((90+phi)/180)*h
   );
 }
 
}

class Point extends Point2D {
 Point(float x, float y) { super(x, y); }
}

class Point2D {
 float x, y;
 Point2D(float x, float y) {
   this.x = x; this.y = y;
 }
}
Re: Sphere degree coordinates to 2D plane?
Reply #4 - Aug 16th, 2008, 4:02am
 
oh and btw. Thats for the great http://www.gpsvisualizer.com/geocoder/ Link... i was working on a project where i need to know the coordinates of 300 Citys and i thought i have to look them up at wikipedia :)
Re: Sphere degree coordinates to 2D plane?
Reply #5 - Aug 16th, 2008, 4:48pm
 
Thanks Cedric

I implemented the formula and I works perfectly.
There was no need for a lot of the code so I sliced it down to this:
(however the project had grown a bit so I added my own Point class to ease the
constant array looping, but it will work just as good just by sending the
x,y, coordinate):

 Point calculateProjection(Point lat_lon)
 {
   float lambda = lat_lon.getY();
   float phi = lat_lon.getX();
   
   return new Point(((180+lambda)/360)*width, height - ((90+phi)/180)*height);
 }

and the Point Class:

class Point{
 
 float x;
 float y;
 
 Point(float _x, float _y)
 {
   x = _x;
   y = _y;
 }
 
 float getX()
 {
    return x;
 }
 float getY()
 {
    return y;
 }
}

If the map has the proportions like the one you added it will work.
It seems to be, as Ben wrote, the same proportions used by google maps and some sort of the Mercator proportions.
If the map has other proportions, a value can be added to the lambda, phi value to compensate.

If you are doing something live or with +500 positions, the google API implementation could be a better approach.
It is also covered in the "Visualizing Data" book. The Google API will let you look up 15.000 positions every 24 hours and it will let you send a partial or scrambled address and google will try and figure it out, it's quite good.
However a bit "americanized" ... looking up "Denmark" will result in a town in the middle of America with a population of 70 people... and not the country :)

Thanks again, this mapping stuff is fun!

Ricki
Page Index Toggle Pages: 1