Convert a map in shapefile into occupancy map
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Fedi Chaarana
am 15 Sep. 2020
Kommentiert: Cris LaPierre
am 5 Okt. 2020
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 Kommentare
KSSV
am 15 Sep. 2020
What is occupancy map? You have any reference? Is shape file a line/curve or polygon? Give more details.
Akzeptierte Antwort
Cris LaPierre
am 1 Okt. 2020
Bearbeitet: Cris LaPierre
am 1 Okt. 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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/369178/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/369181/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/369184/image.png)
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 Kommentare
Cris LaPierre
am 5 Okt. 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.
Weitere Antworten (2)
Cris LaPierre
am 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 Kommentare
Cris LaPierre
am 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?
Fedi Chaarana
am 29 Sep. 2020
2 Kommentare
Cris LaPierre
am 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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!