コード例 #1
0
ファイル: voronoi_may31.py プロジェクト: is0392hr/catkin_ws3
    def update_inner(inner_pos, area_shape):
        pts = [p for p in coords_to_points(inner_pos)
               if p.within(area_shape)]  # converts to shapely Point
        coords = points_to_coords(
            pts)  # convert back to simple NumPy coordinate array

        return coords
コード例 #2
0
ファイル: voronoi_may31.py プロジェクト: is0392hr/catkin_ws3
 def match_pair(poly_shapes, coords, new_centroids):
     sorted_coords = []
     points = coords_to_points(coords)
     for i, p in enumerate(points):
         c = coords[i]
         #print("c: ", c[0],c[1])
         for j, poly in enumerate(poly_shapes):
             if p.within(poly):
                 pair = new_centroids[j]
                 sorted_coords.append(pair)
     return sorted_coords
コード例 #3
0
def _rand_coords_in_shape(area_shape, n_points):
    # generate some random points within the bounds
    minx, miny, maxx, maxy = area_shape.bounds

    randx = np.random.uniform(minx, maxx, n_points)
    randy = np.random.uniform(miny, maxy, n_points)
    coords = np.vstack((randx, randy)).T

    # use only the points inside the geographic area
    pts = [p for p in coords_to_points(coords)
           if p.within(area_shape)]  # converts to shapely Point
    return points_to_coords(pts)
コード例 #4
0
print('CRS:', area.crs)  # gives epsg:4326 -> WGS 84

area = area.to_crs(
    epsg=3035
)  # convert to Albers Equal Area CRS (ETRS89-extended / LAEA Europe)
area_shape = area.iloc[0].geometry  # get the Polygon

# generate some random points within the bounds
minx, miny, maxx, maxy = area_shape.bounds

randx = np.random.uniform(minx, maxx, N_POINTS)
randy = np.random.uniform(miny, maxy, N_POINTS)
coords = np.vstack((randx, randy)).T

# use only the points inside the geographic area
pts = [p for p in coords_to_points(coords)
       if p.within(area_shape)]  # converts to shapely Point
n_pts = len(pts)

print(
    'will use %d of %d randomly generated points that are inside geographic area'
    % (n_pts, N_POINTS))
coords = points_to_coords(pts)  # convert back to simple NumPy coordinate array

del pts

#%%

#
# calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them
#
コード例 #5
0
    def voronoi_ucs(self, ucdb_slice, country_shape):
        """
        return the slice but with an extra column for the extended area.
        """

        all_coords = []
        for row in ucdb_slice.iterrows():

            if row[1].geometry.type == 'Polygon':  # and not row[1].geometry.is_empty:
                all_coords += self._resample_coords(
                    row[1].geometry.exterior.coords)

            elif row[
                    1].geometry.type == 'MultiPolygon':  # and not row[1].geometry.is_empty:
                for subgeom in list(row[1].geometry):
                    all_coords += self._resample_coords(
                        subgeom.exterior.coords)

        vor_coords = np.array(all_coords)

        vor = Voronoi(vor_coords)

        poly_lines = polygon_lines_from_voronoi(vor, country_shape)
        """
        fig, ax = plt.subplots(1,1,figsize=(6,6))

        print ('VLIDDd')
        print (country_shape.is_valid)
        for l in poly_lines:
            xs,ys = l.xy
            ax.plot(xs,ys,c='g')
            #for subpp in list(country_shape):
            #    if subpp.intersects(l):
            #        xs,ys = subpp.exterior.xy
            #        ax.plot(xs,ys,c='r')
        plt.show()
        """

        fix_poly_lines = []

        for l in poly_lines:
            if l.intersects(self.wgs_box.exterior):
                valid_coords = [
                    c for c in l.coords
                    if geometry.Point(c).within(self.wgs_box)
                ]
                if len(valid_coords) > 2:
                    fix_poly_lines.append(geometry.LineString(valid_coords))
            else:
                fix_poly_lines.append(l)
        """
        for l in polygonize(poly_lines):
            try:
                print (l.bounds, l.is_valid, l.intersection(country_shape).is_valid)
            except:
                fig, ax = plt.subplots(1,1,figsize=(6,6))
                xs,ys = l.exterior.xy
                ax.plot(xs,ys,c='g')
                #for subpp in list(country_shape):
                #    if subpp.intersects(l):
                #        xs,ys = subpp.exterior.xy
                #        ax.plot(xs,ys,c='r')
                plt.show()
            if not l.is_valid:
                print (l.is_valid)
        """

        poly_shapes = polygon_shapes_from_voronoi_lines(
            fix_poly_lines, country_shape)
        poly_shapes = [pp for pp in poly_shapes if not pp.is_empty]

        points = coords_to_points(vor_coords)

        tree = STRtree(poly_shapes)

        ucdb_slice['exp_geom'] = ucdb_slice.apply(
            lambda row: self._tree_search_union(tree, row), axis=1)

        return ucdb_slice, poly_shapes, all_coords
コード例 #6
0
assert len(area) == 1

print('CRS:', area.crs)   # gives epsg:4326 -> WGS 84

area = area.to_crs(epsg=3395)    # convert to World Mercator CRS
area_shape = area.iloc[0].geometry   # get the Polygon

# generate some random points within the bounds
minx, miny, maxx, maxy = area_shape.bounds

randx = np.random.uniform(minx, maxx, N_POINTS)
randy = np.random.uniform(miny, maxy, N_POINTS)
coords = np.vstack((randx, randy)).T

# use only the points inside the geographic area
pts = [p for p in coords_to_points(coords) if p.within(area_shape)]  # converts to shapely Point

print('will use %d of %d randomly generated points that are inside geographic area' % (len(pts), N_POINTS))
coords = points_to_coords(pts)   # convert back to simple NumPy coordinate array

del pts

#
# calculate the Voronoi regions, cut them with the geographic area shape and assign the points to them
#

poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(coords, area_shape)

#
# plotting
#
コード例 #7
0
ファイル: voronoi_may31.py プロジェクト: is0392hr/catkin_ws3
    def update(self, outer_pos, inner_pos, UPDATE, EPS=0.1):
        global t
        t = time.time() - start

        outputs = []

        global FLAG
        if FLAG:
            fig, ax = subplot_for_map()
            global ax, fig
            FLAG = False

        def reshape_coords(coords):
            new_coords = []
            for p in poly_shapes:
                for n in coords:
                    m = Point(n)
                    if m.within(p):
                        new_coords.append(n)
            return new_coords

        def reshape_centroids(centroids):
            new_centroids = []
            for p in poly_shapes:
                for n in centroids:
                    m = Point(n)
                    if m.within(p):
                        new_cent
                        roids.append(n)
            return new_centroids

        def match_pair(poly_shapes, coords, new_centroids):
            sorted_coords = []
            points = coords_to_points(coords)
            for i, p in enumerate(points):
                c = coords[i]
                #print("c: ", c[0],c[1])
                for j, poly in enumerate(poly_shapes):
                    if p.within(poly):
                        pair = new_centroids[j]
                        sorted_coords.append(pair)
            return sorted_coords

        N = 4  #len(inner_pos)

        area_shape = Polygon(outer_pos)  #update_outer(outer_pos)

        # generate some random points within the bounds
        minx, miny, maxx, maxy = area_shape.bounds

        pts = [p for p in coords_to_points(inner_pos)
               if p.within(area_shape)]  # converts to shapely Point

        while len(pts) < N:  #isinstance(compensated, int):
            inner_pos = points_to_coords(pts)
            print('%d of %d drone"s pos is available' % (len(pts), N))
            #print("compensated!!", compensated, type(compensated))

            randx = np.random.uniform(minx, maxx, N - len(pts))
            randy = np.random.uniform(miny, maxy, N - len(pts))
            compensated = np.vstack((randx, randy)).T
            inner_pos = np.append(inner_pos, compensated, axis=0)
            #print(inner_pos)
            #inner_pos = inner_pos[sorted(np.random.choice(inner_pos.shape[0], N, replace=False)), :]
            pts = [
                p for p in coords_to_points(inner_pos) if p.within(area_shape)
            ]  # converts to shapely Point

        ax.clear()  # comment out if you want to plot trajectory
        coords = points_to_coords(
            pts)  # convert back to simple NumPy coordinate array
        poly_shapes, pts, poly_to_pt_assignments = voronoi_regions_from_coords(
            coords, area_shape, accept_n_coord_duplicates=0)

        poly_centroids = np.array([p.centroid.coords[0] for p in poly_shapes])
        #new_centroids = reshape_centroids(poly_centroids)

        # plotting
        EPS = EPS
        err = 99999

        #old_coords = coords
        new_centroids = match_pair(poly_shapes, coords, poly_centroids)

        for i in range(len(coords)):
            xo = coords[i][0]
            yo = coords[i][1]
            #old_coords[i][0] = xo
            #old_coords[i][1] = yo
            xc = new_centroids[i][0]
            yc = new_centroids[i][1]
            #err = np.sqrt((xo-xc)**2 + (yo-yc)**2)

            data = [xc, yc]

            outputs.append(data)  #(np.array((xc, yc)).astype(np.float64))

            #if  err > EPS:
            #    # print("UPDARED!!")
            #    coords[i][0] = xc#xo + 0.2*(xc-xo)
            #    coords[i][1] = yc#yo + 0.2*(yc-yo)

        # draw centroid that each drone follow
        for i, centroid in enumerate(new_centroids):
            c1 = centroid
            ax.plot(c1[0], c1[1], '*', label=str(i))
        for coord in coords:
            c = coord
            ax.plot(c[0], c[1], 'o', alpha=0.5)

        fig = plot_voronoi_polys_with_points_in_area(ax, area_shape,
                                                     poly_shapes, coords,
                                                     poly_to_pt_assignments)
        plt.title(str(t) + "[s]")

        plt.pause(0.00001)
        return outputs
コード例 #8
0
def test_coords_to_points_and_points_to_coords(coords):
    assert np.array_equal(points_to_coords(coords_to_points(coords)), coords)
コード例 #9
0
def test_coords_to_points_and_points_to_coords(coords):
    # test for bijectivity of points_to_coords and coords_to_points
    assert np.array_equal(points_to_coords(coords_to_points(coords)), coords)
コード例 #10
0
def Within(pts, shape):
    pts = coords_to_points(pts)
    pts = [p for p in pts if p.within(shape)]
    return [(x, y) for x, y in points_to_coords(pts)]