pysal

Previous topic

pysal.cg — Computational Geometry

This Page

cg.shapes — Shapes

The cg.shapes module provides basic data structures.

New in version 1.0.

Computational geometry code for PySAL: Python Spatial Analysis Library.

class pysal.cg.shapes.Chain(vertices)

Geometric representation of a chain, also known as a polyline.

Attributes

vertices
len
bounding_box

Returns the bounding box of the chain.

bounding_box -> Rectangle

Examples

>>> c = Chain([Point((0, 0)), Point((2, 0)), Point((2, 1)), Point((0, 1))])
>>> c.bounding_box.left
0.0
>>> c.bounding_box.lower
0.0
>>> c.bounding_box.right
2.0
>>> c.bounding_box.upper
1.0
len

Returns the geometric length of the chain.

len -> number

Examples

>>> c = Chain([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((2, 1))])
>>> c.len
3.0
>>> c = Chain([[Point((0, 0)), Point((1, 0)), Point((1, 1))],[Point((10,10)),Point((11,10)),Point((11,11))]])
>>> c.len
4.0
parts

Returns the parts of the chain.

parts -> Point list

Examples

>>> c = Chain([[Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))],[Point((2,1)),Point((2,2)),Point((1,2)),Point((1,1))]])
>>> len(c.parts)
2
vertices

Returns the vertices of the chain in clockwise order.

vertices -> Point list

Examples

>>> c = Chain([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((2, 1))])
>>> verts = c.vertices
>>> len(verts) 
4
class pysal.cg.shapes.Line(m, b)

Geometric representation of line objects.

Attributes

m float slope
b float y-intercept

Methods

y
y(x)

Returns the y-value of the line at a particular x-value.

y(number) -> number

Parameters:x : the x-value to compute y at

Examples

>>> l = Line(1, 0)
>>> l.y(1)
1
class pysal.cg.shapes.LineSegment(start_pt, end_pt)

Geometric representation of line segment objects. ...

Parameters:

start_pt : Point

Point where segment begins

end_pt : Point

Point where segment ends

Attributes

p1
p2
bounding_box
len
line

Methods

get_swap
is_ccw
is_cw
bounding_box

Returns the minimum bounding box of a LineSegment object.

Test tag: <tc>#is#LineSegment.bounding_box</tc> Test tag: <tc>#tests#LineSegment.bounding_box</tc>

bounding_box -> Rectangle

Examples

>>> ls = LineSegment(Point((1, 2)), Point((5, 6)))
>>> ls.bounding_box.left
1.0
>>> ls.bounding_box.lower
2.0
>>> ls.bounding_box.right
5.0
>>> ls.bounding_box.upper
6.0
get_swap()

Returns a LineSegment object which has its endpoints swapped.

get_swap() -> LineSegment

Test tag: <tc>#is#LineSegment.get_swap</tc> Test tag: <tc>#tests#LineSegment.get_swap</tc>

Examples

>>> ls = LineSegment(Point((1, 2)), Point((5, 6)))
>>> swap = ls.get_swap()
>>> swap.p1[0]
5.0
>>> swap.p1[1]
6.0
>>> swap.p2[0]
1.0
>>> swap.p2[1]
2.0
is_ccw(pt)

Returns whether a point is counterclockwise of the segment. Exclusive.

is_ccw(Point) -> bool

Test tag: <tc>#is#LineSegment.is_ccw</tc> Test tag: <tc>#tests#LineSegment.is_ccw</tc>

Parameters:pt : point lying ccw or cw of a segment

Examples

>>> ls = LineSegment(Point((0, 0)), Point((5, 0)))
>>> ls.is_ccw(Point((2, 2)))
True
>>> ls.is_ccw(Point((2, -2)))
False
is_cw(pt)

Returns whether a point is clockwise of the segment. Exclusive.

is_cw(Point) -> bool

Test tag: <tc>#is#LineSegment.is_cw</tc> Test tag: <tc>#tests#LineSegment.is_cw</tc>

Parameters:pt : point lying ccw or cw of a segment

Examples

>>> ls = LineSegment(Point((0, 0)), Point((5, 0)))
>>> ls.is_cw(Point((2, 2)))
False
>>> ls.is_cw(Point((2, -2)))
True
len

Returns the length of a LineSegment object.

Test tag: <tc>#is#LineSegment.len</tc> Test tag: <tc>#tests#LineSegment.len</tc>

len() -> number

Examples

>>> ls = LineSegment(Point((2, 2)), Point((5, 2)))
>>> ls.len
3.0
line

Returns a Line object of the line which the segment lies on.

Test tag: <tc>#is#LineSegment.line</tc> Test tag: <tc>#tests#LineSegment.line</tc>

line() -> Line

Examples

>>> ls = LineSegment(Point((2, 2)), Point((3, 3)))
>>> l = ls.line
>>> l.m
1.0
>>> l.b
0.0
p1

HELPER METHOD. DO NOT CALL.

Returns the p1 attribute of the line segment.

_get_p1() -> Point

Examples

>>> ls = LineSegment(Point((1, 2)), Point((5, 6)))
>>> r = ls._get_p1() 
>>> r == Point((1, 2))
True
p2

HELPER METHOD. DO NOT CALL.

Returns the p2 attribute of the line segment.

_get_p2() -> Point

Examples

>>> ls = LineSegment(Point((1, 2)), Point((5, 6)))
>>> r = ls._get_p2() 
>>> r == Point((5, 6))
True
class pysal.cg.shapes.Point(loc)

Geometric class for point objects.

Attributes

None  
class pysal.cg.shapes.Polygon(vertices, holes=None)

Geometric representation of polygon objects.

Attributes

vertices
len
perimeter
bounding_box
area
centroid
area

Returns the area of the polygon.

area -> number

Examples

>>> p = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
>>> p.area
1.0
>>> p = Polygon([Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))],[Point((2,1)),Point((2,2)),Point((1,2)),Point((1,1))])
>>> p.area
99.0
bounding_box

Returns the bounding box of the polygon.

bounding_box -> Rectangle

Examples

>>> p = Polygon([Point((0, 0)), Point((2, 0)), Point((2, 1)), Point((0, 1))])
>>> p.bounding_box.left
0.0
>>> p.bounding_box.lower
0.0
>>> p.bounding_box.right
2.0
>>> p.bounding_box.upper
1.0
centroid

Returns the centroid of the polygon.

centroid -> Point

Examples

>>> p = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
>>> cent = p.centroid
>>> str(cent)
'(0.5, 0.5)'
holes

Returns the holes of the polygon in clockwise order.

holes -> Point list

Examples

>>> p = Polygon([Point((0, 0)), Point((10, 0)), Point((10, 10)), Point((0, 10))], [Point((1, 2)), Point((2, 2)), Point((2, 1)), Point((1, 1))])
>>> len(p.holes)
1
len

Returns the number of vertices in the polygon.

len -> int

Examples

>>> p1 = Polygon([Point((0, 0)), Point((0, 1)), Point((1, 1)), Point((1, 0))])
>>> p1.len
4
>>> len(p1)
4
parts

Returns the parts of the polygon in clockwise order.

parts -> Point list

Examples

>>> p = Polygon([[Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))], [Point((2,1)),Point((2,2)),Point((1,2)),Point((1,1))]])
>>> len(p.parts)
2
perimeter

Returns the perimeter of the polygon.

perimeter() -> number

Examples

>>> p = Polygon([Point((0, 0)), Point((1, 0)), Point((1, 1)), Point((0, 1))])
>>> p.perimeter
4.0
vertices

Returns the vertices of the polygon in clockwise order.

vertices -> Point list

Examples

>>> p1 = Polygon([Point((0, 0)), Point((0, 1)), Point((1, 1)), Point((1, 0))])
>>> len(p1.vertices)
4
class pysal.cg.shapes.Ray(origin, second_p)

Geometric representation of ray objects.

Attributes

o Point Origin (point where ray originates)
p Point Second point on the ray (not point where ray originates)
class pysal.cg.shapes.Rectangle(left, lower, right, upper)

Geometric representation of rectangle objects.

Attributes

left float Minimum x-value of the rectangle
lower float Minimum y-value of the rectangle
right float Maximum x-value of the rectangle
upper float Maximum y-value of the rectangle

Methods

set_centroid
set_scale
area

Returns the area of the Rectangle.

area -> number

Examples

>>> r = Rectangle(0, 0, 4, 4)
>>> r.area
16.0
height

Returns the height of the Rectangle.

height -> number

Examples

>>> r = Rectangle(0, 0, 4, 4)
>>> r.height
4.0
set_centroid(new_center)

Moves the rectangle center to a new specified point.

set_centroid(Point) -> Point

Parameters:new_center : the new location of the centroid of the polygon

Examples

>>> r = Rectangle(0, 0, 4, 4)
>>> r.set_centroid(Point((4, 4)))
>>> r.left
2.0
>>> r.right
6.0
>>> r.lower
2.0
>>> r.upper
6.0
set_scale(scale)

Rescales the rectangle around its center.

set_scale(number) -> number

Parameters:scale : the ratio of the new scale to the old scale (e.g. 1.0 is current size)

Examples

>>> r = Rectangle(0, 0, 4, 4)
>>> r.set_scale(2)
>>> r.left
-2.0
>>> r.right
6.0
>>> r.lower
-2.0
>>> r.upper
6.0
width

Returns the width of the Rectangle.

width -> number

Examples

>>> r = Rectangle(0, 0, 4, 4)
>>> r.width
4.0