bool TeSQLite::insertPolygon(const string& table, TePolygon &p) { errorMessage_ = ""; std::string objectId = p.objectId(); char* wkb = 0; unsigned int wkbSize = 0; TeWKBGeometryDecoder::encodePolygon(p, wkb, wkbSize); std::string command = "INSERT INTO "; command += table; command += " (object_id, lower_x, lower_y, upper_x, upper_y, spatial_data)"; command += " VALUES (?1, ?2, ?3, ?4, ?5, ?6)"; sqlite3_stmt* recordSet = 0; int retValue = sqlite3_prepare_v2(_conn, command.c_str(), -1, &recordSet, 0); if(retValue != SQLITE_OK) { errorMessage_ = errorMessage(); return false; } sqlite3_bind_text(recordSet, 1, objectId.c_str(), objectId.size(), SQLITE_TRANSIENT); sqlite3_bind_double(recordSet, 2, p.box().x1()); sqlite3_bind_double(recordSet, 3, p.box().y1()); sqlite3_bind_double(recordSet, 4, p.box().x2()); sqlite3_bind_double(recordSet, 5, p.box().y2()); sqlite3_bind_blob(recordSet, 6, wkb, wkbSize, SQLITE_TRANSIENT); delete wkb; retValue = sqlite3_step(recordSet); if(retValue != SQLITE_DONE) { return false; } sqlite3_finalize(recordSet); //int newId = getMaxValue(this, table, "geom_id"); int newId = getLastInsertedSerial(); if(newId >= 0) { p.geomId(newId); } return true; }
bool TeSQLitePortal::fetchGeometry(TePolygon& geom, const unsigned int& initIndex) { errorMessage_ = ""; const char* bValue = (const char*) sqlite3_column_blob(_recordSet, initIndex + 6); unsigned int size = (unsigned int)sqlite3_column_bytes(_recordSet, initIndex + 6); TeWKBGeometryDecoder::decodePolygon(bValue, geom, size); geom.geomId(getInt(initIndex + 0)); geom.objectId(getData(initIndex + 1)); return fetchRow(); }
bool TeSQLite::updatePolygon(const string& table, TePolygon &p) { errorMessage_ = ""; std::string command = "UPDATE " + table + " SET "; command += "object_id=?"; command += ", lower_x=?, lower_y=?, upper_x=?, upper_y=?, "; command += ", spatial_data=? WHERE geom_id = " + Te2String(p.geomId()); std::string objectId = p.objectId(); char* wkb = 0; unsigned int wkbSize = 0; TeWKBGeometryDecoder::encodePolygon(p, wkb, wkbSize); sqlite3_stmt* recordSet = 0; int retValue = sqlite3_prepare_v2(_conn, command.c_str(), -1, &recordSet, 0); if(retValue != SQLITE_OK) { errorMessage_ = errorMessage(); return false; } sqlite3_bind_text(recordSet, 1, objectId.c_str(), objectId.size(), SQLITE_TRANSIENT); sqlite3_bind_double(recordSet, 2, p.box().x1()); sqlite3_bind_double(recordSet, 3, p.box().y1()); sqlite3_bind_double(recordSet, 4, p.box().x2()); sqlite3_bind_double(recordSet, 5, p.box().y2()); sqlite3_bind_blob(recordSet, 6, wkb, wkbSize, SQLITE_TRANSIENT); delete wkb; retValue = sqlite3_step(recordSet); if(retValue != SQLITE_DONE) { return false; } sqlite3_finalize(recordSet); return true; }
bool TeExportPolygonSet2SHP( const TePolygonSet& ps,const std::string& base_file_name ) { // creating files names std::string dbfFilename = base_file_name + ".dbf"; std::string shpFilename = base_file_name + ".shp"; // creating polygons attribute list ( max attribute size == 25 ) TeAttributeList attList; TeAttribute at; at.rep_.type_ = TeSTRING; //the id of the cell at.rep_.numChar_ = 25; at.rep_.name_ = "object_id_"; at.rep_.isPrimaryKey_ = true; attList.push_back(at); /* DBF output file handle creation */ DBFHandle hDBF = TeCreateDBFFile (dbfFilename, attList); if ( hDBF == 0 ) return false; /* SHP output file handle creation */ SHPHandle hSHP = SHPCreate( shpFilename.c_str(), SHPT_POLYGON ); if( hSHP == 0 ) { DBFClose( hDBF ); return false; } /* Writing polygons */ int iRecord = 0; int totpoints = 0; double *padfX, *padfY; SHPObject *psObject; int posXY, npoints, nelem; int nVertices; int* panParts; TePolygonSet::iterator itps; TePolygon poly; for (itps = ps.begin() ; itps != ps.end() ; itps++ ) { poly=(*itps); totpoints = 0; nVertices = poly.size(); for (unsigned int n=0; n<poly.size();n++) { totpoints += poly[n].size(); } panParts = (int *) malloc(sizeof(int) * nVertices); padfX = (double *) malloc(sizeof(double) * totpoints); padfY = (double *) malloc(sizeof(double) * totpoints); posXY = 0; nelem = 0; for (unsigned int l=0; l<poly.size(); ++l) { if (l==0) { if (TeOrientation(poly[l]) == TeCOUNTERCLOCKWISE) { TeReverseLine(poly[l]); } } else { if (TeOrientation(poly[l]) == TeCLOCKWISE) { TeReverseLine(poly[l]); } } npoints = poly[l].size(); panParts[nelem]=posXY; for (int m=0; m<npoints; m++ ) { padfX[posXY] = poly[l][m].x_; padfY[posXY] = poly[l][m].y_; posXY++; } nelem++; } psObject = SHPCreateObject( SHPT_POLYGON, -1, nelem, panParts, NULL, posXY, padfX, padfY, NULL, NULL ); int shpRes = SHPWriteObject( hSHP, -1, psObject ); if (shpRes == -1 ) { DBFClose( hDBF ); SHPClose( hSHP ); return false; } SHPDestroyObject( psObject ); free( panParts ); free( padfX ); free( padfY ); // writing attributes - same creation order DBFWriteStringAttribute(hDBF, iRecord, 0, poly.objectId().c_str() ); iRecord++; } DBFClose( hDBF ); SHPClose( hSHP ); return true; }