Convert a map in shapefile into occupancy map

25 views (last 30 days)
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 Comments
KSSV
KSSV on 15 Sep 2020
What is occupancy map? You have any reference? Is shape file a line/curve or polygon? Give more details.
Fedi Chaarana
Fedi Chaarana on 27 Sep 2020
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

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 1 Oct 2020
Edited: Cris LaPierre on 1 Oct 2020
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 Comments
Fedi Chaarana
Fedi Chaarana on 5 Oct 2020
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 on 5 Oct 2020
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.

Sign in to comment.

More Answers (2)

Cris LaPierre
Cris LaPierre on 27 Sep 2020
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 Comments
Cris LaPierre
Cris LaPierre on 29 Sep 2020
Can you share your shapefile? Or a mat file containg your structure? Also, what is the size/dimensions of your binary grid?

Sign in to comment.


Fedi Chaarana
Fedi Chaarana on 29 Sep 2020
thats the file you asked for
  2 Comments
Cris LaPierre
Cris LaPierre on 29 Sep 2020
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!