TeSTEFunctionsSHP.cpp

Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright © 2001-2004 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00023 #include "TeSTEFunctionsSHP.h"
00024 #include "TeSTElementSet.h"
00025 #include <vector>
00026 
00027 bool TeDecodeShape(SHPObject* psShape, TeMultiGeometry& geomestries, string& objectId)
00028 {
00029         int shpType = psShape->nSHPType;
00030         switch (shpType)
00031         {
00032                 case SHPT_POLYGON:
00033                 case SHPT_POLYGONZ:
00034                         TeSHPPolygonDecode(psShape,geomestries.polygons_,objectId);
00035                         return true;
00036                 case SHPT_ARC:
00037                 case SHPT_ARCZ:
00038                         TeSHPPolyLineDecode(psShape,geomestries.lines_,objectId);
00039                         return true;
00040                 case SHPT_POINT:
00041                 case SHPT_POINTZ:
00042                 case SHPT_MULTIPOINT:
00043                 case SHPT_MULTIPOINTZ:
00044                         TeSHPPointDecode(psShape,geomestries.points_,objectId);
00045                         return true;
00046         }
00047         return false;
00048 }
00049 
00050 bool
00051 TeSTOSetBuildSHP(TeSTElementSet& stoset, const string& fileName)
00052 {
00053         // Read some information about the shapefile
00054         string filePrefix = TeGetName(fileName.c_str());
00055         string shpfileName = filePrefix + ".shp";
00056 
00057         SHPHandle       hSHP;
00058         hSHP = SHPOpen( shpfileName.c_str(), "rb" );
00059 
00060         if( hSHP == 0 )
00061                 return false;
00062 
00063         int             nShapeType, nEntities;
00064         double  adfMinBound[4], adfMaxBound[4];
00065 
00066         SHPGetInfo(hSHP, &nEntities, &nShapeType, adfMinBound, adfMaxBound);
00067 
00068         string dbffileName = filePrefix + ".dbf";
00069   DBFHandle hDBF = DBFOpen( dbffileName.c_str(), "rb" );
00070   if( hDBF == 0  || DBFGetFieldCount(hDBF) == 0) {
00071     SHPClose( hSHP );
00072   
00073                 return false;
00074         }
00075     
00076         int natt = DBFGetFieldCount(hDBF);
00077         TeAttributeList attList;
00078         TeReadDBFAttributeList(shpfileName, attList);
00079 
00080         TeAttributeRep repobjid;
00081         repobjid.name_ = "object_id";
00082         repobjid.numChar_ = 16;
00083 
00084         TeAttribute attobjid;
00085         attobjid.rep_ = repobjid;
00086 
00087         TeProperty propobjid;
00088         propobjid.attr_ = attobjid;
00089         
00090         TeAttributeList attrs; 
00091         attrs.push_back (attobjid);
00092         for(unsigned int j=0; j<attList.size(); ++j)
00093           attrs.push_back(attList[j]);
00094          
00095         stoset.setAttributeList(attrs);         
00096         
00097         int i,n;
00098         SHPObject* psShape;
00099         for (i=0; i<nEntities; i++)
00100         {
00101                 string objectid = Te2String(i);
00102                 TeSTInstance curObj;
00103                 curObj.objectId(objectid);
00104 
00105                 vector<string> prop; //values of the attributes
00106                 psShape = SHPReadObject(hSHP,i);
00107                 if (TeDecodeShape(psShape,curObj.geometries(),objectid))
00108                 {
00109                         prop.push_back(objectid);
00110                         for(n=0;n<natt;n++)
00111                         {
00112                                 string value = DBFReadStringAttribute(hDBF,i,n);
00113                                 prop.push_back(value);
00114                         }
00115                         curObj.setProperties(prop);
00116                         stoset.insertSTInstance(curObj);
00117                         curObj.theme(0);
00118                 }
00119                 SHPDestroyObject(psShape);
00120         }
00121         
00122         DBFClose( hDBF );
00123         SHPClose( hSHP );
00124         
00125         return true;
00126 } 

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