Esempio n. 1
0
    def test_split_multigeom_gdf(self):

        multi_gdf = _check_gdf_load(
            os.path.join(data_dir, 'multigeom.geojson'))
        expected_result = _check_gdf_load(
            os.path.join(data_dir, 'multigeom_split_result.geojson'))
        single_gdf = split_multi_geometries(multi_gdf)

        assert single_gdf.equals(expected_result)
def generate_mask(image_path, json_path, output_path_mask, boundary_width,
                  contact_spacing):
    """[summary]

    Args:
        image_path ([type]): [description]
        json_path ([type]): [description]
        output_path_mask ([type]): [description]

    Returns:
        [type]: [description]
    """
    # filter out null geoms (this is always a worthy check)
    gdf_tmp = _check_gdf_load(json_path)
    if len(gdf_tmp) == 0:
        gdf_nonull = gdf_tmp
    else:
        gdf_nonull = gdf_tmp[gdf_tmp.geometry.notnull()]
        try:
            im_tmp = skimage.io.imread(image_path)
        except:
            print(f'[WARN] failed to load image. Skipping {image_path}')
            return None

    # handle empty geojsons
    if len(gdf_nonull) == 0:
        # create masks
        print(f'[WARN] no building was found in: {json_path}')
        im = gdal.Open(image_path)
        proj = im.GetProjection()
        geo = im.GetGeoTransform()
        im = im.ReadAsArray()
        # set masks to 0 everywhere
        mask_arr = np.zeros((3, im.shape[1], im.shape[2]))
        create_multiband_geotiff(mask_arr, output_path_mask, proj, geo)
        return mask_arr

    # three channel mask (takes awhile)
    # https://github.com/CosmiQ/solaris/blob/master/docs/tutorials/notebooks/api_masks_tutorial.ipynb
    mask = sol.vector.mask.df_to_px_mask(
        df=gdf_nonull,
        out_file=output_path_mask,
        channels=['footprint', 'boundary', 'contact'],
        reference_im=image_path,
        boundary_type='outer',
        boundary_width=boundary_width,
        contact_spacing=contact_spacing,
        meters=True,
        shape=(im_tmp.shape[0], im_tmp.shape[1]))
    return mask
def make_geojsons_and_masks(name_root,
                            image_path,
                            json_path,
                            output_path_mask,
                            output_path_mask_fbc=None):
    '''
    Make the stuffins
    mask_fbc is an (optional) three-channel fbc (footbrint, boundary, contact) mask
    '''

    print("name root:", name_root)
    start_time = time.time()

    # filter out null geoms (this is always a worthy check)
    gdf_tmp = _check_gdf_load(json_path)
    if len(gdf_tmp) == 0:
        gdf_nonull = gdf_tmp
    else:
        gdf_nonull = gdf_tmp[gdf_tmp.geometry.notnull()]
        try:
            im_tmp = skimage.io.imread(image_path)
        except:
            print("Error loading image %s, skipping..." % (image_path))
            return

    # handle empty geojsons
    if len(gdf_nonull) == 0:
        # create masks
        # mask 1 has 1 channel
        # mask_fbc has 3 channel
        print("    Empty labels for name_root!", name_root)
        im = gdal.Open(image_path)
        proj = im.GetProjection()
        geo = im.GetGeoTransform()
        im = im.ReadAsArray()
        # set masks to 0 everywhere
        if output_path_mask_fbc:
            #print('fbc')
            mask_arr = np.zeros((3, im.shape[1], im.shape[2]))
            create_multiband_geotiff(mask_arr, output_path_mask_fbc, proj, geo)
        else:
            #print('no fbc')
            mask_arr = np.zeros((1, im.shape[1], im.shape[2]))
            create_multiband_geotiff(mask_arr, output_path_mask, proj, geo)
        return

    # three channel mask (takes awhile)
    # https://github.com/CosmiQ/solaris/blob/master/docs/tutorials/notebooks/api_masks_tutorial.ipynb
    if output_path_mask_fbc:
        #print('3 channel')
        fbc_mask = sol.vector.mask.df_to_px_mask(
            df=gdf_nonull,
            out_file=output_path_mask_fbc,
            channels=['footprint', 'boundary', 'contact'],
            reference_im=image_path,
            boundary_width=5,
            contact_spacing=10,
            meters=True,
            shape=(im_tmp.shape[0], im_tmp.shape[1]))
    else:
        # make masks (single channel)
        #print('single channel')
        # https://github.com/CosmiQ/solaris/blob/master/docs/tutorials/notebooks/api_masks_tutorial.ipynb
        f_mask = sol.vector.mask.df_to_px_mask(df=gdf_nonull,
                                               out_file=output_path_mask,
                                               channels=['footprint'],
                                               reference_im=image_path,
                                               shape=(im_tmp.shape[0],
                                                      im_tmp.shape[1]))

    end_time = time.time()
    print('time taken: ', end_time - start_time)

    return
Esempio n. 4
0
    def test_loaded_geojson(self):
        geojson_path = os.path.join(data_dir, 'sample.geojson')
        truth_gdf = gpd.read_file(geojson_path)
        test_gdf = _check_gdf_load(truth_gdf.copy())

        assert truth_gdf.equals(test_gdf)