How to assign points to one or several boxes

1 view (last 30 days)
Hi,
I have a small function that assign points to boxes based on latitude and longitude. This code mostly works: point A falls into box A and point B falls into box B. The main flaw is that points that fall on overlaping boxes (like point C which falls into both box A and box B) are only assigned the name of one box (box B in this case). How can I modify this code so that points that fall on overlapping boxes are assigned the name of both boxes ?
Please note that points alway fall into at least one box so the possibility of a point not being assigned any box does not exist.
Thank you,
% Small function to assign points to boxes
% Points positions
lat = [47.5, 45.5, 46.5]';
lon = [-63.5, -61.5, -62.5]';
pts_name = {'A'; 'B'; 'C'};
pts_positions = table(pts_name, lat, lon);
% Min and max values of boxes
box_name = {'box_A'; 'box_B'};
min_lat = [46, 45]';
max_lat = [48, 47]';
min_lon = [-64, -63]';
max_lon = [-62, -61]';
boxes_positions = table(box_name, min_lat, max_lat, min_lon, max_lon);
% Assign points to boxes based on latitude/longitude
for g = 1:height(boxes_positions)
h = boxes_positions(g, 1);
mask = pts_positions.lon > boxes_positions.min_lon(g) & pts_positions.lon < boxes_positions.max_lon(g) & ...
pts_positions.lat > boxes_positions.min_lat(g) & pts_positions.lat < boxes_positions.max_lat(g);
pts_positions.box(mask) = table2cell(h(1, 1));
end
  2 Comments
Mohammad Sami
Mohammad Sami on 31 Jan 2020
Are you only going to have two boxes, or this need to scale up to arbitrary number of boxes ?
Mohammad Sami
Mohammad Sami on 31 Jan 2020
Also can we assume that in your next step you are going to look up by boxes. E.g for point p1, you will classify it into box(es) and then lookup the box(es) to find all the nearby points ?

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 31 Jan 2020
Would this be fine. The point C will be repeated twice as in two boxes.
lat = [47.5, 45.5, 46.5]';
lon = [-63.5, -61.5, -62.5]';
pts_name = {'A'; 'B'; 'C'};
pts_positions = table(pts_name, lat, lon);
% Min and max values of boxes
box_name = {'box_A'; 'box_B'};
min_lat = [46, 45]';
max_lat = [48, 47]';
min_lon = [-64, -63]';
max_lon = [-62, -61]';
boxes_positions = table(box_name, min_lat, max_lat, min_lon, max_lon);
% Assign points to boxes based on latitude/longitude
assigned = cell(height(boxes_positions),1);
for g = 1:height(boxes_positions)
mask = pts_positions.lon > boxes_positions.min_lon(g) & pts_positions.lon < boxes_positions.max_lon(g) & ...
pts_positions.lat > boxes_positions.min_lat(g) & pts_positions.lat < boxes_positions.max_lat(g);
temp = pts_positions(mask,:);
temp.box = repmat(box_name(g),height(temp),1);
assigned{g} = temp;
end
pts_position_assigned = vertcat(assigned{:});

More Answers (1)

012786534
012786534 on 31 Jan 2020
In reality there is a about 50 boxes. Altought few of them overlap, I still need to take into account the case where a point falls into two boxes. There are no cases where more than two boxes overlap.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!