Changeset 2912 for inundation/utilities


Ignore:
Timestamp:
May 18, 2006, 4:32:42 PM (18 years ago)
Author:
sexton
Message:

incorporate plot_polygon in polygon rather than pyvolution.util; also polygon_area functionality added

Location:
inundation/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/utilities/polygon.py

    r2655 r2912  
    378378
    379379
     380def polygon_area(polygon):
     381    """ Determin area of arbitrary polygon
     382    Reference
     383    http://mathworld.wolfram.com/PolygonArea.html
     384    """
     385   
     386    n = len(polygon)
     387    poly_area = 0.0
     388
     389    for i in range(n):
     390        pti = polygon[i]
     391        if i == n-1:
     392            pt1 = polygon[0]
     393        else:
     394            pt1 = polygon[i+1]
     395        xi = pti[0]
     396        yi1 = pt1[1]
     397        xi1 = pt1[0]
     398        yi = pti[1]
     399        poly_area += xi*yi1 - xi1*yi
     400       
     401    return abs(poly_area/2)
     402
     403def plot_polygons(polygons,
     404                  figname,
     405                  verbose = False):
     406   
     407    """ Take list of polygons and plot.
     408
     409    Inputs:
     410
     411    polygons         - list of polygons
     412                       
     413    figname          - name to save figure to
     414
     415    Outputs:
     416
     417    - list of min and max of x and y coordinates
     418    - plot of polygons
     419    """
     420
     421    from pylab import ion, hold, plot, axis, figure, legend, savefig, xlabel, ylabel, title, close
     422
     423    assert type(polygons) == list,\
     424               'polygon must be a list'
     425
     426    ion()
     427    hold(True)
     428
     429    minx = 1e10
     430    maxx = 0.0
     431    miny = 1e10
     432    maxy = 0.0
     433    for i in range(len(polygons)):
     434        x, y = poly_xy(polygons[i]) 
     435        if min(x) < minx: minx = min(x)
     436        if max(x) > maxx: maxx = max(x)
     437        if min(y) < miny: miny = min(y)
     438        if max(y) > maxy: maxy = max(y)
     439        plot(x,y,'r-')
     440        xlabel('x')
     441        ylabel('y')
     442
     443    savefig(figname)
     444
     445    close('all')
     446
     447    vec = [minx,maxx,miny,maxy]
     448
     449    return vec
     450
     451def poly_xy(poly):
     452    """ this is used within plot_polygons so need to duplicate
     453        the first point so can have closed polygon in plot
     454    """
     455
     456    assert type(poly[0]) == list,\
     457               'polygon must be a list of points'
     458   
     459    x = []
     460    y = []
     461    n = len(poly)
     462    firstpt = poly[0]
     463    for i in range(n):
     464        thispt = poly[i]
     465        x.append(thispt[0])
     466        y.append(thispt[1])
     467
     468    x.append(firstpt[0])
     469    y.append(firstpt[1])
     470   
     471    return x, y
    380472
    381473class Polygon_function:
  • inundation/utilities/test_polygon.py

    r2655 r2912  
    548548        assert len(res) == 2
    549549        assert allclose(res, [0,1])
    550        
     550
     551    def test_polygon_area(self):
     552
     553        #Simplest case: Polygon is the unit square
     554        polygon = [[0,0], [1,0], [1,1], [0,1]]
     555        assert polygon_area(polygon) == 1
     556
     557        #Simple case: Polygon is a rectangle
     558        polygon = [[0,0], [1,0], [1,4], [0,4]]
     559        assert polygon_area(polygon) == 4
     560
     561        #Simple case: Polygon is a unit triangle
     562        polygon = [[0,0], [1,0], [0,1]]
     563        assert polygon_area(polygon) == 0.5
     564
     565        #Simple case: Polygon is a diamond
     566        polygon = [[0,0], [1,1], [2,0], [1, -1]]
     567        assert polygon_area(polygon) == 2.0
     568
     569    def test_poly_xy(self):
     570 
     571        #Simplest case: Polygon is the unit square
     572        polygon = [[0,0], [1,0], [1,1], [0,1]]
     573        x, y = poly_xy(polygon)
     574        assert len(x) == len(polygon)+1
     575        assert len(y) == len(polygon)+1
     576        assert x[0] == 0
     577        assert x[1] == 1
     578        assert x[2] == 1
     579        assert x[3] == 0
     580        assert y[0] == 0
     581        assert y[1] == 0
     582        assert y[2] == 1
     583        assert y[3] == 1
     584
     585        #Arbitrary polygon
     586        polygon = [[1,5], [1,1], [100,10], [1,10], [3,6]]
     587        x, y = poly_xy(polygon)
     588        assert len(x) == len(polygon)+1
     589        assert len(y) == len(polygon)+1
     590        assert x[0] == 1
     591        assert x[1] == 1
     592        assert x[2] == 100
     593        assert x[3] == 1
     594        assert x[4] == 3
     595        assert y[0] == 5
     596        assert y[1] == 1
     597        assert y[2] == 10
     598        assert y[3] == 10
     599        assert y[4] == 6
     600
     601    def test_plot_polygons(self):
     602
     603        import os
     604       
     605        #Simplest case: Polygon is the unit square
     606        polygon1 = [[0,0], [1,0], [1,1], [0,1]]
     607        polygon2 = [[1,1], [2,1], [3,2], [2,2]]
     608        v = plot_polygons([polygon1, polygon2],'test1')
     609        assert len(v) == 4
     610        assert v[0] == 0
     611        assert v[1] == 3
     612        assert v[2] == 0
     613        assert v[3] == 2
     614
     615        #Another case
     616        polygon3 = [[1,5], [10,1], [100,10], [50,10], [3,6]]
     617        v = plot_polygons([polygon2,polygon3],'test2')
     618        assert len(v) == 4
     619        assert v[0] == 1
     620        assert v[1] == 100
     621        assert v[2] == 1
     622        assert v[3] == 10
     623
     624        os.remove('test1.png')
     625        os.remove('test2.png')
     626       
    551627#-------------------------------------------------------------
    552628if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.