Beispiel #1
0
def corresponding_roi(rpc1, rpc2, x, y, w, h):
    """
    Uses RPC functions to determine the region of im2 associated to the
    specified ROI of im1.

    Args:
        rpc1, rpc2: two instances of the rpcm.RPCModel class, or paths to
            the xml files
        x, y, w, h: four integers defining a rectangular region of interest
            (ROI) in the first view. (x, y) is the top-left corner, and (w, h)
            are the dimensions of the rectangle.

    Returns:
        four integers defining a ROI in the second view. This ROI is supposed
        to contain the projections of the 3D points that are visible in the
        input ROI.
    """
    # read rpc files
    if not isinstance(rpc1, rpcm.RPCModel):
        rpc1 = rpcm.RPCModel(rpc1)
    if not isinstance(rpc2, rpcm.RPCModel):
        rpc2 = rpcm.RPCModel(rpc2)
    m, M = altitude_range(rpc1, x, y, w, h, 0, 0)

    # build an array with vertices of the 3D ROI, obtained as {2D ROI} x [m, M]
    a = np.array([x, x, x, x, x + w, x + w, x + w, x + w])
    b = np.array([y, y, y + h, y + h, y, y, y + h, y + h])
    c = np.array([m, M, m, M, m, M, m, M])

    # corresponding points in im2
    xx, yy = find_corresponding_point(rpc1, rpc2, a, b, c)[0:2]

    # return coordinates of the bounding box in im2
    out = common.bounding_box2D(np.vstack([xx, yy]).T)
    return np.round(out)
Beispiel #2
0
def check_parameters(d):
    """
    Check that the provided dictionary defines all mandatory s2p arguments.

    Args:
        d: python dictionary
    """
    # verify that input files paths are defined
    if 'images' not in d or len(d['images']) < 2:
        print('ERROR: missing paths to input images')
        sys.exit(1)
    for img in d['images']:
        if not dict_has_keys(img, ['img']):
            print('ERROR: missing img paths for image', img)
            sys.exit(1)

    # read RPCs
    for img in d['images']:
        if 'rpc' in img:
            if isinstance(img['rpc'], str):  # path to an RPC file
                img['rpcm'] = rpcm.rpc_from_rpc_file(img['rpc'])
            elif isinstance(img['rpc'], dict):  # RPC dict in 'rpcm' format
                img['rpcm'] = rpcm.RPCModel(img['rpc'], dict_format='rpcm')
            else:
                raise NotImplementedError(
                    'rpc of type {} not supported'.format(type(img['rpc'])))
        else:
            img['rpcm'] = rpcm.rpc_from_geotiff(img['img'])

    # verify that an input ROI is defined
    if 'full_img' in d and d['full_img']:
        sz = common.image_size_gdal(d['images'][0]['img'])
        d['roi'] = {'x': 0, 'y': 0, 'w': sz[0], 'h': sz[1]}
    elif 'roi' in d and dict_has_keys(d['roi'], ['x', 'y', 'w', 'h']):
        pass
    elif 'roi_geojson' in d:
        ll_poly = geographiclib.read_lon_lat_poly_from_geojson(
            d['roi_geojson'])
        d['roi'] = rpc_utils.roi_process(
            d['images'][0]['rpcm'],
            ll_poly,
            use_srtm=d.get('use_srtm'),
            exogenous_dem=d.get('exogenous_dem'),
            exogenous_dem_geoid_mode=d.get('exogenous_dem_geoid_mode'))
    else:
        print('ERROR: missing or incomplete roi definition')
        sys.exit(1)

    # d['roi'] : all the values must be integers
    d['roi']['x'] = int(np.floor(d['roi']['x']))
    d['roi']['y'] = int(np.floor(d['roi']['y']))
    d['roi']['w'] = int(np.ceil(d['roi']['w']))
    d['roi']['h'] = int(np.ceil(d['roi']['h']))

    # warn about unknown parameters. The known parameters are those defined in
    # the global config.cfg dictionary, plus the mandatory 'images' and 'roi'
    for k in d.keys():
        if k not in ['images', 'roi', 'roi_geojson']:
            if k not in cfg:
                print('WARNING: ignoring unknown parameter {}.'.format(k))
Beispiel #3
0
def fixture_data(tmp_path):
    """
    Copy the test data to a temporary directory
    """
    img1 = data_path(os.path.join("input_pair", "img_01.tif"))
    with rasterio.open(img1) as f:
        rpc1 = rpcm.RPCModel(f.tags(ns="RPC"))
    shutil.copy(img1, tmp_path / "img_01.tif")

    img2 = data_path(os.path.join("input_pair", "img_02.tif"))
    with rasterio.open(img2) as f:
        rpc2 = rpcm.RPCModel(f.tags(ns="RPC"))
    shutil.copy(img2, tmp_path / "img_02.tif")

    config = data_path(os.path.join("input_pair", "config.json"))
    tmp_config = tmp_path / "config.json"
    shutil.copy(config, tmp_config)

    return tmp_config, tmp_path, rpc1, rpc2
Beispiel #4
0
def check_parameters(d):
    """
    Check that the provided dictionary defines all mandatory s2p arguments.

    Args:
        d: python dictionary
    """
    # verify that input files paths are defined
    if 'images' not in d or len(d['images']) < 2:
        print('ERROR: missing paths to input images')
        sys.exit(1)
    for img in d['images']:
        if not dict_has_keys(img, ['img']):
            print('ERROR: missing img paths for image', img)
            sys.exit(1)

    # read RPCs
    for img in d['images']:
        if 'rpc' in img:
            if isinstance(img['rpc'], str):  # path to an RPC file
                img['rpcm'] = rpcm.rpc_from_rpc_file(img['rpc'])
            elif isinstance(img['rpc'], dict):  # RPC dict in 'rpcm' format
                img['rpcm'] = rpcm.RPCModel(img['rpc'], dict_format='rpcm')
            else:
                raise NotImplementedError(
                    'rpc of type {} not supported'.format(type(img['rpc'])))
        else:
            img['rpcm'] = rpcm.rpc_from_geotiff(img['img'])

    # verify that roi or path to preview file are defined
    if 'full_img' in d and d['full_img']:
        sz = common.image_size_gdal(d['images'][0]['img'])
        d['roi'] = {'x': 0, 'y': 0, 'w': sz[0], 'h': sz[1]}
    elif 'roi' in d and dict_has_keys(d['roi'], ['x', 'y', 'w', 'h']):
        pass
    elif 'roi_utm' in d and dict_has_keys(
            d['roi_utm'], ['utm_band', 'hemisphere', 'x', 'y', 'w', 'h']):
        d['roi'] = rpc_utils.utm_roi_to_img_roi(d['images'][0]['rpcm'],
                                                d['roi_utm'],
                                                d.get('use_srtm'))
    elif 'roi_kml' in d:
        # this call defines cfg['utm_zone'] and cfg['utm_bbx'] as side effects
        d['roi'] = rpc_utils.kml_roi_process(d['images'][0]['rpcm'],
                                             d['roi_kml'], d.get('utm_zone'),
                                             d.get('use_srtm'))
    elif 'roi_geojson' in d:
        # this call defines cfg['utm_zone'] and cfg['utm_bbx'] as side effects
        d['roi'] = rpc_utils.geojson_roi_process(d['images'][0]['rpcm'],
                                                 d['roi_geojson'],
                                                 d.get('utm_zone'),
                                                 d.get('use_srtm'))
    else:
        print('ERROR: missing or incomplete roi definition')
        sys.exit(1)

    # d['roi'] : all the values must be integers
    d['roi']['x'] = int(np.floor(d['roi']['x']))
    d['roi']['y'] = int(np.floor(d['roi']['y']))
    d['roi']['w'] = int(np.ceil(d['roi']['w']))
    d['roi']['h'] = int(np.ceil(d['roi']['h']))

    # warn about unknown parameters. The known parameters are those defined in
    # the global config.cfg dictionary, plus the mandatory 'images' and 'roi' or
    # 'roi_utm'
    for k in d.keys():
        if k not in [
                'images', 'roi', 'roi_kml', 'roi_geojson', 'roi_utm',
                'utm_zone'
        ]:
            if k not in cfg:
                print('WARNING: ignoring unknown parameter {}.'.format(k))