1

我想要某些代码,以便给定纬度和经度作为输入,我想得到它所属的大陆?我知道我可以手动读取坐标,然后从地图上看到。但是有没有更简单的方法呢?

4

2 回答 2

9

我已经完成了这段代码只是为了获得大陆。这不是 100% 准确的,但对于大多数地区来说都相当不错。

我这样做是为了按照 PVsyst 的要求来识别大陆,所以我区分了澳大利亚和太平洋,但这很容易改变:

如果您不想区分澳大利亚和太平洋,只需删除澳大利亚部分(LatAus、LonAus,将其从 contientIndex 和大陆列表中删除)。

在此处输入图像描述

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
于 2014-08-01T08:19:07.680 回答
1

您可以使用 Google 地理编码 API。假设我们要搜索 latitude = 22, longitude = 77(落在亚洲),你可以运行这段代码:

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

这将返回一些详细信息,您还可以在其中获得该纬度、经度所在的国家/地区。现在您可以在查找表中查找该国家/地区。您将需要创建此查找表(或下载它)。

我从这个答案中提供的一个创建了这样一个查找表:https ://stackoverflow.com/a/1283168/845528

完整的代码在这里:

     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)
于 2012-12-17T06:47:34.507 回答