How do I get specific data using inpolygon in Matlab?
Show older comments
So,
I've written a code where I'm reading flight data from an excel sheet and trying to find all the flights within a particular bound using the inpolygon code. I to only record want the flight data which has all its cordinates within the polygons bound, and if it is not within the bound I don't want it to be recorded, but it isn't working...
This is the code:
clc;
clear all;
if exist('coords.mat','file')
load coords
else
lat = []; % latitude
lon = []; % longitude
alt = []; % altitude
spd = []; % speed
flg = {}; % flight
tim = []; % time
end
lat1 = xlsread('plot.xlsx', 'C2:C86400');
lon1 = xlsread('plot.xlsx', 'D2:D86400');
alt1 = xlsread('plot.xlsx', 'E2:E86400');
flg1 = xlsread('plot.xlsx', 'A2:A86400');
lat10 = [6.6038,21.841667]; lon10 = [94.4166,62.375000];%P574
% Save and continue
save('coords.mat','lat','lon','alt','flg')
pause(1)
%% Render the recorded data
% Prepare figure
d=37040;
load coords %#ok<*UNRCH>
close all
% Settings
centerLoc = [12.9716,77.5946]; % LEVC
% Prepare UTM scenario
mstruct = defaultm('utm');
mstruct.zone = utmzone(centerLoc(1),centerLoc(2));
mstruct = defaultm(mstruct);
% Plot land contours
SHPdir = '.\SHPs\';
countries = shaperead([SHPdir 'ne_10m_admin_0_countries.shp'],...
'Selector',{@(x) strcmpi(x,'es'),'foo'},'UseGeoCoords', true);
% Change 'ES.VC' for the provinces/states of your preference or use a RegExp
% for all provinces: @(x) strcmpi(x,'ES.VC') => @(x) ~isempty(regexpi(x,'^ES.*$'))
provinces = shaperead([SHPdir 'ne_10m_admin_1_states_provinces.shp'],...
'Selector',{@(x) strcmpi(x,'ES.VC'),'region_cod'},'UseGeoCoords', true);
[x,y] = mfwdtran(mstruct,[countries.Lat provinces.Lat],[countries.Lon provinces.Lon]);
[xc,yc] = mfwdtran(mstruct,centerLoc(1),centerLoc(2));
[x1,y1]= mfwdtran(mstruct,lat1,lon1);
[x10, y10]= mfwdtran(mstruct,lat10,lon10);
X = [x10,fliplr(x10)-d];
Y = [y10,fliplr(y10)];
[in,on] = inpolygon(x1,y1,X,Y);
% (in) contains the indices of the points inside the parallelogram
% (on) contains the indices of the points on the parallelogram
j =1;
i =1;
p = 1;
flightno = [] ;
k = 0;
arr = 0;
for(flg1 = 1:86413)
while(i>1)
if(flg1(i)~=flg1(i-1))
j =j+1;
p = 1;
if (k ==1)
arr=arr+1;
%plot flight no. (j - 1)
flightno(arr) = (j-1);
k = 0;
end
end
end
%checking
%define polygon and write check condition
if(x1(i)~= x1(~in))
if (p == 1)
k = 1;
else
k = 0;
p = 0;
end
end
end
Can you help me out with this please?
Accepted Answer
More Answers (1)
KSSV
on 8 Apr 2019
0 votes
4 Comments
harman bhaveja
on 8 Apr 2019
harman bhaveja
on 8 Apr 2019
KSSV
on 8 Apr 2019
Using inpolygon is straight forward. I don't know where you are stuck..? Attach your data tell us your problem.
harman bhaveja
on 8 Apr 2019
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!