How to convert a graph / edges list into a shapefile (.shp) ?

13 views (last 30 days)
How to convert a graph / edges list into a shapefile (.shp) ?
Here an example of graph "G", built up with Matlab:
s = [1 1 2 2 2 3 3 3];
t = [2 3 3 4 5 6 7 5];
x = [0 0 1 0 4 3 1];
y = [0 1 0 4 5 0 -1];
G = graph(s,t);
G.Nodes.X = x'; G.Nodes.Y = y';
plot(G,'XData',G.Nodes.X,'YData',G.Nodes.Y)

Answers (2)

Christine Tobler
Christine Tobler on 6 Jul 2022
There isn't a direct way to do this with a graph object. The mapping toolbox has a shapewrite function which produces a .shp file, but it will first require putting these vectors into one of its data structures (geolineshape or maplineshape might be the right ones for your case).

Sim
Sim on 11 Jul 2022
Edited: Sim on 11 Jul 2022
@Christine Tobler Since I did not understand exactly how to perform the steps you suggested...
% Matlab graph --> geolineshape or maplineshape --> shapewrite
...I used a workaround, i.e. I imported both the edges list and the x- and y- nodes coordinates to QGIS (a free and open-source cross-platform desktop geographic information system application that supports viewing, editing, printing, and analysis of geospatial data), from where a shapefile can be created.
Thanks to the GIS stackexchange community, I used the following workaround.
First, go to the Python Console's Editor of QGIS:
% open QGIS --> Plugins --> Python Console --> Show Editor
and then, copy and paste the following code (just change the path where to save your shapefile):
# (1) create a layer in QGIS
s_list = [1, 1, 2, 2, 2, 3, 3, 3]
t_list = [2, 3, 3, 4, 5, 6, 7, 5]
x_list = [0, 0, 1, 0, 4, 3, 1]
y_list = [0, 1, 0, 4, 5, 0, -1]
coords = list(zip(x_list, y_list)) # generate a list of lists containing x and y coordinates of every vertex
layer = QgsVectorLayer('LineString?crs=EPSG:4326', 'Layer', 'memory')
layer.startEditing()
for s, e in zip(s_list, t_list): # loop trow a lists of lists containing the start and end vertex index in the coords list
feature = QgsFeature()
start = QgsPointXY(*coords[s - 1]) # start point
end = QgsPointXY(*coords[e - 1]) # end point
feature.setGeometry(QgsGeometry().fromPolylineXY([start, end])) # set the geometry to a QgsFeature object
layer.addFeature(feature) # add feature to the layer
layer.commitChanges()
QgsProject.instance().addMapLayer(layer)
# (2) save the layer as shapefile
QgsVectorFileWriter.writeAsVectorFormat(
layer,
'path/to/your/matlab_graph.shp', # set here the path to your file including the file extension
"utf-8", # file encoding
QgsCoordinateReferenceSystem("EPSG:4326"), # set inside quotes the crs id
"ESRI Shapefile" # set here the vector driver
)
Once the shapefile is saved in your folder, you can just open the shapefile in Matlab:
% read the shapefile
A = shaperead('matlab_graph.shp');
% extract the coordinates of the edges
x_tmp = vertcat(A.X);
y_tmp = vertcat(A.Y);
x = x_tmp(:,1:2);
y = y_tmp(:,1:2);
% plot the shapefile
plot(x',y','linewidth',2)
xlim([-1, 5])

Community Treasure Hunt

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

Start Hunting!