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 : * SPDX-License-Identifier: MIT 11 : ******************************************************************************/ 12 : 13 : #include "gdal_unit_test.h" 14 : #include "gdal.h" 15 : #include "ogr_api.h" 16 : 17 : #include "gtest_include.h" 18 : 19 : namespace 20 : { 21 : 22 : // Test data 23 : struct test_ogr_geometry_stealing : public ::testing::Test 24 : { 25 : GDALDatasetH hDS = nullptr; 26 : OGRLayerH hLayer = nullptr; 27 : 28 2 : test_ogr_geometry_stealing() 29 2 : { 30 : // Build data file path name 31 2 : std::string test_data_file_name(tut::common::data_basedir); 32 2 : test_data_file_name += SEP; 33 2 : test_data_file_name += "multi_geom.csv"; 34 : 35 : // Open data file with multi geometries feature layer 36 2 : const char *const papszOpenOptions[] = { 37 : "AUTODETECT_TYPE=YES", "GEOM_POSSIBLE_NAMES=point,linestring", 38 : "KEEP_GEOM_COLUMNS=NO", nullptr}; 39 2 : hDS = GDALOpenEx(test_data_file_name.c_str(), GDAL_OF_VECTOR, nullptr, 40 : papszOpenOptions, nullptr); 41 2 : if (hDS == nullptr) 42 : { 43 0 : printf("Can't open layer file %s.\n", test_data_file_name.c_str()); 44 0 : return; 45 : } 46 2 : hLayer = GDALDatasetGetLayer(hDS, 0); 47 2 : if (hLayer == nullptr) 48 : { 49 0 : printf("Can't get layer in file %s.\n", 50 : test_data_file_name.c_str()); 51 0 : return; 52 : } 53 : } 54 : 55 2 : ~test_ogr_geometry_stealing() 56 2 : { 57 2 : GDALClose(hDS); 58 2 : } 59 : 60 2 : void SetUp() override 61 : { 62 2 : if (hLayer == nullptr) 63 0 : GTEST_SKIP() << "Cannot open source file"; 64 : } 65 : }; 66 : 67 : // Test 1st geometry stealing from a multigeom csv file 68 4 : TEST_F(test_ogr_geometry_stealing, first_geometry) 69 : { 70 1 : OGRFeatureH hFeature = OGR_L_GetNextFeature(hLayer); 71 1 : OGRGeometryH hGeometryOrig = OGR_G_Clone(OGR_F_GetGeometryRef(hFeature)); 72 1 : OGRGeometryH hGeometryStolen = OGR_F_StealGeometry(hFeature); 73 1 : ASSERT_TRUE(hGeometryOrig); 74 1 : ASSERT_TRUE(hGeometryStolen); 75 1 : ASSERT_TRUE(OGR_G_Equals(hGeometryOrig, hGeometryStolen)); 76 1 : ASSERT_TRUE(OGR_F_GetGeometryRef(hFeature) == nullptr); 77 1 : OGR_G_DestroyGeometry(hGeometryOrig); 78 1 : OGR_G_DestroyGeometry(hGeometryStolen); 79 1 : OGR_F_Destroy(hFeature); 80 : } 81 : 82 : // Test 2nd geometry stealing from a multigeom csv file 83 4 : TEST_F(test_ogr_geometry_stealing, second_geometry) 84 : { 85 1 : OGRFeatureH hFeature = OGR_L_GetNextFeature(hLayer); 86 : OGRGeometryH hGeometryOrig = 87 1 : OGR_G_Clone(OGR_F_GetGeomFieldRef(hFeature, 1)); 88 1 : OGRGeometryH hGeometryStolen = OGR_F_StealGeometryEx(hFeature, 1); 89 1 : ASSERT_TRUE(hGeometryOrig); 90 1 : ASSERT_TRUE(hGeometryStolen); 91 1 : ASSERT_TRUE(OGR_G_Equals(hGeometryOrig, hGeometryStolen)); 92 1 : ASSERT_TRUE(OGR_F_GetGeomFieldRef(hFeature, 1) == nullptr); 93 1 : OGR_G_DestroyGeometry(hGeometryOrig); 94 1 : OGR_G_DestroyGeometry(hGeometryStolen); 95 1 : OGR_F_Destroy(hFeature); 96 : } 97 : 98 : } // namespace