// // Simple test of FDO GeometryFactory with valid and invalid WKB input // Mateusz Loskot // // Building depends on the following components: // - INCLUDES: trunk/Fdo/Unmanaged/Inc // - LIBS: FDO.lib FDOCommon.lib FDOGeometry.lib // #include #include #include #include #include #include #include #include void hex_to_bytes(std::string const& hexstr, std::vector& bytes) { bytes.clear(); for(std::string::size_type i = 0; i < hexstr.size() / 2; ++i) { std::istringstream iss(hexstr.substr(i * 2, 2)); unsigned int n; iss >> std::hex >> n; bytes.push_back(static_cast(n)); } } void make_geometry_from_wkb(std::string const& inputWkt, std::string const& inputHex) { // Convert hex encoded binary to stream of raw bytes std::vector bytes; hex_to_bytes(inputHex, bytes); assert(!bytes.empty()); // Convert vector of bytes to FDO array FdoPtr fdoBytes( FdoByteArray::Create(&bytes[0], static_cast(bytes.size()))); assert(NULL != fdoBytes); assert(fdoBytes->GetCount() > 0); // Get geometry factory FdoPtr gf(FdoFgfGeometryFactory::GetInstance()); assert(NULL != gf); // Create geometry FdoPtr fdoGeom(gf->CreateGeometryFromWkb(fdoBytes)); assert(NULL != fdoGeom); // Get WKT representation FdoStringP outputWkt(fdoGeom->GetText()); std::cout << "\nExpected WKT: " << inputWkt << "\nOutput WKT : " << static_cast(outputWkt) << std::endl; } int main(int argc, char* argv[]) { try { // Expected geometry in WKT form char const* wkt = "POINT(15.123 21.456)"; // 1. Incorrect WKB makes valid geometry with no error thrown // NOTE: Minimum length of WKB is 9 bytes, otherwise exception is thrown char const* wkb1 = "0101000000e5d022db"; make_geometry_from_wkb(wkt, wkb1); // 2. Valid WKB makes valid geometry char const* wkb2 = "0101000000e5d022dbf93e2e40dbf97e6abc743540"; make_geometry_from_wkb(wkt, wkb2); // 3. Invalid WKB (less than 9 bytes), exception thrown char const* wkb3 = "0101"; make_geometry_from_wkb(wkt, wkb3); } catch (FdoException* ex) { std::wcout << L"*** FDO Error:\n"; int i = 5; FdoException* nex = ex; while (nex) { const wchar_t* msg = nex->GetExceptionMessage(); if (NULL == msg) { msg = L"NO MESSAGE"; } std::wcout << std::setw(++i) << L"*** " << msg << std::endl; nex = nex->GetCause(); } ex->Release(); } catch(std::exception& ex) { std::cout << "*** Std error: " << ex.what() << std::endl; } catch(...) { std::cout << "*** Undefined error!\n" << std::endl; } return 0; }