Line data Source code
1 : ///////////////////////////////////////////////////////////////////////////////
2 : //
3 : // Project: C++ Test Suite for GDAL/OGR
4 : // Purpose: Shapefile driver testing. Ported from ogr/ogr_shape.py.
5 : // Author: Mateusz Loskot <mateusz@loskot.net>
6 : //
7 : ///////////////////////////////////////////////////////////////////////////////
8 : // Copyright (c) 2006, Mateusz Loskot <mateusz@loskot.net>
9 : // Copyright (c) 2010, Even Rouault <even dot rouault at spatialys.com>
10 : /*
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "gdal_unit_test.h"
15 :
16 : #include "ogr_api.h"
17 : #include "ogrsf_frmts.h"
18 :
19 : #include <algorithm>
20 : #include <iterator>
21 : #include <string>
22 : #include <vector>
23 :
24 : #include "gtest_include.h"
25 :
26 : namespace
27 : {
28 : using namespace tut; // for CheckEqualGeometries
29 :
30 : // Test data
31 : struct test_ogr_shape : public ::testing::Test
32 : {
33 : OGRSFDriverH drv_ = nullptr;
34 : const char *drv_name_ = "ESRI Shapefile";
35 : std::string data_{};
36 : std::string data_tmp_{};
37 : const char *test_name_ = nullptr;
38 :
39 8 : test_ogr_shape()
40 8 : {
41 8 : drv_ = OGRGetDriverByName(drv_name_);
42 :
43 : // Compose data path for test group
44 8 : data_ = tut::common::data_basedir;
45 8 : data_tmp_ = tut::common::tmp_basedir;
46 8 : }
47 :
48 8 : void TearDown() override
49 : {
50 8 : if (!drv_)
51 0 : return;
52 : OGRDataSourceH ds =
53 8 : OGR_Dr_CreateDataSource(drv_, data_tmp_.c_str(), nullptr);
54 8 : if (ds == nullptr)
55 : {
56 0 : return;
57 : }
58 :
59 8 : if (test_name_)
60 : {
61 8 : const int nlyr = OGR_DS_GetLayerCount(ds);
62 15 : for (int i = 0; i < nlyr; i++)
63 : {
64 7 : OGRLayerH lyr = OGR_DS_GetLayer(ds, i);
65 7 : if (EQUAL(OGR_L_GetName(lyr), test_name_))
66 : {
67 7 : OGR_DS_DeleteLayer(ds, i);
68 : }
69 : }
70 : }
71 : }
72 :
73 8 : void SetUp() override
74 : {
75 8 : if (drv_ == nullptr)
76 : {
77 0 : GTEST_SKIP() << "ESRI Shapefile driver missing";
78 : return;
79 : }
80 :
81 8 : test_name_ =
82 8 : ::testing::UnitTest::GetInstance()->current_test_info()->name();
83 :
84 8 : OGRErr err = OGRERR_NONE;
85 :
86 8 : OGRDataSourceH ds = nullptr;
87 8 : ds = OGR_Dr_CreateDataSource(drv_, data_tmp_.c_str(), nullptr);
88 8 : ASSERT_TRUE(nullptr != ds);
89 :
90 : // Create memory Layer
91 8 : OGRLayerH lyr = nullptr;
92 8 : lyr = OGR_DS_CreateLayer(ds, test_name_, nullptr, wkbPolygon, nullptr);
93 8 : EXPECT_TRUE(nullptr != lyr);
94 8 : if (lyr == nullptr)
95 : {
96 0 : OGR_DS_Destroy(ds);
97 0 : return;
98 : }
99 :
100 : // Create schema
101 8 : OGRFieldDefnH fld = nullptr;
102 :
103 8 : fld = OGR_Fld_Create("AREA", OFTReal);
104 8 : err = OGR_L_CreateField(lyr, fld, true);
105 8 : OGR_Fld_Destroy(fld);
106 8 : EXPECT_EQ(OGRERR_NONE, err);
107 :
108 8 : fld = OGR_Fld_Create("EAS_ID", OFTInteger);
109 8 : err = OGR_L_CreateField(lyr, fld, true);
110 8 : OGR_Fld_Destroy(fld);
111 8 : EXPECT_EQ(OGRERR_NONE, err);
112 :
113 8 : fld = OGR_Fld_Create("PRFEDEA", OFTString);
114 8 : err = OGR_L_CreateField(lyr, fld, true);
115 8 : OGR_Fld_Destroy(fld);
116 8 : EXPECT_EQ(OGRERR_NONE, err);
117 :
118 : // Check schema
119 8 : OGRFeatureDefnH featDefn = OGR_L_GetLayerDefn(lyr);
120 8 : ASSERT_TRUE(nullptr != featDefn);
121 8 : EXPECT_EQ(3, OGR_FD_GetFieldCount(featDefn));
122 :
123 : // Copy ogr/poly.shp to temporary layer
124 8 : OGRFeatureH featDst = OGR_F_Create(featDefn);
125 8 : EXPECT_TRUE(nullptr != featDst);
126 8 : if (featDst)
127 : {
128 16 : std::string source(data_);
129 8 : source += SEP;
130 8 : source += "poly.shp";
131 8 : OGRDataSourceH dsSrc = OGR_Dr_Open(drv_, source.c_str(), false);
132 8 : EXPECT_TRUE(nullptr != dsSrc);
133 8 : if (dsSrc)
134 : {
135 8 : OGRLayerH lyrSrc = OGR_DS_GetLayer(dsSrc, 0);
136 8 : EXPECT_TRUE(nullptr != lyrSrc);
137 8 : if (lyrSrc)
138 : {
139 8 : OGRFeatureH featSrc = nullptr;
140 88 : while (nullptr != (featSrc = OGR_L_GetNextFeature(lyrSrc)))
141 : {
142 80 : err = OGR_F_SetFrom(featDst, featSrc, true);
143 80 : EXPECT_EQ(OGRERR_NONE, err);
144 :
145 80 : err = OGR_L_CreateFeature(lyr, featDst);
146 80 : EXPECT_EQ(OGRERR_NONE, err);
147 :
148 80 : OGR_F_Destroy(featSrc);
149 : }
150 : }
151 : // Release and close resources
152 :
153 8 : OGR_DS_Destroy(dsSrc);
154 : }
155 8 : OGR_F_Destroy(featDst);
156 : }
157 8 : OGR_DS_Destroy(ds);
158 : }
159 : };
160 :
161 : //
162 : // Template of attribute reading function and its specializations
163 : //
164 : template <typename T> inline void read_feature_attribute(OGRFeatureH, int, T &)
165 : {
166 : assert(!"Can't find read_feature_attribute specialization for given type");
167 : }
168 :
169 : template <>
170 16 : inline void read_feature_attribute(OGRFeatureH feature, int index, int &val)
171 : {
172 16 : val = OGR_F_GetFieldAsInteger(feature, index);
173 16 : }
174 :
175 : #ifdef unused
176 : template <>
177 : inline void read_feature_attribute(OGRFeatureH feature, int index, double &val)
178 : {
179 : val = OGR_F_GetFieldAsDouble(feature, index);
180 : }
181 : #endif
182 :
183 : template <>
184 1 : inline void read_feature_attribute(OGRFeatureH feature, int index,
185 : std::string &val)
186 : {
187 1 : val = OGR_F_GetFieldAsString(feature, index);
188 1 : }
189 :
190 : //
191 : // Test layer attributes from given field against expected list of values
192 : //
193 : template <typename T>
194 : ::testing::AssertionResult
195 4 : CheckEqualAttributes(OGRLayerH layer, std::string const &field, T const &list)
196 : {
197 : // Test raw pointers
198 4 : if (nullptr == layer)
199 : {
200 0 : return ::testing::AssertionFailure() << "Layer is NULL";
201 : }
202 :
203 4 : OGRFeatureDefnH featDefn = OGR_L_GetLayerDefn(layer);
204 4 : if (nullptr == featDefn)
205 : {
206 0 : return ::testing::AssertionFailure() << "Layer schema is NULL";
207 : }
208 :
209 4 : int fldIndex = OGR_FD_GetFieldIndex(featDefn, field.c_str());
210 4 : if (fldIndex < 0)
211 : {
212 0 : return ::testing::AssertionFailure() << "Can't find field " << field;
213 : }
214 :
215 : // Test value in tested field from subsequent features
216 4 : OGRFeatureH feat = nullptr;
217 4 : OGRFieldDefnH fldDefn = nullptr;
218 2 : typename T::value_type attrVal;
219 :
220 21 : for (const auto &attr : list)
221 : {
222 17 : feat = OGR_L_GetNextFeature(layer);
223 :
224 17 : fldDefn = OGR_F_GetFieldDefnRef(feat, fldIndex);
225 17 : if (nullptr == fldDefn)
226 : {
227 0 : return ::testing::AssertionFailure() << "Field schema is NULL";
228 : }
229 :
230 17 : read_feature_attribute(feat, fldIndex, attrVal);
231 :
232 17 : OGR_F_Destroy(feat);
233 :
234 : // Test attribute against expected value
235 17 : if (attr != attrVal)
236 : {
237 : return ::testing::AssertionFailure()
238 0 : << "Attributes not equal. Expected " << attr << ", got "
239 0 : << attrVal;
240 : }
241 : }
242 :
243 : // Check if not too many features filtered
244 4 : feat = OGR_L_GetNextFeature(layer);
245 4 : OGR_F_Destroy(feat);
246 :
247 4 : if (nullptr != feat)
248 : {
249 : return ::testing::AssertionFailure()
250 0 : << "Got more features than expected";
251 : }
252 4 : return ::testing::AssertionSuccess();
253 : }
254 :
255 : // Test Create/Destroy empty directory datasource
256 4 : TEST_F(test_ogr_shape, create)
257 : {
258 : // Try to remove tmp and ignore error code
259 1 : OGR_Dr_DeleteDataSource(drv_, data_tmp_.c_str());
260 :
261 1 : OGRDataSourceH ds = nullptr;
262 1 : ds = OGR_Dr_CreateDataSource(drv_, data_tmp_.c_str(), nullptr);
263 1 : ASSERT_TRUE(nullptr != ds);
264 :
265 1 : OGR_DS_Destroy(ds);
266 : }
267 :
268 : // Test attributes written to new table
269 4 : TEST_F(test_ogr_shape, attributes)
270 : {
271 1 : OGRErr err = OGRERR_NONE;
272 1 : const int size = 5;
273 1 : const int expect[size] = {168, 169, 166, 158, 165};
274 :
275 1 : std::string source(data_tmp_);
276 1 : source += SEP;
277 1 : source += test_name_;
278 1 : source += ".shp";
279 1 : OGRDataSourceH ds = OGR_Dr_Open(drv_, source.c_str(), false);
280 1 : ASSERT_TRUE(nullptr != ds);
281 :
282 1 : OGRLayerH lyr = OGR_DS_GetLayer(ds, 0);
283 1 : EXPECT_TRUE(nullptr != lyr);
284 1 : if (lyr)
285 : {
286 1 : err = OGR_L_SetAttributeFilter(lyr, "eas_id < 170");
287 1 : EXPECT_EQ(OGRERR_NONE, err);
288 :
289 : // Prepare tester collection
290 2 : std::vector<int> list;
291 1 : std::copy(expect, expect + size, std::back_inserter(list));
292 :
293 2 : EXPECT_TRUE(CheckEqualAttributes(lyr, "eas_id", list));
294 : }
295 1 : OGR_DS_Destroy(ds);
296 : }
297 :
298 : // Test geometries written to new shapefile
299 4 : TEST_F(test_ogr_shape, geometries)
300 : {
301 : // Original shapefile
302 1 : std::string orig(data_);
303 1 : orig += SEP;
304 1 : orig += "poly.shp";
305 1 : OGRDataSourceH dsOrig = OGR_Dr_Open(drv_, orig.c_str(), false);
306 1 : ASSERT_TRUE(nullptr != dsOrig);
307 :
308 1 : OGRLayerH lyrOrig = OGR_DS_GetLayer(dsOrig, 0);
309 1 : EXPECT_TRUE(nullptr != lyrOrig);
310 1 : if (lyrOrig)
311 : {
312 : // Copied shapefile
313 2 : std::string tmp(data_tmp_);
314 1 : tmp += SEP;
315 1 : tmp += test_name_;
316 1 : tmp += ".shp";
317 1 : OGRDataSourceH dsTmp = OGR_Dr_Open(drv_, tmp.c_str(), false);
318 1 : EXPECT_TRUE(nullptr != dsTmp);
319 :
320 1 : OGRLayerH lyrTmp = OGR_DS_GetLayer(dsTmp, 0);
321 1 : EXPECT_TRUE(nullptr != lyrTmp);
322 1 : if (lyrTmp)
323 : {
324 : // Iterate through features and compare geometries
325 1 : OGRFeatureH featOrig = OGR_L_GetNextFeature(lyrOrig);
326 1 : OGRFeatureH featTmp = OGR_L_GetNextFeature(lyrTmp);
327 :
328 11 : while (nullptr != featOrig && nullptr != featTmp)
329 : {
330 10 : OGRGeometryH lhs = OGR_F_GetGeometryRef(featOrig);
331 10 : OGRGeometryH rhs = OGR_F_GetGeometryRef(featTmp);
332 :
333 10 : EXPECT_TRUE(CheckEqualGeometries(lhs, rhs, 0.000000001));
334 :
335 : // TODO: add ensure_equal_attributes()
336 :
337 10 : OGR_F_Destroy(featOrig);
338 10 : OGR_F_Destroy(featTmp);
339 :
340 : // Move to next feature
341 10 : featOrig = OGR_L_GetNextFeature(lyrOrig);
342 10 : featTmp = OGR_L_GetNextFeature(lyrTmp);
343 : }
344 : }
345 1 : OGR_DS_Destroy(dsTmp);
346 : }
347 :
348 1 : OGR_DS_Destroy(dsOrig);
349 : }
350 :
351 : // Write a feature without a geometry, then read it back
352 4 : TEST_F(test_ogr_shape, no_geometry)
353 : {
354 : // Create feature without geometry
355 1 : std::string tmp(data_tmp_);
356 1 : tmp += SEP;
357 1 : tmp += test_name_;
358 1 : tmp += ".shp";
359 :
360 : // Write the feature
361 : {
362 1 : OGRDataSourceH ds = OGR_Dr_Open(drv_, tmp.c_str(), true);
363 1 : ASSERT_TRUE(nullptr != ds);
364 :
365 1 : OGRLayerH lyr = OGR_DS_GetLayer(ds, 0);
366 1 : EXPECT_TRUE(nullptr != lyr);
367 1 : if (lyr != nullptr)
368 : {
369 1 : OGRFeatureDefnH featDefn = OGR_L_GetLayerDefn(lyr);
370 1 : EXPECT_TRUE(nullptr != featDefn);
371 :
372 1 : OGRFeatureH featNonSpatial = OGR_F_Create(featDefn);
373 1 : EXPECT_TRUE(nullptr != featNonSpatial);
374 1 : if (featDefn && featNonSpatial)
375 : {
376 1 : int fldIndex = OGR_FD_GetFieldIndex(featDefn, "PRFEDEA");
377 1 : EXPECT_TRUE(fldIndex >= 0);
378 1 : if (fldIndex >= 0)
379 : {
380 1 : OGR_F_SetFieldString(featNonSpatial, fldIndex, "nulled");
381 :
382 1 : OGRErr err = OGR_L_CreateFeature(lyr, featNonSpatial);
383 1 : EXPECT_EQ(OGRERR_NONE, err);
384 : }
385 : }
386 :
387 1 : OGR_F_Destroy(featNonSpatial);
388 : }
389 1 : OGR_DS_Destroy(ds);
390 : }
391 :
392 : // Read back the non-spatial feature and get the geometry
393 : {
394 1 : OGRDataSourceH ds = OGR_Dr_Open(drv_, tmp.c_str(), false);
395 1 : ASSERT_TRUE(nullptr != ds);
396 :
397 1 : OGRLayerH lyr = OGR_DS_GetLayer(ds, 0);
398 1 : EXPECT_TRUE(nullptr != lyr);
399 1 : if (lyr != nullptr)
400 : {
401 1 : OGRErr err = OGR_L_SetAttributeFilter(lyr, "PRFEDEA = 'nulled'");
402 1 : EXPECT_EQ(OGRERR_NONE, err);
403 :
404 : // Fetch feature without geometry
405 1 : OGRFeatureH featNonSpatial = OGR_L_GetNextFeature(lyr);
406 1 : EXPECT_TRUE(nullptr != featNonSpatial);
407 1 : if (featNonSpatial != nullptr)
408 : {
409 : // Null geometry is expected
410 1 : OGRGeometryH nonGeom = OGR_F_GetGeometryRef(featNonSpatial);
411 1 : EXPECT_TRUE(nullptr == nonGeom);
412 :
413 1 : OGR_F_Destroy(featNonSpatial);
414 : }
415 : }
416 1 : OGR_DS_Destroy(ds);
417 : }
418 : }
419 :
420 : // Test ExecuteSQL() results layers without geometry
421 4 : TEST_F(test_ogr_shape, ExecuteSQL_no_geometry)
422 : {
423 1 : const int size = 10;
424 1 : const int expect[size] = {179, 173, 172, 171, 170, 169, 168, 166, 165, 158};
425 :
426 : // Open directory as a datasource
427 1 : OGRDataSourceH ds = OGR_Dr_Open(drv_, data_.c_str(), false);
428 1 : ASSERT_TRUE(nullptr != ds);
429 :
430 2 : std::string sql("select distinct eas_id from poly order by eas_id desc");
431 1 : OGRLayerH lyr = OGR_DS_ExecuteSQL(ds, sql.c_str(), nullptr, nullptr);
432 1 : EXPECT_TRUE(nullptr != lyr);
433 1 : if (lyr)
434 : {
435 : // Prepare tester collection
436 2 : std::vector<int> list;
437 1 : std::copy(expect, expect + size, std::back_inserter(list));
438 :
439 2 : EXPECT_TRUE(CheckEqualAttributes(lyr, "eas_id", list));
440 :
441 1 : OGR_DS_ReleaseResultSet(ds, lyr);
442 : }
443 1 : OGR_DS_Destroy(ds);
444 : }
445 :
446 : // Test ExecuteSQL() results layers with geometry
447 4 : TEST_F(test_ogr_shape, ExecuteSQL_geometry)
448 : {
449 : // Open directory as a datasource
450 1 : OGRDataSourceH ds = OGR_Dr_Open(drv_, data_.c_str(), false);
451 1 : ASSERT_TRUE(nullptr != ds);
452 :
453 2 : std::string sql("select * from poly where prfedea = '35043413'");
454 1 : OGRLayerH lyr = OGR_DS_ExecuteSQL(ds, sql.c_str(), nullptr, nullptr);
455 1 : EXPECT_TRUE(nullptr != lyr);
456 1 : if (lyr)
457 : {
458 : // Prepare tester collection
459 2 : std::vector<std::string> list;
460 1 : list.push_back("35043413");
461 :
462 : // Test attributes
463 2 : EXPECT_TRUE(CheckEqualAttributes(lyr, "prfedea", list));
464 :
465 : // Test geometry
466 1 : const char *wkt =
467 : "POLYGON ((479750.688 4764702.000,479658.594 4764670.000,"
468 : "479640.094 4764721.000,479735.906 4764752.000,"
469 : "479750.688 4764702.000))";
470 :
471 1 : OGRGeometryH testGeom = nullptr;
472 1 : OGRErr err = OGR_G_CreateFromWkt((char **)&wkt, nullptr, &testGeom);
473 1 : EXPECT_EQ(OGRERR_NONE, err);
474 1 : if (testGeom)
475 : {
476 1 : OGR_L_ResetReading(lyr);
477 1 : OGRFeatureH feat = OGR_L_GetNextFeature(lyr);
478 1 : EXPECT_TRUE(nullptr != feat);
479 1 : if (feat)
480 : {
481 1 : EXPECT_TRUE(CheckEqualGeometries(OGR_F_GetGeometryRef(feat),
482 : testGeom, 0.001));
483 1 : OGR_F_Destroy(feat);
484 : }
485 1 : OGR_G_DestroyGeometry(testGeom);
486 : }
487 1 : OGR_DS_ReleaseResultSet(ds, lyr);
488 : }
489 1 : OGR_DS_Destroy(ds);
490 : }
491 :
492 : // Test spatial filtering
493 4 : TEST_F(test_ogr_shape, spatial_filtering)
494 : {
495 1 : OGRErr err = OGRERR_NONE;
496 :
497 : // Read feature without geometry
498 1 : std::string tmp(data_tmp_);
499 1 : tmp += SEP;
500 1 : tmp += "poly.shp";
501 1 : OGRDataSourceH ds = OGR_Dr_Open(drv_, data_.c_str(), false);
502 1 : ASSERT_TRUE(nullptr != ds);
503 :
504 1 : OGRLayerH lyr = OGR_DS_GetLayer(ds, 0);
505 1 : EXPECT_TRUE(nullptr != lyr);
506 1 : if (lyr)
507 : {
508 : // Set empty filter for attributes
509 1 : err = OGR_L_SetAttributeFilter(lyr, nullptr);
510 1 : EXPECT_EQ(OGRERR_NONE, err);
511 :
512 : // Set spatial filter
513 1 : const char *wkt = "LINESTRING(479505 4763195,480526 4762819)";
514 1 : OGRGeometryH filterGeom = nullptr;
515 1 : err = OGR_G_CreateFromWkt((char **)&wkt, nullptr, &filterGeom);
516 1 : ASSERT_EQ(OGRERR_NONE, err);
517 1 : if (filterGeom)
518 : {
519 1 : OGR_L_SetSpatialFilter(lyr, filterGeom);
520 :
521 : // Prepare tester collection
522 2 : std::vector<int> list;
523 1 : list.push_back(158);
524 :
525 : // Test attributes
526 2 : EXPECT_TRUE(CheckEqualAttributes(lyr, "eas_id", list));
527 :
528 1 : OGR_G_DestroyGeometry(filterGeom);
529 : }
530 : }
531 1 : OGR_DS_Destroy(ds);
532 : }
533 :
534 4 : TEST_F(test_ogr_shape, create_gdal)
535 : {
536 : GDALDriver *shpDriver =
537 1 : GetGDALDriverManager()->GetDriverByName("ESRI Shapefile");
538 : GDALDataset *pShpDst =
539 1 : shpDriver->Create("/vsimem/test.shp", 0, 0, 0, GDT_Unknown, nullptr);
540 1 : EXPECT_EQ(pShpDst->GetAccess(), GA_Update);
541 1 : GDALClose(pShpDst);
542 1 : }
543 :
544 : } // namespace
|