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); }
void object::test<10>() { OGRErr err = OGRERR_NONE; // Read feature without geometry std::string tmp(data_tmp_); tmp += SEP; tmp += "tpoly.shp"; OGRDataSourceH ds = OGR_Dr_Open(drv_, tmp.c_str(), false); ensure("Can't open layer", NULL != ds); OGRLayerH lyr = OGR_DS_GetLayer(ds, 0); ensure("Can't get layer", NULL != lyr); // Set empty filter for attributes err = OGR_L_SetAttributeFilter(lyr, NULL); ensure_equals("Can't set attribute filter", OGRERR_NONE, err); // Set spatial filter const char* wkt = "LINESTRING(479505 4763195,480526 4762819)"; OGRGeometryH filterGeom = NULL; err = OGR_G_CreateFromWkt((char**) &wkt, NULL, &filterGeom); ensure_equals("Can't create geometry from WKT", OGRERR_NONE, err); OGR_L_SetSpatialFilter(lyr, filterGeom); // Prepare tester collection std::vector<int> list; list.push_back(158); list.push_back(0); // Test attributes ensure_equal_attributes(lyr, "eas_id", list); OGR_G_DestroyGeometry(filterGeom); OGR_DS_Destroy(ds); }
void object::test<3>() { OGRErr err = OGRERR_NONE; OGRDataSourceH ds = NULL; ds = OGR_Dr_CreateDataSource(drv_, data_tmp_.c_str(), NULL); ensure("Can't open or create data source", NULL != ds); // Create memory Layer OGRLayerH lyr = NULL; lyr = OGR_DS_CreateLayer(ds, "tpoly", NULL, wkbPolygon, NULL); ensure("Can't create layer", NULL != lyr); // Create schema OGRFieldDefnH fld = NULL; fld = OGR_Fld_Create("AREA", OFTReal); err = OGR_L_CreateField(lyr, fld, true); OGR_Fld_Destroy(fld); ensure_equals("Can't create field", OGRERR_NONE, err); fld = OGR_Fld_Create("EAS_ID", OFTInteger); err = OGR_L_CreateField(lyr, fld, true); OGR_Fld_Destroy(fld); ensure_equals("Can't create field", OGRERR_NONE, err); fld = OGR_Fld_Create("PRFEDEA", OFTString); err = OGR_L_CreateField(lyr, fld, true); OGR_Fld_Destroy(fld); ensure_equals("Can't create field", OGRERR_NONE, err); // Check schema OGRFeatureDefnH featDefn = OGR_L_GetLayerDefn(lyr); ensure("Layer schema is NULL", NULL != featDefn); ensure_equals("Fields creation failed", 3, OGR_FD_GetFieldCount(featDefn)); // Copy ogr/poly.shp to temporary layer OGRFeatureH featDst = OGR_F_Create(featDefn); ensure("Can't create empty feature", NULL != featDst); std::string source(data_); source += SEP; source += "poly.shp"; OGRDataSourceH dsSrc = OGR_Dr_Open(drv_, source.c_str(), false); ensure("Can't open source layer", NULL != dsSrc); OGRLayerH lyrSrc = OGR_DS_GetLayer(dsSrc, 0); ensure("Can't get source layer", NULL != lyrSrc); OGRFeatureH featSrc = NULL; while (NULL != (featSrc = OGR_L_GetNextFeature(lyrSrc))) { err = OGR_F_SetFrom(featDst, featSrc, true); ensure_equals("Can't set festure from source", OGRERR_NONE, err); err = OGR_L_CreateFeature(lyr, featDst); ensure_equals("Can't write feature to layer", OGRERR_NONE, err); OGR_F_Destroy(featSrc); } // Release and close resources OGR_F_Destroy(featDst); OGR_DS_Destroy(dsSrc); OGR_DS_Destroy(ds); }
s_sat * sat_aoi(s_sat * sat, const char * shp_aoi_fname, const char * shp_forest_fname) { try; OGRDataSourceH a_ds = NULL, f_ds = NULL; OGRLayerH a_layer = NULL, f_layer = NULL; OGRGeometryH a_geometry = NULL, fa_geometry = NULL, union_geometry = NULL, intersection_geometry = NULL, simplify_geometry = NULL; OGRFeatureH next_feature = NULL; s_sat * fa_img = NULL; throw_null((a_ds = OGR_Dr_Open(drv_shp, shp_aoi_fname, FALSE))); throw_null((f_ds = OGR_Dr_Open(drv_shp, shp_forest_fname, FALSE))); throw_null((a_layer = OGR_DS_GetLayer(a_ds, 0))); throw_null((f_layer = OGR_DS_GetLayer(f_ds, 0))); throw_null((a_geometry = OGR_G_CreateGeometry(wkbPolygon))); throw_null((fa_geometry = OGR_G_CreateGeometry(wkbPolygon))); OGR_L_ResetReading(a_layer); while((next_feature = OGR_L_GetNextFeature(a_layer)) != NULL) { throw_null((union_geometry = OGR_G_Union(a_geometry, OGR_F_GetGeometryRef(next_feature)))); OGR_G_DestroyGeometry(a_geometry); a_geometry = union_geometry; union_geometry = NULL; } OGR_L_SetSpatialFilter(f_layer, a_geometry); OGR_L_ResetReading(f_layer); while((next_feature = OGR_L_GetNextFeature(f_layer)) != NULL) { throw_null((intersection_geometry = OGR_G_Intersection(a_geometry, OGR_F_GetGeometryRef(next_feature)))); throw_null((union_geometry = OGR_G_Union(fa_geometry, intersection_geometry))); OGR_G_DestroyGeometry(intersection_geometry); OGR_G_DestroyGeometry(fa_geometry); fa_geometry = union_geometry; union_geometry = intersection_geometry = NULL; } throw_null((simplify_geometry = OGR_G_Simplify(fa_geometry, 0))); OGR_G_DestroyGeometry(fa_geometry); fa_geometry = simplify_geometry; simplify_geometry = NULL; throw_null((fa_img = sat_rasterize_copy(sat, fa_geometry))); catch; sat_destroy(fa_img); fa_img = NULL; finally; if(next_feature != NULL) OGR_F_Destroy(next_feature); if(a_geometry != NULL) OGR_G_DestroyGeometry(a_geometry); if(fa_geometry != NULL) OGR_G_DestroyGeometry(fa_geometry); if(union_geometry != NULL) OGR_G_DestroyGeometry(union_geometry); if(intersection_geometry != NULL) OGR_G_DestroyGeometry(intersection_geometry); if(simplify_geometry != NULL) OGR_G_DestroyGeometry(simplify_geometry); if(a_ds != NULL) OGRReleaseDataSource(a_ds); if(f_ds != NULL) OGRReleaseDataSource(f_ds); return fa_img; }