File Exchange

## spatialgraph2D

version 2.0.2 (3.3 KB) by Matt J

### Matt J (view profile)

Creates a graph object with 2D spatial locations associated with the nodes

Updated 05 Jun 2020

This submission defines a graph class which allows 2D (x,y) location data to be attached to the nodes. It is useful for studying and manipulating mosaics of polygons. Currently, its main capability is analyzing a graph and determining the constituent polygons that it consists of. As an example, given the graph,

s = [1 1 1 2 3 3 4 4 5 6 7 6 8 9 10 10 12 12 13 14 15 16 17 17 18 19 20 21 20 25];
t = [2 8 18 3 4 23 5 21 6 7 8 11 9 10 11 12 14 13 15 18 16 17 18 25 19 20 1 22 24 26];
G = graph(s,t);

we associate the nodes of G to the following x, y data

x = [0.5 0 0 0 0.5 1 1.5 2 3 3 3 5.5 6 4 6 6 4 3 2 0.5 -1 -2 -1 1.5 4.5 4.5];
y = [0 0.5 1 1.5 2 2 1.5 1 1 1.5 2 1 0.5 0.5 0 -1 -1 -0.5 -1 -1 1 0.5 0.5 -0.5 -0.5 0];

We create a spatialgraph2D object as follows,

obj=spatialgraph2D(G,x,y);

Then, we can obtain a polyshape array of all the constituent polygons as follows,

pgon=polyshape(obj);

To visualize, use the following pair of overlaid plots. This was used to generate the image thumbnail above.

plot(obj);
hold on
plot(pgon);
hold off

### Cite As

Matt J (2020). spatialgraph2D (https://www.mathworks.com/matlabcentral/fileexchange/73630-spatialgraph2d), MATLAB Central File Exchange. Retrieved .

Cheng Ting Tsai

Na

Matt J

### Matt J (view profile)

So, just to clarify, your examples have multiple mosaics, since some of the polygons "dangle" off the others by a single vertex. As mentioned in the description above, the class wasn't designed for such cases. However, you can try putting the line obj=prune(obj) back in, but modifying the prune() method to the following:

function obj = prune(obj)
%Remove dangling branches

Gp=obj.G;
xp=obj.x;
yp=obj.y;
lp=obj.labels;

tips=find(degree(Gp)<=1);

while ~isempty(tips)

Gp=rmnode(Gp,tips);
xp(tips)=[];
yp(tips)=[];
lp(tips)=[];
tips=find(degree(Gp)<=1);

end

obj=spatialgraph2D(Gp,xp,yp,lp);

end

Na

### Na (view profile)

s = [1 2 2 1 39 9 8 8 7 6 6 11 11 12 10 10 13 14 15 16 19 19 20 16 24 23 23 22 21 16 16 17 18 3 4 4 5 2 26 26 29 29 28 27 27];
t = [2 25 30 39 9 8 5 7 6 31 11 10 12 13 13 32 14 15 16 19 33 20 34 24 23 36 22 35 22 21 17 18 3 4 5 14 6 3 25 29 38 28 26 26 17];
G = graph(s,t);
x = [10 9 7 7 9 9 10 10 11 6 8 7 6 6 5 4 5 6 3 2 4 3 2 3 8 7 6 6 7 10 9 5 3 1 2 1 9 7 11];
y = [6 7 7 6 3 2 2 3 4 2 2 3 4 5 5 7 8 8 6 5 8 9 8 7 8 9 9 10 10 8 1 1 5 4 10 9 9 11 5];
obj=spatialgraph2D(G,x,y);
[pgon,loops]=polyshape(obj);
plot(obj);
hold on
plot(pgon);
hold off
I got this error
Index exceeds the number of array elements (0).
Error in spatialgraph2D/polyshortest (line 102)
E=G.findedge(P,[P(2:end),P(1)]);
Error in spatialgraph2D/polyshape (line 131)
[P,E]=polyshortest(obj,E0);

Matt J

Remove the line

obj=prune(obj)

Na

### Na (view profile)

I have this graph
s = [1 1 1 1 1 2 3 3 4 6];
t = [2 3 5 6 7 3 5 4 5 7];
G = graph(s,t);
x = [5 6 4 3 2 5 7];
y = [6 4 4 3 5 8 7];
obj=spatialgraph2D(G,x,y);
[pgon,loops]=polyshape(obj);
plot(obj);
hold on
plot(pgon);
hold off
how can I get [1,6,7] as a loops?