In [2]:
from shapeanalyzer.boundings import BoundingGeometry
from shapeanalyzer.calipers import RotatingCaliper
from shapeanalyzer.shapes import *
import math

points = []
fp = open("data/twpoi.csv")
lines = fp.readlines()
del lines[0]
for r in lines:
    r = r.strip().split(",")
    pt = Point(float(r[0]), float(r[1]))
    points.append(pt)
    
bg = BoundingGeometry(points)
bbox = bg.Envelope()
hull = bg.ConvexHull()
circle, support = bg.MinimumAreaCircle()
rect = bg.MinimumWidthRectangle()
rc = RotatingCaliper(hull)
antipodals = rc.AntiPodals()
diameter, dsup = rc.Diameter()
width, wsup = rc.Width()
#print bbox
#print hull
#print circle, support
#print antipodals
print diameter, dsup
print width, wsup
print rect


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-695a54d5904d> in <module>()
     18 hull = bg.ConvexHull()
     19 circle, support = bg.MinimumAreaCircle()
---> 20 rect = bg.MinimumWidthRectangle()
     21 rc = RotatingCaliper(hull)
     22 antipodals = rc.AntiPodals()

/Users/luwei/GeoInsights/ShapeAnalyzer/shapeanalyzer/boundings.pyc in MinimumWidthRectangle(self)
     46         hull = self.ConvexHull()
     47         rc = RotatingCaliper(hull)
---> 48         width, support = rc.Width()
     49         min0, max0 = 0, 0
     50         V0 = Vector2(support[1].x - support[0].x, support[1].y - support[0].y)

/Users/luwei/GeoInsights/ShapeAnalyzer/shapeanalyzer/calipers.pyc in Width(self)
     59             for j in range(self._antipodals[i][0], self._antipodals[i][1] + 1):
     60                 pt = self._points[j]
---> 61                 td = lseg.distance(pt)
     62                 if td < w:
     63                     w = td

/Users/luwei/GeoInsights/ShapeAnalyzer/shapeanalyzer/shapes.pyc in distance(self, pt)
     43 
     44     def distance(self, pt):
---> 45         v1 = Vector2(pt.x - self.fpt.x, pt.y - self.fpt.y)
     46         v2 = Vector2(self.tpt.x - self.fpt.x, self.tpt.y  - self.fpt.y)
     47         height = math.fabs(v1.cross(v2)) / v1.length()

NameError: global name 'Vector2' is not defined

In [ ]:
import matplotlib.pyplot as plt
import numpy as np
plt.axis("equal")
fig_size = [10,10]
plt.rcParams["figure.figsize"] = fig_size

def plot_bbox(bbox):
    minx, miny, maxx, maxy = bbox
    xs = [minx, minx, maxx, maxx, minx]
    ys = [miny, maxy, maxy, miny, miny]
    plt.plot(xs,ys,'r')

def plot_poly(poly):
    parr = []
    for p in poly:
        parr.append(p.coords())
    parr.append(poly[len(poly)-1].coords())
    parr = np.asarray(parr)
    plt.plot(parr[:,0], parr[:,1],"g-")

    
def plot_circle(circle):
    #plt.plot(circle.center.x, circle.center.y, "or")
    circle = plt.Circle((circle.center.x, circle.center.y), radius=circle.radius, color='c', fill=False)
    plt.gca().add_patch(circle)
    plt.axis('scaled')

def plot_rect(rect):
    plt.plot(rect.center.x, rect.center.y, "or")
    center = rect.center.coords()
    axx = [rect.axes[0].x,rect.axes[0].y]
    axy = [rect.axes[1].x,rect.axes[1].y]
    extx = rect.extents[0] / 2
    exty = rect.extents[1] / 2
    pt1 = np.asarray(center) + extx * np.asarray(axx) + exty * np.asarray(axy)
    pt2 = np.asarray(center) + extx * np.asarray(axx) - exty * np.asarray(axy)
    pt3 = np.asarray(center) - extx * np.asarray(axx) - exty * np.asarray(axy)
    pt4 = np.asarray(center) - extx * np.asarray(axx) + exty * np.asarray(axy)
    r = np.array([pt1,pt2,pt3,pt4,pt1])
    plt.plot(r[:,0], r[:,1],"m-")
parr = []
for p in points:
    parr.append(p.coords())
parr = np.asarray(parr)
plt.plot(parr[:,0], parr[:,1],"ko")
plot_bbox(bbox)
plot_poly(hull)
plot_circle(circle)
plot_poly(wsup)
plot_rect(rect)

plt.show()

In [ ]:


In [ ]: