Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: C++ Test Suite for GDAL/OGR 4 : * Purpose: Test geometry stealing from OGR feature 5 : * Author: mathieu17g (https://github.com/mathieu17g/) 6 : * 7 : ****************************************************************************** 8 : * Copyright (C) 2022 mathieu17g 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining 11 : * a copy of this software and associated documentation files (the 12 : * "Software"), to deal in the Software without restriction, including 13 : * without limitation the rights to use, copy, modify, merge, publish, 14 : * distribute, sublicense, and/or sell copies of the Software, and to 15 : * permit persons to whom the Software is furnished to do so, subject to 16 : * the following conditions: 17 : * 18 : * The above copyright notice and this permission notice shall be 19 : * included in all copies or substantial portions of the Software. 20 : * 21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 : * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 : * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 : * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 : ******************************************************************************/ 29 : 30 : #include "gdal_unit_test.h" 31 : #include "gdal.h" 32 : #include "ogr_api.h" 33 : 34 : #include "gtest_include.h" 35 : 36 : namespace 37 : { 38 : 39 : // Test data 40 : struct test_ogr_geometry_stealing : public ::testing::Test 41 : { 42 : GDALDatasetH hDS = nullptr; 43 : OGRLayerH hLayer = nullptr; 44 : 45 2 : test_ogr_geometry_stealing() 46 2 : { 47 : // Build data file path name 48 2 : std::string test_data_file_name(tut::common::data_basedir); 49 2 : test_data_file_name += SEP; 50 2 : test_data_file_name += "multi_geom.csv"; 51 : 52 : // Open data file with multi geometries feature layer 53 2 : const char *const papszOpenOptions[] = { 54 : "AUTODETECT_TYPE=YES", "GEOM_POSSIBLE_NAMES=point,linestring", 55 : "KEEP_GEOM_COLUMNS=NO", nullptr}; 56 2 : hDS = GDALOpenEx(test_data_file_name.c_str(), GDAL_OF_VECTOR, nullptr, 57 : papszOpenOptions, nullptr); 58 2 : if (hDS == nullptr) 59 : { 60 0 : printf("Can't open layer file %s.\n", test_data_file_name.c_str()); 61 0 : return; 62 : } 63 2 : hLayer = GDALDatasetGetLayer(hDS, 0); 64 2 : if (hLayer == nullptr) 65 : { 66 0 : printf("Can't get layer in file %s.\n", 67 : test_data_file_name.c_str()); 68 0 : return; 69 : } 70 : } 71 : 72 2 : ~test_ogr_geometry_stealing() 73 2 : { 74 2 : GDALClose(hDS); 75 2 : } 76 : 77 2 : void SetUp() override 78 : { 79 2 : if (hLayer == nullptr) 80 0 : GTEST_SKIP() << "Cannot open source file"; 81 : } 82 : }; 83 : 84 : // Test 1st geometry stealing from a multigeom csv file 85 4 : TEST_F(test_ogr_geometry_stealing, first_geometry) 86 : { 87 1 : OGRFeatureH hFeature = OGR_L_GetNextFeature(hLayer); 88 1 : OGRGeometryH hGeometryOrig = OGR_G_Clone(OGR_F_GetGeometryRef(hFeature)); 89 1 : OGRGeometryH hGeometryStolen = OGR_F_StealGeometry(hFeature); 90 1 : ASSERT_TRUE(hGeometryOrig); 91 1 : ASSERT_TRUE(hGeometryStolen); 92 1 : ASSERT_TRUE(OGR_G_Equals(hGeometryOrig, hGeometryStolen)); 93 1 : ASSERT_TRUE(OGR_F_GetGeometryRef(hFeature) == nullptr); 94 1 : OGR_G_DestroyGeometry(hGeometryOrig); 95 1 : OGR_G_DestroyGeometry(hGeometryStolen); 96 1 : OGR_F_Destroy(hFeature); 97 : } 98 : 99 : // Test 2nd geometry stealing from a multigeom csv file 100 4 : TEST_F(test_ogr_geometry_stealing, second_geometry) 101 : { 102 1 : OGRFeatureH hFeature = OGR_L_GetNextFeature(hLayer); 103 : OGRGeometryH hGeometryOrig = 104 1 : OGR_G_Clone(OGR_F_GetGeomFieldRef(hFeature, 1)); 105 1 : OGRGeometryH hGeometryStolen = OGR_F_StealGeometryEx(hFeature, 1); 106 1 : ASSERT_TRUE(hGeometryOrig); 107 1 : ASSERT_TRUE(hGeometryStolen); 108 1 : ASSERT_TRUE(OGR_G_Equals(hGeometryOrig, hGeometryStolen)); 109 1 : ASSERT_TRUE(OGR_F_GetGeomFieldRef(hFeature, 1) == nullptr); 110 1 : OGR_G_DestroyGeometry(hGeometryOrig); 111 1 : OGR_G_DestroyGeometry(hGeometryStolen); 112 1 : OGR_F_Destroy(hFeature); 113 : } 114 : 115 : } // namespace