2

I want to have certain code such that given the latitude and longitude as input, I want to get the continent which it belongs to? I know I can manually read the coordinates and then see that from the map. But is there an easier way to do it?

user34790
  • 2,020
  • 7
  • 30
  • 37
  • This thread might be useful... http://gis.stackexchange.com/questions/30254/dividing-earth-into-continent-polygons – bla Dec 17 '12 at 07:02

2 Answers2

9

I've done this code just for getting the continent. It is not 100% exact, but pretty good for most territories.

I did this to identify continents as asked by PVsyst, so I differenciate between Australia and Pacific, but this can be easily changed:

If you do not want to differenciate between Australia and Pacific, just remove the Australian part (LatAus,LonAus, remove it form contientIndex and from Continent List).

enter image description here

function Continent = GetContinent(Lat,Lon,plotTrueFalse)

LatNAm = [90       90  78.13   57.5  15  15  1.25  1.25  51  60    60];
LonNAm = [-168.75 -10 -10     -37.5 -30 -75 -82.5 -105  -180 -180 -168.75];
LatNA2 = [51    51  60];
LonNA2 = [166.6 180 180];

LatSAm = [1.25   1.25  15  15 -60 -60];
LonSAm = [-105 -82.5  -75 -30 -30 -105];

LatEur = [90   90  42.5 42.5 40.79 41 40.55 40.40 40.05 39.17 35.46 33   38  35.42 28.25 15  57.5  78.13];
LonEur = [-10 77.5 48.8 30   28.81 29 27.31 26.75 26.36 25.19 27.91 27.5 10 -10  -13   -30 -37.5 -10];

LatAfr = [15  28.25 35.42 38 33   31.74 29.54 27.78 11.3 12.5 -60 -60];
LonAfr = [-30 -13   -10 10 27.5 34.58 34.92 34.46 44.3 52    75 -30];

LatAus = [-11.88 -10.27 -10 -30    -52.5 -31.88];
LonAus = [110      140  145 161.25 142.5  110];

LatAsi = [90   42.5 42.5 40.79 41 40.55 40.4  40.05 39.17 35.46 33   31.74 29.54 27.78 11.3 12.5 -60 -60 -31.88 -11.88 -10.27 33.13 51    60  90];
LonAsi = [77.5 48.8 30   28.81 29 27.31 26.75 26.36 25.19 27.91 27.5 34.58 34.92 34.46 44.3 52   75  110  110   110    140    140   166.6 180 180];
LatAs2 = [90    90      60      60];
LonAs2 = [-180 -168.75 -168.75 -180];

LatAnt = [-60 -60 -90 -90];
LonAnt = [-180 180 180 -180];

inNAm         = inpolygon(Lat,Lon,LatNAm,LonNAm) + inpolygon(Lat,Lon,LatNA2,LonNA2);
inEur         = inpolygon(Lat,Lon,LatEur,LonEur);
[inSAm,onSAm] = inpolygon(Lat,Lon,LatSAm,LonSAm);
[inAfr,onAfr] = inpolygon(Lat,Lon,LatAfr,LonAfr);
[inAus,onAus] = inpolygon(Lat,Lon,LatAus,LonAus);
[inAsi,onAsi] = inpolygon(Lat,Lon,LatAsi,LonAsi);
[inAs2,onAs2] = inpolygon(Lat,Lon,LatAs2,LonAs2);

ContinentIndex  = 1 +(inAfr||onAfr)*1 + (inAus||onAus)*2 + (inAsi||onAsi||inAs2||onAs2)*3 + inEur*4 + inNAm*5 + (inSAm||onSAm)*6;
ContinentList   = {'Pacific','Africa','Australia','Asia','Europe','North_America','South_America'};

Continent       = ContinentList{ContinentIndex};
%% If you want to show the figure, set plotTrueFalse to true or to ~=0.
if plotTrueFalse
    geoshow('landareas.shp', 'FaceColor', [1 1 1]);
    axis([-180 180 -90 90])
    set(gca,'xtick',-180:15:180,'ytick',-90:15:90)
    grid on;
    hold on;
    patch(LonNAm,LatNAm,'r');
    patch(LonNA2,LatNA2,'r');
    patch(LonSAm,LatSAm,'g');
    patch(LonEur,LatEur,'b');
    patch(LonAfr,LatAfr,'k');
    patch(LonAus,LatAus,[1 0.5 0]);
    patch(LonAsi,LatAsi,'y');
    patch(LonAs2,LatAs2,'y');
    patch(LonAnt,LatAnt,[0 1 1]);
    alpha(0.5);
end
Daniel Pereira
  • 391
  • 3
  • 6
  • For implementing this in Java, you may refer to this: https://stackoverflow.com/questions/8721406/how-to-determine-if-a-point-is-inside-a-2d-convex-polygon – Ahmet Noyan Kızıltan Jul 17 '17 at 02:25
1

You can use Google geocoding API. Suppose we want to search for latitude = 22, longitude = 77(falls in Asia), you can run this code:

  url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=22,77&sensor=true';
  urlread(url)

This will return a number of details amongst which you also get the country in which that latitude, longitude is found. Now you can look up that country in a look up table. You will need to create this look up table(or download it).

I created such a look up table from the one provided in this answer: https://stackoverflow.com/a/1283168/845528

The complete code is here:

     str = {'AD','Europe';'AE','Asia';'AF','Asia';'AG','North America';'AI','North America';'AL','Europe';'AM','Asia';'AN','North America';'AO','Africa';'AQ','Antarctica';'AR','South America';'AS','Australia';'AT','Europe';'AU','Australia';'AW','North America';'AZ','Asia';'BA','Europe';'BB','North America';'BD','Asia';'BE','Europe';'BF','Africa';'BG','Europe';'BH','Asia';'BI','Africa';'BJ','Africa';'BM','North America';'BN','Asia';'BO','South America';'BR','South America';'BS','North America';'BT','Asia';'BW','Africa';'BY','Europe';'BZ','North America';'CA','North America';'CC','Asia';'CD','Africa';'CF','Africa';'CG','Africa';'CH','Europe';'CI','Africa';'CK','Australia';'CL','South America';'CM','Africa';'CN','Asia';'CO','South America';'CR','North America';'CU','North America';'CV','Africa';'CX','Asia';'CY','Asia';'CZ','Europe';'DE','Europe';'DJ','Africa';'DK','Europe';'DM','North America';'DO','North America';'DZ','Africa';'EC','South America';'EE','Europe';'EG','Africa';'EH','Africa';'ER','Africa';'ES','Europe';'ET','Africa';'FI','Europe';'FJ','Australia';'FK','South America';'FM','Australia';'FO','Europe';'FR','Europe';'GA','Africa';'GB','Europe';'GD','North America';'GE','Asia';'GF','South America';'GG','Europe';'GH','Africa';'GI','Europe';'GL','North America';'GM','Africa';'GN','Africa';'GP','North America';'GQ','Africa';'GR','Europe';'GS','Antarctica';'GT','North America';'GU','Australia';'GW','Africa';'GY','South America';'HK','Asia';'HN','North America';'HR','Europe';'HT','North America';'HU','Europe';'ID','Asia';'IE','Europe';'IL','Asia';'IM','Europe';'IN','Asia';'IO','Asia';'IQ','Asia';'IR','Asia';'IS','Europe';'IT','Europe';'JE','Europe';'JM','North America';'JO','Asia';'JP','Asia';'KE','Africa';'KG','Asia';'KH','Asia';'KI','Australia';'KM','Africa';'KN','North America';'KP','Asia';'KR','Asia';'KW','Asia';'KY','North America';'KZ','Asia';'LA','Asia';'LB','Asia';'LC','North America';'LI','Europe';'LK','Asia';'LR','Africa';'LS','Africa';'LT','Europe';'LU','Europe';'LV','Europe';'LY','Africa';'MA','Africa';'MC','Europe';'MD','Europe';'ME','Europe';'MG','Africa';'MH','Australia';'MK','Europe';'ML','Africa';'MM','Asia';'MN','Asia';'MO','Asia';'MP','Australia';'MQ','North America';'MR','Africa';'MS','North America';'MT','Europe';'MU','Africa';'MV','Asia';'MW','Africa';'MX','North America';'MY','Asia';'MZ','Africa';'NA','Africa';'NC','Australia';'NE','Africa';'NF','Australia';'NG','Africa';'NI','North America';'NL','Europe';'NO','Europe';'NP','Asia';'NR','Australia';'NU','Australia';'NZ','Australia';'OM','Asia';'PA','North America';'PE','South America';'PF','Australia';'PG','Australia';'PH','Asia';'PK','Asia';'PL','Europe';'PM','North America';'PN','Australia';'PR','North America';'PS','Asia';'PT','Europe';'PW','Australia';'PY','South America';'QA','Asia';'RE','Africa';'RO','Europe';'RS','Europe';'RU','Europe';'RW','Africa';'SA','Asia';'SB','Australia';'SC','Africa';'SD','Africa';'SE','Europe';'SG','Asia';'SH','Africa';'SI','Europe';'SJ','Europe';'SK','Europe';'SL','Africa';'SM','Europe';'SN','Africa';'SO','Africa';'SR','South America';'ST','Africa';'SV','North America';'SY','Asia';'SZ','Africa';'TC','North America';'TD','Africa';'TF','Antarctica';'TG','Africa';'TH','Asia';'TJ','Asia';'TK','Australia';'TM','Asia';'TN','Africa';'TO','Australia';'TR','Asia';'TT','North America';'TV','Australia';'TW','Asia';'TZ','Africa';'UA','Europe';'UG','Africa';'US','North America';'UY','South America';'UZ','Asia';'VC','North America';'VE','South America';'VG','North America';'VI','North America';'VN','Asia';'VU','Australia';'WF','Australia';'WS','Australia';'YE','Asia';'YT','Africa';'ZA','Africa';'ZM','Africa';'ZW','Africa'};
    url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=22,77&sensor=true';
    queryResult = urlread(url)
    countryName = queryResult(672:673);
    strfind(cellstr(str), countryName);
    continent = str(find(strcmp(countryName, str)), 2)
Community
  • 1
  • 1
Spectre
  • 682
  • 10
  • 26
  • nice idea, but for the example you give only the country name is obtained (and info below that resolution, i.e. county, city...). In order to get the continent one needs to add at least one more line of code. – bla Dec 17 '12 at 06:59
  • Well, yes. But do take a look at this: http://stackoverflow.com/a/4477549/845528. – Spectre Dec 17 '12 at 07:08
  • Well, here's a url that will give you the link between continent and country... http://www.worldatlas.com/cntycont.htm – bla Dec 17 '12 at 07:18