void object::test<14>() { char* wkt1 = "POLYGON((0 0, 10 10, 10 0, 0 0))"; err_ = OGR_G_CreateFromWkt(&wkt1, NULL, &g1_); ensure_equals("Can't import geometry from WKT", OGRERR_NONE, err_); ensure("Can't create geometry", NULL != g1_); char* wkt2 = "POLYGON((0 0, 0 10, 10 0, 0 0))"; err_ = OGR_G_CreateFromWkt(&wkt2, NULL, &g2_); ensure_equals("Can't import geometry from WKT", OGRERR_NONE, err_); ensure("Can't create geometry", NULL != g2_); g3_ = OGR_G_SymmetricDifference(g1_, g2_); ensure("OGR_G_SymmetricDifference failed with NULL", NULL != g3_); OGRGeometryH expect = NULL; char* wktExpect = "MULTIPOLYGON (((5 5,0 0,0 10,5 5)),((5 5,10 10,10 0,5 5)))"; err_ = OGR_G_CreateFromWkt(&wktExpect, NULL, &expect); ensure_equals("Can't import geometry from WKT", OGRERR_NONE, err_); ensure("Can't create geometry", NULL != expect); // Compare operation result against expected geometry ensure_equal_geometries(g3_, expect, 0.0001); OGR_G_DestroyGeometry(expect); }
void object::test<11>() { char* wkt1 = "POINT(10 20)"; err_ = OGR_G_CreateFromWkt(&wkt1, NULL, &g1_); ensure_equals("Can't import geometry from WKT", OGRERR_NONE, err_); ensure("Can't create geometry", NULL != g1_); char* wkt2 = "POINT(30 20)"; err_ = OGR_G_CreateFromWkt(&wkt2, NULL, &g2_); ensure_equals("Can't import geometry from WKT", OGRERR_NONE, err_); ensure("Can't create geometry", NULL != g2_); g3_ = OGR_G_Union(g1_, g2_); ensure("OGR_G_Union failed with NULL", NULL != g3_); OGRGeometryH expect = NULL; char* wktExpect = "MULTIPOINT (10 20,30 20)"; err_ = OGR_G_CreateFromWkt(&wktExpect, NULL, &expect); ensure_equals("Can't import geometry from WKT", OGRERR_NONE, err_); ensure("Can't create geometry", NULL != expect); // Compare operation result against expected geometry ensure_equal_geometries(g3_, expect, 0.0001); OGR_G_DestroyGeometry(expect); }
void ensure_equal_geometries(OGRGeometryH lhs, OGRGeometryH rhs, double tolerance) { // Test raw pointers ensure("First geometry is NULL", NULL != lhs); ensure("Second geometry is NULL", NULL != rhs); ensure("Passed the same pointers to geometry", lhs != rhs); // Test basic properties ensure_equals("Geometry names do not match", std::string(OGR_G_GetGeometryName(lhs)), std::string(OGR_G_GetGeometryName(rhs))); ensure_equals("Sub-geometry counts do not match", OGR_G_GetGeometryCount(lhs), OGR_G_GetGeometryCount(rhs)); ensure_equals("Point counts do not match", OGR_G_GetPointCount(lhs), OGR_G_GetPointCount(rhs)); if (OGR_G_GetGeometryCount(lhs) > 0) { // Test sub-geometries recursively const int count = OGR_G_GetGeometryCount(lhs); for (int i = 0; i < count; ++i) { ensure_equal_geometries(OGR_G_GetGeometryRef(lhs, i), OGR_G_GetGeometryRef(rhs, i), tolerance); } } else { // Test geometry points const std::size_t csize = 3; double a[csize] = { 0 }; double b[csize] = { 0 }; double d[csize] = { 0 }; double dmax = 0; const int count = OGR_G_GetPointCount(lhs); for (int i = 0; i < count; ++i) { OGR_G_GetPoint(lhs, i, &a[0], &a[1], &a[2]); OGR_G_GetPoint(rhs, i, &b[0], &b[1], &b[2]); // Test vertices for (std::size_t c = 0; c < csize; ++c) { d[c] = std::fabs(a[c] - b[c]); } const double* pos = std::max_element(d, d + csize); dmax = *pos; std::ostringstream os; os << "Error in vertex " << i << " off by " << dmax; ensure(os.str(), dmax < tolerance); } } }
void object::test<5>() { // Original shapefile std::string orig(data_); orig += SEP; orig += "poly.shp"; OGRDataSourceH dsOrig = OGR_Dr_Open(drv_, orig.c_str(), false); ensure("Can't open layer", NULL != dsOrig); OGRLayerH lyrOrig = OGR_DS_GetLayer(dsOrig, 0); ensure("Can't get layer", NULL != lyrOrig); // Copied shapefile std::string tmp(data_tmp_); tmp += SEP; tmp += "tpoly.shp"; OGRDataSourceH dsTmp = OGR_Dr_Open(drv_, tmp.c_str(), false); ensure("Can't open layer", NULL != dsTmp); OGRLayerH lyrTmp = OGR_DS_GetLayer(dsTmp, 0); ensure("Can't get layer", NULL != lyrTmp); // Iterate through features and compare geometries OGRFeatureH featOrig = OGR_L_GetNextFeature(lyrOrig); OGRFeatureH featTmp = OGR_L_GetNextFeature(lyrTmp); while (NULL != featOrig && NULL != featTmp) { OGRGeometryH lhs = OGR_F_GetGeometryRef(featOrig); OGRGeometryH rhs = OGR_F_GetGeometryRef(featTmp); ensure_equal_geometries(lhs, rhs, 0.000000001); // TODO: add ensure_equal_attributes() OGR_F_Destroy(featOrig); OGR_F_Destroy(featTmp); // Move to next feature featOrig = OGR_L_GetNextFeature(lyrOrig); featTmp = OGR_L_GetNextFeature(lyrTmp); } OGR_DS_Destroy(dsOrig); OGR_DS_Destroy(dsTmp); }
void object::test<9>() { // Open directory as a datasource OGRDataSourceH ds = OGR_Dr_Open(drv_, data_tmp_ .c_str(), false); ensure("Can't open datasource", NULL != ds); std::string sql("select * from tpoly where prfedea = '35043413'"); OGRLayerH lyr = OGR_DS_ExecuteSQL(ds, sql.c_str(), NULL, NULL); ensure("Can't create layer from query", NULL != lyr); // Prepare tester collection std::vector<std::string> list; list.push_back("35043413"); // Test attributes ensure_equal_attributes(lyr, "prfedea", list); // Test geometry const char* wkt = "POLYGON ((479750.688 4764702.000,479658.594 4764670.000," "479640.094 4764721.000,479735.906 4764752.000," "479750.688 4764702.000))"; OGRGeometryH testGeom = NULL; OGRErr err = OGR_G_CreateFromWkt((char**) &wkt, NULL, &testGeom); ensure_equals("Can't create geometry from WKT", OGRERR_NONE, err); OGR_L_ResetReading(lyr); OGRFeatureH feat = OGR_L_GetNextFeature(lyr); ensure("Can't featch feature", NULL != feat); ensure_equal_geometries(OGR_F_GetGeometryRef(feat), testGeom, 0.001); OGR_F_Destroy(feat); OGR_G_DestroyGeometry(testGeom); OGR_DS_ReleaseResultSet(ds, lyr); OGR_DS_Destroy(ds); }