How to find points inside Singapore?

1 回表示 (過去 30 日間)
Shunan Sheng
Shunan Sheng 2022 年 1 月 13 日
編集済み: Adam Danz 2022 年 11 月 3 日
I have generated a set of geographical points with latitude and longtitude, I want to pick up the points residing inside the border of Singapore. There are similar answers in https://www.mathworks.com/matlabcentral/answers/311766-how-to-mask-data-points-outside-a-border-using-geoshow and https://www.mathworks.com/matlabcentral/answers/444653-how-can-i-remove-the-region-outside-of-the-vector-data-when-i-combine-the-vector-and-raster-data-on. However. I failde to replicate the code as it seems that shaperead does not contain information about Singapore . Where can I find the border information needed?
S = shaperead('landareas', 'UseGeoCoords', true,...
'Selector',{@(name) strcmp(name,'Singapore'), 'Name'}); % the original code is "Australia"
  2 件のコメント
Adam Danz
Adam Danz 2022 年 1 月 13 日
編集済み: Adam Danz 2022 年 1 月 13 日
The landareas.shp file referenced in your first link contains continent land areas which is why Singapore, Germany, USA, etc is not included. Here is a list of names in that file:
landareas = shaperead('landareas.shp','UseGeoCoords',true);
names = {landareas.Name};
names(~cellfun('isempty',names))'
ans = 25×1 cell array
{'Antarctica' } {'Africa and Eurasia' } {'North and South America' } {'Greenland' } {'Australia' } {'Baffin Island' } {'Ellesmere Island' } {'New Guinea' } {'Great Britain' } {'Borneo' } {'Honshu' } {'Victoria Island' } {'Celebes' } {'New Zealand North Island'} {'Sumatra' } {'Madagascar' } {'Iceland' } {'New Zealand South Island'} {'Newfoundland' } {'Luzon' } {'Devon Island' } {'Ireland' } {'Cuba' } {'Java' } {'Mindanao' }
You could try using the borders function from the file exchange to get the (lon,lat) coordinates of Singapore but it may not be precise enough for you since it looks like it doesn't return the satellite islands and islets.
Shunan Sheng
Shunan Sheng 2022 年 1 月 14 日
編集済み: Shunan Sheng 2022 年 1 月 14 日
Thank you so much for the reply! Do you know places where I can find precise border for small countries like Singapore?

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

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 1 月 13 日
編集済み: Cris LaPierre 2022 年 1 月 13 日
You are correct, landareas does not contain a shape with the name 'Singapore'.
landareas = shaperead('landareas.shp','UseGeoCoords',true);
figure;
axesm('miller','MapLatLimit',[-1 4],'MapLonLimit',[100 108]);
geoshow(landareas)
find(strcmp({landareas.Name},'Singapore'))
ans = 1×0 empty double row vector
If you want to capture the polygon here that may represent Singapore in landareas.shp, you will have to find it manually.
figure
geoshow(landareas(372));
You can extract the associated lat and lon values, and use inpolygon to search for points that are within the shape.
singLat = landareas(372).Lat;
singLon = landareas(372).Lon;
  4 件のコメント
Shunan Sheng
Shunan Sheng 2022 年 1 月 14 日
Thank you so much! I will look at the package.
Sean de Wolski
Sean de Wolski 2022 年 1 月 14 日
inpolygon will not work unless you project the coordinates from lat/lon to a cartesian coordinate system. In R2021b, there's a new class geopolyshape that has an isinterior method that makes this much easier than it used to be.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2022 年 1 月 14 日
編集済み: Adam Danz 2022 年 11 月 3 日
> Do you know places where I can find precise border for small countries like Singapore?
Search the internet for shp files containing information about Singapore. I briefly searched data.gov.sg and found 294 shp files on Singapore from census data, municiple planning areas, and lots of different maps.
Feel free leave a comment below this answer with resources you've found for shp files.
Here's a demo using the 2010 Singapore Region Census.
  1. Go to the 2010 census link above, download the zip file; unzip all files.
  2. Add the folder containing Region_Census2010.shp to your MATLAB path using addpath(...)
  3. Run the code below to see the map produced below. Note, these data break Singapore into Central, West, North, North-East, and East regions but is also includes at least some of the island borders.
  4. See notes in code below for more info.
First let's look at the shape info
info = shapeinfo('Region_Census2010.shp')
info =
struct with fields:
Filename: [3×108 char]
ShapeType: 'Polygon'
BoundingBox: [2×2 double]
NumFeatures: 5
Attributes: [8×1 struct]
CoordinateReferenceSystem: [1×1 projcrs]
info.CoordinateReferenceSystem
ans =
projcrs with properties:
Name: "SVY21"
GeographicCRS: [1×1 geocrs]
ProjectionMethod: "Transverse Mercator"
LengthUnit: "meter"
ProjectionParameters: [1×1 map.crs.ProjectionParameters]
The ProjectionMethod is "Transverse Mercator" which is "tranmerc" in Matlab if you choose to use map axes (eg, axesm('tranmerc')).
The coordinate reference system is not a geocrs object so this dataset does not use geographical coordinates (lat/lon) as described here. If you need Lat/Lon you'll either need to find a different sph file or perhaps these values can be converted.
Plot singapore
% read shp file
% Note that S is a 5x1 structure: 1 for each region of singapore
S = shaperead('Region_Census2010.shp','UseGeoCoords',true)
% Plot regions
axes()
hold on
for i = 1:numel(S)
plot(S(i).Lon, S(i).Lat)
end
axis equal
  3 件のコメント
Adam Danz
Adam Danz 2022 年 1 月 18 日
Thanks for the tips, @Sean de Wolski
Adam Danz
Adam Danz 2022 年 11 月 3 日
編集済み: Adam Danz 2022 年 11 月 3 日
Additional shp file resources I've found

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

Community Treasure Hunt

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

Start Hunting!

Translated by