#!/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. IOS开发资料汇总

    1 IOS账号注册.程序发布流程 1)http://jamesli.cn/blog/?p=955 2)http://jamesli.cn/blog/?p=966 3)http://jamesli.cn ...

  2. OpenGL Shader in OpenCASCADE

    OpenGL Shader in OpenCASCADE eryar@163.com Abstract. As implementation of one of the strategic steps ...

  3. 斐讯Fir302b救砖教程

    首先本人是路由器小白,不算是硬件改装高手,昨天收到了微信活动中的斐讯Fir302b,大概当时得奖的有300人,所以最近肯定很大一批朋友手里有这样的一款路由. 上网查了一下,此款路由可以刷基于tomat ...

  4. ScrollView 嵌套ListView、RecyclerView(持续更新)

    ListView: 只要重写ListView或GridView的onMeasure()方法即可: @Override protected void onMeasure(int widthMeasure ...

  5. java的栈图形演示

    import java.awt.*; import javax.swing.*; import java.awt.event.*; /* 指示发生了组件定义的动作的语义事件.当特定于组件的动作(比如被 ...

  6. 配置 linux-bridge mechanism driver - 每天5分钟玩转 OpenStack(77)

    本节开始我们将学习 Linux Bridge 如何实现 Neutron 的各种功能.首先需要配置 linux-bridge mechanism driver. Neutorn ML2 plugin 默 ...

  7. isPrototypeOf&&getPrototypeOf

    在JavaScript这个一切皆为对象的世界里,难免会判断原型链的问题.那么我们就有必要了解了解isPrototypeOf和getPrototypeOf这两个方法咯. 1.isPrototypeOf ...

  8. 使用ikmv将jar转换为dll,在.NET中直接调用

    ikmv百科介绍 ikvm官网 使用ikvm前需要先安装java. ikvm的使用比较简单,到官网下载ikvm需要的组件,解压后在path中配置 %IKVM_HOME%\bin 在cmd中使用命令 i ...

  9. (转)J2EE的13种核心技术

    一.JDBC(Java Database Connectivity)  JDBC API为访问不同的数据库提供了一种统一的途径,象ODBC一样,JDBC对开发者屏蔽了一些细节问题,另外,JDBC对数据 ...

  10. 用脚本如何实现将Linux下的txt文件批量转化为Windows下的txt文件?

    众所周知,Windows和Linux的文件换行回车格式不同,Windows下换行格式是\r\n(回车+换行),Linux下换行格式为\n(只是换行),因此,其中一个操作系统的文本文件若需要在另外一个中 ...