osgearth_city--osgearth加载TMS服务数据

开发Osg有一段时间了,之前一直用的别人提供的sdk,底层代码很少去看,今天突然发现居然不知道如何去加载tm瓦片数据,在网上的资料也是少之又少,唯一一个是osgEarthDrivers的头文件去加载的,结果打开我的osgearth一看,根本没有这个头文件,网上也有这部分资料,一开始还以为是那个不知名的库,后续才知道,因为版本迭代,已经不叫这个名字了,搞了半天在osgearth自带的例子中发现了如何加载tms,故特地记录一下,后续也会记录一下osgearth的每个例子,熟悉一下接口。
1.接口功能
加载tms瓦片数据,加载高程,加载矢量
2. 代码解析
源码全复制于osgearth_city(不同版本不一样,以官网例子为主)
加载tms瓦片数据

void addImagery(Map* map)
{
    // add a TMS imagery layer:
    TMSImageLayer* layer = new TMSImageLayer();
    layer->setURL(IMAGERY_URL);
    map->addLayer(layer);
}

例子十分简洁明了,
创建一个tms图层,设置tms的地址,可以为线上的,也可以为线下的,如xxx/xxx/tms.xml,对应的即为瓦片数据。
map为mapnode的一个接口变量

加载高程

void addElevation(Map* map)
{
    // add a TMS elevation layer:
    TMSElevationLayer* layer = new TMSElevationLayer();
    layer->setURL(ELEVATION_URL);
    map->addLayer(layer);
}

与上一个影像无不同

加载矢量

void addStreets(Map* map)
{
    // create a feature source to load the street shapefile.
    OGRFeatureSource* data = new OGRFeatureSource();
    data->setURL(STREETS_URL);
    data->options().buildSpatialIndex() = true;

    //设置采样率
    ResampleFilterOptions resample;
    resample.minLength() = 0.0f;
    resample.maxLength() = 25.0f;
    data->options().filters().push_back( resample );

    //设置样式
    Style style;
    style.setName( "streets" );

    // 将数据渲染为7.5m宽的半透明黄色线
    LineSymbol* line = style.getOrCreate<LineSymbol>();
    line->stroke()->color() = Color(Color::Yellow, 0.5f);
    line->stroke()->width() = 7.5f;
    line->stroke()->widthUnits() = Units::METERS;

    // 贴近地形
    AltitudeSymbol* alt = style.getOrCreate<AltitudeSymbol>();
    alt->clamping() = alt->CLAMP_TO_TERRAIN;

    // 应用深度偏移来避免z-fighting
    RenderSymbol* render = style.getOrCreate<RenderSymbol>();
    render->depthOffset()->minBias()->set(6.6, Units::METERS);

    // 设置分页布局。贴图大小因素和可见范围结合在一起
	//确定贴图大小,使贴图半径=最大范围/贴图大小因子
    FeatureDisplayLayout layout;
    layout.tileSize() = 500;

    // create a model layer that will render the buildings according to our style sheet.
    FeatureModelLayer* layer = new FeatureModelLayer();
    layer->setName("Streets");
    layer->setFeatureSource(data);
    layer->options().layout() = layout;
    layer->setStyleSheet(new StyleSheet());
    layer->getStyleSheet()->addStyle(style);
    layer->setMaxVisibleRange(5000.0f);

    map->addLayer(layer);
}

基本上就是先绑定矢量数据的地址,再设置矢量的样式,最后将其设置矢量图层的对象,oe3.2都统一使用addlayer来进行添加。

添加高程

    // Add a composite elevation layer tha tcombines two other sources:
    GDALElevationLayer* elev1 = new GDALElevationLayer();
    elev1->setURL("../data/terrain/mt_fuji_90m.tif");
    GDALElevationLayer* elev2 = new GDALElevationLayer();
    elev2->setURL("../data/terrain/mt_everest_90m.tif");
    CompositeElevationLayer* compElev = new CompositeElevationLayer();
    compElev->addLayer(elev1);
    compElev->addLayer(elev2);
    map->addLayer(compElev);

添加tif

    GDALImageLayer* comp1 = new GDALImageLayer();
    comp1->setURL("../data/boston-inset-wgs84.tif");
    GDALImageLayer* comp2 = new GDALImageLayer();
    comp2->setURL("../data/nyc-inset-wgs84.tif");
    CompositeImageLayer* compImage = new CompositeImageLayer();
    compImage->addLayer(comp1);
    compImage->addLayer(comp2);
    map->addLayer(compImage);

添加wms

    WMSImageLayer* wms = new WMSImageLayer();
    wms->setURL("http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi");
    wms->setFormat("png");
    wms->setLayers("nexrad-n0r");
    wms->setSRS("EPSG:4326");
    wms->setTransparent(true);
    wms->options().cachePolicy() = CachePolicy::NO_CACHE;
    map->addLayer(wms);

添加xyz


    // add a semi-transparent XYZ layer:
    XYZImageLayer* osm = new XYZImageLayer();
    osm->setURL("http://[abc].tile.openstreetmap.org/{z}/{x}/{y}.png");
    osm->setProfile(Profile::create(Profile::SPHERICAL_MERCATOR));
    osm->setOpacity(0.5f);
    map->addLayer(osm);

你可能感兴趣的:(c++)