3 views (last 30 days)

Hi all,

I have GPS coordinates of an off-road vehicle and I have to check when the vehicle lies on a road or on a field. I have downloaded the shape file of my region, where there are the bounding box of each field. Below, you will find my code, that is very slow because in the shape file there are around 3e5 polygons. How could I speed up the code? In my opinion, the calculation could be speeded up, if I can avoud the inner for, but I do not know how can I do it.

for i=1:length(GPSData)

OnField{i}=zeros(size(GPSData(i).Longitude));

for iS=1:length(FieldUseSHP)

a=inpolygon(GPSData(i).Longitude*pi/180,GPSData(i).Latitude*pi/180,FieldUseSHP(iS).Lon,FieldUseSHP(iS).Lat);

if any(a)==1

OnField{i}=iS.*a;

end

end

end

Thank you in advance

Best regards,

Pietro

Matt J
on 3 Apr 2019

fconv=@(z) z(:).';

Longitude=cell2mat( cellfun( fconv, {GPSData.Longitude},'uni',0) )*pi/180;

Latitude=cell2mat( cellfun( fconv, {GPSData.Latitude},'uni',0) )*pi/180;

Onfield=cell(1,length(FieldUseSHP));

for iS=1:length(FieldUseSHP)

a=inpolygon(Longitude, Latitude,FieldUseSHP(iS).Lon,FieldUseSHP(iS).Lat);

OnField{iS}=iS.*a;

end

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689574

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689574

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689672

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689672

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689687

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689687

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689840

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/454216-how-to-calculate-the-points-of-a-line-which-lies-inside-polygons#comment_689840

Sign in to comment.