This function returns the corner points for a
    polygon from scipy voronoi information
480558
def _get_voronoi_poly_points(vert_index_list, voronoi_vertices,
                             voronoi_centroid):
    
    voronoi_poly_points = []
    if -1 not in vert_index_list and len(vert_index_list) > 3:
        voronoi_poly_points = voronoi_vertices[vert_index_list]
    elif vert_index_list.size > 0:
        # ASSUME RECTANGLE
        vert_index_list = vert_index_list[vert_index_list >= 0]
        voronoi_poly_points = voronoi_vertices[vert_index_list]
        # CASE 1: 2 valid voronoi vertices
        if vert_index_list.size == 2:
            center_lon = voronoi_centroid[0]
            center_lat = voronoi_centroid[1]
            corner_lon1 = voronoi_poly_points[0][0]
            corner_lat1 = voronoi_poly_points[0][1]
            corner_lon2 = voronoi_poly_points[1][0]
            corner_lat2 = voronoi_poly_points[1][1]

            # check if need to add points in lon or lat
            if abs(corner_lon1-corner_lon2) > abs(corner_lat1-corner_lat2):
                dLat = center_lat - corner_lat1
                # append the corners in order
                voronoi_poly_points = np.array([
                    [corner_lon1, corner_lat1],
                    [corner_lon2, corner_lat2],
                    [corner_lon2, center_lat + dLat],
                    [corner_lon1, center_lat + dLat]
                ])
            else:
                dLon = center_lon - corner_lon1
                # append the corners in order
                voronoi_poly_points = np.array([
                    [corner_lon1, corner_lat1],
                    [corner_lon2, corner_lat2],
                    [center_lon + dLon, corner_lat2],
                    [center_lon + dLon, corner_lat1]
                ])
        # CASE 2: 1 valid voronoi vertex
        elif vert_index_list.size == 1:
            center_lon = voronoi_centroid[0]
            center_lat = voronoi_centroid[1]
            corner_lon = voronoi_poly_points[0][0]
            corner_lat = voronoi_poly_points[0][1]
            dLat = center_lat - corner_lat
            dLon = center_lon - corner_lon
            # append the corners in order
            voronoi_poly_points = np.array([
                [corner_lon, corner_lat],
                [center_lon + dLon, corner_lat],
                [center_lon + dLon, center_lat + dLat],
                [corner_lon, center_lat + dLat]
            ])

    return voronoi_poly_points
961825
function fastConvexHull(points) {
    var left = points[0];
    var top = points[0];
    var right = points[0];
    var bottom = points[0];

    // find the leftmost, rightmost, topmost and bottommost points
    for (var i = 0; i < points.length; i++) {
        var p = points[i];
        if (p[0] < left[0]) left = p;
        if (p[0] > right[0]) right = p;
        if (p[1] < top[1]) top = p;
        if (p[1] > bottom[1]) bottom = p;
    }

    // filter out points that are inside the resulting quadrilateral
    var cull = [left, top, right, bottom];
    var filtered = cull.slice();
    for (i = 0; i < points.length; i++) {
        if (!pointInPolygon(points[i], cull)) filtered.push(points[i]);
    }

    // get convex hull around the filtered points
    var indices = convexHull(filtered);

    // return the hull as array of points (rather than indices)
    var hull = [];
    for (i = 0; i < indices.length; i++) hull.push(filtered[indices[i]]);
    return hull;
}
38083
def get_bounding_box(obj, maxdist):
    
    if hasattr(obj, 'get_bounding_box'):
        return obj.get_bounding_box(maxdist)
    elif hasattr(obj, 'polygon'):
        bbox = obj.polygon.get_bbox()
    else:
        if isinstance(obj, list):  # a list of locations
            lons = numpy.array([loc.longitude for loc in obj])
            lats = numpy.array([loc.latitude for loc in obj])
        else:  # assume an array with fields lon, lat
            lons, lats = obj['lon'], obj['lat']
        min_lon, max_lon = lons.min(), lons.max()
        if cross_idl(min_lon, max_lon):
            lons %= 360
        bbox = lons.min(), lats.min(), lons.max(), lats.max()
    a1 = min(maxdist * KM_TO_DEGREES, 90)
    a2 = min(angular_distance(maxdist, bbox[1], bbox[3]), 180)
    return bbox[0] - a2, bbox[1] - a1, bbox[2] + a2, bbox[3] + a1
275978
def get_bounding_box(df_points):
    '''
    
    '''
    xy_min = df_points[['x', 'y']].min()
    xy_max = df_points[['x', 'y']].max()

    wh = xy_max - xy_min
    wh.index = 'width', 'height'
    bbox = pd.concat([xy_min, wh])
    bbox.name = 'bounding_box'
    return bbox
50359
def convex_hull_image(image):
    ''''''
    labels = image.astype(int)
    points, counts = convex_hull(labels, np.array([1]))
    output = np.zeros(image.shape, int)
    for i in range(counts[0]):
        inext = (i+1) % counts[0]
        draw_line(output, points[i,1:], points[inext,1:],1)
    output = fill_labeled_holes(output)
    return output == 1
643134
def points(self):
        
        x = vtk_to_numpy(self.GetXCoordinates())
        y = vtk_to_numpy(self.GetYCoordinates())
        z = vtk_to_numpy(self.GetZCoordinates())
        xx, yy, zz = np.meshgrid(x,y,z, indexing='ij')
        return np.c_[xx.ravel(), yy.ravel(), zz.ravel()]
381968
def simplices(self):
        
        return [Simplex([self.points[i] for i in v]) for v in self.vertices]
1322468
def get_edges(ont):
    
    logging.info("QUERYING:"+ont)
    edges = [(c,SUBCLASS_OF, d) for (c,d) in fetchall_isa(ont)]
    edges += fetchall_svf(ont)
    edges += [(c,SUBPROPERTY_OF, d) for (c,d) in fetchall_subPropertyOf(ont)]
    if len(edges) == 0:
        logging.warn("No edges for {}".format(ont))
    return edges
971783
def extract_polygon_area(self, pid, polygon_points):
        
        polygon = shapgeo.Polygon(polygon_points)
        xy = self.grid.get_element_centroids()
        in_poly = []
        for nr, point in enumerate(xy):
            if shapgeo.Point(point).within(polygon):
                in_poly.append(nr)

        values = self.parsets[pid][in_poly]
        return np.array(in_poly), values
302075
def corners_to_keypoints(corners):
    """"""
    if corners is None:
        keypoints = []
    else:
        keypoints = [cv2.KeyPoint(kp[0][0], kp[0][1], 1) for kp in corners]

    return keypoints
456862
def _polygon_from_coords(coords, fix_geom=False, swap=True, dims=2):
    
    assert len(coords) % dims == 0
    number_of_points = len(coords)/dims
    coords_as_array = np.array(coords)
    reshaped = coords_as_array.reshape(number_of_points, dims)
    points = [
        (float(i[1]), float(i[0])) if swap else ((float(i[0]), float(i[1])))
        for i in reshaped.tolist()
        ]
    polygon = Polygon(points).buffer(0)
    try:
        assert polygon.is_valid
        return polygon
    except AssertionError:
        if fix_geom:
            return polygon.buffer(0)
        else:
            raise RuntimeError("Geometry is not valid.")
334356
def compute_convex_hull(feed: "Feed") -> Polygon:
    
    m = sg.MultiPoint(feed.stops[["stop_lon", "stop_lat"]].values)
    return m.convex_hull
1355607
def get_bounding_box(points):
    
    assert len(points) > 0, "At least one point has to be given."
    min_x, max_x = points[0]['x'], points[0]['x']
    min_y, max_y = points[0]['y'], points[0]['y']
    for point in points:
        min_x, max_x = min(min_x, point['x']), max(max_x, point['x'])
        min_y, max_y = min(min_y, point['y']), max(max_y, point['y'])
    p1 = Point(min_x, min_y)
    p2 = Point(max_x, max_y)
    return BoundingBox(p1, p2)
1324338
def grid_coords_from_corners(upper_left_corner, lower_right_corner, size):
    '''  '''
    assert upper_left_corner.wkt == lower_right_corner.wkt
    geotransform = np.array([upper_left_corner.lon, -(upper_left_corner.lon - lower_right_corner.lon) / float(size[1]), 0,
                            upper_left_corner.lat, 0, -(upper_left_corner.lat - lower_right_corner.lat) / float(size[0])])
    return GridCoordinates(geotransform=geotransform,
                               wkt=upper_left_corner.wkt,
                               y_size=size[0],
                               x_size=size[1])
1321876
def convex_hull(self):
        
        points = util.vstack_empty([m.vertices for m in self.dump()])
        hull = convex.convex_hull(points)
        return hull
851510
def points(self, points):
        
        if not isinstance(points, np.ndarray):
            raise TypeError('Points must be a numpy array')
        # get the unique coordinates along each axial direction
        x = np.unique(points[:,0])
        y = np.unique(points[:,1])
        z = np.unique(points[:,2])
        nx, ny, nz = len(x), len(y), len(z)
        # TODO: this needs to be tested (unique might return a tuple)
        dx, dy, dz = np.unique(np.diff(x)), np.unique(np.diff(y)), np.unique(np.diff(z))
        ox, oy, oz = np.min(x), np.min(y), np.min(z)
        # Build the vtk object
        self._from_specs((nx,ny,nz), (dx,dy,dz), (ox,oy,oz))
        #self._point_ref = points
        self.Modified()
807590
def to_polygon(self):
        
        x,y = self.corners.T
        vertices = PixCoord(x=x, y=y)
        return PolygonPixelRegion(vertices=vertices, meta=self.meta,
                                  visual=self.visual)