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

조회 수: 20(최근 30일)
Sim 2022년 7월 6일
편집: Sim 2022년 7월 11일
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)

댓글을 달려면 로그인하십시오.

답변(2개)

Christine Tobler 2022년 7월 6일
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).
댓글 수: 1표시숨기기 없음
Sim 2022년 7월 6일
ok, thanks a lot @Christine Tobler!
Do you have a Minimum Working Example (MWE) to show me ?

댓글을 달려면 로그인하십시오.

Sim 2022년 7월 11일
편집: Sim 2022년 7월 11일
@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.commitChanges()
# (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:
% 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])

댓글을 달려면 로그인하십시오.

범주

Find more on File Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by