pysal

Next topic

pysal.esda — Exploratory Spatial Data Analysis

This Page

cg.standalone — Standalone

The cg.standalone module provides ...

New in version 1.0.

Helper functions for computational geometry in PySAL

pysal.cg.standalone.convex_hull(points)

Returns the convex hull of a set of points.

convex_hull(Point list) -> Polygon

Parameters:points : a list of points to compute the convex hull for

Examples

>>> points = [Point((0, 0)), Point((4, 4)), Point((4, 0)), Point((3, 1))]
>>> convex_hull(points)
[(0.0, 0.0), (4.0, 0.0), (4.0, 4.0)]
pysal.cg.standalone.get_angle_between(ray1, ray2)

Returns the angle formed between a pair of rays which share an origin get_angle_between(Ray, Ray) -> number

Parameters:

ray1 : a ray forming the beginning of the angle measured

ray2 : a ray forming the end of the angle measured

Examples

>>> get_angle_between(Ray(Point((0, 0)), Point((1, 0))), Ray(Point((0, 0)), Point((1, 0)))) 
0.0
pysal.cg.standalone.get_bounding_box(items)

Examples

>>> bb = get_bounding_box([Point((-1, 5)), Rectangle(0, 6, 11, 12)])
>>> bb.left
-1.0
>>> bb.lower
5.0
>>> bb.right
11.0
>>> bb.upper
12.0
pysal.cg.standalone.get_point_at_angle_and_dist(ray, angle, dist)

Returns the point at a distance and angle relative to the origin of a ray.

get_point_at_angle_and_dist(Ray, number, number) -> Point

Parameters:

ray : the ray which the angle and distance are relative to

angle : the angle relative to the ray at which the point is located

dist : the distance from the ray origin at which the point is located

Examples

>>> ray = Ray(Point((0, 0)), Point((1, 0)))
>>> pt = get_point_at_angle_and_dist(ray, math.pi, 1.0)
>>> isinstance(pt, Point)
True
>>> round(pt[0], 8)
-1.0
>>> round(pt[1], 8)
0.0
pysal.cg.standalone.get_points_dist(pt1, pt2)

Returns the distance between a pair of points.

get_points_dist(Point, Point) -> number

Parameters:

pt1 : a point

pt2 : the other point

Examples

>>> get_points_dist(Point((4, 4)), Point((4, 8)))
4.0
>>> get_points_dist(Point((0, 0)), Point((0, 0)))
0.0
pysal.cg.standalone.get_polygon_point_dist(poly, pt)

Returns the distance between a polygon and point.

get_polygon_point_dist(Polygon, Point) -> number

Parameters:

poly : a polygon to compute distance from

pt : a point to compute distance from

Examples

>>> poly = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
>>> pt = Point((2, 0.5))
>>> get_polygon_point_dist(poly, pt)
1.0
>>> pt2 = Point((0.5, 0.5))
>>> get_polygon_point_dist(poly, pt2) 
0.0
pysal.cg.standalone.get_polygon_point_intersect(poly, pt)

Returns the intersection of a polygon and point.

get_polygon_point_intersect(Polygon, Point) -> Point

Parameters:

poly : a polygon to check intersection for

pt : a point to check intersection for

Examples

>>> poly = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
>>> pt = Point((0.5, 0.5))
>>> i = get_polygon_point_intersect(poly, pt)
>>> str(i)
'(0.5, 0.5)'
>>> pt2 = Point((2, 2))
>>> get_polygon_point_intersect(poly, pt2)
pysal.cg.standalone.get_ray_segment_intersect(ray, seg)

Returns the intersection of a ray and line segment.

get_ray_segment_intersect(Ray, Point) -> Point or LineSegment

Parameters:

ray : a ray to check intersection for

seg : a line segment to check intersection for

Examples

>>> ray = Ray(Point((0, 0)), Point((0, 1)))
>>> seg = LineSegment(Point((-1, 10)), Point((1, 10)))
>>> i = get_ray_segment_intersect(ray, seg)
>>> isinstance(i, Point)
True
>>> str(i)
'(0.0, 10.0)'
>>> seg2 = LineSegment(Point((10, 10)), Point((10, 11)))
>>> get_ray_segment_intersect(ray, seg2)
pysal.cg.standalone.get_rectangle_point_intersect(rect, pt)

Returns the intersection of a rectangle and point.

get_rectangle_point_intersect(Rectangle, Point) -> Point

Parameters:

rect : a rectangle to check intersection for

pt : a point to check intersection for

Examples

>>> rect = Rectangle(0, 0, 5, 5)
>>> pt = Point((1, 1))
>>> i = get_rectangle_point_intersect(rect, pt)
>>> str(i)
'(1.0, 1.0)'
>>> pt2 = Point((10, 10))
>>> get_rectangle_point_intersect(rect, pt2)
pysal.cg.standalone.get_segment_point_dist(seg, pt)

Returns the distance between a line segment and point and distance along the segment of the closest point on the segment to the point as a ratio of the length of the segment.

get_segment_point_dist(LineSegment, Point) -> (number, number)

Parameters:

seg : a line segment to compute distance from

pt : a point to compute distance from

Examples

>>> seg = LineSegment(Point((0, 0)), Point((10, 0)))
>>> pt = Point((5, 5))
>>> get_segment_point_dist(seg, pt)
(5.0, 0.5)
>>> pt2 = Point((0, 0))
>>> get_segment_point_dist(seg, pt2)
(0.0, 0.0)
pysal.cg.standalone.get_segment_point_intersect(seg, pt)

Returns the intersection of a segment and point.

get_segment_point_intersect(LineSegment, Point) -> Point

Parameters:

seg : a segment to check intersection for

pt : a point to check intersection for

Examples

>>> seg = LineSegment(Point((0, 0)), Point((0, 10)))
>>> pt = Point((0, 5))
>>> i = get_segment_point_intersect(seg, pt)
>>> str(i)
'(0.0, 5.0)'
>>> pt2 = Point((5, 5))
>>> get_segment_point_intersect(seg, pt2)  
pysal.cg.standalone.get_segments_intersect(seg1, seg2)

Returns the intersection of two segments.

get_segments_intersect(LineSegment, LineSegment) -> Point

Parameters:

seg1 : a segment to check intersection for

seg2 : a segment to check intersection for

Examples

>>> seg1 = LineSegment(Point((0, 0)), Point((0, 10)))
>>> seg2 = LineSegment(Point((-5, 5)), Point((5, 5)))
>>> i = get_segments_intersect(seg1, seg2)
>>> isinstance(i, Point)
True
>>> str(i)
'(0.0, 5.0)'
>>> seg3 = LineSegment(Point((100, 100)), Point((100, 101)))
>>> i = get_segments_intersect(seg2, seg3)
pysal.cg.standalone.is_clockwise(vertices)

Returns whether a list of points describing a polygon are clockwise or counterclockwise.

is_clockwise(Point list) -> bool

Parameters:vertices : a list of points that form a single ring

Examples

>>> is_clockwise([Point((0, 0)), Point((10, 0)), Point((0, 10))])
False
>>> is_clockwise([Point((0, 0)), Point((0, 10)), Point((10, 0))])
True
>>> v = [(-106.57798, 35.174143999999998), (-106.583412, 35.174141999999996), (-106.58417999999999, 35.174143000000001), (-106.58377999999999, 35.175542999999998), (-106.58287999999999, 35.180543), (-106.58263099999999, 35.181455), (-106.58257999999999, 35.181643000000001), (-106.58198299999999, 35.184615000000001), (-106.58148, 35.187242999999995), (-106.58127999999999, 35.188243), (-106.58138, 35.188243), (-106.58108, 35.189442999999997), (-106.58104, 35.189644000000001), (-106.58028, 35.193442999999995), (-106.580029, 35.194541000000001), (-106.57974399999999, 35.195785999999998), (-106.579475, 35.196961999999999), (-106.57922699999999, 35.198042999999998), (-106.578397, 35.201665999999996), (-106.57827999999999, 35.201642999999997), (-106.57737999999999, 35.201642999999997), (-106.57697999999999, 35.201543000000001), (-106.56436599999999, 35.200311999999997), (-106.56058, 35.199942999999998), (-106.56048, 35.197342999999996), (-106.56048, 35.195842999999996), (-106.56048, 35.194342999999996), (-106.56048, 35.193142999999999), (-106.56048, 35.191873999999999), (-106.56048, 35.191742999999995), (-106.56048, 35.190242999999995), (-106.56037999999999, 35.188642999999999), (-106.56037999999999, 35.187242999999995), (-106.56037999999999, 35.186842999999996), (-106.56037999999999, 35.186552999999996), (-106.56037999999999, 35.185842999999998), (-106.56037999999999, 35.184443000000002), (-106.56037999999999, 35.182943000000002), (-106.56037999999999, 35.181342999999998), (-106.56037999999999, 35.180433000000001), (-106.56037999999999, 35.179943000000002), (-106.56037999999999, 35.178542999999998), (-106.56037999999999, 35.177790999999999), (-106.56037999999999, 35.177143999999998), (-106.56037999999999, 35.175643999999998), (-106.56037999999999, 35.174444000000001), (-106.56037999999999, 35.174043999999995), (-106.560526, 35.174043999999995), (-106.56478, 35.174043999999995), (-106.56627999999999, 35.174143999999998), (-106.566541, 35.174144999999996), (-106.569023, 35.174157000000001), (-106.56917199999999, 35.174157999999998), (-106.56938, 35.174143999999998), (-106.57061499999999, 35.174143999999998), (-106.57097999999999, 35.174143999999998), (-106.57679999999999, 35.174143999999998), (-106.57798, 35.174143999999998)]
>>> is_clockwise(v)
True
pysal.cg.standalone.is_collinear(p1, p2, p3)

Returns whether a triplet of points is collinear.

is_collinear(Point, Point, Point) -> bool

Parameters:

p1 : a point (Point)

p2 : another point (Point)

p3 : yet another point (Point)

Examples

>>> is_collinear(Point((0, 0)), Point((1, 1)), Point((5, 5)))
True
>>> is_collinear(Point((0, 0)), Point((1, 1)), Point((5, 0)))
False