Generating 50 Random locations inside geo map data
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Omar Elsayed
2017-5-26
Can you please elaborate on what exactly you want your code to do? How big do the locations have to be, Points or Regions ?
回答(1 个)
Kelly Kearney
2017-5-26
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!