Esempio n. 1
0
def test_get_data_dir__missing():
    with proj_env(), pytest.raises(DataDirError), patch(
        "pyproj.datadir.os.path.abspath", return_value="INVALID"
    ), patch("pyproj.datadir.find_executable", return_value=None):
        set_data_dir(None)
        os.environ.pop("PROJ_LIB", None)
        get_data_dir()
Esempio n. 2
0
def test_get_data_dir__from_env_var__multiple():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os.path.abspath", return_value="INVALID"):
        unset_data_dir()
        os.environ["PROJ_LIB"] = os.pathsep.join([tmpdir, tmpdir, tmpdir])
        create_projdb(tmpdir)
        assert get_data_dir() == os.pathsep.join([tmpdir, tmpdir, tmpdir])
Esempio n. 3
0
def test_get_data_dir__from_env_var__multiple(tmp_path):
    tmpdir = os.pathsep.join([str(tmp_path) for _ in range(3)])
    with proj_env(), patch.dict(os.environ, {"PROJ_LIB": tmpdir}, clear=True), patch(
        "pyproj.datadir.Path.absolute", return_value=_INVALID_PATH
    ), patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)):
        create_projdb(tmp_path)
        assert get_data_dir() == tmpdir
Esempio n. 4
0
def test_get_data_dir__missing():
    with proj_env(), pytest.raises(DataDirError), patch(
        "pyproj.datadir.os.path.abspath", return_value="INVALID"
    ), patch("pyproj.datadir.find_executable", return_value=None):
        set_data_dir(None)
        os.environ.pop("PROJ_LIB", None)
        assert get_data_dir() is None
Esempio n. 5
0
def test_get_data_dir__from_env_var():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os.path.abspath", return_value="INVALID"):
        set_data_dir(None)
        os.environ["PROJ_LIB"] = tmpdir
        create_projdb(tmpdir)
        assert get_data_dir() == tmpdir
Esempio n. 6
0
def test_get_data_dir__from_prefix(tmp_path):
    with proj_env(), patch.dict(os.environ, {}, clear=True), patch(
        "pyproj.datadir.Path.absolute", return_value=_INVALID_PATH
    ), patch("pyproj.datadir.sys.prefix", str(tmp_path)):
        proj_dir = tmp_path / "share" / "proj"
        proj_dir.mkdir(parents=True)
        create_projdb(proj_dir)
        assert get_data_dir() == str(proj_dir)
Esempio n. 7
0
def test_get_data_dir__from_env_var(tmp_path):
    with proj_env(), patch.dict(
        os.environ, {"PROJ_LIB": str(tmp_path)}, clear=True
    ), patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH), patch(
        "pyproj.datadir.sys.prefix", str(_INVALID_PATH)
    ):
        create_projdb(tmp_path)
        assert get_data_dir() == str(tmp_path)
Esempio n. 8
0
def test_get_data_dir__from_env_var(tmp_path):
    tmpdir = str(tmp_path)
    with proj_env(), patch("pyproj.datadir.os") as os_mock, patch(
            "pyproj.datadir.sys") as sys_mock:
        setup_os_mock(os_mock, proj_dir=tmpdir)
        sys_mock.prefix = _INVALID_PATH
        create_projdb(tmpdir)
        assert get_data_dir() == tmpdir
Esempio n. 9
0
def test_get_data_dir__from_env_var():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os") as os_mock, patch(
                "pyproj.datadir.sys") as sys_mock:
        setup_os_mock(os_mock, proj_dir=tmpdir)
        sys_mock.prefix = _INVALID_PATH
        create_projdb(tmpdir)
        assert get_data_dir() == tmpdir
Esempio n. 10
0
def test_get_data_dir__missing():
    with proj_env(), pytest.raises(DataDirError), patch(
            "pyproj.datadir.find_executable",
            return_value=None), patch("pyproj.datadir.os") as os_mock, patch(
                "pyproj.datadir.sys") as sys_mock:
        sys_mock.prefix = _INVALID_PATH
        setup_os_mock(os_mock)
        assert get_data_dir() is None
Esempio n. 11
0
def test_get_data_dir__from_env_var__multiple():
    with proj_env(), temporary_directory() as tmpdir, patch(
        "pyproj.datadir.os.path.abspath", return_value="INVALID"
    ):
        set_data_dir(None)
        os.environ["PROJ_LIB"] = os.pathsep.join([tmpdir, tmpdir, tmpdir])
        create_projdb(tmpdir)
        assert get_data_dir() == os.pathsep.join([tmpdir, tmpdir, tmpdir])
Esempio n. 12
0
def test_get_data_dir__from_env_var__multiple(tmp_path):
    tmpdir = str(tmp_path)
    with proj_env(), patch("pyproj.datadir.os") as os_mock, patch(
            "pyproj.datadir.sys") as sys_mock:
        setup_os_mock(os_mock,
                      proj_dir=os.pathsep.join([tmpdir, tmpdir, tmpdir]))
        sys_mock.prefix = _INVALID_PATH
        create_projdb(tmpdir)
        assert get_data_dir() == os.pathsep.join([tmpdir, tmpdir, tmpdir])
Esempio n. 13
0
def test_get_data_dir__from_env_var__multiple():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os") as os_mock, patch(
                "pyproj.datadir.sys") as sys_mock:
        setup_os_mock(os_mock,
                      proj_dir=os.pathsep.join([tmpdir, tmpdir, tmpdir]))
        sys_mock.prefix = _INVALID_PATH
        unset_data_dir()
        create_projdb(tmpdir)
        assert get_data_dir() == os.pathsep.join([tmpdir, tmpdir, tmpdir])
Esempio n. 14
0
def epsg_to_proj4(epsg_code):
    data = open(os.path.join(datadir.get_data_dir(), 'epsg')).read()
    match = re.search('(?<=<{0}>).*(?=<>)'.format(epsg_code), data)
    if not match:
        raise ValueError(
            'ERROR: EPSG {0} not found in proj4 data file'.format(epsg_code))

    return match.group().strip().replace(
        'longlat',
        'latlong')  # pyproj stores the longlat instead of latlong as used here
Esempio n. 15
0
def test_get_data_dir__from_prefix(tmp_path):
    tmpdir = str(tmp_path)
    with proj_env(), patch("pyproj.datadir.os") as os_mock, patch(
            "pyproj.datadir.sys") as sys_mock:
        setup_os_mock(os_mock)
        sys_mock.prefix = tmpdir
        proj_dir = os.path.join(tmpdir, "share", "proj")
        os.makedirs(proj_dir)
        create_projdb(proj_dir)
        assert get_data_dir() == proj_dir
Esempio n. 16
0
def test_get_data_dir__from_path():
    with proj_env(), temporary_directory() as tmpdir, patch(
        "pyproj.datadir.os.path.abspath", return_value="INVALID"
    ), patch("pyproj.datadir.find_executable") as find_exe:
        set_data_dir(None)
        os.environ.pop("PROJ_LIB", None)
        find_exe.return_value = os.path.join(tmpdir, "bin", "proj")
        proj_dir = os.path.join(tmpdir, "share", "proj")
        os.makedirs(proj_dir)
        create_projdb(proj_dir)
        assert get_data_dir() == proj_dir
Esempio n. 17
0
def test_get_data_dir__from_prefix():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os.path.abspath",
            return_value="INVALID"), patch("pyproj.datadir.sys") as sys_mock:
        unset_data_dir()
        os.environ.pop("PROJ_LIB", None)
        sys_mock.prefix = tmpdir
        proj_dir = os.path.join(tmpdir, "share", "proj")
        os.makedirs(proj_dir)
        create_projdb(proj_dir)
        assert get_data_dir() == proj_dir
Esempio n. 18
0
def test_get_data_dir__internal():
    with proj_env(), temporary_directory() as tmpdir:
        set_data_dir(None)
        os.environ["PROJ_LIB"] = tmpdir
        create_projdb(tmpdir)
        internal_proj_dir = os.path.join(tmpdir, "proj_dir", "share", "proj")
        os.makedirs(internal_proj_dir)
        create_projdb(internal_proj_dir)
        with patch("pyproj.datadir.os.path.abspath") as abspath_mock:
            abspath_mock.return_value = os.path.join(tmpdir, "randomfilename.py")
            assert get_data_dir() == internal_proj_dir
Esempio n. 19
0
def test_get_data_dir__missing():
    with proj_env(), pytest.raises(DataDirError), patch.dict(
        os.environ, {}, clear=True
    ), patch("pyproj.datadir.Path.absolute", return_value=_INVALID_PATH), patch(
        "pyproj.datadir.find_executable", return_value=None
    ), patch(
        "pyproj.datadir.Path.absolute", return_value=_INVALID_PATH
    ), patch(
        "pyproj.datadir.sys.prefix", str(_INVALID_PATH)
    ):
        assert get_data_dir() is None
Esempio n. 20
0
def test_get_data_dir__internal():
    with proj_env(), temporary_directory() as tmpdir:
        set_data_dir(None)
        os.environ["PROJ_LIB"] = tmpdir
        create_projdb(tmpdir)
        internal_proj_dir = os.path.join(tmpdir, "proj_dir", "share", "proj")
        os.makedirs(internal_proj_dir)
        create_projdb(internal_proj_dir)
        with patch("pyproj.datadir.os.path.abspath") as abspath_mock:
            abspath_mock.return_value = os.path.join(tmpdir, "randomfilename.py")
            assert get_data_dir() == internal_proj_dir
Esempio n. 21
0
def test_get_data_dir__from_prefix():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os") as os_mock, patch(
                "pyproj.datadir.sys") as sys_mock:
        setup_os_mock(os_mock)
        unset_data_dir()
        sys_mock.prefix = tmpdir
        proj_dir = os.path.join(tmpdir, "share", "proj")
        os.makedirs(proj_dir)
        create_projdb(proj_dir)
        assert get_data_dir() == proj_dir
Esempio n. 22
0
def test_get_data_dir__from_path():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os.path.abspath", return_value="INVALID"), patch(
                "pyproj.datadir.find_executable") as find_exe:
        set_data_dir(None)
        os.environ.pop("PROJ_LIB", None)
        find_exe.return_value = os.path.join(tmpdir, "bin", "proj")
        proj_dir = os.path.join(tmpdir, "share", "proj")
        os.makedirs(proj_dir)
        create_projdb(proj_dir)
        assert get_data_dir() == proj_dir
Esempio n. 23
0
def _is_download_needed(grid_name: str) -> bool:
    """
    Run through all of the PROJ directories to see if the
    file already exists.
    """
    if Path(get_user_data_dir(), grid_name).exists():
        return False
    for data_dir in get_data_dir().split(os.pathsep):
        if Path(data_dir, grid_name).exists():
            return False
    return True
Esempio n. 24
0
def test_get_data_dir__from_path(tmp_path):
    tmpdir = str(tmp_path)
    with proj_env(), patch("pyproj.datadir.os") as os_mock, patch(
            "pyproj.datadir.sys") as sys_mock, patch(
                "pyproj.datadir.find_executable") as find_exe:
        setup_os_mock(os_mock)
        sys_mock.prefix = _INVALID_PATH
        find_exe.return_value = os.path.join(tmpdir, "bin", "proj")
        proj_dir = os.path.join(tmpdir, "share", "proj")
        os.makedirs(proj_dir)
        create_projdb(proj_dir)
        assert get_data_dir() == proj_dir
Esempio n. 25
0
def test_append_data_dir__internal(tmp_path):
    tmpdir = str(tmp_path)
    with proj_env(), patch("pyproj.datadir.os") as os_mock:
        setup_os_mock(os_mock, os.path.join(tmpdir, "randomfilename.py"))
        create_projdb(tmpdir)
        internal_proj_dir = os.path.join(tmpdir, "proj_dir", "share", "proj")
        os.makedirs(internal_proj_dir)
        create_projdb(internal_proj_dir)
        extra_datadir = str(os.path.join(tmpdir, "extra_datumgrids"))
        append_data_dir(extra_datadir)
        assert get_data_dir() == os.pathsep.join(
            [internal_proj_dir, extra_datadir])
Esempio n. 26
0
def proj_env():
    """
    Ensure environment variable the same at the end of the test.
    """
    unset_data_dir()
    try:
        yield
    finally:
        # make sure the data dir is cleared
        unset_data_dir()
        # reset back to the original path
        set_data_dir(get_data_dir())
Esempio n. 27
0
def grids_available(*grid_names, check_network=True, check_all=False):
    """
    Check if the grids are available
    """
    if check_network and os.environ.get("PROJ_NETWORK") == "ON":
        return True
    available = [(Path(get_data_dir(), grid_name).exists()
                  or Path(get_user_data_dir(), grid_name).exists())
                 for grid_name in grid_names]
    if check_all:
        return all(available)
    return any(available)
Esempio n. 28
0
def test_append_data_dir__internal(projdir_type, tmp_path):
    with proj_env(), patch.dict(os.environ, {}, clear=True), patch(
        "pyproj.datadir.Path.absolute", return_value=tmp_path / "datadir.py"
    ), patch("pyproj.datadir.sys.prefix", str(_INVALID_PATH)):
        create_projdb(tmp_path)
        internal_proj_dir = tmp_path / "proj_dir" / "share" / "proj"
        internal_proj_dir.mkdir(parents=True)
        create_projdb(internal_proj_dir)
        extra_datadir = tmp_path / "extra_datumgrids"
        append_data_dir(projdir_type(extra_datadir))
        assert get_data_dir() == os.pathsep.join(
            [str(internal_proj_dir), str(extra_datadir)]
        )
Esempio n. 29
0
def test_sync_download__system_directory(
    load_grid_geojson_mock, download_mock, parse_args_mock
):
    load_grid_geojson_mock.return_value = _load_grid_geojson()
    main()
    datadir = get_data_dir().split(os.path.sep)[0]
    download_mock.assert_called_with(
        directory=datadir,
        file_url="https://cdn.proj.org/fr_ign_ntf_r93.tif",
        sha256="0aa738b3e00fd2d64f8e3cd0e76034d4792374624fa0e133922433c9491bbf2a",
        short_name="fr_ign_ntf_r93.tif",
        verbose=True,
    )
    load_grid_geojson_mock.assert_called_with(target_directory=datadir)
Esempio n. 30
0
def test_append_data_dir__internal():
    with proj_env(), temporary_directory() as tmpdir:
        unset_data_dir()
        os.environ["PROJ_LIB"] = tmpdir
        create_projdb(tmpdir)
        internal_proj_dir = os.path.join(tmpdir, "proj_dir", "share", "proj")
        os.makedirs(internal_proj_dir)
        create_projdb(internal_proj_dir)
        extra_datadir = str(os.path.join(tmpdir, "extra_datumgrids"))
        with patch("pyproj.datadir.os.path.abspath") as abspath_mock:
            abspath_mock.return_value = os.path.join(tmpdir,
                                                     "randomfilename.py")
            append_data_dir(extra_datadir)
            assert get_data_dir() == os.pathsep.join(
                [internal_proj_dir, extra_datadir])
Esempio n. 31
0
def test_get_data_dir__internal(tmp_path):
    tmpdir = tmp_path / "proj"
    tmpdir.mkdir()
    tmpdir_fake = tmp_path / "proj_fake"
    tmpdir_fake.mkdir()
    with proj_env(), patch.dict(
        os.environ, {"PROJ_LIB": str(tmpdir_fake)}, clear=True
    ), patch("pyproj.datadir.Path.absolute", return_value=tmpdir / "datadir.py"), patch(
        "pyproj.datadir.sys.prefix", str(tmpdir_fake)
    ):
        create_projdb(tmpdir)
        create_projdb(tmpdir_fake)
        internal_proj_dir = tmpdir / "proj_dir" / "share" / "proj"
        internal_proj_dir.mkdir(parents=True)
        create_projdb(internal_proj_dir)
        assert get_data_dir() == str(internal_proj_dir)
Esempio n. 32
0
def grids_available(*grid_names, check_network=True, check_all=False):
    """
    Check if the grids are available
    """
    if check_network and pyproj.network.is_network_enabled():
        return True
    available = [
        (
            Path(get_data_dir(), grid_name).exists()
            or Path(get_user_data_dir(), grid_name).exists()
        )
        for grid_name in grid_names
    ]
    if check_all:
        return all(available)
    return any(available)
Esempio n. 33
0
def test_get_data_dir__internal():
    with proj_env(), temporary_directory() as tmpdir, patch(
            "pyproj.datadir.os") as os_mock, temporary_directory(
            ) as tmpdir_fake, patch("pyproj.datadir.sys") as sys_mock:
        setup_os_mock(
            os_mock,
            abspath_return=os.path.join(tmpdir, "randomfilename.py"),
            proj_dir=tmpdir_fake,
        )
        sys_mock.prefix = tmpdir_fake
        create_projdb(tmpdir)
        create_projdb(tmpdir_fake)
        internal_proj_dir = os.path.join(tmpdir, "proj_dir", "share", "proj")
        os.makedirs(internal_proj_dir)
        create_projdb(internal_proj_dir)
        assert get_data_dir() == internal_proj_dir
Esempio n. 34
0
def test_get_data_dir__from_user(projdir_type, tmp_path):
    tmpdir = tmp_path / "proj"
    tmpdir.mkdir()
    tmpdir_env = tmp_path / "proj_env"
    tmpdir_env.mkdir()
    with proj_env(), patch.dict(
        os.environ, {"PROJ_LIB": str(tmpdir_env)}, clear=True
    ), patch("pyproj.datadir.Path.absolute", return_value=tmpdir / "datadir.py"), patch(
        "pyproj.datadir.sys.prefix", str(tmpdir_env)
    ):  # noqa: E501
        create_projdb(tmpdir)
        create_projdb(tmpdir_env)
        set_data_dir(projdir_type(tmpdir))
        internal_proj_dir = tmpdir / "proj_dir" / "share" / "proj"
        internal_proj_dir.mkdir(parents=True)
        create_projdb(internal_proj_dir)
        assert get_data_dir() == str(tmpdir)