Skip to content

Commit

Permalink
KML: Remove the --kml option (since you can just load it directly), a…
Browse files Browse the repository at this point in the history
…utomatically place mapnode siblings under the mapnode, and update the AnnotationsGUI to have better zoom-to-placemark capabilities.
  • Loading branch information
gwaldron committed Nov 8, 2023
1 parent 8cb338c commit 108194b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 58 deletions.
60 changes: 9 additions & 51 deletions src/osgEarth/ExampleResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
#include <osgEarth/ActivityMonitorTool>
#include <osgEarth/LogarithmicDepthBuffer>
#include <osgEarth/SimpleOceanLayer>
#include <osgEarth/AnnotationLayer>
#include <osgEarth/Registry>

#include <osgEarth/AnnotationData>
#include <osgEarth/TerrainEngineNode>
#include <osgEarth/NodeUtils>
#include <osgEarth/GLUtils>
#include <osgEarth/CullingUtils>

#include <osgEarthDrivers/kml/KML>

#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgGA/StateSetManipulator>
Expand Down Expand Up @@ -383,6 +381,14 @@ MapNodeHelper::loadWithoutControls(
return node;
}

// move any mapnode siblings under the mapnode.
auto siblings = findSiblings(mapNode.get());
for (auto& sibling : siblings)
{
mapNode->addChild(sibling);
sibling->getParent(0)->removeChild(sibling);
}

// GPU tessellation?
if (args.read("--tessellation") || args.read("--tess"))
{
Expand Down Expand Up @@ -630,10 +636,6 @@ MapNodeHelper::parse(
bool showActivity = useControls && args.read("--activity");
bool useLogDepth2 = args.read("--logdepth2");
bool useLogDepth = !args.read("--nologdepth") && !useLogDepth2; //args.read("--logdepth");
bool kmlUI = useControls && args.read("--kmlui");

std::string kmlFile;
args.read( "--kml", kmlFile );

// animation path:
std::string animpath;
Expand Down Expand Up @@ -676,47 +678,6 @@ MapNodeHelper::parse(
view->addEventHandler(new ToggleCanvasEventHandler(canvas, 'y'));
}

// Loading KML from the command line:
if ( !kmlFile.empty() && mapNode )
{
KML::KMLOptions kml_options;
kml_options.declutter() = true;

// set up a default icon for point placemarks:
IconSymbol* defaultIcon = new IconSymbol();
defaultIcon->url()->setLiteral(KML_PUSHPIN_URL);
kml_options.defaultIconSymbol() = defaultIcon;

TextSymbol* defaultText = new TextSymbol();
defaultText->halo() = Stroke(0.3,0.3,0.3,1.0);
kml_options.defaultTextSymbol() = defaultText;

URI kmlFileURI( kmlFile );
osg::Node* kml = KML::load(kmlFileURI, mapNode, kml_options );
if ( kml )
{
if (kmlUI)
{
Control* c = AnnotationGraphControlFactory().create(kml, view);
if ( c )
{
c->setVertAlign( Control::ALIGN_TOP );
mainContainer->addControl( c );
}
}

auto kmllayer = new AnnotationLayer();
kmllayer->setName(kmlFileURI.base());
kmllayer->addChild(kml);

mapNode->getMap()->addLayer(kmllayer);
}
else
{
OE_NOTICE << "Failed to load " << kmlFile << std::endl;
}
}

// Configure the mouse coordinate readout:
if ( useCoords && mapNode )
{
Expand Down Expand Up @@ -963,9 +924,6 @@ MapNodeHelper::usage() const
{
return Stringify()
<< " --sky : add a sky model\n"
<< " --kml <file.kml> : load a KML or KMZ file\n"
<< " --kmlui : display a UI for toggling nodes loaded with --kml\n"
<< " --coords : display map coords under mouse\n"
<< " --ortho : use an orthographic camera\n"
<< " --logdepth : activates the logarithmic depth buffer\n"
<< " --logdepth2 : activates logarithmic depth buffer with per-fragment interpolation\n"
Expand Down
21 changes: 15 additions & 6 deletions src/osgEarth/ImGui/AnnotationsGUI
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,25 @@ namespace
{
auto anode = dynamic_cast<osgEarth::AnnotationNode*>(&node);
auto data = dynamic_cast<osgEarth::AnnotationData*>(node.getUserData());
std::string name;

if (data)
{
ImGui::PushID((std::uintptr_t)data);

auto name = data->getName();
auto vp = data->getViewpoint();
name = data->getName();
auto vp_ptr = data->getViewpoint();
auto desc = data->getDescription();

Viewpoint vp;
if (vp_ptr) vp = *vp_ptr;

if (!vp.valid())
{
osgEarth::Util::ViewFitter fitter(srs, camera);
fitter.createViewpoint(&node, vp);
}

bool visible = node.getNodeMask() != 0;
if (ImGui::Checkbox("", &visible))
{
Expand All @@ -83,9 +93,9 @@ namespace
}
ImGui::SameLine();
bool is_selected = false;
if (ImGui::Selectable(name.c_str(), &is_selected) && manip && vp)
if (ImGui::Selectable(name.c_str(), &is_selected) && manip && vp.valid())
{
manip->setViewpoint(*vp);
manip->setViewpoint(vp);
}

ImGui::PopID();
Expand All @@ -96,7 +106,7 @@ namespace
{
ImGui::PushID((std::uintptr_t)data);

auto name = anode->getName();
name = anode->getName();
if (name.empty()) name = "[" + std::string(anode->className()) + "]";

bool visible = node.getNodeMask() != 0;
Expand All @@ -118,7 +128,6 @@ namespace
ImGui::PopID();

ImGui::Indent();

}

traverse(node);
Expand Down
14 changes: 14 additions & 0 deletions src/osgEarth/NodeUtils
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@ namespace osgEarth { namespace Util
return 0L;
}

/** Finds all the siblings of a node */
inline std::vector<osg::ref_ptr<osg::Node>> findSiblings(osg::Node* node)
{
std::vector<osg::ref_ptr<osg::Node>> output;
if (node)
{
auto parent = node->getParent(0);
for(unsigned i=0; i<parent->getNumChildren(); ++i)
if (parent->getChild(i) != node)
output.push_back(parent->getChild(i));
}
return output;
}

class FindNamedNodeVisitor : public osg::NodeVisitor
{
public:
Expand Down
1 change: 1 addition & 0 deletions src/osgEarth/Viewpoint
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace osgEarth
* a valid tracked node.
*/
bool isValid() const;
bool valid() const { return isValid(); }

/**
* Gets or sets the name of the viewpoint.
Expand Down
10 changes: 9 additions & 1 deletion src/osgEarthDrivers/kml/KMLOptions
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ namespace osgEarth { namespace KML
const optional<osg::Quat>& modelRotation() const { return _modelRotation; }

public:
KMLOptions() : _declutter( true ), _iconBaseScale( 1.0f ), _iconMaxSize(32), _modelScale(1.0f) { }
KMLOptions() : _declutter(true), _iconBaseScale(1.0f), _iconMaxSize(32), _modelScale(1.0f)
{
_defaultTextSymbol = new TextSymbol();
_defaultTextSymbol->size() = 18.0f;
_defaultTextSymbol->halo() = Stroke(0.3f, 0.3f, 0.3f, 1.0f);

_defaultIconSymbol = new IconSymbol();
_defaultIconSymbol->url()->setLiteral("https://github.com/gwaldron/osgearth/blob/master/data/placemark32.png?raw=true");
}

virtual ~KMLOptions() { }

protected:
Expand Down

0 comments on commit 108194b

Please sign in to comment.