2014-09-12 38 views
0

我有一個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。有小費嗎?

+0

你能肯定地說這個圖是非循環的嗎?如果不是,應該怎麼做週期? – Phonon 2014-09-12 21:02:28

+0

該圖肯定是週期性的;它代表了高速公路網絡。不過,該圖並不一定需要定向。我可以將'H'創建爲'Graph'或者'DiGraph'並且輸出是相同的。 – gregmacfarlane 2014-09-13 02:56:01

回答

0

簡稱:如果這是一次性項目,則從NetworkX調整write_gexf函數。

Long:

對於MATSim,您需要一個有向圖。 Gexf允許那個defaultedgetype="directed"。您可以在邊緣添加更多屬性,例如容量,車道...

<edge weight="1.0" target="109001663" source="109001672" label="network link" id="99999"> 
<attvalues> 
    <attvalue start="0.0" value="0" for="capacity"/> 
    <attvalue start="0.0" value="0" for="length"/> 
</attvalues> 
</edge> 

由於您已經擁有gexf,所以最後一步是一對一轉換。 Gexf節點直接轉換爲MATSim節點,而邊是MATSim術語中的鏈接。但是,您需要爲MATSim提供網絡屬性。

節點只需要在x,y座標

對於您可能要添加鏈接(而非歐氏距離)的實際長度鏈接。此外,您需要允許的最大速度(自由速度),容量(通常在每小時PCU中給出)以及車道數量。

請注意,容量取決於限期,即capperiod="01:00:00"表示有效期爲一小時。

P.S.您還可以看看the gexf package,您可以在其中找到一些主要用於將MATSim網絡轉換爲gexf的gexf helper類。在Gephi中進行分析。