Convert a map in shapefile into occupancy map

16 次查看(过去 30 天)
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 个评论
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by