#!/usr/bin/env python
# -*- coding: utf-8 -*-
import networkx as nx
import numpy as np
import json
import matplotlib.pyplot as plt
from shapely.geometry import asLineString, asMultiPoint

def get_path(n0, n1):
"""If n0 and n1 are connected nodes in the graph,
this function will return an array of point
coordinates along the line linking
these two nodes.""" return np.array(json.loads(nx_list_subgraph[n0][n1]['Json'])['coordinates']) def get_full_path(path):
"""
Create numpy array line result
:param path: results of nx.shortest_path function
:return: coordinate pairs along a path
"""
p_list = []
curp = None
for i in range(len(path)-1):
p = get_path(path[i], path[i+1])
if curp is None:
curp = p
if np.sum((p[0]-curp)**2) > np.sum((p[-1]-curp)**2):
p = p[::-1, :]
p_list.append(p)
curp = p[-1]
return np.vstack(p_list) def write_geojson(outfilename, indata):
"""
create GeoJSON file
:param outfilename: name of output file
:param indata:
:return: a new GeoJSON file
""" with open(outfilename, "w") as file_out:
file_out.write(json.dumps(indata)) if __name__ == '__main__': # use Networkx to load a Noded shapefile
# returns a graph where each node is a coordinate pair
# and the edge is the line connecting the two nodes
#点数据类转换为node,线转换为edge。坐标对作为keys,保持属性,线被简化为只保留起始点。
nx_load_shp = nx.read_shp("../geodata/shp/e01_network_lines_3857.shp") # A graph is not always connected, so we take the largest connected subgraph
# by using the connected_component_subgraphs function.
#获取连接子图
nx_list_subgraph = list(nx.connected_component_subgraphs(nx_load_shp.to_undirected()))[0] #获取所有节点
# get all the nodes in the network
nx_nodes = np.array(nx_list_subgraph.nodes()) # output the nodes to a GeoJSON file to view in QGIS
network_nodes = asMultiPoint(nx_nodes)
write_geojson("../geodata/ch08_final_netx_nodes.geojson",
network_nodes.__geo_interface__) # this number represents the nodes position
# in the array to identify the node
start_node_pos = 30
end_node_pos = 21 #计算最短路径
# Compute the shortest path. Dijkstra's algorithm.
nx_short_path = nx.shortest_path(nx_list_subgraph,
source=tuple(nx_nodes[start_node_pos]),
target=tuple(nx_nodes[end_node_pos]),
weight='distance') print(nx_short_path)
# create numpy array of coordinates representing result path
nx_array_path = get_full_path(nx_short_path)
print("------------------------------------")
print(nx_short_path) #将路径点连成线
# convert numpy array to Shapely Linestring
out_shortest_path = asLineString(nx_array_path) write_geojson("../geodata/ch08_final_netx_sh_path.geojson",
out_shortest_path.__geo_interface__)

nx.draw(nx_list_subgraph)     #绘制网络G
    #plt.savefig("ba.png")    #输出方式1: 将图像存为一个png格式的图片文件
    plt.show()

路径分析之NetworkX实例的更多相关文章

  1. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  2. ArcGIS网络分析之Silverlight客户端路径分析(三)

    原文:ArcGIS网络分析之Silverlight客户端路径分析(三) 首先贴上最终的效果图: a.路径查询 2.最近设施点查询 3.服务区分析 说明: 1.以上的示例使用的数据是随意在ArcMap中 ...

  3. ArcGIS API for JavaScript 4.2学习笔记[27] 网络分析之最短路径分析【RouteTask类】

    要说网页端最经典的GIS应用,非网络分析莫属了. 什么?你没用过?百度高德谷歌地图的路线分析就是活生生的例子啊!只不过它们是根据大实际背景优化了结果显示而已. 这个例子使用RouteTask进行网络分 ...

  4. PostGIS pgrouting路径分析

    --让数据库支持PostGIS和pgRouting的函数和基础表(安装后第一次使用时执行,以后都不再执行) CREATE EXTENSION postgis; CREATE EXTENSION pgr ...

  5. networkx整理

    1.基础知识 1.1.介绍 networkx在2002年5月产生,是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析.仿真建模等工作 ...

  6. ActiveMQ消息队列和SignalR之日志实时监控及警报小实例

    主要技术: log4net-生成日志. ActiveMQ-生成日志的时候发送消息,并实时监控日志. SignalR-将ActiveMQ监控的日志实时显示到浏览器上,而不用刷新浏览器. 小实例介绍: 左 ...

  7. Python数模笔记-NetworkX(3)条件最短路径

    1.带有条件约束的最短路径问题 最短路径问题是图论中求两个顶点之间的最短路径问题,通常是求最短加权路径. 条件最短路径,指带有约束条件.限制条件的最短路径.例如,顶点约束,包括必经点或禁止点的限制:边 ...

  8. js-静态、原型、实例属性

    本篇来说一下js中的属性: 1.静态属性 2.原型属性 3.实例属性 静态属性: function klass(){} var obj=new klass(); klass.count=0; klas ...

  9. ZIP压缩算法详细分析及解压实例解释

    最近自己实现了一个ZIP压缩数据的解压程序,觉得有必要把ZIP压缩格式进行一下详细总结,数据压缩是一门通信原理和计算机科学都会涉及到的学科,在通信原理中,一般称为信源编码,在计算机科学里,一般称为数据 ...

随机推荐

  1. SQL Server 默认跟踪(Default Trace)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) 查看默认跟踪信息(Default Tr ...

  2. java笔试题

    下面有关JVM内存,说法错误的是? A.程序计数器是一个比较小的内存区域,用于指示当前线程所执行的字节码执行到了第几行,是线程隔离的 B.Java方法执行内存模型,用于存储局部变量,操作数栈,动态链接 ...

  3. PHP+MySQL代码部署在Linux(Ubuntu)上注意事项

    最近帮同学做一个网站,同学买的是阿里云服务器,Linux发行版是Ubuntu12.04.我在本地把程序写好,都调试好了.然后他让我自己发布和部署.之前在大学里上操作系统课程时,也用过一段时间的Ubun ...

  4. 通过SSH连接linux服务器

    SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专 ...

  5. 小菜学习编程-Winform系列(初学者)

    前言 记得上次写<小菜的程序员道路(二)>,这篇文章的时候说过,要把工作以来整理的编程知识分享给大家,因为这半年来的工作实在是忙,现在也在忙着公司产品上线,但是答应的一定要实现,大家看我上 ...

  6. C# Excel 为图表添加趋势线、误差线

    Excel图表能够将数据可视化,在图表中另行添加趋势线和误差线,可对数据进行进一步的数据分析和统计的可视化处理.Excel中的趋势线可用于趋势预测/回归分析,共6中类型:指数(X),线性(L),对数( ...

  7. android防止内存溢出浅析

    Android的虚拟机是基于寄存器的Dalvik,它的最大堆大小一般是16M.但是Android采用的是Java语言编写,所以在很大程度上,Android的内存机制等同于Java的内存机制,在刚开始开 ...

  8. html5 canvas 画图表

    (function () { var canvas = document.createElement("canvas"); canvas.width = 800; canvas.h ...

  9. 锁升级(Lock Escalations)——它们经常发生么?

    前段时间,我写了一些SQL Server里锁升级的基础知识,还有它是如何影响执行计划的.今天,我想进一步谈下锁升级: 锁升级什么时候发生? 通常在SQL Server里如果在SQL语句里你请求的行数超 ...

  10. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...