我有一個shapefile,我將其轉換爲.xml
文件以用於MATSim。文件結構如下:輸出圖爲`.xml`
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE network SYSTEM "http://www.matsim.org/files/dtd/network_v1.dtd">
<network name="VISUM export national network 2007-11-28">
<!-- ====================================================================== -->
<nodes>
<node id="1000" x="730065.3125" y="220415.9531" type="2" origid="1000" />
<node id="1001" x="731010.5" y="220146.2969" type="2" origid="1001" />
.....
</nodes>
<!-- ====================================================================== -->
<links capperiod="01:00:00" effectivecellsize="7.5" effectivelanewidth="3.75">
<link id="100365" from="226" to="227" length="921.0" freespeed="33.3333333333333" capacity="5600.0" permlanes="2.0" oneway="1" modes="car" origid="183" type="10" />
...
</links>
<!-- ====================================================================== -->
</network>
我與NetworkX Python庫,它可以讀取shapefile爲圖形,圖形導出爲GEXF
對象這樣做。這個代碼(本質上)輸出的東西接近但不夠接近網絡規範。
import networkx as nx
G = nx.read_shp("expcuts1.shp")
start = 0
G = nx.convert_node_labels_to_integers(G, first_label=start,
label_attribute = "coord")
# Build a new object with the elements that you need
H = nx.DiGraph(name = "Python NetworkX export from FAF 3.4")
H.add_edges_from(G.edges())
# store coordinates in node attributes
for i in range(len(H)):
H.node[i]['x'] = G.node[i]['coord'][0]
H.node[i]['y'] = G.node[i]['coord'][1]
# export as xml (really, gexl, but that's pretty close)
nx.write_gexf(H, 'test.xml')
<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="undirected" mode="static">
<attributes class="node" mode="static">
<attribute id="0" title="y" type="double" />
<attribute id="1" title="x" type="double" />
</attributes>
<nodes>
<node id="0" label="0">
<attvalues>
<attvalue for="0" value="1389860.27495" />
<attvalue for="1" value="2237913.99085" />
</attvalues>
</node>
除此之外,該GEXL使用節點和邊,但MATSim詞彙請求節點和鏈接。我試圖決定是否可以更容易地從NetworkX調整write_gexf
函數或手動寫入XML,例如ElementTree API。有小費嗎?
你能肯定地說這個圖是非循環的嗎?如果不是,應該怎麼做週期? – Phonon 2014-09-12 21:02:28
該圖肯定是週期性的;它代表了高速公路網絡。不過,該圖並不一定需要定向。我可以將'H'創建爲'Graph'或者'DiGraph'並且輸出是相同的。 – gregmacfarlane 2014-09-13 02:56:01