Ejemplo n.º 1
0
def slider(aoi, pid, chipsize=512, extend=512, tms=['Google']):

    workdir = config.get_value(['paths', 'temp'])
    path = f'{workdir}/{aoi}/{pid}/'
    bg_path = f'{path}/backgrounds/'

    for t in tms:
        if not os.path.isfile(f'{bg_path}{t.lower()}.tif'):
            bg.by_pid(aoi, pid, chipsize, extend, t, True)

    with open(f'{path}info.json', "r") as f:
        json_data = json.load(f)

    def overlay_parcel(img, geom):
        patche = [
            PolygonPatch(feature,
                         edgecolor="yellow",
                         facecolor="none",
                         linewidth=2) for feature in geom['geom']
        ]
        return patche

    with rasterio.open(f'{bg_path}{tms[0].lower()}.tif') as img:
        img_epsg = img.crs.to_epsg()
        geom = spatial_utils.transform_geometry(json_data, img_epsg)
        patches = overlay_parcel(img, geom)

    selection = SelectionSlider(options=tms,
                                value=tms[0],
                                disabled=False,
                                continuous_update=False,
                                orientation='horizontal',
                                readout=True)
    output = Output()

    fig, ax = plt.subplots(figsize=(10, 10))
    with output:
        with rasterio.open(f'{bg_path}{selection.value.lower()}.tif') as img:
            for patch in patches:
                ax.add_patch(copy(patch))
            show(img, ax=ax)
        plt.show()

    def on_value_change(change):
        with output:
            output.clear_output()
            fig, ax = plt.subplots(figsize=(10, 10))
            with rasterio.open(
                    f'{bg_path}{selection.value.lower()}.tif') as im:
                for patch in patches:
                    ax.add_patch(copy(patch))
                show(im, ax=ax)
            plt.show()

    selection.observe(on_value_change, names='value')
    return VBox([selection, output])
 def test_index_trigger(self):
     slider = SelectionSlider(options=['a', 'b', 'c'])
     observations = []
     def f(change):
         observations.append(change.new)
     slider.observe(f, 'index')
     assert slider.index == 0
     slider.options = [4, 5, 6]
     assert slider.index == 0
     assert slider.value == 4
     assert slider.label == '4'
     assert observations == [0]
 def test_index_trigger(self):
     slider = SelectionSlider(options=['a', 'b', 'c'])
     observations = []
     def f(change):
         observations.append(change.new)
     slider.observe(f, 'index')
     assert slider.index == 0
     slider.options = [4, 5, 6]
     assert slider.index == 0
     assert slider.value == 4
     assert slider.label == '4'
     assert observations == [0]