Skip to content
Permalink
Browse files

shows mouse click WGS coordinates

  • Loading branch information
mbernasocchi authored and pka committed Feb 18, 2011
1 parent 64d1129 commit 9bca3a737a1273379e71f9980df22fa0a9f3a1fb
Showing with 75 additions and 1 deletion.
  1. +57 −1 src/plugins/globe/globe_plugin.cpp
  2. +18 −0 src/plugins/globe/globe_plugin.h
@@ -158,6 +158,10 @@ void GlobePlugin::run()
viewer.addEventHandler( new FlyToExtentHandler( this ) );
viewer.addEventHandler( new KeyboardControlHandler( manip, mQGisIface ) );

viewer.addEventHandler( new QueryCoordinatesHandler( this,
mMapNode->getMap()->getProfile()->getSRS() )
);

// add some stock OSG handlers:
viewer.addEventHandler( new osgViewer::StatsHandler() );
viewer.addEventHandler( new osgViewer::WindowSizeHandler() );
@@ -345,6 +349,58 @@ bool FlyToExtentHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIAct
return false;
}

bool QueryCoordinatesHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
if ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH )
{
osgViewer::View* view = static_cast<osgViewer::View*>(aa.asView());
getCoords( ea.getX(), ea.getY(), view );
}

return false;
}

void QueryCoordinatesHandler::getCoords( float x, float y, osgViewer::View* view )
{
osgUtil::LineSegmentIntersector::Intersections results;
if ( view->computeIntersections( x, y, results, 0x01 ) )
{
// find the first hit under the mouse:
osgUtil::LineSegmentIntersector::Intersection first = *(results.begin());
osg::Vec3d point = first.getWorldIntersectPoint();

// transform it to map coordinates:
double lat_rad, lon_rad, height;
_mapSRS->getEllipsoid()->convertXYZToLatLongHeight( point.x(), point.y(), point.z(), lat_rad, lon_rad, height );

// query the elevation at the map point:
double lat_deg = osg::RadiansToDegrees( lat_rad );
double lon_deg = osg::RadiansToDegrees( lon_rad );

OE_NOTICE << "coords at " << lat_deg << ", " << lon_deg << std::endl ;

/* osg::Matrixd out_mat;
double query_resolution = 0.1; // 1/10th of a degree
double out_elevation = 0.0;
double out_resolution = 0.0;
if ( _elevMan->getPlacementMatrix(
lon_deg, lat_deg, 0,
query_resolution, NULL,
out_mat, out_elevation, out_resolution ) )
{
OE_NOTICE << "Elevation at " << lat_deg << ", " << lon_deg
<< " is " << out_elevation << std::endl;
}
else
{
OE_NOTICE
<< "getElevation FAILED! at (" << lat_deg << ", " << lon_deg << ")" << std::endl;
}
*/
}
}

void GlobePlugin::syncExtent()
{
osgEarthUtil::EarthManipulator* manip = dynamic_cast<osgEarthUtil::EarthManipulator*>( viewer.getCameraManipulator() );
@@ -362,7 +418,7 @@ void GlobePlugin::syncExtent()
double height = projectedExtent.height();
double distance = height / tan( viewAngle * osg::PI / 180 ); //c = b*cotan(B(rad))

OE_NOTICE << "map extent: " << height << "camera distance: " << distance << std::endl;
OE_NOTICE << "map extent: " << height << " camera distance: " << distance << std::endl;

osgEarthUtil::Viewpoint viewpoint( osg::Vec3d( extent.center().x(), extent.center().y(), 0.0 ), 0.0, -90.0, distance );
manip->setViewpoint( viewpoint, 4.0 );
@@ -129,6 +129,24 @@ class FlyToExtentHandler : public osgGA::GUIEventHandler
GlobePlugin* mGlobe;
};

// An event handler that will print out the coordinates at the clicked point
class QueryCoordinatesHandler : public osgGA::GUIEventHandler
{
public:
QueryCoordinatesHandler( GlobePlugin* globe, const osgEarth::SpatialReference* mapSRS )
: mGlobe ( globe ), _mapSRS( mapSRS ), _mouseDown( false ) { }

bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);

virtual void getCoords( float x, float y, osgViewer::View* view );

private:
GlobePlugin* mGlobe;
osg::ref_ptr<const SpatialReference> _mapSRS;
osg::ref_ptr<osgEarthUtil::ElevationManager> _elevMan;
bool _mouseDown;
};


class KeyboardControlHandler : public osgGA::GUIEventHandler
{

0 comments on commit 9bca3a7

Please sign in to comment.
You can’t perform that action at this time.