How to define the areas of specific polygons?

3 views (last 30 days)
A LL
A LL on 22 Sep 2021
Answered: Ayush on 21 Nov 2024
I am looking for a clever way to define the points of the polygons A, B, C, E, F (see attached figure 2007_zones.png) on my map.
I have several maps, and I want to find a technique that I can reuse even when the shapes of the polygons A, B, C, E, F vary.
I am working on a grid of 360x360 with x and y values from 1 to 361.
The shape in light green is an alphaShape object, and I can access all the points (x,y) forming this shape with the file shpfile.mat.
The positions of all the points in the orange contour are contained in the file startfile.mat with the position defined by (xstart,ystart).
The positions of all the points in the purple contour are contained in the file endfile.mat with the positions defined by (xend,yend).
I can define the regions A (blue) and the combined regions BCEF (red) using the inShape function of alphaShape.
-->see attached figure main_zones.fig and code below
I am now looking for a way to partition the respective regions B, C, E ,and F, and I have no clue how to proceed. Maybe using the numRegions function for the alphaShape object?
Here is my code to find the points respectively in region A (blue) and BCEF (red)
%Define polygons A and BCEF
%Load
load('shpfile.mat'); %var name: shp
load('startfile.mat'); %var name: SICSTART, xstart, ystart
load('endfile.mat'); %var name: SICEND, xstart, yend
%Define the index of the points that are inside shp and inside the orange contour:
%0 --> point is not in shp but in the orange contour (region BCEF)
%1 --> point is both in shp and in the orange contour
tf1 = inShape(shp,xstart,ystart);
index0 = find(tf1==0);
x0 = xstart(index0);
y0 = ystart(index0);
shpBCEF = alphaShape(x0,y0,1);
N = numRegions(shpBCEF);
index1 = find(tf1==1);
x1 = xstart(index1);
y1 = ystart(index1);
shp1 = alphaShape(x1,y1,2);
%Define the index of the points that are inside shp and inside the purple contour:
%0 --> point is not in shp but in the purple contour
%1 --> point is both in shp and in the purple contour (i.e. entire shp)
tf2 = inShape(shp,xend,yend);
index2 = find(tf2==1);
x2 = xend(index2);
y2 = yend(index2);
%Define the index of the points that are inside both shp and shp1:
%0 --> point is not in shp1 but in shp (region A)
%1 --> point is both in shp1 and in shp
tf3 = inShape(shp1,x2,y2);
index3 = find(tf3==0);
x3 = x2(index3);
y3 = y2(index3);
%Grid
x = [1:361];
y = x;
[xx,yy] = meshgrid(x,y);
%Figure
figure(1);
hold on;
axis ij;
grid on;
plot(shp,'FaceAlpha',0.2)
%plot(shpBCEF,'FaceAlpha',0.2)
%plot(shp1,'FaceAlpha',0.2)
[M1,c1] = contour(xx,yy,SICSTART,1,'LineWidth',3,'Color', [1.0000 0.5984 0.2000]);
[M2,c2] = contour(xx,yy,SICEND,1,'LineWidth',3,'Color', [0.6769 0.4447 0.7114]);
plot(x3,y3,'x'); %region A (blue)
plot(x0,y0,'x'); %region BCEF (red)
%plot(x1,y1,'x');
%plot(x2,y2,'x');
xlim([110 270]);
ylim([70 260]);
legend('shp','start','end')
Thank you

Answers (1)

Ayush
Ayush on 21 Nov 2024 at 4:34
Hi A LL,
I understand that you need a clever way to identify specific regions on a map.
To break it down and approach the problem systematically, you could leverage a combination of techniques related to computational geometry (specifically alpha shapes, in this case). Here’s a step-by-step guide to help you define and partition the regions effectively:
  1. Since you already use the inShape function with your alphaShape object to define regions A (blue) and BCEF (red), make sure the identification process works for both fixed and variable shapes. You might want to verify that the inShape function returns points inside the desired polygon for both shapes.
You can refer to the following code for reference:
load('shpfile.mat'); % AlphaShape object with points forming the light green shape
load('startfile.mat');
load('endfile.mat');
alphaShapeObj = alphaShape(x_points, y_points, alpha);
% Check which points are inside the region defined by the alphaShape
inA = inShape(alphaShapeObj, x_points, y_points); % Points inside the blue region (A)
inBCEF = inShape(alphaShapeObj, xstart, ystart); % Points inside the red region (BCEF)
2. To partition region BCEF into regions B, C, E, and F, there are several methods you could try, but a good way to start is by investigating whether these regions can be distinguished using boundary points or contour lines. You may need additional information, such as a contour map of the regions (e.g., from your start and end files), or partitioning the region using geometric methods.
3. One approach is to use a “Voronoi diagram or Delaunay triangulation to divide up the space inside the alphaShape into regions. The idea is to partition the space based on proximity to certain points (like your start or end points). You can use the voronoifunction in MATLAB or Delaunay triangulation to help separate the regions.
For example, using a Delaunay triangulation:
% Delaunay triangulation to split region BCEF
DT = delaunayTriangulation(xstart, ystart);
% Get the triangular regions formed by the Delaunay triangulation
T = DT.ConnectivityList;
Then, depending on the specific location and boundary points, you could assign the regions (B, C, E, F) based on how these triangles or Voronoi cells relate to the positions of your start and end points.
You can read more about Delaunay triangulation here: https://www.mathworks.com/help/matlab/ref/delaunay.html and “Voronoi diagram” here: https://www.mathworks.com/help/matlab/ref/voronoi.html
4. If the regions inside BCEF are contiguous and non-overlapping, you might be able to use the numRegions function from the alphaShape object to check how many connected sub-regions exist in BCEF. Then, based on proximity or distances from boundary points (such as from xstart, ystart), you can map the regions B, C, E, and F to their respective points.
% Check the number of connected regions in the alphaShape object
numRegions = numRegions(alphaShapeObj);
% For each region, determine which part belongs to B, C, E, or F by proximity to boundaries
for i = 1:numRegions
regionPoints = regionPoints(alphaShapeObj, i);
% Based on proximity or boundary conditions, you can assign labels for B, C, E, F.
end
5. You can automate the process of assigning the regions B, C, E, and F based on boundary conditions (e.g., based on proximity to start and end points). This might involve checking if the point belongs to a region inside the boundary defined by xstart, ystart (orange contour), and then checking if it lies within the purple contour (xend, yend).
6. Once you have an initial method for partitioning, you’ll would want to iterate and refine the process. You could consider:
  • Using additional geometric properties like the convex hull of the regions to determine boundaries.
  • Checking the angle or distance between points to better define each sub-region (B, C, E, F).
  • Adjusting the algorithm to account for more complex shapes.
You can read more about “inshape” function here: https://www.mathworks.com/help/matlab/ref/alphashape.inshape.html
You can read more about “alphaShape” here:
Hope it helps!

Categories

Find more on Bounding Regions in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!