How to find points inside Singapore?

6 ビュー (過去 30 日間)
Shunan Sheng
Shunan Sheng 2022 年 1 月 13 日
コメント済み: Adam Danz 2022 年 1 月 18 日
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 and 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 件のコメント
Shunan Sheng
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);
axesm('miller','MapLatLimit',[-1 4],'MapLonLimit',[100 108]);
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.
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 件のコメント
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 年 1 月 14 日
> 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 and found 294 shp files on Singapore from census data, municiple planning areas, and lots of different maps.
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]
ans =
projcrs with properties:
Name: "SVY21"
GeographicCRS: [1×1 geocrs]
ProjectionMethod: "Transverse Mercator"
LengthUnit: "meter"
ProjectionParameters: [1×1]
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
hold on
for i = 1:numel(S)
plot(S(i).Lon, S(i).Lat)
axis equal
  2 件のコメント
Adam Danz
Adam Danz 2022 年 1 月 18 日
Thanks for the tips, @Sean de Wolski


Community Treasure Hunt

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

Start Hunting!

Translated by