Convert a map in shapefile into occupancy map

조회 수: 26 (최근 30일)
Fedi Chaarana
Fedi Chaarana 2020년 9월 15일
댓글: Cris LaPierre 2020년 10월 5일
Hallo
I have a 2Dmaps of a buildings that are saved in shapefiles and I need to convert it into an occupancygrid map so I can plan a Path using RRT toolbox(navigation toolbox)
I already searched online in Matlab documentation but I found only an exemple of how to convert a picture into an occupancy map.
Glad to have an answer Or ay Tipp that can help
thanx
  댓글 수: 2
KSSV
KSSV 2020년 9월 15일
What is occupancy map? You have any reference? Is shape file a line/curve or polygon? Give more details.
Fedi Chaarana
Fedi Chaarana 2020년 9월 27일
Hallo
My Shapefile is a polygon of a building and the occupancy grid map is a binary map where i can use the toolbox of robotics.
the maini goal of my work is to create a route planning tool in this shapefiles of a building
this cant be done unless i can convert the maps saved as shapefiles into occupancy maps

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 10월 1일
편집: Cris LaPierre 2020년 10월 1일
Thank you for sharing your shapefile. The first thing to realize is that your shapefile's X and Y data contains data for 9 polygons, each separated by NaN (at indices [115 127 150 174 184 199 235 243 261]).
S= shaperead("C2018_wege_garten.shp");
% For simplicity, shift towards origin (10 pixel buffer)
S.X = S.X-min(S.X(:))+10;
S.Y = S.Y-min(S.Y(:))+10;
% Find polygons
s = [0,find(isnan(S.X))];
% Plot each polygon separately
for p = 2:length(s)
plot(S.X(s(p-1)+1:s(p)-1),S.Y(s(p-1)+1:s(p)-1))
hold on
end
Now that you have the polygon information you can use poly2mask in combination with inpolygon to create your binary map. I'll leave it to you to read the documentation pages to figure out how each of these work. The approach I took was to use poly2mask to turn the outline into mask where everything inside was 1.
I then used inpolygon with the remaining polygons and subtracted them from the mask. The result is a binary map where everything yellow in your original image is represented by a 1 in the mask (white), and everything else is black.
The code looks more intimidating that it is. It's mostly math on the indices.
% Create grid containing all x and y indices for map
[x,y]=meshgrid(1:300,1:250);
for p = 2:length(s)
if p==2
% create binary map bg using first polygon (indices 1:114)
bg = poly2mask(S.X(s(p-1)+1:s(p)-1),S.Y(s(p-1)+1:s(p)-1),250,300);
else
% determine indices inside the remaining polygons
tmp = inpolygon(x,y,S.X(s(p-1)+1:s(p)-1),S.Y(s(p-1)+1:s(p)-1));
% Subtract the interior polygons from binary map
bg = bg - tmp;
end
end
% display the final binary map, bg
figure
imshow(bg)
axis xy
axis equal
  댓글 수: 2
Fedi Chaarana
Fedi Chaarana 2020년 10월 5일
only one comment: the pictures you posted are the opposite of the code
the white areas are when i tested the black ones and vice versa
Cris LaPierre
Cris LaPierre 2020년 10월 5일
Not when I run it on my computer, but it appears to be working for you, so I guess it doesn't matter either way.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2020년 9월 27일
I'll take the "offer a tip" route. That means I may not know what I'm talking about.
I have some familiarity with shapefiles, but not occupancy maps (MATLAB seems to use the term 'Occupancy Grid'). A quick read suggests that any area representing a building, etc should have a value of true in a binary grid.
Therefore, my suggestion is to load your shapefile, and then for each polygon in the returned structure, use the inpolygon function to set the corresponding grid values within that polygon to true.
  댓글 수: 5
Fedi Chaarana
Fedi Chaarana 2020년 9월 28일
Cris LaPierre
Cris LaPierre 2020년 9월 29일
Can you share your shapefile? Or a mat file containg your structure? Also, what is the size/dimensions of your binary grid?

댓글을 달려면 로그인하십시오.


Fedi Chaarana
Fedi Chaarana 2020년 9월 29일
thats the file you asked for
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2020년 9월 29일
That is the code you use to load your shape file. However, I would need the actual shapefile 'C2018_wege_garten.shp' to do anything more.
Fedi Chaarana
Fedi Chaarana 2020년 9월 29일
sorry that u didnt get it
here is the zip datei

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by