diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2020-01-22 14:09:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-22 14:09:14 +0100 |
| commit | c5fb54168665d41503ef3a08f0534da58949b632 (patch) | |
| tree | 19e2adc8809290881a6a13ec6001013d03170c7d /test | |
| parent | a6390b59ae2bad2a763e7ab3341ee4c80e708b3d (diff) | |
| parent | 66fd99a8831955034cb25c8468ecfe1f9d3a7d62 (diff) | |
| download | PROJ-c5fb54168665d41503ef3a08f0534da58949b632.tar.gz PROJ-c5fb54168665d41503ef3a08f0534da58949b632.zip | |
Merge pull request #1839 from rouault/rfc4_utf8
[RFC4_dev] Use Win32 Unicode APIs and expect all strings to be UTF-8 (fixes #1765)
Diffstat (limited to 'test')
| -rw-r--r-- | test/cli/ntv2_out.dist | 8 | ||||
| -rw-r--r-- | test/cli/td_out.dist | 2 | ||||
| -rwxr-xr-x | test/cli/testntv2 | 11 | ||||
| -rw-r--r-- | test/gie/geotiff_grids.gie | 14 | ||||
| -rw-r--r-- | test/unit/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | test/unit/Makefile.am | 2 | ||||
| -rw-r--r-- | test/unit/test_c_api.cpp | 22 | ||||
| -rw-r--r-- | test/unit/test_grids.cpp | 227 | ||||
| -rw-r--r-- | test/unit/test_network.cpp | 192 |
9 files changed, 470 insertions, 20 deletions
diff --git a/test/cli/ntv2_out.dist b/test/cli/ntv2_out.dist index 650a69d8..ce866f37 100644 --- a/test/cli/ntv2_out.dist +++ b/test/cli/ntv2_out.dist @@ -12,3 +12,11 @@ Try with NTv2 and NTv1 together ... falls back to NTv1 ############################################################## Switching between NTv2 subgrids -112.5839956 49.4914451 0 -112.58307487 49.49145197 0.00000000 +############################################################## +Interpolating very close (and sometimes a bit outside) to the edges a NTv2 subgrid (#209) +-115.58333333 51.25000000 0 -115.58228512 51.24997866 0.00000000 +-115.58333333 51.25000010 0 -115.58228512 51.24997876 0.00000000 +-115.58333334 51.25000000 0 -115.58228513 51.24997866 0.00000000 +-115.49166667 51.07500000 0 -115.49062909 51.07497666 0.00000000 +-115.49166668 51.07500000 0 -115.49062910 51.07497666 0.00000000 +-115.49166667 51.07499990 0 -115.49062909 51.07497656 0.00000000 diff --git a/test/cli/td_out.dist b/test/cli/td_out.dist index 82b64321..76c6e6ec 100644 --- a/test/cli/td_out.dist +++ b/test/cli/td_out.dist @@ -21,7 +21,7 @@ edge or even a wee bit outside (#141). -5.5001 52.0 -5.500100000000 52.000000000000 0.000000000000 -5.5 52.0 -5.498893534472 52.000109529716 0.000000000000 -5.5000000000001 52.0000000000001 -5.498893534472 52.000109529717 0.000000000000 --5.4999 51.9999 -5.498793541695 52.000009529743 0.000000000000 +-5.4999 51.9999 -5.498793593803 52.000009531513 0.000000000000 -5.5001 52.0 -5.500100000000 52.000000000000 0.000000000000 ############################################################## NAD27 -> NAD83: 1st through ntv1, 2nd through conus diff --git a/test/cli/testntv2 b/test/cli/testntv2 index 2a31304e..d15ad7aa 100755 --- a/test/cli/testntv2 +++ b/test/cli/testntv2 @@ -60,6 +60,17 @@ $EXE +proj=latlong +datum=NAD83 +to +proj=latlong +ellps=clrk66 +nadgrids=ntv2_0 -112.5839956 49.4914451 0 EOF +echo "##############################################################" >> ${OUT} +echo "Interpolating very close (and sometimes a bit outside) to the edges a NTv2 subgrid (#209)" >> ${OUT} +$EXE +proj=latlong +datum=NAD83 +to +proj=latlong +ellps=clrk66 +nadgrids=ntv2_0.gsb -E -d 8 >>${OUT} <<EOF +-115.58333333 51.25000000 0 +-115.58333333 51.25000010 0 +-115.58333334 51.25000000 0 +-115.49166667 51.07500000 0 +-115.49166668 51.07500000 0 +-115.49166667 51.07499990 0 +EOF + # ############################################################################## # Done! diff --git a/test/gie/geotiff_grids.gie b/test/gie/geotiff_grids.gie index 85c0cbe9..62a5b16d 100644 --- a/test/gie/geotiff_grids.gie +++ b/test/gie/geotiff_grids.gie @@ -277,6 +277,20 @@ accept -80.5041667 44.5458333 0 expect -80.50401615833 44.5458827236 0 ------------------------------------------------------------------------------- +# Check a nested grid of a nested grid only based on spatial extent analysis +------------------------------------------------------------------------------- +operation +proj=hgridshift +grids=tests/test_hgrid_with_two_level_of_subgrids_no_grid_name.tif +------------------------------------------------------------------------------- +accept -45.0 22.5 +accept -44.9983333334 22.5013888889 + +# Check a nested grid of a nested grid only based on spatial extent analysis +------------------------------------------------------------------------------- +operation +proj=vgridshift +grids=tests/test_hgrid_with_two_level_of_subgrids_no_grid_name.tif +multiplier=1 +------------------------------------------------------------------------------- +accept -45.0 22.5 0 +accept -45.0 22.5 5 + ------------------------------------------------------------------------------- operation +proj=hgridshift +grids=tests/test_vgrid.tif ------------------------------------------------------------------------------- diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index e1eefcf0..e14d4c70 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -128,14 +128,20 @@ add_executable(proj_test_cpp_api test_operation.cpp test_datum.cpp test_factory.cpp - test_c_api.cpp) + test_c_api.cpp + test_grids.cpp) target_link_libraries(proj_test_cpp_api GTest::gtest ${PROJ_LIBRARIES} ${SQLITE3_LIBRARY}) add_test(NAME proj_test_cpp_api COMMAND proj_test_cpp_api) -set_property(TEST proj_test_cpp_api - PROPERTY ENVIRONMENT "PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES;PROJ_LIB=${PROJECT_BINARY_DIR}/data") +if(MSVC) + set_property(TEST proj_test_cpp_api + PROPERTY ENVIRONMENT "PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES;PROJ_LIB=${PROJECT_BINARY_DIR}/data\\;${PROJECT_SOURCE_DIR}/data") +else() + set_property(TEST proj_test_cpp_api + PROPERTY ENVIRONMENT "PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES;PROJ_LIB=${PROJECT_BINARY_DIR}/data:${PROJECT_SOURCE_DIR}/data") +endif() add_executable(gie_self_tests diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 7ffb06ae..b7a26d4e 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -49,7 +49,7 @@ proj_context_test_LDADD = ../../src/libproj.la @GTEST_LIBS@ proj_context_test-check: proj_context_test PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES ./proj_context_test -test_cpp_api_SOURCES = test_util.cpp test_common.cpp test_crs.cpp test_metadata.cpp test_io.cpp test_operation.cpp test_datum.cpp test_factory.cpp test_c_api.cpp main.cpp +test_cpp_api_SOURCES = test_util.cpp test_common.cpp test_crs.cpp test_metadata.cpp test_io.cpp test_operation.cpp test_datum.cpp test_factory.cpp test_c_api.cpp test_grids.cpp main.cpp test_cpp_api_LDADD = ../../src/libproj.la @GTEST_LIBS@ @SQLITE3_LIBS@ test_cpp_api-check: test_cpp_api diff --git a/test/unit/test_c_api.cpp b/test/unit/test_c_api.cpp index f87f6589..8871f679 100644 --- a/test/unit/test_c_api.cpp +++ b/test/unit/test_c_api.cpp @@ -4434,4 +4434,26 @@ TEST_F(CApi, proj_create_derived_geographic_crs) { "+o_lat_p=-2 +lon_0=3 +datum=WGS84 +no_defs " "+type=crs")); } + +// --------------------------------------------------------------------------- + +TEST_F(CApi, proj_context_set_sqlite3_vfs_name) { + + PJ_CONTEXT *ctx = proj_context_create(); + proj_log_func(ctx, nullptr, [](void *, int, const char *) -> void {}); + + // Set a dummy VFS and check it is taken into account + // (failure to open proj.db) + proj_context_set_sqlite3_vfs_name(ctx, "dummy_vfs_name"); + ASSERT_EQ(proj_create(ctx, "EPSG:4326"), nullptr); + + // Restore default VFS + proj_context_set_sqlite3_vfs_name(ctx, nullptr); + PJ *crs_4326 = proj_create(ctx, "EPSG:4326"); + ASSERT_NE(crs_4326, nullptr); + proj_destroy(crs_4326); + + proj_context_destroy(ctx); +} + } // namespace diff --git a/test/unit/test_grids.cpp b/test/unit/test_grids.cpp new file mode 100644 index 00000000..5240949e --- /dev/null +++ b/test/unit/test_grids.cpp @@ -0,0 +1,227 @@ +/****************************************************************************** + * + * Project: PROJ + * Purpose: Test grids.hpp + * Author: Even Rouault <even dot rouault at spatialys dot com> + * + ****************************************************************************** + * Copyright (c) 2020, Even Rouault <even dot rouault at spatialys dot com> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + ****************************************************************************/ + +#include "gtest_include.h" + +#include "grids.hpp" + +#include "proj_internal.h" // M_PI + +namespace { + +// --------------------------------------------------------------------------- + +class GridTest : public ::testing::Test { + + static void DummyLogFunction(void *, int, const char *) {} + + protected: + void SetUp() override { + m_ctxt = proj_context_create(); + proj_log_func(m_ctxt, nullptr, DummyLogFunction); + m_ctxt2 = proj_context_create(); + proj_log_func(m_ctxt2, nullptr, DummyLogFunction); + } + + void TearDown() override { + proj_context_destroy(m_ctxt); + proj_context_destroy(m_ctxt2); + } + + PJ_CONTEXT *m_ctxt = nullptr; + PJ_CONTEXT *m_ctxt2 = nullptr; +}; + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, VerticalShiftGridSet_null) { + auto gridSet = NS_PROJ::VerticalShiftGridSet::open(m_ctxt, "null"); + ASSERT_NE(gridSet, nullptr); + auto grid = gridSet->gridAt(0.0, 0.0); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 3); + EXPECT_EQ(grid->height(), 3); + EXPECT_EQ(grid->extentAndRes().westLon, -M_PI); + EXPECT_TRUE(grid->isNullGrid()); + EXPECT_FALSE(grid->hasChanged()); + float out = -1.0f; + EXPECT_TRUE(grid->valueAt(0, 0, out)); + EXPECT_EQ(out, 0.0f); + EXPECT_FALSE(grid->isNodata(0.0f, 0.0)); + gridSet->reassign_context(m_ctxt2); + gridSet->reopen(m_ctxt2); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, VerticalShiftGridSet_gtx) { + ASSERT_EQ(NS_PROJ::VerticalShiftGridSet::open(m_ctxt, "foobar"), nullptr); + auto gridSet = + NS_PROJ::VerticalShiftGridSet::open(m_ctxt, "tests/test_nodata.gtx"); + ASSERT_NE(gridSet, nullptr); + ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr); + auto grid = gridSet->gridAt(4.15 / 180 * M_PI, 52.15 / 180 * M_PI); + ASSERT_NE(grid, nullptr); + EXPECT_TRUE(grid->isNodata(-88.8888f, 1.0)); + gridSet->reassign_context(m_ctxt2); + gridSet->reopen(m_ctxt2); + grid = gridSet->gridAt(4.15 / 180 * M_PI, 52.15 / 180 * M_PI); + EXPECT_NE(grid, nullptr); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, HorizontalShiftGridSet_null) { + auto gridSet = NS_PROJ::HorizontalShiftGridSet::open(m_ctxt, "null"); + ASSERT_NE(gridSet, nullptr); + auto grid = gridSet->gridAt(0.0, 0.0); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 3); + EXPECT_EQ(grid->height(), 3); + EXPECT_EQ(grid->extentAndRes().westLon, -M_PI); + EXPECT_TRUE(grid->isNullGrid()); + EXPECT_FALSE(grid->hasChanged()); + float out1 = -1.0f; + float out2 = -1.0f; + EXPECT_TRUE(grid->valueAt(0, 0, false, out1, out2)); + EXPECT_EQ(out1, 0.0f); + EXPECT_EQ(out2, 0.0f); + gridSet->reassign_context(m_ctxt2); + gridSet->reopen(m_ctxt2); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, HorizontalShiftGridSet_gtiff) { + auto gridSet = + NS_PROJ::HorizontalShiftGridSet::open(m_ctxt, "tests/test_hgrid.tif"); + ASSERT_NE(gridSet, nullptr); + EXPECT_EQ(gridSet->format(), "gtiff"); + EXPECT_TRUE(gridSet->name().find("tests/test_hgrid.tif") != + std::string::npos) + << gridSet->name(); + EXPECT_EQ(gridSet->grids().size(), 1U); + ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr); + auto grid = gridSet->gridAt(5.5 / 180 * M_PI, 53.5 / 180 * M_PI); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 4); + EXPECT_EQ(grid->height(), 4); + EXPECT_EQ(grid->extentAndRes().westLon, 4.0 / 180 * M_PI); + EXPECT_FALSE(grid->isNullGrid()); + EXPECT_FALSE(grid->hasChanged()); + float out1 = -1.0f; + float out2 = -1.0f; + EXPECT_TRUE(grid->valueAt(0, 3, false, out1, out2)); + EXPECT_EQ(out1, static_cast<float>(14400.0 / 3600. / 180 * M_PI)); + EXPECT_EQ(out2, static_cast<float>(900.0 / 3600. / 180 * M_PI)); + gridSet->reassign_context(m_ctxt2); + gridSet->reopen(m_ctxt2); + grid = gridSet->gridAt(5.5 / 180 * M_PI, 53.5 / 180 * M_PI); + EXPECT_NE(grid, nullptr); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, GenericShiftGridSet_null) { + auto gridSet = NS_PROJ::GenericShiftGridSet::open(m_ctxt, "null"); + ASSERT_NE(gridSet, nullptr); + auto grid = gridSet->gridAt(0.0, 0.0); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 3); + EXPECT_EQ(grid->height(), 3); + EXPECT_EQ(grid->extentAndRes().westLon, -M_PI); + EXPECT_TRUE(grid->isNullGrid()); + EXPECT_FALSE(grid->hasChanged()); + float out = -1.0f; + EXPECT_TRUE(grid->valueAt(0, 0, 0, out)); + EXPECT_EQ(out, 0.0f); + EXPECT_EQ(grid->unit(0), ""); + EXPECT_EQ(grid->description(0), ""); + EXPECT_EQ(grid->metadataItem("foo"), ""); + EXPECT_EQ(grid->samplesPerPixel(), 0); + gridSet->reassign_context(m_ctxt2); + gridSet->reopen(m_ctxt2); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, GenericShiftGridSet_gtiff) { + ASSERT_EQ(NS_PROJ::GenericShiftGridSet::open(m_ctxt, "foobar"), nullptr); + auto gridSet = NS_PROJ::GenericShiftGridSet::open( + m_ctxt, "tests/nkgrf03vel_realigned_extract.tif"); + ASSERT_NE(gridSet, nullptr); + ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr); + auto grid = gridSet->gridAt(21.3333333 / 180 * M_PI, 63.0 / 180 * M_PI); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 5); + EXPECT_EQ(grid->height(), 5); + EXPECT_EQ(grid->extentAndRes().westLon, 21.0 / 180 * M_PI); + EXPECT_FALSE(grid->isNullGrid()); + EXPECT_FALSE(grid->hasChanged()); + float out = -1.0f; + EXPECT_FALSE(grid->valueAt(0, 0, grid->samplesPerPixel(), out)); + EXPECT_EQ(grid->metadataItem("area_of_use"), "Nordic and Baltic countries"); + EXPECT_EQ(grid->metadataItem("non_existing"), std::string()); + EXPECT_EQ(grid->metadataItem("non_existing", 1), std::string()); + EXPECT_EQ(grid->metadataItem("non_existing", 10), std::string()); + gridSet->reassign_context(m_ctxt2); + gridSet->reopen(m_ctxt2); + grid = gridSet->gridAt(21.3333333 / 180 * M_PI, 63.0 / 180 * M_PI); + EXPECT_NE(grid, nullptr); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, GenericShiftGridSet_gtiff_with_subgrid) { + auto gridSet = NS_PROJ::GenericShiftGridSet::open( + m_ctxt, "tests/test_hgrid_with_subgrid.tif"); + ASSERT_NE(gridSet, nullptr); + ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr); + auto grid = + gridSet->gridAt(-115.5416667 / 180 * M_PI, 51.1666667 / 180 * M_PI); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 11); + EXPECT_EQ(grid->height(), 21); + EXPECT_EQ(grid->metadataItem("grid_name"), "ALbanff"); +} + +// --------------------------------------------------------------------------- + +TEST_F(GridTest, + GenericShiftGridSet_gtiff_with_two_level_of_subgrids_no_grid_name) { + auto gridSet = NS_PROJ::GenericShiftGridSet::open( + m_ctxt, "tests/test_hgrid_with_two_level_of_subgrids_no_grid_name.tif"); + ASSERT_NE(gridSet, nullptr); + ASSERT_EQ(gridSet->gridAt(-100, -100), nullptr); + auto grid = gridSet->gridAt(-45.5 / 180 * M_PI, 22.5 / 180 * M_PI); + ASSERT_NE(grid, nullptr); + EXPECT_EQ(grid->width(), 8); + EXPECT_EQ(grid->height(), 8); +} + +} // namespace diff --git a/test/unit/test_network.cpp b/test/unit/test_network.cpp index 4e66d8c5..688e61e5 100644 --- a/test/unit/test_network.cpp +++ b/test/unit/test_network.cpp @@ -1574,27 +1574,45 @@ TEST(networking, download_whole_files) { proj_cleanup(); unlink("proj_test_tmp/cache.db"); - unlink("proj_test_tmp/ntf_r93.tif"); + unlink("proj_test_tmp/dvr90.tif"); rmdir("proj_test_tmp"); putenv(const_cast<char *>("PROJ_IGNORE_USER_WRITABLE_DIRECTORY=")); putenv(const_cast<char *>("PROJ_USER_WRITABLE_DIRECTORY=./proj_test_tmp")); - putenv(const_cast<char *>("PROJ_FULL_FILE_CHUNK_SIZE=30000")); + putenv(const_cast<char *>("PROJ_FULL_FILE_CHUNK_SIZE=100000")); auto ctx = proj_context_create(); proj_context_set_enable_network(ctx, true); - ASSERT_TRUE(proj_is_download_needed(ctx, "ntf_r93.gsb", false)); + ASSERT_TRUE(proj_is_download_needed(ctx, "dvr90.gtx", false)); - ASSERT_TRUE( - proj_download_file(ctx, "ntf_r93.gsb", false, nullptr, nullptr)); + ASSERT_TRUE(proj_download_file(ctx, "dvr90.gtx", false, nullptr, nullptr)); - FILE *f = fopen("proj_test_tmp/ntf_r93.tif", "rb"); + FILE *f = fopen("proj_test_tmp/dvr90.tif", "rb"); ASSERT_NE(f, nullptr); - fseek(f, 0, SEEK_END); - ASSERT_EQ(ftell(f), 93581); fclose(f); - ASSERT_FALSE(proj_is_download_needed(ctx, "ntf_r93.gsb", false)); + proj_context_set_enable_network(ctx, false); + + const char *pipeline = + "+proj=pipeline " + "+step +proj=unitconvert +xy_in=deg +xy_out=rad " + "+step +proj=vgridshift +grids=dvr90.gtx +multiplier=1 " + "+step +proj=unitconvert +xy_in=rad +xy_out=deg"; + + auto P = proj_create(ctx, pipeline); + ASSERT_NE(P, nullptr); + + double lon = 12; + double lat = 56; + double z = 0; + proj_trans_generic(P, PJ_FWD, &lon, sizeof(double), 1, &lat, sizeof(double), + 1, &z, sizeof(double), 1, nullptr, 0, 0); + EXPECT_NEAR(z, 36.5909996032715, 1e-10); + proj_destroy(P); + + proj_context_set_enable_network(ctx, true); + + ASSERT_FALSE(proj_is_download_needed(ctx, "dvr90.gtx", false)); { sqlite3 *hDB = nullptr; @@ -1613,7 +1631,7 @@ TEST(networking, download_whole_files) { } // If we ignore TTL settings, then no network access will be done - ASSERT_FALSE(proj_is_download_needed(ctx, "ntf_r93.gsb", true)); + ASSERT_FALSE(proj_is_download_needed(ctx, "dvr90.gtx", true)); { sqlite3 *hDB = nullptr; @@ -1633,7 +1651,7 @@ TEST(networking, download_whole_files) { } // Should recheck from the CDN, update last_checked and do nothing - ASSERT_FALSE(proj_is_download_needed(ctx, "ntf_r93.gsb", false)); + ASSERT_FALSE(proj_is_download_needed(ctx, "dvr90.gtx", false)); { sqlite3 *hDB = nullptr; @@ -1662,10 +1680,10 @@ TEST(networking, download_whole_files) { sqlite3_close(hDB); } - ASSERT_TRUE(proj_is_download_needed(ctx, "ntf_r93.gsb", false)); + ASSERT_TRUE(proj_is_download_needed(ctx, "dvr90.gtx", false)); // Redo download with a progress callback this time. - unlink("proj_test_tmp/ntf_r93.tif"); + unlink("proj_test_tmp/dvr90.tif"); const auto cbk = [](PJ_CONTEXT *l_ctx, double pct, void *user_data) -> int { auto vect = static_cast<std::vector<std::pair<PJ_CONTEXT *, double>> *>( @@ -1675,7 +1693,7 @@ TEST(networking, download_whole_files) { }; std::vector<std::pair<PJ_CONTEXT *, double>> vectPct; - ASSERT_TRUE(proj_download_file(ctx, "ntf_r93.gsb", false, cbk, &vectPct)); + ASSERT_TRUE(proj_download_file(ctx, "dvr90.gtx", false, cbk, &vectPct)); ASSERT_EQ(vectPct.size(), 3U); ASSERT_EQ(vectPct.back().first, ctx); ASSERT_EQ(vectPct.back().second, 1.0); @@ -1685,7 +1703,151 @@ TEST(networking, download_whole_files) { putenv(const_cast<char *>("PROJ_USER_WRITABLE_DIRECTORY=")); putenv(const_cast<char *>("PROJ_FULL_FILE_CHUNK_SIZE=")); unlink("proj_test_tmp/cache.db"); - unlink("proj_test_tmp/ntf_r93.tif"); + unlink("proj_test_tmp/dvr90.tif"); + rmdir("proj_test_tmp"); +} + +// --------------------------------------------------------------------------- + +TEST(networking, file_api) { + if (!networkAccessOK) { + return; + } + + proj_cleanup(); + unlink("proj_test_tmp/cache.db"); + unlink("proj_test_tmp/dvr90.tif"); + rmdir("proj_test_tmp"); + + putenv(const_cast<char *>("PROJ_IGNORE_USER_WRITABLE_DIRECTORY=")); + putenv(const_cast<char *>("PROJ_USER_WRITABLE_DIRECTORY=./proj_test_tmp")); + putenv(const_cast<char *>("PROJ_FULL_FILE_CHUNK_SIZE=30000")); + auto ctx = proj_context_create(); + proj_context_set_enable_network(ctx, true); + + struct UserData { + bool in_open = false; + bool in_read = false; + bool in_write = false; + bool in_seek = false; + bool in_tell = false; + bool in_close = false; + bool in_exists = false; + bool in_mkdir = false; + bool in_unlink = false; + bool in_rename = false; + }; + + struct PROJ_FILE_API api; + api.version = 1; + api.open_cbk = [](PJ_CONTEXT *, const char *filename, + PROJ_OPEN_ACCESS access, + void *user_data) -> PROJ_FILE_HANDLE * { + static_cast<UserData *>(user_data)->in_open = true; + return reinterpret_cast<PROJ_FILE_HANDLE *>(fopen( + filename, + access == PROJ_OPEN_ACCESS_READ_ONLY + ? "rb" + : access == PROJ_OPEN_ACCESS_READ_UPDATE ? "r+b" : "w+b")); + }; + api.read_cbk = [](PJ_CONTEXT *, PROJ_FILE_HANDLE *handle, void *buffer, + size_t sizeBytes, void *user_data) -> size_t { + static_cast<UserData *>(user_data)->in_read = true; + return fread(buffer, 1, sizeBytes, reinterpret_cast<FILE *>(handle)); + }; + api.write_cbk = [](PJ_CONTEXT *, PROJ_FILE_HANDLE *handle, + const void *buffer, size_t sizeBytes, + void *user_data) -> size_t { + static_cast<UserData *>(user_data)->in_write = true; + return fwrite(buffer, 1, sizeBytes, reinterpret_cast<FILE *>(handle)); + }; + api.seek_cbk = [](PJ_CONTEXT *, PROJ_FILE_HANDLE *handle, long long offset, + int whence, void *user_data) -> int { + static_cast<UserData *>(user_data)->in_seek = true; + return fseek(reinterpret_cast<FILE *>(handle), + static_cast<long>(offset), whence) == 0; + }; + api.tell_cbk = [](PJ_CONTEXT *, PROJ_FILE_HANDLE *handle, + void *user_data) -> unsigned long long { + static_cast<UserData *>(user_data)->in_tell = true; + return ftell(reinterpret_cast<FILE *>(handle)); + }; + api.close_cbk = [](PJ_CONTEXT *, PROJ_FILE_HANDLE *handle, + void *user_data) -> void { + static_cast<UserData *>(user_data)->in_close = true; + fclose(reinterpret_cast<FILE *>(handle)); + }; + api.exists_cbk = [](PJ_CONTEXT *, const char *filename, + void *user_data) -> int { + static_cast<UserData *>(user_data)->in_exists = true; + struct stat buf; + return stat(filename, &buf) == 0; + }; + api.mkdir_cbk = [](PJ_CONTEXT *, const char *filename, + void *user_data) -> int { + static_cast<UserData *>(user_data)->in_mkdir = true; +#ifdef _WIN32 + return mkdir(filename) == 0; +#else + return mkdir(filename, 0755) == 0; +#endif + }; + api.unlink_cbk = [](PJ_CONTEXT *, const char *filename, + void *user_data) -> int { + static_cast<UserData *>(user_data)->in_unlink = true; + return unlink(filename) == 0; + }; + api.rename_cbk = [](PJ_CONTEXT *, const char *oldPath, const char *newPath, + void *user_data) -> int { + static_cast<UserData *>(user_data)->in_rename = true; + return rename(oldPath, newPath) == 0; + }; + + UserData userData; + ASSERT_TRUE(proj_context_set_fileapi(ctx, &api, &userData)); + + ASSERT_TRUE(proj_is_download_needed(ctx, "dvr90.gtx", false)); + + ASSERT_TRUE(proj_download_file(ctx, "dvr90.gtx", false, nullptr, nullptr)); + + ASSERT_TRUE(userData.in_open); + ASSERT_FALSE(userData.in_read); + ASSERT_TRUE(userData.in_write); + ASSERT_TRUE(userData.in_close); + ASSERT_TRUE(userData.in_exists); + ASSERT_TRUE(userData.in_mkdir); + ASSERT_TRUE(userData.in_unlink); + ASSERT_TRUE(userData.in_rename); + + proj_context_set_enable_network(ctx, false); + + const char *pipeline = + "+proj=pipeline " + "+step +proj=unitconvert +xy_in=deg +xy_out=rad " + "+step +proj=vgridshift +grids=dvr90.gtx +multiplier=1 " + "+step +proj=unitconvert +xy_in=rad +xy_out=deg"; + + auto P = proj_create(ctx, pipeline); + ASSERT_NE(P, nullptr); + + double lon = 12; + double lat = 56; + double z = 0; + proj_trans_generic(P, PJ_FWD, &lon, sizeof(double), 1, &lat, sizeof(double), + 1, &z, sizeof(double), 1, nullptr, 0, 0); + EXPECT_NEAR(z, 36.5909996032715, 1e-10); + + proj_destroy(P); + + ASSERT_TRUE(userData.in_read); + ASSERT_TRUE(userData.in_seek); + + proj_context_destroy(ctx); + putenv(const_cast<char *>("PROJ_IGNORE_USER_WRITABLE_DIRECTORY=YES")); + putenv(const_cast<char *>("PROJ_USER_WRITABLE_DIRECTORY=")); + putenv(const_cast<char *>("PROJ_FULL_FILE_CHUNK_SIZE=")); + unlink("proj_test_tmp/cache.db"); + unlink("proj_test_tmp/dvr90.tif"); rmdir("proj_test_tmp"); } |
