// // Copyright (C) 2007 Refractions Research, Inc. // // This library is free software; you can redistribute it and/or // modify it under the terms of version 2.1 of the GNU Lesser // General Public License as published by the Free Software Foundation. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // #include #include #include #include #include #include #include "fdoutility.h" using namespace fdoutility; using namespace std; int main(int argc, char* argv[]) { try { std::wstring provider(L"OSGeo.PostGIS.3.2"); // Connection string with datastore specified std::wstring ds(L"public"); std::wstring connStr(L"service=test@localhost;username=mloskot;password=pantera;"); // Connect FdoPtr conn = fdoutility::create_connection(provider); connStr += L"datastore=" + ds; conn->SetConnectionString(connStr.c_str()); FdoConnectionState state = conn->Open(); cout << "Connection state: " << state << endl; // FDO Developer’s Guide, Inserting Values, Page 92 // // 1. Create the insert command object (type FdoIInsert); this object can be // reused for multiple insert operations. FdoPtr cmd; cmd = static_cast(conn->CreateCommand(FdoCommandType_Insert)); assert(0 != cmd); // 2. Point the insert command object at the feature class to which you are adding values // FdoPostGIS is the only schema available when using PostGIS provider, so you can // a) use it cmd->SetFeatureClassName(L"FdoPostGIS:public~buildings"); // b) or omit it in class name //cmd->SetFeatureClassName(L"FdoPostGIS:public~Buildings"); // 3. From the insert command object, obtain a pointer to a value collection object. // You will add property values to the insert command object by adding values to the collection object. FdoPtr values = cmd->GetPropertyValues(); FdoInt32 valueIndex = 0; // 4. Create a data value or geometry value object. FdoPtr dataValueId = FdoDataValue::Create(1); FdoPtr propValueId; // 5. Create a property value object, which involves passing the data value or // geometry value object as an argument to a static Create() method. propValueId = FdoPropertyValue::Create(L"AttrId", dataValueId); // 6. Add the property value object to the value collection object. valueIndex = values->Add(propValueId); // 4-6 steps repeat for next value FdoPtr dataValueName = FdoDataValue::Create(L"Green Point"); FdoPtr propValueName; propValueName = FdoPropertyValue::Create(L"AttrName", dataValueName); valueIndex = values->Add(propValueName); // 4-6 steps repeat for geometry property FdoPtr gf(FdoFgfGeometryFactory::GetInstance()); assert(NULL != gf); double coords[] = { 16.63, 53.02 }; FdoPtr point; point = gf->CreatePoint(FdoDimensionality_XY, coords); FdoPtr geomValue = NULL; geomValue = FdoPtr(FdoGeometryValue::Create(FdoPtr(gf->GetFgf(point)))); FdoPtr propValueGeom; propValueGeom = FdoPropertyValue::Create(L"samplegeometry", geomValue); valueIndex = values->Add(propValueGeom); // 7. Execute the Insert command // The Insert command returns an FdoIFeatureReader. FdoPtr reader = cmd->Execute(); conn->Close(); return 0; } catch (FdoException* ex) { std::wcout << L"*** FDO Error:\n"; int i = 5; FdoException* nex = ex; while (NULL != nex) { const wchar_t* msg = nex->GetExceptionMessage(); if (NULL == msg) { msg = L"NO MESSAGE"; } std::wcout << std::setw(++i) << L"*** " << msg << std::endl; FdoException* p = nex; nex = nex->GetCause(); p->Release(); } } catch(std::exception& ex) { std::cout << "*** Std Error: " << ex.what() << std::endl; } catch(...) { std::cout << "*** Undefined error!\n" << std::endl; } return 0; }