How to assign points to one or several boxes

1 次查看(过去 30 天)
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 个评论
Mohammad Sami
Mohammad Sami 2020-1-31
Are you only going to have two boxes, or this need to scale up to arbitrary number of boxes ?
Mohammad Sami
Mohammad Sami 2020-1-31
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 ?

请先登录,再进行评论。

采纳的回答

Mohammad Sami
Mohammad Sami 2020-1-31
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{:});

更多回答(1 个)

012786534
012786534 2020-1-31
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.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by