필터 지우기
필터 지우기

Generating 50 Random locations inside geo map data

조회 수: 4 (최근 30일)
matthew
matthew 2014년 5월 17일
답변: Kelly Kearney 2017년 5월 26일
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
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
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by