2

I know that the latitude and longitude of Chicago is: 41.8819° N, 87.6278° W. Also the lat and long of united states is: 38.8833° N, 77.0167° W . How do I determine from latitude and longitude that chicago is in united states: is there some algorithm to do the same?

Also should not the latitude and longitude of a place be described by a lot of values rather than a single value -- e.g. united states is a big country, then how can we describe it by two points: 41.8819° N, 87.6278° W??

Can someone please help with this. Also I am coding in C/C++, it will be great if there is an library which will help me do the same using C/C++?

Jannat Arora
  • 2,759
  • 8
  • 44
  • 70
  • 1
    `38.8833° N, 77.0167° W` is somewhere in Washington DC, hardly the entire US. – T.C. Jul 05 '14 at 05:28
  • @T.C. Right..but that is what Google returned when I searched! This looked odd to me too. Therefore I am unable to understand as how can latitude and longitude values for a particular country be unique! – Jannat Arora Jul 05 '14 at 05:31
  • You might have better luck asking this on the GIS SE site – thurizas Jul 05 '14 at 05:39
  • 1
    The only thing a latitude-longitude pair represents is an exact point, not an area. (`41.8819° N, 87.6278° W` is only some place in Chicago rather than the whole city.) Looks like when you force Google to give you a single point for an entire country, it decided to give you the coordinate of some point in the capital. – T.C. Jul 05 '14 at 05:41
  • 2
    If you want to do it yourself, you need to get the whole set of coordinates that map out the borders of the (continental) U.S. and go from there. Google Maps has a [helpful API](http://stackoverflow.com/questions/4013606/google-maps-how-to-get-country-state-province-region-city-given-a-lat-long-va) for this, though. – T.C. Jul 05 '14 at 05:42
  • yep either map borders to polygon and then test if point is inside polygon or you can simplify your country to set of ellipses and test if you are inside ellipse ... of coarse that will give you wrong answer for near border points. You can obtain the polygon from vector maps (try find some svg image of US or globe) and extract polygon or path from it ... – Spektre Jul 05 '14 at 06:44
  • @Spektre Is there some C/C++ library which may help me either test if a point is within a polygon or it is inside eclipse – Jannat Arora Jul 05 '14 at 06:46
  • look for hit-test but there are many others ... added answer with some useful links for you – Spektre Jul 05 '14 at 07:13
  • @Spektre Thanks for your amazing responses. Can I find the latitude and longitude values given city names (e.g. given London or Chicago). I want to find latitude and longitude values of the boundary region. Is there some way by which I may achieve the same – Jannat Arora Jul 05 '14 at 09:23
  • may be some script on google maps will do but that is way out of mine field of knowledge. The easiest way will be to get somewhere some XML vector map of area you want and extract it from there. Local governments usually provide full GIS data of their country or regions but this service is always paid :( and not very cheap for non-commercial use either. try to look for some free navigation programs for data minnig but always check the data licensing so you do not broke some laws !!! – Spektre Jul 05 '14 at 10:41
  • I usually use NASA mrsid files from geocover surveys for mine astro/geo apps + special spatial vector GIS data provided from government geodetic office. Beware use of licensed data make sharing and distribution of SW very difficult to obey local and international laws. – Spektre Jul 05 '14 at 10:46
  • I think this may be all you need http://www.partow.net/miscellaneous/airportdatabase/ (international airports with GPS coordinates) – Spektre Jul 05 '14 at 10:55

1 Answers1

2
  1. A good idea is to convert spherical coordinates to ortogonal/orthonormal cartesian coordinates

    that will ensure avoidance of angle overflow errors. Here is entire answer about it

  2. map borders to polygon and then test if point is inside polygon

    You can obtain the polygon from vector maps. Try find some svg image of US or globe and extract polygon or path from it. To test inside of polygon there are many examples even here on SE ... so try search question for polygon+inside.

    this stuff is pretty easy so no lib is needed

  3. simplify your country to set of ellipses/circles/or what ever and test if you are inside

    Of coarse this will give you wrong answer for near border points because this representation is not accurate enough. Inside ellipse is easy you just test coordinate against ellipse equation

    enter image description here

    it is single if statement:

    if (((x-x0)*(x-x0)/a)+((y-y0)*(y-y0)/b)<=1.0f) { /* is inside */ };
    
    • x,y is your tested point
    • x0,y0 is tested ellipse center
    • a,b are its semi axises

    if you want also rotated ellipses then you need to add rotation and just rotate x,y by angle before test...

    //---------------------------------------------------------------------------
    void rotate2d(double alfa,double &x,double &y)
         {
         double   a=x,b=y,c,s;
         c=cos(alfa);
         s=sin(alfa);
         x=a*c-b*s;
         y=a*s+b*c;
         }
    //---------------------------------------------------------------------------
    
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • @Spektree Thanks for the awesome answer. But why do I need to convert lat and long to 3D. Why can not lat and long be converted to 2D points. And the 2D points be stored in an R-tree. And then R-tree's can be used to find whether or not a point is inside a polygon/ – Jannat Arora Jul 05 '14 at 07:37
  • @Spektre: If the only purpose is to see whether a given point is within a country defined by a polygon (vertices in lat/lon format), the test can be carried out by the two coordinates of the vertices as long as the vertices are not very far away from each other. – DrV Jul 05 '14 at 08:05
  • @DrV the 3D stuff is relevant for the RADAR question I have not stated it must be 3D here (still can use local 2D coordinates) but even 3D is much simpler then long/lat corections: because they must be corrected to ensure there is not the angular boundary crossed. luckily there is no state covering more then 180 degree of longitude (for now) so simple shift/offset will do but the same go for latitude where there is no such luck (poles are problematic) there must be applied some nasty corrections. – Spektre Jul 05 '14 at 08:52
  • @Jannat Arora comment above is also for you. (btw. how to safely link comment to you when your name contains a space?) – Spektre Jul 05 '14 at 08:53
  • @Spektre: My point was that if the polygon limiting a country is defined at any meaningful resolution, the projection error is negligible. Actually, many countries (and states) have borders which go along some longitude or latitude, in which case projecting the lat/lon onto cartesian 2D coordinates is the simplest projection. I am not aware of any country with borders going long distances along great circle which is not a longitude or equator. The projection is naturally awful for almost anything else, and this is a special case. – DrV Jul 05 '14 at 13:55