How to find points inside a country border

13 ビュー (過去 30 日間)
Thao Linh Tran
Thao Linh Tran 2019 年 1 月 14 日
コメント済み: Adam 2024 年 2 月 27 日
Hi everyone, I have a 2 dimension matrix containing lat, lon of multiple points. I would like to ask whether there is any way in Matlab to define which points are inside the border of a specified country. Any suggestion would be greatly appreciated!

採用された回答

Akira Agata
Akira Agata 2019 年 1 月 15 日
How abou the following?
(In this script, I have used shape file provided in http://thematicmapping.org/downloads/world_borders.php)
% Load shape file of country border
S = shaperead('TM_WORLD_BORDERS_SIMPL-0.3.shp');
S = struct2table(S);
% Target lat and lon (e.g Tokyo)
lat = 35.7;
lon = 139.7;
% Find which country contains (lat,lon)
idx = cellfun(@(X,Y) inpolygon(lon,lat,X,Y),S.X, S.Y);
% Show the result
disp(S.NAME(idx))
>> disp(S.NAME(idx))
'Japan'
  3 件のコメント
FRANCO CERVERA
FRANCO CERVERA 2022 年 7 月 5 日
Hello,
I am trying to select coordinates points from a specified countries and your script would help me very much but i have problems by running it. Could you please help me to understand what to do with this error message from the script above ?
thank you !
Error using tabular/dotParenReference
Unrecognized table variable name 'NAME'.
Error in test_shapefile (line 12)
disp(S.NAME(idx))
Adam
Adam 2024 年 2 月 27 日
Thank you @Akira Agata! Your code above works beautifly. However, when I tried the coordinates for a coastal city, the display returned no results. Upon closer inspection, I found the polygons contained in your shapefile were too coarse. I updated to the World Bank Official Boundaries shapefile (download), updated the table header from "NAME" to "FORMAL_EN" and everything is working well.

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2019 年 1 月 15 日
Read about inpolygon
  1 件のコメント
Thao Linh Tran
Thao Linh Tran 2019 年 1 月 15 日
Thank you for your suggestion!

サインインしてコメントする。


FRANCO CERVERA
FRANCO CERVERA 2022 年 7 月 5 日
Hello,
I am trying to select coordinates points from a specified countries and your script would help me very much but i have problems by running it. Could you please help me to understand what to do with this error message from the script above ?
thank you !
Error using tabular/dotParenReference
Unrecognized table variable name 'NAME'.
Error in test_shapefile (line 12)
disp(S.NAME(idx))
  1 件のコメント
KSSV
KSSV 2022 年 7 月 5 日
The error says, in the table S, there is no variable/ column with name NAME.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by