Ignore:
Timestamp:
Jun 2, 2006, 1:12:20 PM (19 years ago)
Author:
ole
Message:

Changed semantics of inside_polygon (and outside_polygon) so that one point is treated as a list containing that one point. For the old boolean functionality use is_inside_polygon and is_outside_polygon.
Fixed unit tests as well

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/utilities/polygon.py

    r3051 r3052  
    4343
    4444
    45 def inside_polygon(points, polygon, closed = True, verbose = False,
     45def is_inside_polygon(point, polygon, closed=True, verbose=False,
     46                      points_geo_ref=None, polygon_geo_ref=None):
     47    """Determine if one point is inside a polygon
     48
     49    See inside_polygon for more details
     50    """
     51
     52    indices = inside_polygon(point, polygon, closed, verbose,
     53                             points_geo_ref, polygon_geo_ref)
     54
     55    if indices.shape[0] == 1:
     56        return True
     57    elif indices.shape[0] == 0:
     58        return False
     59    else:
     60        msg = 'is_inside_polygon must be invoked with one point only'
     61        raise msg
     62   
     63
     64def inside_polygon(points, polygon, closed=True, verbose=False,
    4665                   points_geo_ref=None, polygon_geo_ref=None):
    4766    """Determine points inside a polygon
     
    7392
    7493    if len(points.shape) == 1:
    75         one_point = True
    76         points = reshape(points, (1,2))
    77     else:
    78         one_point = False
     94        # Only one point was passed in. Convert to array of points
     95        points = reshape(points, (1,2))
    7996
    8097    indices, count = separate_points_by_polygon(points, polygon,
     
    8299                                                verbose=verbose)
    83100
    84     if one_point:
    85         return count == 1
     101    # Return indices of points inside polygon
     102    return indices[:count]
     103
     104
     105
     106def is_outside_polygon(point, polygon, closed=True, verbose=False,
     107                       points_geo_ref=None, polygon_geo_ref=None):
     108    """Determine if one point is outside a polygon
     109
     110    See outside_polygon for more details
     111    """
     112
     113    indices = outside_polygon(point, polygon, closed, verbose)
     114                              #points_geo_ref, polygon_geo_ref)
     115
     116    if indices.shape[0] == 1:
     117        return True
     118    elif indices.shape[0] == 0:
     119        return False
    86120    else:
    87         return indices[:count]
     121        msg = 'is_outside_polygon must be invoked with one point only'
     122        raise msg
     123   
    88124
    89125def outside_polygon(points, polygon, closed = True, verbose = False):
     
    115151
    116152
    117 
    118153    if len(points.shape) == 1:
    119         one_point = True
    120         points = reshape(points, (1,2))
    121     else:
    122         one_point = False
     154        # Only one point was passed in. Convert to array of points
     155        points = reshape(points, (1,2))
    123156
    124157    indices, count = separate_points_by_polygon(points, polygon,
     
    126159                                                verbose=verbose)
    127160
    128    
    129     if one_point:
    130         return count != 1
     161    # Return indices of points outside polygon
     162    if count == len(indices):
     163        # No points are outside
     164        return array([])
    131165    else:
    132         if count == len(indices):
    133             # No points are outside
    134             return []
    135         else:
    136             return indices[count:][::-1]  #return reversed
     166        return indices[count:][::-1]  #return reversed
    137167       
    138168
     
    162192        raise msg
    163193
    164 
    165 
    166194    if len(points.shape) == 1:
    167         one_point = True
    168         points = reshape(points, (1,2))
    169     else:
    170         one_point = False
     195        # Only one point was passed in. Convert to array of points
     196        points = reshape(points, (1,2))
     197
    171198
    172199    indices, count = separate_points_by_polygon(points, polygon,
    173200                                                closed=closed,
    174201                                                verbose=verbose)
    175     # Returns an array of points inside and an array of points outside
     202   
     203    # Returns indices of points inside and indices of points outside
    176204    # the polygon
    177  
     205
    178206    if count == len(indices):
    179207        # No points are outside
     
    637665
    638666        append = False
    639         if inside_polygon( [x,y], polygon ):
     667        if is_inside_polygon([x,y], polygon):
    640668
    641669            append = True
     
    644672            if exclude is not None:
    645673                for ex_poly in exclude:
    646                     if inside_polygon( [x,y], ex_poly ):
     674                    if is_inside_polygon([x,y], ex_poly):
    647675                        append = False
    648676
     
    689717                        point = [x_delta, y_delta]
    690718                        #print "point",point
    691                         if inside_polygon( point, polygon, closed = False ):
     719                        if is_inside_polygon(point, polygon, closed=False):
    692720                            raise Found
    693721        except Found:
Note: See TracChangeset for help on using the changeset viewer.