[原][osg][osgearth]简单的通过osgDB,读取高程tif,修改高程tif

 1 ReadResult result;
 2     osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif");
 3     std::string name("D:\\gd.tif");
 4     osgDB::ReaderWriter::Options* opt= NULL;
 5     osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt);
 6 
 7     if (rr.validImage())
 8     {
 9         result = ReadResult(rr.takeImage());
10         result.getImage()->setName("nameNoUse.tif");
11     }
12 
13     if (result.succeeded())
14     {
15         result.getObject();
16         result.metadata();
17         osg::ref_ptr<osg::Image> image = result.getImage();
18 
19         osgEarth::ImageToHeightFieldConverter conv;
20         osg::HeightField* hf = conv.convert(image.get());
21 
22 
23 
24         for (unsigned col = 0; col < hf->getNumColumns(); ++col)
25         {
26             for (unsigned row = 0; row < hf->getNumRows(); ++row)
27             {
28                 float height = hf->getHeight(col, row);
29                 if (height < 1.0)
30                 {
31                     float newh = cos(height*3.141593f);
32                     //float rf = rand()% 500;
33                     hf->setHeight(col, row, -1000* newh);
34                 }
35                 else// if(height > 1)
36                 {
37                     //height = 100;//下断点看看
38                 }
39             }
40         }
41 
42         osg::Image* newimage = conv.convert(hf);
43         std::string nameofnew("D:\\gd2.tif");
44         reader->writeImage(*newimage, nameofnew);
45 
46     }

如题

 

加SB的“平滑”功能

float fBegin = 0.1;
    //float fEnd = 0.000001;
    float fLowestValue = 1000.0;
    int fWide = 100.0;

    ReadResult result;
    osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif");
    std::string name("D:\\gd.tif");
    osgDB::ReaderWriter::Options* opt= NULL;
    osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt);

    if (rr.validImage())
    {
        result = ReadResult(rr.takeImage());
        result.getImage()->setName("guandao.tif");
    }

    if (result.succeeded())
    {
        result.getObject();
        result.metadata();
        osg::ref_ptr<osg::Image> image = result.getImage();

        osgEarth::ImageToHeightFieldConverter conv;
        osg::HeightField* hf = conv.convert(image.get());

        int *fFlag = new int[hf->getNumColumns()*hf->getNumRows()];

        for (unsigned col = 0; col < hf->getNumColumns(); ++col)
        {
            for (unsigned row = 0; row < hf->getNumRows(); ++row)
            {
                fFlag[col*hf->getNumRows() + row] = 0;
                float height = hf->getHeight(col, row);
                if (height < fBegin)
                {
                    fFlag[col*hf->getNumRows() + row] = 1;
                    hf->setHeight(col, row, -fLowestValue);
                    /*
                    float newh = -1000.0;
                    if(height > 0.00001)
                        newh = 0.1 - (0.1 - height)/ (0.1-0.00001)*1000.0;
                    hf->setHeight(col, row, newh);*/
                }
            }
        }

        for (int i = 0; i < hf->getNumColumns()*hf->getNumRows(); i++)
        {
            if (fFlag[i] == 1)//如果这值在海面以下
            {
                bool isNearSide = false;
                int nowX = i/hf->getNumRows();
                int nowY = i%hf->getNumRows();
                for (int j = 0; j <= fWide; j++)
                {
                    //从离此值最近的值开始找附近的岸边,往外延伸
                    //向东南西北四个方向找,没层都遍历一圈
                    for ( int x = 0;x <= j;x++ )
                    {
                        //如果找到有岸边
                        int fDifValueX = x;
                        int fDifValueY = j - x;
                        int realX = nowX - fDifValueX;
                        if (realX > 0)
                        {
                            int realY = nowY - fDifValueY;
                            if (realY > 0)
                            {
                                if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边
                                    isNearSide = true;
                            }
                            realY = nowY + fDifValueY;
                            if (realY < hf->getNumRows())
                            {
                                if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边
                                    isNearSide = true;
                            }
                        }

                        realX = nowX + fDifValueX;
                        if (realX < hf->getNumColumns())
                        {
                            int realY = nowY - fDifValueY;
                            if (realY > 0)
                            {
                                if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边
                                    isNearSide = true;
                            }
                            realY = nowY + fDifValueY;
                            if (realY < hf->getNumRows())
                            {
                                if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边
                                    isNearSide = true;
                            }
                        }
                    }
                    
                    //查找这个范围内是否有值,如果有值则用此值
                    if (isNearSide)
                    {
                        float fRealHeight = fBegin - j * fLowestValue / fWide;
                        hf->setHeight((i / hf->getNumRows()), (i % hf->getNumRows()), fRealHeight);
                        break;//退出当前寻找的延伸
                    }
                }
            }
        }

        osg::Image* newimage = conv.convert(hf);
        std::string nameofnew("D:\\gd2.tif");
        reader->writeImage(*newimage, nameofnew);
        delete[]fFlag;
    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
osgEarth 是一个基于 OpenSceneGraph 的地理信息渲染引擎,它可以加载各种格式的地图数据,包括矢量地图、栅格地图和影像地图等。osgEarth 通过地图图层的方式来加载和渲染地图,因此,要加载地图需要定义一个或多个图层。 osgEarth 支持多种方式加载地图切片,其中一种方式就是通过 Earth 文件来定义图层和加载地图切片。Earth 文件是一种 XML 格式的文件,它包含了定义图层和地图切片的信息。以下是一个 Earth 文件的示例: ``` <earth> <layer name="OpenStreetMap" type="tms"> <url>http://tile.openstreetmap.org/${z}/${x}/${y}.png</url> <profile>global-mercator</profile> </layer> </earth> ``` 上面的 Earth 文件定义了一个名为 "OpenStreetMap" 的图层,它的类型为 "tms",即 TMS 格式的瓦片地图。图层的切片 URL 为 "http://tile.openstreetmap.org/${z}/${x}/${y}.png",其中 ${z}、${x} 和 ${y} 表示瓦片的行列号和层级。图层的投影方式为 "global-mercator",即 WGS84 Web Mercator 投影。 要在 osgEarth 中加载这个地图,可以使用以下代码: ``` osg::ref_ptr<osgEarth::Map> map = new osgEarth::Map(); map->load("path/to/earth/file.earth"); osg::ref_ptr<osgEarth::MapNode> mapNode = new osgEarth::MapNode(map); viewer.setSceneData(mapNode); ``` 上面的代码首先创建一个 Map 对象,并通过 load() 方法加载 Earth 文件。然后创建一个 MapNode 对象,并将其设置为场景根节点。这样就可以在场景中显示加载的地图了。 需要注意的是,要使用 Earth 文件加载地图需要确保 Earth 文件中定义的图层数据可用,并且与 osgEarth 版本兼容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值