Giter Club home page Giter Club logo

pyvisgraph's Introduction

Pyvisgraph - Python Visibility Graph

MIT License PyPI

Given a set of simple obstacle polygons, build a visibility graph and find the shortest path between two points.

Figure 1

Pyvisgraph is a MIT-licensed Python package for building visibility graphs from a list of simple obstacle polygons. The visibility graph algorithm (D.T. Lee) runs in O(n^2 log n) time. The shortest path is found using Djikstra's algorithm.

To see how visibility graphs work interactively, take a look at the Visibility Graph Simulator built with Pyvisgraph.

Installing Pyvisgraph

$ pip install pyvisgraph

Pyvisgraph supports Python 2 and 3.

Usage

Here is an example of building a visibility graph given a list of simple polygons:

>>> import pyvisgraph as vg
>>> polys = [[vg.Point(0.0,1.0), vg.Point(3.0,1.0), vg.Point(1.5,4.0)],
>>>          [vg.Point(4.0,4.0), vg.Point(7.0,4.0), vg.Point(5.5,8.0)]]
>>> g = vg.VisGraph()
>>> g.build(polys)
>>> shortest = g.shortest_path(vg.Point(1.5,0.0), vg.Point(4.0, 6.0))
>>> print shortest
[Point(1.50, 0.00), Point(3.00, 1.00), Point(4.00, 6.00)]

Once the visibility graph is built, it can be saved and subsequently loaded. This is useful for large graphs where build time is long. pickle is used for saving and loading.

>>> g.save('graph.pk1')
>>> g2 = VisGraph()
>>> g2.load('graph.pk1')

For obstacles with a large number of points, Pyvisgraph can take advantage of processors with multiple cores using the multiprocessing module. Simply add the number of workers (processes) to the build method:

>>> g.build(polys, workers=4)

Pyvisgraph also has some useful helper functions:

  • g.update([list of Points]): Updates the visibility graph by checking visibility of each Point in the list.
  • g.point_in_polygon(Point): Check if Point is in the interior of any of the obstacle polygons. Returns the polygon_id of said polygon, -1 if not inside any polygon.
  • g.closest_point(Point, polygon_id): Return the closest point outside polygon with polygon_id from Point.

For further examples, please look at the scripts provided in the examples folder.

Example & performance

This example uses a shapefile representing world shorelines as obstacles. Two vessels were picked randomly and their current location found using AIS. Red lines are laden voyage legs (carrying cargo) and dotted blue lines are ballast legs (no cargo, moving to load destination). Pyvisgraph has the following performance on a Microsoft Surface Pro 3 (Intel i7-4650U @ 1.7Ghz, 8GB DDR3 RAM), where time is in seconds:

Shoreline obstacle graph [points: 4335 edges: 4335]
Using 4 worker processes...
Time to create visibility graph: 554.683238029
Visibility graph edges: 118532
Time to update visgraph & find shortest path: 1.09287905693
Shorest path nodes: 19
Time to find shortest path between existing points: 0.508340835571

For one vessel the origin and destination Points were not part of the built visibility graph and had to first be computed. For the second vessel, these Points were first added to the visibility graph using update, then finding the shortest path is faster. Using Matplotlib basemap to visualize the routes: Figure 2

For more information about the implementation, see these series of articles:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.