路径分析之NetworkX实例
#!/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实例的更多相关文章
- 最近学习工作流 推荐一个activiti 的教程文档
全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...
- ArcGIS网络分析之Silverlight客户端路径分析(三)
原文:ArcGIS网络分析之Silverlight客户端路径分析(三) 首先贴上最终的效果图: a.路径查询 2.最近设施点查询 3.服务区分析 说明: 1.以上的示例使用的数据是随意在ArcMap中 ...
- ArcGIS API for JavaScript 4.2学习笔记[27] 网络分析之最短路径分析【RouteTask类】
要说网页端最经典的GIS应用,非网络分析莫属了. 什么?你没用过?百度高德谷歌地图的路线分析就是活生生的例子啊!只不过它们是根据大实际背景优化了结果显示而已. 这个例子使用RouteTask进行网络分 ...
- PostGIS pgrouting路径分析
--让数据库支持PostGIS和pgRouting的函数和基础表(安装后第一次使用时执行,以后都不再执行) CREATE EXTENSION postgis; CREATE EXTENSION pgr ...
- networkx整理
1.基础知识 1.1.介绍 networkx在2002年5月产生,是一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析.仿真建模等工作 ...
- ActiveMQ消息队列和SignalR之日志实时监控及警报小实例
主要技术: log4net-生成日志. ActiveMQ-生成日志的时候发送消息,并实时监控日志. SignalR-将ActiveMQ监控的日志实时显示到浏览器上,而不用刷新浏览器. 小实例介绍: 左 ...
- Python数模笔记-NetworkX(3)条件最短路径
1.带有条件约束的最短路径问题 最短路径问题是图论中求两个顶点之间的最短路径问题,通常是求最短加权路径. 条件最短路径,指带有约束条件.限制条件的最短路径.例如,顶点约束,包括必经点或禁止点的限制:边 ...
- js-静态、原型、实例属性
本篇来说一下js中的属性: 1.静态属性 2.原型属性 3.实例属性 静态属性: function klass(){} var obj=new klass(); klass.count=0; klas ...
- ZIP压缩算法详细分析及解压实例解释
最近自己实现了一个ZIP压缩数据的解压程序,觉得有必要把ZIP压缩格式进行一下详细总结,数据压缩是一门通信原理和计算机科学都会涉及到的学科,在通信原理中,一般称为信源编码,在计算机科学里,一般称为数据 ...
随机推荐
- 【转】C#调用Windows图片和传真查看器打开图片
//建立新的系统进程 System.Diagnostics.Process process = new System.Diagnostics.Process(); //设置文件名,此处为图片的真实路径 ...
- 【开源】OSharp框架解说系列(6.1):日志系统设计
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- IOS开发之自定义Button(集成三种回调模式)
前面在做东西的时候都用到了storyboard,在今天的代码中就纯手写代码自己用封装个Button.这个Button继承于UIView类,在封装的时候用上啦OC中的三种回调模式:目标动作回调,委托回调 ...
- Ubuntu杂记——双系统重装Win7后找不到Ubuntu的解决办法
之前装过Ubuntu和Win7的双系统,后来重装了Win7,发现Ubuntu不见了,那会没怎么用,也没去解决问题.现在再看Android内核剖析,大部分都是在Ubuntu环境下进行的,所以百度了一些方 ...
- linux下遍历目录
遍历目录的主要思想 由于目录就是一颗树,所以遍历目录就转换为遍历一棵树.谈到树的遍历就再熟悉不过了,有树的前序.层次和后序遍历,我使用的是前序遍历,后序遍历和前序遍历本质上一样,而层次遍历要比前两个麻 ...
- 如何获取byte的各个bit值以及常见位操作
项目中通过信号采集板的数据获取车上仪表盘指示灯的信息,将接收到的数据转成byte后,还要将每一个Byte的各个Bit值分离出来,这样才知道每个bit的值代表的具体信息.这里记录下如何获取byte的各个 ...
- 1Z0-053 争议题目解析502
1Z0-053 争议题目解析502 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 502.A database is running In ARCHIVBXXMS mode. It ...
- iOS-LaunchImage启动页
一. 目标: 设置一个漂亮的启动页. 二.步骤 1. 先创建LaunchImage 2. 进一步设置需要适配的启动页机型 3. 设置完成的效果 4.往里面拖图片,如图是适配的图片的配置 5. 在项目中 ...
- Winform在线更新
引言 2015年第一篇,Winform在线更新,算是重操旧业吧,09年刚到北京时一直做硬件联动编程,所以大多数时间都在搞Winform, 但是从来没做过在线更新这个功能,前几天参与部门另一个项目,上来 ...
- Azure Application Gateway (4) 设置URL路由 - PowerShell
<Windows Azure Platform 系列文章目录> 本文将介绍如果使用Azure PowerShell,创建Azure Application Gateway URL Rout ...