예제 #1
0
class GraphicsComponent():
    def __init__(self, color, fill, edge_color):
        self.color = color
        self.edge_color = edge_color
        self.fill = fill
        self.patch = None

    def update(self, geometry_component, canvas):
        if self.patch is None:
            self.patch = PolygonPatch(geometry_component.polygon,
                                      fc=self.color,
                                      ec=self.edge_color,
                                      fill=self.fill)
            #print(geometry_component._position)
            #print(geometry_component.polygon.representative_point())
            canvas.axes.add_patch(self.patch)
        else:
            path = PolygonPatch(geometry_component.polygon,
                                fc=self.color,
                                fill=self.fill).get_path()
            self.patch._path = path
            self.patch.set_facecolor(self.color)

    def remove(self):
        self.patch.remove()
예제 #2
0
    def _diagnostic_plot(self, polys, height_model, detected):
        """
        Plots the variable windows as geometries in the canopy height model.
        """
        import matplotlib.pyplot as plt
        from descartes import PolygonPatch

        fig, ax = plt.subplots()
        ax.matshow(height_model.array)

        # TODO REMOVE
        container = np.zeros((height_model.array.shape[0], height_model.array.shape[1], 4))
        container[:, :, 0][detected > 0] = 1
        container[:, :, 3][detected > 0] = 1
        ax.imshow(container)

        for poly in polys:
            patch = PolygonPatch(poly, facecolor='#cccccc', edgecolor='#999999', alpha = 1)
            patch.set_facecolor('none')
            ax.add_patch(patch)
예제 #3
0
def plot_counties(
        ax,
        counties,
        values=None,
        edgecolors=None,
        contourcolor="white",
        hatch_surround=None,
        xlim=None,
        ylim=None,
        background=True,
        xticks=True,
        yticks=True,
        grid=True,
        frame=True,
        xlabel="Longitude [in dec. degrees]",
        ylabel="Latitude [in dec. degrees]",
        lw=1):
    polygons = [r["shape"] for r in counties.values()]
    # extend german borders :S and then shrink them again after unifying
    # gets rid of many holes at the county boundaries
    contour = cascaded_union([pol.buffer(0.01)
                              for pol in polygons]).buffer(-0.01)

    xmin, ymin, xmax, ymax = contour.bounds
    if xlim is None:
        xlim = [xmin, xmax]
    if ylim is None:
        ylim = [ymin, ymax]

    surround = PolygonPatch(Polygon([(xlim[0], ylim[0]), (xlim[0], ylim[1]), (
        xlim[1], ylim[1]), (xlim[1], ylim[0])]).difference(contour))
    contour = PolygonPatch(contour, lw=lw)

    pc = PatchCollection([PolygonPatch(p, lw=lw)
                          for p in polygons], cmap=matplotlib.cm.magma, alpha=1.0)


    

    

    if values is not None:
        if isinstance(values, (dict, OrderedDict)):
            values = np.array([values.setdefault(r, np.nan)
                               for r in counties.keys()])
        elif isinstance(values, str):
            values = np.array([r.setdefault(values, np.nan)
                               for r in counties.values()])
        else:
            assert np.size(values) == len(counties), "Number of values ({}) doesn't match number of counties ({})!".format(
                np.size(values), len(counties))
        pc.set_clim(0, 10)
        nans = np.isnan(values)
        values[nans] = 0

        values = np.ma.MaskedArray(values, mask=nans)
        pc.set(array=values, cmap='magma')
    else:
        pc.set_facecolors("none")

    if edgecolors is not None:
        if isinstance(edgecolors, (dict, OrderedDict)):
            edgecolors = np.array([edgecolors.setdefault(r, "none")
                                   for r in counties.keys()])
        elif isinstance(edgecolors, str):
            edgecolors = np.array([r.setdefault(edgecolors, "none")
                                   for r in counties.values()])
        pc.set_edgecolors(edgecolors)
    else:
        pc.set_edgecolors("none")

    if hatch_surround is not None:
        surround.set_hatch(hatch_surround)
        surround.set_facecolor("none")
        ax.add_patch(surround)

    cb = plt.colorbar(pc, shrink=0.6)
    cb.set_ticks([0,5,10])
    #cb.set_yticks([0.00004])
    ax.add_collection(pc)
    

    if contourcolor is not None:
        contour.set_edgecolor(contourcolor)
        contour.set_facecolor("none")
        ax.add_patch(contour)

    if isinstance(background, bool):
        ax.patch.set_visible(background)
    else:
        ax.patch.set_color(background)

    ax.grid(grid)

    ax.set_frame_on(frame)

    ax.set_xlim(*xlim)
    ax.set_ylim(*ylim)
    ax.set_aspect(1.43)
    if xlabel:
        ax.set_xlabel(xlabel, fontsize=14)
    if ylabel:
        ax.set_ylabel(ylabel, fontsize=14)

    ax.tick_params(axis="x", which="both", bottom=xticks, labelbottom=xticks)
    ax.tick_params(axis="y", which="both", left=yticks, labelleft=yticks)

    #plt.colorbar()
    

    return pc, contour, surround
    def createMap(self):
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        # plt.imshow(np.ones((self.height, self.width)), cmap = "binary")
        for zone in self.noFlyZones:
            # create a tetrahedron from the no fly zone and display it as a polycollection
            zonePoly = zone.polygonObj
            baseHeight = zone.bottom
            topHeight = zone.top
            vertices = []
            topFace = []
            bottomFace = []
            for coord in zone.coords:
                bottomFace.append(list(coord) + [baseHeight])
                topFace.append(list(coord) + [topHeight])

            sideFaces = []

            for i in range(0, len(zone.coords)):
                face = [
                    list(zone.coords[i]) + [baseHeight],
                    list(zone.coords[i]) + [topHeight],
                    list(zone.coords[i - 1]) + [topHeight],
                    list(zone.coords[i - 1]) + [baseHeight]
                ]
                sideFaces.append(face)
            collection = Poly3DCollection([topFace, bottomFace] + sideFaces,
                                          linewidths=1,
                                          alpha=0.2)
            collection.set_edgecolor("orange")
            collection.set_facecolor("black")
            ax.add_collection3d(collection)

        i = 0
        for waypoint in self.waypoints:
            plotStyle = None
            if i == 0:  # display the first and second waypoints differently
                color = 'r'
            elif i == 1:
                color = 'b'
            else:
                color = 'k'
            ax.scatter(waypoint.x, waypoint.y, waypoint.z, s=30, c=color)
            i = i + 1

        # show area which we CAN fly in with green
        flyZonePatch = PolygonPatch(self.flyZone)
        flyZonePatch.set_facecolor("green")
        flyZonePatch.set_alpha(0.2)
        #ax.add_patch(flyZonePatch)

        # now lets show the path
        ax.scatter(self.finalPath[0][0],
                   self.finalPath[0][1],
                   self.finalPath[0][2],
                   s=50,
                   c='g')
        ax.scatter(self.finalPath[-1][0],
                   self.finalPath[-1][1],
                   self.finalPath[-1][2],
                   s=50,
                   c='g')

        xvals = [row[0] for row in self.finalPath]
        yvals = [row[1] for row in self.finalPath]
        zvals = [row[2] for row in self.finalPath]
        plt.plot(xvals, yvals, zvals, '.-')
        plt.show()