'returns true if tuple point (x,y) is inside polygon of the form ((a,b),(c,d),...,(a,b)) the polygon should be closed'

def _pointInsidePolygon(self, point, polygon):

    if (len(polygon) < 3):
        logger.warning('feature._pointInsidePolygon    -    this    is    not    a    valid    polygon')
        return False
    if (not isinstance(polygon, list)):
        logger.warning('feature._pointInsidePolygon    -    this    is    not    a    valid    polygon')
        return False
    counter = 0
    retVal = True
    p1 = None
    poly = copy.deepcopy(polygon)
    poly.append(polygon[0])
    N = len(poly)
    p1 = poly[0]
    for i in range(1, (N + 1)):
        p2 = poly[(i % N)]
        if (point[1] > np.min((p1[1], p2[1]))):
            if (point[1] <= np.max((p1[1], p2[1]))):
                if (point[0] <= np.max((p1[0], p2[0]))):
                    if (p1[1] != p2[1]):
                        test = (float(((point[1] - p1[1]) * (p2[0] - p1[0]))) / float(((p2[1] - p1[1]) + p1[0])))
                        if ((p1[0] == p2[0]) or (point[0] <= test)):
                            counter = (counter + 1)
        p1 = p2
    if ((counter % 2) == 0):
        retVal = False
        return retVal
    return retVal
