Generating 50 Random locations inside geo map data

12 views (last 30 days)
I have a function called map() which takes a string input and uses it to generate a particulare map
function map(Name)
worldmap({Name})
land = shaperead('landareas.shp', 'UseGeoCoords', true);
geoshow(land, 'FaceColor', [0.15 0.5 0.15])
what I would like to do is randomly generate from a seed 50 locations and place them on land or within the regions border,
such as i run map('Australia') and i get 50 randomly generated locations in australia.
  1 Comment
Omar Elsayed
Omar Elsayed on 26 May 2017
Can you please elaborate on what exactly you want your code to do? How big do the locations have to be, Points or Regions ?

Sign in to comment.

Answers (1)

Kelly Kearney
Kelly Kearney on 26 May 2017
Do you want the points to be specifically located in the named land mass? Or anywhere on land within the designated map limits? For example, the map limits for Australia also include bits on Indonesia and New Zealand.
If you're okay with any land mass, then the easiest way to do this would be to generate some points, test whether they're within the land polygons ( inpolygon ), and keep only those that are. Repeat until you get enough points.
Name = 'Australia';
h = worldmap({Name});
land = shaperead('landareas.shp', 'UseGeoCoords', true);
geoshow(land, 'FaceColor', [0.15 0.5 0.15])
latlim = getm(h, 'MapLatLimit');
lonlim = getm(h, 'MapLonLimit');
npt = 50;
nin = 0;
x = [];
y = [];
while nin < npt
xtmp = rand(npt,1)*diff(lonlim) + lonlim(1);
ytmp = rand(npt,1)*diff(latlim) + latlim(1);
isin = inpolygon(xtmp,ytmp, [land.Lon], [land.Lat]);
x = [x; xtmp(isin)];
y = [y; ytmp(isin)];
nin = length(x);
end
x = x(1:npt); % get rid of extra
y = y(1:npt);
plotm(y,x,'yo');
If you want to restrict the points to a specific land mass, then you'll need to filter out the correct polygons first. Using the Name property would be the easiest way to do this, but that only works for worldmap areas that share a name with the landareas.shp polygons. So Name = 'Australia' would be easy to do... for something like Name = 'Europe', you'd have to crop the 'Africa and Eurasia' polygon to your map limits before running the inpolygon test.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!