前言
osgearth_wfs示例,展示的如何创建wfs驱动,通过http服务加载wfs数据。
由于本示例中的链接无效,所以并没有执行出应有的效果。
关于wfs服务:
WFS(Web Feature Service,Web 要素服务)服务,该服务符合 OGC(Open Geospatial Consortium,开放地理信息联盟)制定的 WFS 实现规范。WFS 通过 GML(Geography Markup Language,地理标记语言)传递地理空间数据,它支持在基于 HTTP 协议的分布式计算平台上对地理要素进行插入(INSERT)、更新(UPDATE)、删除(DELETE)和发现(DISCOVERY)等操作,并且在这些操作的过程中保证了地理数据变化的一致性。
代码分析
#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>
#include <osgEarth/Units>
#include <osgEarth/MapNode>
// include for WFS feature driver:
#include <osgEarthDrivers/feature_wfs/WFSFeatureOptions>
// include for feature geometry model renderer:
#include <osgEarthAnnotation/FeatureNode>
#include <osgEarthSymbology/Style>
#define LC "[wfs example] "
using namespace osgEarth;
using namespace osgEarth::Annotation;
using namespace osgEarth::Util;
using namespace osgEarth::Features;
using namespace osgEarth::Symbology;
int
usage(const char* name)
{
OE_NOTICE
<< "\nUsage: " << name << " file.earth" << std::endl
<< MapNodeHelper().usage() << std::endl;
return 0;
}
int
main(int argc, char** argv)
{
osg::ArgumentParser arguments(&argc,argv);
if ( arguments.read("--help") )
return usage(argv[0]);
// general setup:一般设置
osgViewer::Viewer viewer(arguments);
viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( false, false );
viewer.setCameraManipulator( new EarthManipulator(arguments) );
viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
viewer.getCamera()->setNearFarRatio(0.00002);
// Get the bounds from the command line.从命令窗口获取 --bounds值
Bounds bounds;
double xmin = DBL_MAX, ymin = DBL_MAX, xmax = DBL_MIN, ymax = DBL_MIN;
while (arguments.read("--bounds", xmin, ymin, xmax, ymax))
{
bounds.xMin() = xmin, bounds.yMin() = ymin, bounds.xMax() = xmax, bounds.yMax() = ymax;
}
// load an earth file, and support all or our example command-line options
// and earth file <external> tags
osg::Node* node = MapNodeHelper().load( arguments, &viewer );
if ( node )
{
MapNode* mapNode = MapNode::get(node);
if ( !mapNode )
return usage(argv[0]);
// Create the WFS driver: 创建WFS驱动
osgEarth::Drivers::WFSFeatureOptions wfs;
wfs.url() = osgEarth::URI("http://demo.mapserver.org/cgi-bin/wfs");
wfs.typeName() = "cities";
wfs.outputFormat() = "gml2"; // JSON or GML
if (wfs.empty()) {
return usage("wfs is empty!");
}
// Create the feature source from the options 创建特征源
osg::ref_ptr< FeatureSource > featureSource = FeatureSourceFactory::create(wfs);
Status s = featureSource->open();
// Set the query with the bounds if one was specified.
Query query;
if (bounds.isValid())
{
query.bounds() = bounds;
}
// Get the features 获取特征
osg::ref_ptr< FeatureCursor > cursor = featureSource->createFeatureCursor(query, 0L);
FeatureList features;
cursor->fill(features);
OE_NOTICE << "Got " << features.size() << " features" << std::endl;
// Create a style 创建文本风格
Style style;
style.getOrCreateSymbol<TextSymbol>()->content() = StringExpression("[NAME]");
// Create the FeatureNode with the features and the style.
osg::ref_ptr< FeatureNode > featureNode = new FeatureNode(features, style);
mapNode->addChild(featureNode.get());
viewer.setSceneData( node );
return viewer.run();
}
else
{
return usage(argv[0]);
}
}