Commit 47186acb authored by Emmanuel Christophe's avatar Emmanuel Christophe

ENH: add CoordinateToName

parent 53834270
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbCoordinateToName_h
#define __otbCoordinateToName_h
#include "itkObject.h"
#include "itkObjectFactory.h"
namespace otb
{
/**
* \class CoordinateToName
* \brief Retrieve Geographical information for Longitude and Latitude coordinates
*
*/
class ITK_EXPORT CoordinateToName : public itk::Object
{
public:
/** Standard class typedefs. */
typedef CoordinateToName Self;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef itk::Object Superclass;
itkTypeMacro(CoordinateToName, Object);
/** Method for creation through the object factory. */
itkNewMacro(Self);
itkGetMacro( Lon, double );
itkGetMacro( Lat, double );
itkGetMacro( PlaceName, std::string );
itkGetMacro( CountryName, std::string );
itkSetMacro( Lon, double );
itkSetMacro( Lat, double );
virtual bool Evaluate();
protected:
CoordinateToName();
~CoordinateToName() {};
void PrintSelf(std::ostream& os, itk::Indent indent) const;
void RetrieveXML(std::ostringstream& urlStream);
void ParseXMLGeonames();
private:
CoordinateToName( const Self& ); //purposely not implemented
void operator=( const Self& ); //purposely not implemented
double m_Lon;
double m_Lat;
std::string m_PlaceName;
std::string m_CountryName;
};
} // namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbCoordinateToName.txx"
#endif
#endif
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbCoordinateToName_txx
#define __otbCoordinateToName_txx
#include "otbCoordinateToName.h"
#include "tinyxml.h"
#include <curl/curl.h>
#include "otbMacro.h"
namespace otb
{
/**
* Constructor
*/
CoordinateToName::CoordinateToName()
{
m_Lon = -1000.0;
m_Lat = -1000.0;
m_PlaceName = "";
m_CountryName = "";
}
/**
*
*/
void
CoordinateToName
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
this->Superclass::PrintSelf(os,indent);
os << indent << " m_Lon " << m_Lon << std::endl;
os << indent << " m_Lat " << m_Lat << std::endl;
os << indent << " m_PlaceName " << m_PlaceName << std::endl;
}
bool CoordinateToName::Evaluate()
{
std::ostringstream urlStream;
urlStream << "http://ws.geonames.org/findNearbyPlaceName?lat=";
urlStream << m_Lat;
urlStream << "&lng=";
urlStream << m_Lon;
otbMsgDevMacro("CoordinateToName: retrieve url " << urlStream.str());
RetrieveXML(urlStream);
ParseXMLGeonames();
return true;
}
/*
//This method will be necessary to process the file directly in memory
//without writing it to the disk. Waiting for the xml lib to handle that
//also
static size_t
curlHandlerWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
void *data)
{
register int realsize = (int)(size * nmemb);
std::vector<char> *vec
= static_cast<std::vector<char>*>(data);
const char* chPtr = static_cast<char*>(ptr);
vec->insert(vec->end(), chPtr, chPtr + realsize);
return realsize;
}
*/
void CoordinateToName::RetrieveXML(std::ostringstream& urlStream)
{
CURL *curl;
CURLcode res;
FILE* output_file = fopen("out.xml","w");
curl = curl_easy_init();
// std::cout << "URL data " << urlStream.str().data() << std::endl;
char url[256];
strcpy(url,urlStream.str().data());
// std::cout << url << std::endl;
if (curl)
{
std::vector<char> chunk;
curl_easy_setopt(curl, CURLOPT_URL, url);
/*
//Step needed to handle curl without temporary file
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,this->curlHandlerWriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_FILE, (void *)&chunk);
*/
curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file);
res = curl_easy_perform(curl);
fclose(output_file);
/* always cleanup */
curl_easy_cleanup(curl);
}
}
void CoordinateToName::ParseXMLGeonames()
{
TiXmlDocument doc( "out.xml" );
doc.LoadFile();
TiXmlHandle docHandle( &doc );
TiXmlElement* childName = docHandle.FirstChild( "geonames" ).FirstChild( "geoname" ).FirstChild( "name" ).Element();
if ( childName )
{
m_PlaceName=childName->GetText();
}
TiXmlElement* childCountryName = docHandle.FirstChild( "geonames" ).FirstChild( "geoname" ).FirstChild( "countryName" ).Element();
if ( childCountryName )
{
m_CountryName=childCountryName->GetText();
}
}
} // namespace otb
#endif
......@@ -22,6 +22,10 @@ TARGET_LINK_LIBRARIES(OrthoRectificationExample OTBProjections OTBCommon OTBIO)
IF( OTB_USE_CURL )
ADD_EXECUTABLE(PlaceNameToLonLatExample PlaceNameToLonLatExample.cxx )
TARGET_LINK_LIBRARIES(PlaceNameToLonLatExample OTBProjections OTBCommon OTBIO ${CURL_LIBRARY} tinyXML)
ADD_EXECUTABLE(CoordinateToNameExample CoordinateToNameExample.cxx )
TARGET_LINK_LIBRARIES(CoordinateToNameExample OTBProjections OTBCommon OTBIO ${CURL_LIBRARY} tinyXML)
ENDIF( OTB_USE_CURL )
ADD_EXECUTABLE(VectorDataProjectionExample VectorDataProjectionExample.cxx )
......@@ -111,6 +115,10 @@ ADD_TEST(prTePlaceNameToLonLatExampleTest ${EXE_TESTS2}
PlaceNameToLonLatExampleTest
Toulouse
)
ADD_TEST(prTeCoordinateToNameExampleTest ${EXE_TESTS2}
CoordinateToNameExampleTest
103.78 1.29
)
ENDIF( OTB_USE_CURL )
INCLUDE_DIRECTORIES(${OTB_SOURCE_DIR}/Testing/Code)
......
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "otbCoordinateToName.h"
int main( int argc, char* argv[] )
{
if (argc!=3)
{
std::cout << argv[0] <<" <lon> <lat>"
<< std::endl;
return EXIT_FAILURE;
}
otb::CoordinateToName::Pointer conv = otb::CoordinateToName::New();
conv->SetLon(atof(argv[1]));
conv->SetLat(atof(argv[2]));
conv->Evaluate();
std::string name = conv->GetPlaceName();
std::string country = conv->GetCountryName();
std::cout << "Nearby place: " << name << std::endl;
std::cout << "Country: " << country << std::endl;
return EXIT_SUCCESS;
}
......@@ -27,9 +27,12 @@
void RegisterTests()
{
REGISTER_TEST(PlaceNameToLonLatExampleTest);
REGISTER_TEST(CoordinateToNameExampleTest);
}
#undef main
#define main PlaceNameToLonLatExampleTest
#include "PlaceNameToLonLatExample.cxx"
#define main CoordinateToNameExampleTest
#include "CoordinateToNameExample.cxx"
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment