0001 classdef QueryNeighborhood
0002
0003
0004
0005 properties
0006 data;
0007 searchType;
0008 distanceType;
0009 searchKNN;
0010 searchRadius
0011 end
0012
0013 methods
0014 function obj = QueryNeighborhood(data, varargin)
0015
0016 validData = @(x) size(x, 1) > 0 && size(x, 2) == 3;
0017 validScalarGreaterThanZero = @(x) isscalar(x) && x > 0;
0018 validSearchType = @(x) ischar(x) && strcmpi(x, 'radial') || strcmpi(x, 'knn');
0019 validDistanceType = @(x) ischar(x) && strcmpi(x, 'euclidean') || strcmpi(x, 'haversine');
0020
0021
0022 p = inputParser;
0023 addRequired(p, 'data', validData);
0024 addParameter(p, 'Radius', 5, validScalarGreaterThanZero);
0025 addParameter(p, 'SearchType', 'radial', validSearchType);
0026 addParameter(p, 'K', 25, validScalarGreaterThanZero);
0027 addParameter(p, 'DistanceType', 'euclidean', validDistanceType);
0028 parse(p, data, varargin{:});
0029
0030
0031 obj.data = p.Results.data;
0032 obj.searchRadius = p.Results.Radius;
0033 obj.searchKNN = p.Results.K;
0034 obj.searchType = p.Results.SearchType;
0035 obj.distanceType = p.Results.DistanceType;
0036 end
0037
0038 function [ind, distances] = getNeighbors(obj, x, y)
0039
0040 if strcmpi(obj.searchType, 'radial')
0041 if strcmpi(obj.distanceType, 'euclidean')
0042 [ind, distances] = rangesearch(obj.data(:, 1:2), [x(:) y(:)], obj.searchRadius);
0043 else
0044 [ind, distances] = rangesearch(obj.data(:, 1:2), [x(:) y(:)], obj.searchRadius, 'Distance', @haversine);
0045 end
0046 else
0047 if strcmpi(obj.distanceType, 'euclidean')
0048 [ind, distances] = knnsearch(obj.data(:, 1:2), [x(:) y(:)], 'K', obj.searchKNN);
0049 else
0050 [ind, distances] = knnsearch(obj.data(:, 1:2), [x(:) y(:)], 'K', obj.searchKNN, 'Distance', @haversine);
0051 end
0052
0053 ind = num2cell(ind, 2);
0054 distances = num2cell(distances, 2);
0055 end
0056 end
0057 end
0058 end
0059