Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Note that although I am using Python Console from within QGIS for this, I don't think it's a QGIS issue.

Below is the difference of how NetworkX is dealing with 2 line layers created by the same source and of the same geographic area:

Layer 1 behaviour:

>>> import networkx as nx
>>> import matplotlib.pyplot as plt
>>> lyr_1 = nx.read_shp(str(iface.activeLayer().source()))
>>> lyr_1.nodes()[:10]
[(-66.9813, 46.988354), (-66.927948, 45.855299), (-66.019929, 45.902869), (-64.862855, 46.686669), (-65.678366, 47.000052), (-67.335783, 47.350849), (-64.903621, 46.102999), (-66.525846, 46.049331), (-64.834094, 46.105918), (-64.780438, 46.095201)]

>>> lyr_1.edges()[:10]
[((-66.9813, 46.988354), (-66.973318, 46.982436)), ((-66.927948, 45.855299), (-66.929311, 45.855625)), ((-66.927948, 45.855299), (-66.92766, 45.857282)), ((-66.019929, 45.902869), (-66.017847, 45.902763)), ((-64.862855, 46.686669), (-64.861357, 46.688359)), ((-64.862855, 46.686669), (-64.861859, 46.685471)), ((-65.678366, 47.000052), (-65.67414, 47.00105)), ((-67.335783, 47.350849), (-67.335363, 47.353686)), ((-67.335783, 47.350849), (-67.326822, 47.349927)), ((-64.903621, 46.102999), (-64.903472, 46.103577))]

>>> route = nx.shortest_path(lyr_1, lyr_1.nodes()[1], lyr_1.nodes()[10])

>>> route_length = nx.shortest_path_length(lyr_1, lyr_1.nodes()[1], lyr_1.nodes()[10])
>>> route_length
158

>>> route_graph = nx.subgraph(lyr_1, route) 
>>> nx.draw(route_graph)
>>> plt.show()

The following is the result: enter image description here

Layer 2 behaviour:

>>> lyr_2 = nx.read_shp(str(iface.activeLayer().source()))
>>> lyr_2.nodes()
[]

>>> lyr_2.edges()
[]

>>> nx.draw(lyr_2)
>>> plt.show()

No plot is shown of the second layer. I am confused to this problem as both layers were created by the same source.

Question:

Why isn't NetworkX reading my second layer properly?

share|improve this question
    
have you selected layer2 when you try lyr_2 = nx.read_shp(str(iface.activeLayer().source())) ? –  gene Jul 7 '13 at 11:38
    
@gene yes I have –  dassouki Jul 7 '13 at 12:43
    
Did you get the same result when trying this from a python prompt outside QGIS? –  om_henners Jul 8 '13 at 0:17
    
@om_henners yes –  dassouki Jul 10 '13 at 9:29
    
If the new shapefiles are being created by the script, has the second one flushed to the disk? In general a lot of Python tools (inc GDAL/OGR) will not flush to the desk unless the object drops out of scope or is explicitly written. –  om_henners Jul 11 '13 at 1:10
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.