// $Id: RwPointCoordFile.cpp,v 1.2 2010/02/01 09:45:17 philw Exp $ // RiverWare Coordinate Pair File // // The following data is maintained for each Object Record: // (1) Object Name String // (2) Object Type String // (3) X coordinate (double) // (4) Y coordinate (double) // // The initial implementation includes ESRI ShapeFile I/O, with these // three ShapeFile file parts: // // XXX.shp - holds the actual vertices. // XXX.shx - holds index data pointing to the structures in the .shp file. // XXX.dbf - holds the attributes in xBase (dBase) format. // // The SHP file contains 2D Point Data (Shape type: SHPT_POINT). // The DBF file contains: the Object Name String and Object Type String. // // The ShapeFile interface makes use of these three files from the // Shapefile C Library V1.2 (http://shapelib.maptools.org/) // // shpopen.c: ANSI C code for access to .shp/.shx vertex files. // dbfopen.c: ANSI C code for access to .dbf attribute file. // shapefil.h: Include file for all services of dbfopen.c and shpopen.c. // // Initial Application: // RiverWare 5.3 (February 2010) Georeferencing Simulation Objects // //--- #ifndef RwPointCoordFileINCLUDED #include "RwPointCoordFile.hpp" #endif // Shapefile C Library V1.2 (http://shapelib.maptools.org/) #include // constructor 1 of 1 RwPointCoordFile::RwPointCoordFile() : _objRecs() // QList { } RwPointCoordFile::~RwPointCoordFile() { } void RwPointCoordFile::clearObjRecs() { _objRecs.clear(); } void RwPointCoordFile::addObjRec (const RwPointCoordFile::Rec& newRec) { _objRecs.append (newRec); } okstat RwPointCoordFile::writeShapeFile (const QString& filePath) { static const char (*mname) ("RwPointCoordFile::writeShapeFile"); //------------------------------------ // Write Records: QList _objRecs; //------------------------------------ // Create new POINT ShapeFile SHPHandle shapeFileHandle = SHPCreate (filePath.ascii(), SHPT_POINT); std::cout << mname << " shapeFileHandle.fpSHP " << std::hex << ((long) shapeFileHandle->fpSHP) << std::dec << std::endl; std::cout << mname << " shapeFileHandle.fpSHX " << std::hex << ((long) shapeFileHandle->fpSHX) << std::dec << std::endl; // Create Attribute DBF File DBFHandle xBaseFileHandle = DBFCreate (filePath.ascii()); std::cout << mname << " xBaseFileHandle.fp " << std::hex << ((long) xBaseFileHandle->fp) << std::dec << std::endl; // Create Attribute DBF Columns ("Fields") static int nWidth (60); // maximum character count for strings static int nDecimals (0); // number of fractional decimal digits const int objNameFieldNum = DBFAddField (xBaseFileHandle, "ObjName", FTString, nWidth, nDecimals); const int objTypeFieldNum = DBFAddField (xBaseFileHandle, "ObjType", FTString, nWidth, nDecimals); std::cout << mname << " objNameField: " << objNameFieldNum << std::endl; std::cout << mname << " objTypeField: " << objTypeFieldNum << std::endl; // Maintain a list of dynamically created SHPObjects. // These need to be destroyed after use. QList deleteShapeObjectList; const int cnt = _objRecs.count(); for (int inx = 0; inx < cnt; ++inx) { // (technically const, but 'mutable' is needed). Rec rec = _objRecs [inx]; std::cout << mname << " [" << inx << "]" << " '" << qPrintable (rec._objName) << "'" << " '" << qPrintable (rec._objType) << "'" << " (" << rec._xCoord << "," << rec._yCoord << ")" << std::endl; static const int nVertices (1); SHPObject* shapeObj = // Needs to be Destroyed after use. SHPCreateSimpleObject (SHPT_POINT, nVertices, &rec._xCoord, &rec._yCoord, NULL); std::cout << mname << " [" << inx << "]" << " shapeObj: " << std::hex << ((long) shapeObj) << std::dec << std::endl; // Save for subsequent destruction deleteShapeObjectList.append (shapeObj); // Write Shape File const int iShape = SHPWriteObject (shapeFileHandle, -1, shapeObj); std::cout << mname << " [" << inx << "]" << " iShape: " << iShape << std::endl; // Write DBF Attribute File DBFWriteStringAttribute (xBaseFileHandle, iShape, objNameFieldNum, rec._objName.ascii()); DBFWriteStringAttribute (xBaseFileHandle, iShape, objTypeFieldNum, rec._objType.ascii()); } // Close Shape and DBF Attribute Files SHPClose (shapeFileHandle); DBFClose (xBaseFileHandle); const int delCnt = deleteShapeObjectList.count(); for (int delInx = 0; delInx < delCnt; ++delInx) { SHPObject* delShpObj = deleteShapeObjectList [delInx]; SHPDestroyObject (delShpObj); } deleteShapeObjectList.clear(); return okstat (true); } okstat RwPointCoordFile::readShapeFile (const QString& /*filePath*/) { return okstat ("Not Implemented"); } //--- (end RwPointCoordFile.cpp) ---