|
我有一个shape文件,为点文件,是一些地名。昨天我做了一个mfc的例子程序,将这个shape文件导入地形中作为地名标注。其中的主要函数如下:
osg::ref_ptr<osg::Group> CCoreOSG::ImportOSGShpLabel( std::string filename )
{
osg::ref_ptr<osg::Node> nodLable = osgDB::readNodeFile(filename);
osg::ref_ptr<osg::Geode> labGeode = dynamic_cast<osg::Geode*>(nodLable.get());
unsigned int numDrawables = labGeode->getNumDrawables();
osg::ref_ptr<osg::Group> mLable = new osg::Group;
for( unsigned int i = 0; i < numDrawables; i++ )
{
osg::Geometry *geom = dynamic_cast<osg::Geometry *>(labGeode->getDrawable(i));
if( geom != 0L )
{
osg::Vec3 vecvertex;
osg::Vec3Array* pArray = dynamic_cast<osg::Vec3Array*>(geom->getVertexArray());
if(pArray->getDataSize() > 0)
{
vecvertex = pArray->at(0);
vecvertex.z() = 5000.0;
double height = osgSim::HeightAboveTerrain::computeHeightAboveTerrain(mModel, vecvertex);
vecvertex.z() -= (height - 4000);
}
osg::ref_ptr<osgSim::ShapeAttributeList> lstShpAttrib = dynamic_cast<osgSim::ShapeAttributeList*>(geom->getUserData());
std::string txt = getAttributeByName(lstShpAttrib, "名称");
mLable->addChild(create3DText(vecvertex, 6000,txt).get());
}
}
return mLable;
}
osg::ref_ptr<osg::Group> CCoreOSG::create3DText( const osg::Vec3& center,float radius, std::string txt )
{
osg::Geode* geode = new osg::Geode;
float characterSize=radius*0.2f;
osg::Vec3 pos(center.x()-radius*.5f,center.y()-radius*.5f,center.z()-radius*.5f);
osgText::Text* text = new osgText::Text;
text->setFont("fonts/simhei.TTF");
text->setCharacterSize(characterSize);
text->setPosition(pos);
text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS);
text->setAxisAlignment(osgText::Text::SCREEN);
const char* cstr=txt.c_str();
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, cstr, -1, NULL, 0);
wchar_t *pwText = new wchar_t[dwNum];
MultiByteToWideChar (CP_ACP, 0, cstr, -1, pwText, dwNum);
text->setText(pwText);
geode->addDrawable(text);
osg::ref_ptr<osg::Group> rootNode = new osg::Group;
rootNode->addChild(geode);
return rootNode;
}
std::string CCoreOSG::getAttributeByName( osg::ref_ptr<osgSim::ShapeAttributeList> attribList, std::string name )
{
osgSim::ShapeAttributeList::iterator it;
for(it = attribList->begin(); it != attribList->end(); ++it)
{
if(it->getName() == name)
return it->getString();
}
return "";
}
文件路径为D:\data\GIS\merge.shp。读取效果如附件1。
今天我做了一个基于控制台的程序,相关函数基本都是直接拷贝过去的,可是结果出错了,错误信息详见附件2,显示结果如附件3。 |
|