00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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;
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 }