Shapefile format
[Geographical data formats]


Detailed Description

Uses some functions related to DBF attribute table format.


Modules

 decode
 Auxiliary functions to decode one shape using shapelib.

Functions

SHP_DLL DBFHandle TeCreateDBFFile (const string &dbfFilename, TeAttributeList &attList)
SHP_DLL bool TeExportLayerToShapefile (TeLayer *layer, const string &baseName="")
 Exports a layer in a TerraLib database to a shapefile.
SHP_DLL bool TeExportPolygonSet2SHP (const TePolygonSet &ps, const string &base_file_name)
 Exports a TerraLib polygon set to a shapefile Each polygon will be a shape.
SHP_DLL bool TeExportQuerierToShapefile (TeQuerier *querier, const std::string &baseName)
 Exports a querier to a shapefile.
SHP_DLL bool TeExportShapefile (TeLayer *layer, const string &shpFileName, const string &tableName, const string &restriction="")
 Exports a layer in a TerraLib database to a file in MID/MIF format.
SHP_DLL bool TeExportThemeToShapefile (TeTheme *theme, TeSelectedObjects selOb=TeAll, const std::string &baseName="", const std::vector< std::string > attributes=std::vector< std::string >())
 Exports a theme in a TerraLib database to a shapefile.
SHP_DLL bool TeGetSHPInfo (std::string shpfileName, unsigned int &nShapes, TeBox &box, TeGeomRep &rep)
 Get the metadata information of a shapefile.
SHP_DLL bool TeImportShape (TeLayer *layer, const string &shpFileName, string attrTableName="", string objectIdAttr="", unsigned int chunkSize=60, const bool &useTransaction=true)
 Imports a shapefile to a layer TerraLib.
SHP_DLL TeLayerTeImportShape (const string &shpFileName, TeDatabase *db, const string &layerName="")
 Imports a geo data in a shapefile format to a TerraLib database.
SHP_DLL bool TeSTOSetBuildSHP (TeSTElementSet &stoset, const string &fileName)
 Builds the spatial object set from database according to the restrictions previously defined.
SHP_DLL void TeWriteDataProjectionToFile (TeProjection *projection, const std::string &fileName)


Function Documentation

SHP_DLL DBFHandle TeCreateDBFFile ( const string dbfFilename,
TeAttributeList attList 
)

Definition at line 1128 of file TeDriverSHPDBF.cpp.

References DBFAddField(), DBFCreate(), FTDate, FTDouble, FTInteger, FTLogical, FTString, TeAttributeRep::name_, TeAttributeRep::numChar_, TeAttribute::rep_, Te2String(), TeBOOLEAN, TeCHARACTER, TeDATETIME, TeINT, TeREAL, TeReplaceSpecialChars(), TeSTRING, and TeAttributeRep::type_.

01129 {
01130         DBFHandle hDBF = DBFCreate( dbfFilename.c_str() );
01131         if( hDBF == 0 )
01132                 return 0;
01133         
01134         int i =0;
01135         TeAttributeList::iterator it=attList.begin();
01136 
01137         std::set<std::string> setNames;
01138         while ( it != attList.end() )
01139         {
01140                 TeAttribute at = (*it);
01141                 string atName = at.rep_.name_;
01142                 TeReplaceSpecialChars(atName);
01143 
01144                 if (atName.size() > 10)
01145                 {
01146                         int extra = (int)(atName.size() - 10)/2;
01147                         int middle = (int)(atName.size()/2);
01148                         string str = atName.substr(0,middle-extra-1);
01149                         str += atName.substr(middle+extra);
01150                         atName = str;
01151                         unsigned int count = 0;
01152                         while(setNames.find(atName) != setNames.end())
01153                         {
01154                                 std::string strNum = Te2String(count);
01155                                 if(strNum.length() == 1)
01156                                 {
01157                                         atName[atName.size()-1] = strNum[0];
01158                                 }
01159                                 else
01160                                 {
01161                                         atName[atName.size()-2] = strNum[0];
01162                                         atName[atName.size()-1] = strNum[1];
01163                                 }
01164 
01165                                 ++count;
01166                         }
01167                 }
01168 
01169                 setNames.insert(atName);
01170 
01171                 basic_string <char>::size_type  index;
01172                 if((index=atName.find(".")) !=string::npos)
01173                 {
01174                         atName.replace(index,1,"_");
01175                 }
01176 
01177                 if (at.rep_.type_ == TeSTRING )
01178                 {
01179                         int numChar = at.rep_.numChar_;
01180                         if(numChar == 0)
01181                         {
01182                                 numChar = 255;
01183                         }
01184                         if (DBFAddField( hDBF, atName.c_str(), FTString, numChar, 0 ) == -1 )
01185                                 return 0;
01186                 }
01187                 else if (at.rep_.type_ == TeINT)
01188                 {
01189                         if (DBFAddField( hDBF, atName.c_str(), FTInteger, 32, 0 ) == -1 )
01190                                 return 0;
01191                 }
01192                 else if (at.rep_.type_ == TeREAL)
01193                 {
01194                         if (DBFAddField( hDBF, atName.c_str(), FTDouble, 40, 15 ) == -1 )
01195                                 return 0;
01196                 }
01197                 // OBS: shapelib doesnt deal with xBase field type for Date 
01198                 // we are transforming it to string
01199                 else if (at.rep_.type_ == TeDATETIME)
01200                 {
01201                         if (DBFAddField( hDBF, atName.c_str(), FTDate, 8, 0 ) == -1 )
01202                                 return 0;
01203                 }
01204                 else if(at.rep_.type_ == TeCHARACTER)
01205                 {
01206                         if (DBFAddField( hDBF, atName.c_str(), FTString, 1, 0 ) == -1 )
01207                                 return 0;
01208                 }
01209                 else if(at.rep_.type_ == TeBOOLEAN)
01210                 {
01211                         if (DBFAddField( hDBF, atName.c_str(), FTLogical, 32, 0 ) == -1 )
01212                                 return 0;
01213                 }
01214                 else
01215                 {
01216                         int a = 0;
01217                 }
01218                 ++i;
01219                 ++it;
01220         }
01221         return hDBF;
01222 }

SHP_DLL bool TeExportLayerToShapefile ( TeLayer layer,
const string baseName = "" 
)

Parameters:
layer pointer to the layer
baseName name of the output shapefile. If empty, the file will have the same name as the Layer.
Returns:
TRUE if the data was successfully exported and FALSE otherwise

Definition at line 1777 of file TeDriverSHPDBF.cpp.

References TeLayer::attrTables(), TeTheme::collectionAuxTable(), TeTheme::collectionTable(), TeTheme::layer(), TeLayer::name(), TeTheme::setAttTables(), TeQuerierParams::setParams(), TeExportQuerierToShapefile(), and TeTheme::TeTheme().

01778 {
01779         if (!layer)
01780                 return false;
01781 
01782         string fbase = baseName;
01783         if (baseName.empty())
01784                 fbase = layer->name();
01785 
01786         TeTheme* tempTheme = new TeTheme();
01787         tempTheme->layer(layer);
01788         tempTheme->collectionTable("");
01789         tempTheme->collectionAuxTable("");
01790         tempTheme->setAttTables(layer->attrTables());
01791 
01792         TeQuerierParams qPar(true, true);
01793         qPar.setParams(tempTheme);
01794 
01795         TeQuerier* tQuerier = new TeQuerier(qPar);
01796         bool res = TeExportQuerierToShapefile(tQuerier, baseName);
01797         delete tQuerier;
01798         delete tempTheme;
01799 
01800         return res ;
01801 }

SHP_DLL bool TeExportPolygonSet2SHP ( const TePolygonSet ps,
const string base_file_name 
)

Only one attribute will be created: the object id of the polygon.

Parameters:
ps the polygon set
base_file_name the base file name used to build the shapefile
Returns:
TRUE if the data was successfully exported and FALSE otherwise

SHP_DLL bool TeExportQuerierToShapefile ( TeQuerier querier,
const std::string baseName 
)

Parameters:
querier pointer to a valid querier
baseName name of the output shapefile
Returns:
TRUE if the data was successfully exported and FALSE otherwise

Definition at line 1329 of file TeDriverSHPDBF.cpp.

References TeGeomComposite< T >::begin(), DBFClose(), DBFWriteDoubleAttribute(), DBFWriteIntegerAttribute(), DBFWriteStringAttribute(), TeGeomComposite< T >::end(), TeQuerier::fetchInstance(), TeQuerier::getAttrList(), TeSTInstance::getCells(), TeSTInstance::getLines(), TeSTInstance::getPoints(), TeSTInstance::getPolygons(), TeBaseSTInstance< GeometryType, TimeType >::getPropertyValue(), TeSTInstance::hasCells(), TeSTInstance::hasLines(), TeSTInstance::hasPoints(), TeSTInstance::hasPolygons(), TeProgress::instance(), TeAttributeRep::isPrimaryKey_, TeTheme::layer(), TeQuerierParams::layer(), TeQuerier::loadInstances(), TeAttributeRep::name_, NULL, TeAttributeRep::numChar_, TeQuerier::numElemInstances(), TeBaseSTInstance< GeometryType, TimeType >::objectId(), TeQuerier::params(), TeLayer::projection(), TeAttribute::rep_, TeProgressBase::reset(), TeProgressBase::setProgress(), TeProgressBase::setTotalSteps(), SHPClose(), SHPCreate(), SHPCreateObject(), SHPDestroyObject(), SHPT_ARC, SHPT_POINT, SHPT_POLYGON, SHPWriteObject(), TeGeomComposite< T >::size(), TeComposite< T >::size(), TeCLOCKWISE, TeCOUNTERCLOCKWISE, TeCreateDBFFile(), TeDATETIME, TeGetName(), TeINT, TeOrientation(), TeREAL, TeReverseLine(), TeSTRING, TeWriteDataProjectionToFile(), TeQuerierParams::theme(), and TeAttributeRep::type_.

01330 {
01331         // check initial conditions
01332         if (!querier)
01333                 return false;
01334 
01335         if (!querier->loadInstances())
01336                 return false;
01337 
01338         // Get the list of attributes defined by the input querier
01339         bool onlyObjId = false;
01340         TeAttributeList qAttList = querier->getAttrList();
01341         if (qAttList.empty())
01342         {
01343                 TeAttributeList qAttList;
01344                 TeAttribute at;
01345                 at.rep_.type_ = TeSTRING;               
01346                 at.rep_.numChar_ = 100;
01347                 at.rep_.name_ = "ID";
01348                 at.rep_.isPrimaryKey_ = true;
01349                 qAttList.push_back(at);
01350                 onlyObjId = true;
01351         }
01352 
01353         // Handles to each type of geometries that will be created if necessary
01354         DBFHandle hDBFPol = 0;
01355         SHPHandle hSHPPol = 0;
01356         DBFHandle hDBFLin = 0;
01357         SHPHandle hSHPLin = 0;
01358         DBFHandle hDBFPt = 0;
01359         SHPHandle hSHPPt = 0;
01360 
01361         // Some auxiliary variables
01362     int totpoints;
01363     double*     padfX;
01364         double* padfY;
01365     unsigned int nVertices;
01366     int* panPart;
01367     SHPObject *psObject;
01368     unsigned int posXY, npoints, nelem;
01369         int shpRes;
01370 
01371         // progress information
01372         if (TeProgress::instance())
01373                 TeProgress::instance()->setTotalSteps(querier->numElemInstances());
01374         clock_t t0, t1, t2;
01375         t2 = clock();
01376         t0 = t1 = t2;
01377         int dt = CLOCKS_PER_SEC/2;
01378         int dt2 = CLOCKS_PER_SEC; //* .000001;
01379 
01380         TeProjection* outputProj = 0;
01381         if(querier->params().layer())
01382         {
01383                 outputProj = querier->params().layer()->projection();
01384         }
01385         else
01386         {
01387                 outputProj = querier->params().theme()->layer()->projection();
01388         }
01389 
01390         // Loop through the instances writting their geometries and attributes
01391         unsigned int iRecPol=0, iRecLin=0, iRecPt=0;
01392         unsigned int n, l, m;
01393         unsigned int nIProcessed = 0;
01394         TeSTInstance st;
01395         while(querier->fetchInstance(st))
01396         {
01397                 totpoints = 0;
01398                 nVertices = 0;
01399                 if (st.hasPolygons())
01400                 {
01401                         TePolygonSet& polSet = st.getPolygons();
01402                         TePolygonSet::iterator itps;
01403                         int nVerticesCount = 0;
01404                         for (itps = polSet.begin(); itps != polSet.end(); ++itps) 
01405                         {
01406                                 nVertices = (*itps).size();
01407                                 nVerticesCount += nVertices;
01408                                 for (n=0; n<nVertices;++n) 
01409                                         totpoints += (*itps)[n].size();
01410                         }
01411 
01412                         panPart = (int *) malloc(sizeof(int) * nVerticesCount);
01413                         padfX = (double *) malloc(sizeof(double) * totpoints);
01414                         padfY = (double *) malloc(sizeof(double) * totpoints);
01415 
01416                         posXY = 0;
01417                         nelem = 0;
01418                         for (itps = polSet.begin(); itps != polSet.end(); ++itps) 
01419                         {
01420                                 TePolygon poly = *itps;
01421                                 for (l=0; l<poly.size(); ++l) 
01422                                 {
01423                                         if (l==0) 
01424                                         {
01425                                                 if (TeOrientation(poly[l]) == TeCOUNTERCLOCKWISE) 
01426                                                         TeReverseLine(poly[l]);
01427                                         }
01428                                         else 
01429                                         {
01430                                                 if (TeOrientation(poly[l]) == TeCLOCKWISE)
01431                                                         TeReverseLine(poly[l]);
01432                                         }
01433                                         npoints = poly[l].size();
01434                                         panPart[nelem]=posXY;
01435         
01436                                         for (m=0; m<npoints; ++m) 
01437                                         {
01438                                                 padfX[posXY] = poly[l][m].x_;
01439                                                 padfY[posXY] = poly[l][m].y_;
01440                                                 posXY++;
01441                                         }
01442                                         nelem++;
01443                                 }
01444                         }
01445                         psObject = SHPCreateObject(SHPT_POLYGON, -1, nelem, panPart, NULL, posXY, padfX, padfY, NULL, NULL);
01446                         if (hSHPPol == 0)
01447                         {                               
01448                                 string fname = base + "_pol.shp";
01449                                 hSHPPol = SHPCreate(fname.c_str(), SHPT_POLYGON);
01450                                 assert (hSHPPol != 0);
01451                                 
01452                                 TeWriteDataProjectionToFile(outputProj, TeGetName(fname.c_str()));
01453                         }       
01454                         shpRes = SHPWriteObject(hSHPPol, -1, psObject);
01455                         SHPDestroyObject(psObject);
01456                         free(panPart);
01457                         free(padfX);
01458                         free(padfY);
01459                         assert(shpRes != -1);   
01460                         if (hDBFPol == 0)
01461                         {
01462                                 hDBFPol = TeCreateDBFFile(base + "_pol.dbf", qAttList);
01463                                 if(hDBFPol == 0)
01464                                 {
01465                                         return false;
01466                                 }
01467                                 assert (hDBFPol != 0);
01468                         }
01469                         if (onlyObjId)
01470                         {
01471                                 DBFWriteStringAttribute(hDBFPol, iRecPol, 0, st.objectId().c_str());
01472                         }
01473                         else
01474                         {
01475                                 string val;
01476                                 for (n=0; n<qAttList.size();++n) 
01477                                 {
01478                                         st.getPropertyValue(val,n);
01479                                         if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME)
01480                                         {
01481                                                 DBFWriteStringAttribute(hDBFPol, iRecPol, n, val.c_str());
01482                                         }
01483                                         else if (qAttList[n].rep_.type_ == TeINT)
01484                                         {
01485                                                 DBFWriteIntegerAttribute(hDBFPol, iRecPol, n, atoi(val.c_str()));
01486                                         }
01487                                         else if (qAttList[n].rep_.type_ == TeREAL)
01488                                         {
01489                                                 DBFWriteDoubleAttribute(hDBFPol, iRecPol, n, atof(val.c_str()));
01490                                         }
01491                                 }
01492                         }
01493                         ++iRecPol;
01494                 }
01495                 if (st.hasCells())
01496                 {
01497                         TeCellSet& cellSet = st.getCells();
01498                         nVertices = cellSet.size();
01499                         totpoints = nVertices*5;
01500                         panPart = (int *) malloc(sizeof(int) * nVertices);
01501                         padfX = (double *) malloc(sizeof(double) * totpoints);
01502                         padfY = (double *) malloc(sizeof(double) * totpoints);
01503                         posXY = 0;
01504                         nelem = 0;
01505                         TeCellSet::iterator itcs;
01506                         for (itcs=cellSet.begin(); itcs!=cellSet.end(); ++itcs)
01507                         {
01508                                 panPart[nelem]=posXY;
01509                                 padfX[posXY] = (*itcs).box().lowerLeft().x();
01510                                 padfY[posXY] = (*itcs).box().lowerLeft().y();
01511                                 posXY++;                
01512                                 padfX[posXY] = (*itcs).box().upperRight().x();
01513                                 padfY[posXY] = (*itcs).box().lowerLeft().y();
01514                                 posXY++;                
01515                                 padfX[posXY] = (*itcs).box().upperRight().x();
01516                                 padfY[posXY] = (*itcs).box().upperRight().y();
01517                                 posXY++;                
01518                                 padfX[posXY] = (*itcs).box().lowerLeft().x();
01519                                 padfY[posXY] = (*itcs).box().upperRight().y();
01520                                 posXY++;                
01521                                 padfX[posXY] = (*itcs).box().lowerLeft().x();
01522                                 padfY[posXY] = (*itcs).box().lowerLeft().y();
01523                                 ++posXY;        
01524                                 ++nelem;
01525                         } 
01526                         psObject = SHPCreateObject(SHPT_POLYGON, -1, nelem, panPart, NULL,posXY, padfX, padfY, NULL, NULL);
01527                         if (hSHPPol == 0)
01528                         {
01529                                 string fname = base + "_pol.shp";
01530                                 hSHPPol = SHPCreate(fname.c_str(), SHPT_POLYGON);
01531                                 assert (hSHPPol != 0);
01532 
01533                                 TeWriteDataProjectionToFile(outputProj, TeGetName(fname.c_str()));
01534                         }       
01535                         shpRes = SHPWriteObject(hSHPPol, -1, psObject);
01536                         SHPDestroyObject(psObject);
01537                         free(panPart);
01538                         free(padfX);
01539                         free(padfY);
01540                         assert(shpRes != -1);
01541                         if (hDBFPol == 0)
01542                         {
01543                                 hDBFPol = TeCreateDBFFile(base + "_pol.dbf", qAttList);
01544                                 assert (hDBFPol != 0);
01545                         }
01546 
01547                         if (onlyObjId)
01548                         {
01549                                 DBFWriteStringAttribute(hDBFPol, iRecPol, 0, st.objectId().c_str());
01550                         }
01551                         else
01552                         {
01553                                 string val;
01554                                 for (n=0; n<qAttList.size();++n) 
01555                                 {
01556                                         st.getPropertyValue(val,n);
01557                                         if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME)
01558                                         {
01559                                                 DBFWriteStringAttribute(hDBFPol, iRecPol, n, val.c_str());
01560                                         }
01561                                         else if (qAttList[n].rep_.type_ == TeINT)
01562                                         {
01563                                                 DBFWriteIntegerAttribute(hDBFPol, iRecPol, n, atoi(val.c_str()));
01564                                         }
01565                                         else if (qAttList[n].rep_.type_ == TeREAL)
01566                                         {
01567                                                 DBFWriteDoubleAttribute(hDBFPol, iRecPol, n, atof(val.c_str()));
01568                                         }
01569                                 }
01570                         }
01571                         ++iRecPol;
01572                 }
01573                 if (st.hasLines())
01574                 {
01575                         TeLineSet& lineSet = st.getLines();
01576                         nVertices = lineSet.size();
01577                         TeLineSet::iterator itls;
01578                         for (itls=lineSet.begin(); itls!=lineSet.end(); ++itls)
01579                                 totpoints += (*itls).size();
01580                         panPart = (int *) malloc(sizeof(int) * nVertices);
01581                         padfX = (double *) malloc(sizeof(double) * totpoints);
01582                         padfY = (double *) malloc(sizeof(double) * totpoints);
01583                         posXY = 0;
01584                         nelem = 0;
01585                         for (itls=lineSet.begin(); itls!=lineSet.end(); ++itls)
01586                         {
01587                                 panPart[nelem]=posXY;
01588                                 for (l=0; l<(*itls).size(); ++l)
01589                                 {
01590                                         padfX[posXY] = (*itls)[l].x();
01591                                         padfY[posXY] = (*itls)[l].y();
01592                                         ++posXY;
01593                                 }
01594                                 ++nelem;
01595                         }
01596                         psObject = SHPCreateObject(SHPT_ARC, -1, nVertices, panPart, NULL,      posXY, padfX, padfY, NULL, NULL);
01597                         if (hSHPLin == 0)
01598                         {
01599                                 string fname = base + "_lin.shp"; 
01600                                 hSHPLin = SHPCreate(fname.c_str(), SHPT_ARC);
01601                                 assert (hSHPLin != 0);
01602 
01603                                 TeWriteDataProjectionToFile(outputProj, TeGetName(fname.c_str()));
01604                         }               
01605                         shpRes = SHPWriteObject(hSHPLin, -1, psObject);
01606                         SHPDestroyObject(psObject);
01607                         free(panPart);
01608                         free(padfX);
01609                         free(padfY);
01610                         assert(shpRes != -1);
01611                         if (hDBFLin == 0)
01612                         {
01613                                 hDBFLin = TeCreateDBFFile(base + "_lin.dbf", qAttList);
01614                                 assert (hDBFLin != 0);
01615                         }
01616                         if (onlyObjId)
01617                         {
01618                                 DBFWriteStringAttribute(hDBFLin, iRecLin, 0, st.objectId().c_str());
01619                         }
01620                         else
01621                         {
01622                                 string val;
01623                                 for (n=0; n<qAttList.size();++n) 
01624                                 {
01625                                         st.getPropertyValue(val,n);
01626                                         if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME)
01627                                         {
01628                                                 DBFWriteStringAttribute(hDBFLin, iRecLin, n, val.c_str());
01629                                         }
01630                                         else if (qAttList[n].rep_.type_ == TeINT)
01631                                         {
01632                                                 DBFWriteIntegerAttribute(hDBFLin, iRecLin, n, atoi(val.c_str()));
01633                                         }
01634                                         else if (qAttList[n].rep_.type_ == TeREAL)
01635                                         {
01636                                                 DBFWriteDoubleAttribute(hDBFLin, iRecLin, n, atof(val.c_str()));
01637                                         }
01638                                 }
01639                         }
01640                         ++iRecLin;
01641                 }
01642                 if (st.hasPoints())
01643                 {
01644                         TePointSet& pointSet = st.getPoints();
01645                         nVertices = pointSet.size();
01646                         panPart = (int *) malloc(sizeof(int) * nVertices);
01647                         padfX = (double *) malloc(sizeof(double) * nVertices);
01648                         padfY = (double *) malloc(sizeof(double) * nVertices);
01649                         nelem = 0;
01650                         TePointSet::iterator itpts;
01651                         for (itpts=pointSet.begin(); itpts!=pointSet.end(); ++itpts)
01652                         {
01653                                 panPart[nelem] = nelem;
01654                                 padfX[nelem] = (*itpts).location().x();
01655                                 padfY[nelem] = (*itpts).location().y();
01656                                 ++nelem;
01657                         }
01658                         psObject = SHPCreateObject(SHPT_POINT, -1, nVertices, panPart, NULL, nVertices, padfX, padfY, NULL, NULL );
01659                         if (hSHPPt == 0)
01660                         {
01661                                 string fname = base + "_pt.shp";
01662                                 hSHPPt = SHPCreate(fname.c_str(), SHPT_POINT);
01663                                 assert (hSHPPt != 0);
01664 
01665                                 TeWriteDataProjectionToFile(outputProj, TeGetName(fname.c_str()));
01666                         }               
01667                         shpRes = SHPWriteObject(hSHPPt, -1, psObject);
01668                         SHPDestroyObject(psObject);
01669                         free(panPart);
01670                         free(padfX);
01671                         free(padfY);
01672                         assert(shpRes != -1);
01673                         if (hDBFPt == 0)
01674                         {
01675                                 hDBFPt = TeCreateDBFFile(base + "_pt.dbf", qAttList);
01676                                 assert (hDBFPt != 0);
01677                         }
01678                         if (onlyObjId)
01679                         {
01680                                 DBFWriteStringAttribute(hDBFPt, iRecPt, 0, st.objectId().c_str());
01681                         }
01682                         else
01683                         {
01684                                 string val;
01685                                 for (n=0; n<qAttList.size();++n) 
01686                                 {
01687                                         st.getPropertyValue(val,n);
01688                                                 if (qAttList[n].rep_.type_ == TeSTRING || qAttList[n].rep_.type_ == TeDATETIME)
01689                                         {
01690                                                 DBFWriteStringAttribute(hDBFPt, iRecPt, n, val.c_str());
01691                                         }
01692                                         else if (qAttList[n].rep_.type_ == TeINT)
01693                                         {
01694                                                 DBFWriteIntegerAttribute(hDBFPt, iRecPt, n, atoi(val.c_str()));
01695                                         }
01696                                         else if (qAttList[n].rep_.type_ == TeREAL)
01697                                         {
01698                                                 DBFWriteDoubleAttribute(hDBFPt, iRecPt, n, atof(val.c_str()));
01699                                         }
01700                                 }
01701                         }
01702                         ++iRecPt;
01703                 }
01704                 ++nIProcessed;
01705                 if (TeProgress::instance() && int(t2-t1) > dt)
01706                 {
01707                         t1 = t2;
01708                         if(((int)(t2-t0) > dt2))
01709                         {
01710                                 if (TeProgress::instance()->wasCancelled())
01711                                         break;
01712                                 else
01713                                         TeProgress::instance()->setProgress(nIProcessed);
01714                         }
01715                 }
01716 
01717         }
01718         if (hDBFPol != 0)
01719                 DBFClose(hDBFPol);
01720         if (hSHPPol != 0)
01721         SHPClose(hSHPPol);
01722         if (hDBFLin != 0)
01723                 DBFClose(hDBFLin);
01724         if (hSHPLin != 0)
01725         SHPClose(hSHPLin);
01726         if (hDBFPt != 0)
01727                 DBFClose(hDBFPt);
01728         if (hSHPPt != 0)
01729         SHPClose(hSHPPt);
01730 
01731         if (TeProgress::instance())
01732                 TeProgress::instance()->reset();        
01733 
01734         return true;
01735 }

SHP_DLL bool TeExportShapefile ( TeLayer layer,
const string shpFileName,
const string tableName,
const string restriction = "" 
)

Deprecated:
This is an overloaded function, provided for compatibility. It behaves essentially like the above function.

Definition at line 1739 of file TeDriverSHPDBF.cpp.

References TeAbstractTheme::attributeRest(), TeLayer::attrTables(), TeTheme::collectionAuxTable(), TeTheme::collectionTable(), TeTheme::layer(), TeTheme::setAttTables(), TeQuerierParams::setParams(), TeExportQuerierToShapefile(), and TeTheme::TeTheme().

01740 {
01741         if (!layer || shpFileName.empty())
01742                 return false;
01743 
01744         // check if asked table exist
01745         TeAttrTableVector& vTables = layer->attrTables();
01746         TeAttrTableVector::iterator it = vTables.begin();
01747         while (it != vTables.end())
01748         {
01749                 if (it->name() == tableName) 
01750                         break;
01751                 ++it;
01752         }
01753         if (it == vTables.end())
01754                 return false;
01755         TeAttrTableVector askedTable;
01756         askedTable.push_back(*it);
01757 
01758         TeTheme* tempTheme = new TeTheme();
01759         tempTheme->attributeRest(restriction);
01760         tempTheme->layer(layer);
01761         tempTheme->collectionTable("");
01762         tempTheme->collectionAuxTable("");
01763         tempTheme->setAttTables(askedTable);
01764 
01765         TeQuerierParams qPar(true, true);
01766         qPar.setParams(tempTheme);
01767 
01768         TeQuerier* tQuerier = new TeQuerier(qPar);
01769         bool res = TeExportQuerierToShapefile(tQuerier, shpFileName);
01770         delete tQuerier;
01771         delete tempTheme;
01772 
01773     return res ;
01774 }

SHP_DLL bool TeExportThemeToShapefile ( TeTheme theme,
TeSelectedObjects  selOb = TeAll,
const std::string baseName = "",
const std::vector< std::string attributes = std::vector< std::string >() 
)

Parameters:
theme pointer to the layer
selOb the objetct selection: TeALL, TePOINTED, TeQUERIED...
baseName name of the output shapefile. If empty, the file will have the same name as the Theme.
attributes list of attributes to be exported. If empty, the file will have all the attributes.
Returns:
TRUE if the data was successfully exported and FALSE otherwise

SHP_DLL bool TeGetSHPInfo ( std::string  shpfileName,
unsigned int &  nShapes,
TeBox box,
TeGeomRep rep 
)

Parameters:
shpFileName Shapefile name
nShapes to return the number of shapes in the shapefile
box to return the boundary box of the shapes contained in the shapefile
rep to return the type of geometry exist in the shapefile
Returns:
true if succeeds and and false otherwise

Definition at line 65 of file TeDriverSHPDBF.cpp.

References SHPClose(), SHPGetInfo(), SHPOpen(), SHPT_ARC, SHPT_ARCM, SHPT_ARCZ, SHPT_MULTIPOINT, SHPT_MULTIPOINTM, SHPT_MULTIPOINTZ, SHPT_NULL, SHPT_POINT, SHPT_POINTM, SHPT_POINTZ, SHPT_POLYGON, SHPT_POLYGONM, SHPT_POLYGONZ, TeGEOMETRYNONE, TeLINES, TePOINTS, TePOLYGONS, TeBox::x1_, TeBox::x2_, TeBox::y1_, and TeBox::y2_.

00066 {
00067         nShapes = 0;
00068         rep = TeGEOMETRYNONE;
00069         box = TeBox();
00070 
00071         SHPHandle       hSHP;
00072         hSHP = SHPOpen( shpfileName.c_str(), "rb" );
00073 
00074         if( hSHP == 0 )
00075                 return false;
00076 
00077         int             nShapeType, nentities=0;
00078         double  adfMinBound[4], adfMaxBound[4];
00079 
00080         SHPGetInfo(hSHP, &nentities, &nShapeType, adfMinBound, adfMaxBound);
00081         nShapes = nentities;
00082         
00083         box.x1_ = adfMinBound[0];
00084         box.y1_ = adfMinBound[1];
00085         box.x2_ = adfMaxBound[0];
00086         box.y2_ = adfMaxBound[1];
00087 
00088         switch (nShapeType) 
00089         {
00090                 case SHPT_NULL:
00091                         rep = TeGEOMETRYNONE;
00092                 break;
00093                 // --- point geometries ---
00094                 case SHPT_POINTZ:       
00095                 case SHPT_MULTIPOINTZ:
00096                 case SHPT_POINTM:
00097                 case SHPT_MULTIPOINTM:
00098                 case SHPT_POINT:
00099                 case SHPT_MULTIPOINT:
00100                         rep = TePOINTS;
00101                 break;
00102                 // --- line geometries ---
00103                 case SHPT_ARCZ:
00104                 case SHPT_ARCM:
00105                 case SHPT_ARC:
00106                         rep = TeLINES;
00107                 break;
00108                 // --- polygon geometries ---
00109                 case SHPT_POLYGON:
00110                 case SHPT_POLYGONM:
00111                 case SHPT_POLYGONZ:
00112                         rep = TePOLYGONS;
00113         }
00114         SHPClose(hSHP);
00115         return true;
00116 }

SHP_DLL bool TeImportShape ( TeLayer layer,
const string shpFileName,
string  attrTableName = "",
string  objectIdAttr = "",
unsigned int  chunkSize = 60,
const bool useTransaction = true 
)

Parameters:
layer pointer to a layer
shpFileName shapefile file name
attrTableName the name that the attribute table will be saved
objectIdAttr name of the attribute that has the identification of objects
chunkSize the number of objects in memory before save them in the layer

SHP_DLL TeLayer* TeImportShape ( const string shpFileName,
TeDatabase db,
const string layerName = "" 
)

Parameters:
shpFileName Shapefile name
db pointer to a TerraLib database
layerName name of the layer that will contain the data (optional)
Returns:
a pointer to created layer if the data was imported successfully and 0 otherwise

Definition at line 188 of file TeDriverSHPDBF.cpp.

References TeDatabase::deleteLayer(), TeLayer::id(), TeDatabase::layerMap(), Te2String(), TeGetBaseName(), TeGetName(), TeImportShape(), and TeStringCompare().

00189 {
00190         if (!db || shpFileName.empty()) 
00191                 return 0;
00192 
00193         // check if format is complete (SHP, SHX e DBF files exist)
00194         string filePrefix = TeGetName(shpFileName.c_str());
00195         string ftest = filePrefix + ".dbf";
00196         if (access(ftest.c_str(),04) == -1)
00197         {
00198                 ftest = filePrefix + ".DBF";
00199                 if (access(ftest.c_str(),04) == -1)
00200                         return 0;
00201         }
00202         ftest = filePrefix + ".shx";
00203         if (access(ftest.c_str(),04) == -1)
00204         {
00205                 ftest = filePrefix + ".SHX";
00206                 if (access(ftest.c_str(),04) == -1)
00207                         return 0;
00208         }
00209 
00210         // found a valid layer name
00211         string lName;
00212         string baseName = TeGetBaseName(shpFileName.c_str());
00213         if (layerName.empty())
00214                 lName = baseName;
00215         else
00216                 lName = layerName;
00217 
00218         string newLayerName = lName;
00219         TeLayerMap& layerMap = db->layerMap();
00220         TeLayerMap::iterator it;
00221         bool flag = true;
00222         int n = 1;
00223         while (flag)
00224         {
00225                 for (it = layerMap.begin(); it != layerMap.end(); ++it)
00226                 {
00227                         if (TeStringCompare(it->second->name(),newLayerName))
00228                                 break;
00229                 }
00230                 if (it == layerMap.end())
00231                         flag = 0;
00232                 else
00233                         newLayerName = lName + "_" +Te2String(n);
00234                 n++;    
00235         }
00236 
00237         // find projection
00238         TeLayer* newLayer = new TeLayer(newLayerName,db);
00239         if (newLayer->id() <= 0 )
00240                 return 0;                               // couldn�t create new layer
00241 
00242         bool res = TeImportShape(newLayer,shpFileName);
00243         if (res)
00244                 return newLayer;
00245         else
00246         {
00247                 db->deleteLayer(newLayer->id());
00248                 delete newLayer;
00249                 return 0;
00250         }
00251 }

SHP_DLL bool TeSTOSetBuildSHP ( TeSTElementSet stoset,
const string fileName 
)

Parameters:
stoset the STOSet that will be filled
fileName shape file name

Definition at line 119 of file TeDriverSHPDBF.cpp.

References TeProperty::attr_, DBFGetFieldCount(), DBFOpen(), DBFReadStringAttribute(), TeBaseSTInstance< GeometryType, TimeType >::geometries(), TeBaseSTInstanceSet< GeometryType, TimeType, InstanceType >::insertSTInstance(), TeAttributeRep::name_, TeAttributeRep::numChar_, TeBaseSTInstance< GeometryType, TimeType >::objectId(), TeBaseSTInstance< GeometryType, TimeType >::properties(), psShape, TeAttribute::rep_, TeBaseSTInstanceSet< GeometryType, TimeType, InstanceType >::setAttributeList(), SHPDestroyObject(), SHPGetInfo(), SHPOpen(), SHPReadObject(), Te2String(), TeDecodeShape(), TeGetName(), TeReadDBFAttributeList(), TeBaseSTInstance< GeometryType, TimeType >::theme(), and TeProperty::value_.

00120 {
00121         // Read some information about the shapefile
00122         string filePrefix = TeGetName(fileName.c_str());
00123         string shpfileName = filePrefix + ".shp";
00124 
00125         SHPHandle       hSHP;
00126         hSHP = SHPOpen( shpfileName.c_str(), "rb" );
00127 
00128         if( hSHP == 0 )
00129                 return false;
00130 
00131         int             nShapeType, nEntities;
00132         double  adfMinBound[4], adfMaxBound[4];
00133 
00134         SHPGetInfo(hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound);
00135 
00136         string dbffileName = filePrefix + ".dbf";
00137         DBFHandle hDBF = DBFOpen( dbffileName.c_str(), "rb" );
00138         if( hDBF == 0  || DBFGetFieldCount(hDBF) == 0)
00139                 return false;
00140     
00141         int natt = DBFGetFieldCount(hDBF);
00142         TeAttributeList attList;
00143         TeReadDBFAttributeList(shpfileName, attList);
00144         
00145         stoset.setAttributeList( attList );
00146 
00147         TeAttributeRep repobjid;
00148         repobjid.name_ = "object_id";
00149         repobjid.numChar_ = 16;
00150 
00151         TeAttribute attobjid;
00152         attobjid.rep_ = repobjid;
00153 
00154         TeProperty propobjid;
00155         propobjid.attr_ = attobjid;
00156         
00157         int i,n;
00158         SHPObject* psShape;
00159         for (i=0; i<nEntities; i++)
00160         {
00161                 string objectid = Te2String(i);
00162                 TeSTInstance curObj;
00163                 curObj.objectId(objectid);
00164 
00165                 TePropertyVector prop;
00166                 psShape = SHPReadObject(hSHP,i);
00167                 if (TeDecodeShape(psShape,curObj.geometries(),objectid))
00168                 {
00169                         propobjid.value_ = objectid;
00170                         prop.push_back(propobjid);
00171                         for(n=0;n<natt;n++)
00172                         {
00173                                 string value = DBFReadStringAttribute(hDBF,i,n);
00174                                 TeProperty p;
00175                                 p.attr_ = attList[n];
00176                                 p.value_ = value;
00177                                 prop.push_back(p);
00178                         }
00179                         curObj.properties(prop);
00180                         stoset.insertSTInstance(curObj);
00181                         curObj.theme(0);
00182                 }
00183                 SHPDestroyObject(psShape);
00184         }
00185         return true;
00186 } 

SHP_DLL void TeWriteDataProjectionToFile ( TeProjection projection,
const std::string fileName 
)

Definition at line 54 of file TeDriverSHPDBF.cpp.

References TeGetName(), TeGetWKTFromTeProjection(), and TeWriteToFile().

00055 {
00056         string filePrefix = TeGetName(fileName.c_str());
00057         string prjFileName_ = filePrefix + ".prj";
00058 
00059         std::string wktProj = TeGetWKTFromTeProjection(projection);
00060 
00061         TeWriteToFile(prjFileName_, wktProj, "w");
00062 }


Generated on Sun Jul 29 04:04:35 2012 for TerraLib - Development Source by  doxygen 1.5.3