ENH: add CoordinateToName
authorEmmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Thu Jul 02 17:07:08 2009 +0800 (14 months ago)
changeset 58668e0668f2ed2a
parent 5865 9011890bd3c8
child 5867 b4b2fcdc50fa
ENH: add CoordinateToName
Code/Projections/otbCoordinateToName.h
Code/Projections/otbCoordinateToName.txx
Examples/Projections/CMakeLists.txt
Examples/Projections/CoordinateToNameExample.cxx
Examples/Projections/otbProjectionsExamplesTests2.cxx
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Code/Projections/otbCoordinateToName.h	Thu Jul 02 17:07:08 2009 +0800
     1.3 @@ -0,0 +1,83 @@
     1.4 +/*=========================================================================
     1.5 +
     1.6 +  Program:   ORFEO Toolbox
     1.7 +  Language:  C++
     1.8 +  Date:      $Date$
     1.9 +  Version:   $Revision$
    1.10 +
    1.11 +
    1.12 +  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
    1.13 +  See OTBCopyright.txt for details.
    1.14 +
    1.15 +
    1.16 +     This software is distributed WITHOUT ANY WARRANTY; without even
    1.17 +     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    1.18 +     PURPOSE.  See the above copyright notices for more information.
    1.19 +
    1.20 +=========================================================================*/
    1.21 +#ifndef __otbCoordinateToName_h
    1.22 +#define __otbCoordinateToName_h
    1.23 +
    1.24 +#include "itkObject.h"
    1.25 +#include "itkObjectFactory.h"
    1.26 +
    1.27 +namespace otb
    1.28 +{
    1.29 +
    1.30 +/**
    1.31 + * \class CoordinateToName
    1.32 + * \brief Retrieve Geographical information for Longitude and Latitude coordinates
    1.33 + *
    1.34 +
    1.35 + */
    1.36 +
    1.37 +
    1.38 +class ITK_EXPORT CoordinateToName : public itk::Object
    1.39 +{
    1.40 +public:
    1.41 +  /** Standard class typedefs. */
    1.42 +  typedef CoordinateToName                                      Self;
    1.43 +  typedef itk::SmartPointer<Self>                                Pointer;
    1.44 +  typedef itk::SmartPointer<const Self>                          ConstPointer;
    1.45 +
    1.46 +
    1.47 +  typedef itk::Object Superclass;
    1.48 +
    1.49 +  itkTypeMacro(CoordinateToName, Object);
    1.50 +  /** Method for creation through the object factory. */
    1.51 +  itkNewMacro(Self);
    1.52 +
    1.53 +  itkGetMacro( Lon, double );
    1.54 +  itkGetMacro( Lat, double );
    1.55 +  itkGetMacro( PlaceName, std::string );
    1.56 +  itkGetMacro( CountryName, std::string );
    1.57 +
    1.58 +  itkSetMacro( Lon, double );
    1.59 +  itkSetMacro( Lat, double );
    1.60 +
    1.61 +  virtual bool Evaluate();
    1.62 +
    1.63 +protected:
    1.64 +  CoordinateToName();
    1.65 +  ~CoordinateToName() {};
    1.66 +  void PrintSelf(std::ostream& os, itk::Indent indent) const;
    1.67 +  void RetrieveXML(std::ostringstream& urlStream);
    1.68 +  void ParseXMLGeonames();
    1.69 +
    1.70 +private:
    1.71 +  CoordinateToName( const Self& ); //purposely not implemented
    1.72 +  void operator=( const Self& ); //purposely not implemented
    1.73 +
    1.74 +  double m_Lon;
    1.75 +  double m_Lat;
    1.76 +  std::string m_PlaceName;
    1.77 +  std::string m_CountryName;
    1.78 +};
    1.79 +
    1.80 +} // namespace otb
    1.81 +
    1.82 +#ifndef OTB_MANUAL_INSTANTIATION
    1.83 +#include "otbCoordinateToName.txx"
    1.84 +#endif
    1.85 +
    1.86 +#endif
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Code/Projections/otbCoordinateToName.txx	Thu Jul 02 17:07:08 2009 +0800
     2.3 @@ -0,0 +1,146 @@
     2.4 +/*=========================================================================
     2.5 +
     2.6 +  Program:   ORFEO Toolbox
     2.7 +  Language:  C++
     2.8 +  Date:      $Date$
     2.9 +  Version:   $Revision$
    2.10 +
    2.11 +
    2.12 +  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
    2.13 +  See OTBCopyright.txt for details.
    2.14 +
    2.15 +
    2.16 +     This software is distributed WITHOUT ANY WARRANTY; without even
    2.17 +     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    2.18 +     PURPOSE.  See the above copyright notices for more information.
    2.19 +
    2.20 +=========================================================================*/
    2.21 +#ifndef __otbCoordinateToName_txx
    2.22 +#define __otbCoordinateToName_txx
    2.23 +
    2.24 +#include "otbCoordinateToName.h"
    2.25 +#include "tinyxml.h"
    2.26 +#include <curl/curl.h>
    2.27 +#include "otbMacro.h"
    2.28 +
    2.29 +namespace otb
    2.30 +{
    2.31 +
    2.32 +/**
    2.33 +   * Constructor
    2.34 +   */
    2.35 +
    2.36 +CoordinateToName::CoordinateToName()
    2.37 +{
    2.38 +  m_Lon = -1000.0;
    2.39 +  m_Lat = -1000.0;
    2.40 +  m_PlaceName = "";
    2.41 +  m_CountryName = "";
    2.42 +}
    2.43 +
    2.44 +/**
    2.45 +   *
    2.46 +   */
    2.47 +
    2.48 +void
    2.49 +CoordinateToName
    2.50 +::PrintSelf(std::ostream& os, itk::Indent indent) const
    2.51 +{
    2.52 +  this->Superclass::PrintSelf(os,indent);
    2.53 +  os << indent << " m_Lon "  << m_Lon << std::endl;
    2.54 +  os << indent << " m_Lat "  << m_Lat << std::endl;
    2.55 +  os << indent << " m_PlaceName "  << m_PlaceName << std::endl;
    2.56 +}
    2.57 +
    2.58 +
    2.59 +bool CoordinateToName::Evaluate()
    2.60 +{
    2.61 +
    2.62 +  std::ostringstream urlStream;
    2.63 +  urlStream << "http://ws.geonames.org/findNearbyPlaceName?lat=";
    2.64 +  urlStream << m_Lat;
    2.65 +  urlStream << "&lng=";
    2.66 +  urlStream << m_Lon;
    2.67 +  otbMsgDevMacro("CoordinateToName: retrieve url " << urlStream.str());
    2.68 +  RetrieveXML(urlStream);
    2.69 +  ParseXMLGeonames();
    2.70 +
    2.71 +  return true;
    2.72 +}
    2.73 +
    2.74 +/*
    2.75 +//This method will be necessary to process the file directly in memory
    2.76 +//without writing it to the disk. Waiting for the xml lib to handle that
    2.77 +//also
    2.78 +static size_t
    2.79 +curlHandlerWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
    2.80 +  void *data)
    2.81 +{
    2.82 +  register int realsize = (int)(size * nmemb);
    2.83 +
    2.84 +  std::vector<char> *vec
    2.85 +    = static_cast<std::vector<char>*>(data);
    2.86 +  const char* chPtr = static_cast<char*>(ptr);
    2.87 +  vec->insert(vec->end(), chPtr, chPtr + realsize);
    2.88 +
    2.89 +  return realsize;
    2.90 +}
    2.91 +*/
    2.92 +
    2.93 +void CoordinateToName::RetrieveXML(std::ostringstream& urlStream)
    2.94 +{
    2.95 +
    2.96 +  CURL *curl;
    2.97 +  CURLcode res;
    2.98 +
    2.99 +  FILE* output_file = fopen("out.xml","w");
   2.100 +  curl = curl_easy_init();
   2.101 +
   2.102 +//   std::cout << "URL data " << urlStream.str().data() << std::endl;
   2.103 +
   2.104 +
   2.105 +  char url[256];
   2.106 +  strcpy(url,urlStream.str().data());
   2.107 +
   2.108 +//   std::cout << url << std::endl;
   2.109 +  if (curl)
   2.110 +  {
   2.111 +    std::vector<char> chunk;
   2.112 +    curl_easy_setopt(curl, CURLOPT_URL, url);
   2.113 +    /*
   2.114 +    //Step needed to handle curl without temporary file
   2.115 +    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,this->curlHandlerWriteMemoryCallback);
   2.116 +    curl_easy_setopt(curl, CURLOPT_FILE, (void *)&chunk);
   2.117 +    */
   2.118 +    curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file);
   2.119 +    res = curl_easy_perform(curl);
   2.120 +
   2.121 +    fclose(output_file);
   2.122 +    /* always cleanup */
   2.123 +    curl_easy_cleanup(curl);
   2.124 +  }
   2.125 +
   2.126 +}
   2.127 +
   2.128 +
   2.129 +void CoordinateToName::ParseXMLGeonames()
   2.130 +{
   2.131 +  TiXmlDocument doc( "out.xml" );
   2.132 +  doc.LoadFile();
   2.133 +  TiXmlHandle docHandle( &doc );
   2.134 +
   2.135 +  TiXmlElement* childName = docHandle.FirstChild( "geonames" ).FirstChild( "geoname" ).FirstChild( "name" ).Element();
   2.136 +  if ( childName )
   2.137 +  {
   2.138 +    m_PlaceName=childName->GetText();
   2.139 +  }
   2.140 +  TiXmlElement* childCountryName = docHandle.FirstChild( "geonames" ).FirstChild( "geoname" ).FirstChild( "countryName" ).Element();
   2.141 +  if ( childCountryName )
   2.142 +  {
   2.143 +    m_CountryName=childCountryName->GetText();
   2.144 +  }
   2.145 +}
   2.146 +
   2.147 +} // namespace otb
   2.148 +
   2.149 +#endif
     3.1 --- a/Examples/Projections/CMakeLists.txt	Thu Jul 02 16:01:56 2009 +0800
     3.2 +++ b/Examples/Projections/CMakeLists.txt	Thu Jul 02 17:07:08 2009 +0800
     3.3 @@ -22,6 +22,10 @@
     3.4  IF( OTB_USE_CURL )
     3.5  ADD_EXECUTABLE(PlaceNameToLonLatExample PlaceNameToLonLatExample.cxx )
     3.6  TARGET_LINK_LIBRARIES(PlaceNameToLonLatExample OTBProjections OTBCommon OTBIO ${CURL_LIBRARY} tinyXML)
     3.7 +
     3.8 +ADD_EXECUTABLE(CoordinateToNameExample CoordinateToNameExample.cxx )
     3.9 +TARGET_LINK_LIBRARIES(CoordinateToNameExample OTBProjections OTBCommon OTBIO ${CURL_LIBRARY} tinyXML)
    3.10 +
    3.11  ENDIF( OTB_USE_CURL )
    3.12  
    3.13  ADD_EXECUTABLE(VectorDataProjectionExample VectorDataProjectionExample.cxx )
    3.14 @@ -111,6 +115,10 @@
    3.15      PlaceNameToLonLatExampleTest
    3.16          Toulouse
    3.17  )
    3.18 +ADD_TEST(prTeCoordinateToNameExampleTest ${EXE_TESTS2}
    3.19 +    CoordinateToNameExampleTest
    3.20 +         103.78 1.29
    3.21 +)
    3.22  ENDIF( OTB_USE_CURL )
    3.23  
    3.24  INCLUDE_DIRECTORIES(${OTB_SOURCE_DIR}/Testing/Code)
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/Examples/Projections/CoordinateToNameExample.cxx	Thu Jul 02 17:07:08 2009 +0800
     4.3 @@ -0,0 +1,52 @@
     4.4 +/*=========================================================================
     4.5 +
     4.6 +  Program:   ORFEO Toolbox
     4.7 +  Language:  C++
     4.8 +  Date:      $Date$
     4.9 +  Version:   $Revision$
    4.10 +
    4.11 +
    4.12 +  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
    4.13 +  See OTBCopyright.txt for details.
    4.14 +
    4.15 +
    4.16 +     This software is distributed WITHOUT ANY WARRANTY; without even
    4.17 +     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    4.18 +     PURPOSE.  See the above copyright notices for more information.
    4.19 +
    4.20 +=========================================================================*/
    4.21 +#if defined(_MSC_VER)
    4.22 +#pragma warning ( disable : 4786 )
    4.23 +#endif
    4.24 +
    4.25 +
    4.26 +#include "otbCoordinateToName.h"
    4.27 +
    4.28 +
    4.29 +int main( int argc, char* argv[] )
    4.30 +{
    4.31 +
    4.32 +  if (argc!=3)
    4.33 +  {
    4.34 +    std::cout << argv[0] <<" <lon> <lat>"
    4.35 +              << std::endl;
    4.36 +
    4.37 +    return EXIT_FAILURE;
    4.38 +  }
    4.39 +
    4.40 +
    4.41 +  otb::CoordinateToName::Pointer conv = otb::CoordinateToName::New();
    4.42 +  conv->SetLon(atof(argv[1]));
    4.43 +  conv->SetLat(atof(argv[2]));
    4.44 +  conv->Evaluate();
    4.45 +
    4.46 +  std::string name = conv->GetPlaceName();
    4.47 +  std::string country = conv->GetCountryName();
    4.48 +
    4.49 +  std::cout << "Nearby place: " << name << std::endl;
    4.50 +  std::cout << "Country: " << country << std::endl;
    4.51 +
    4.52 +
    4.53 +  return EXIT_SUCCESS;
    4.54 +
    4.55 +}
     5.1 --- a/Examples/Projections/otbProjectionsExamplesTests2.cxx	Thu Jul 02 16:01:56 2009 +0800
     5.2 +++ b/Examples/Projections/otbProjectionsExamplesTests2.cxx	Thu Jul 02 17:07:08 2009 +0800
     5.3 @@ -27,9 +27,12 @@
     5.4  void RegisterTests()
     5.5  {
     5.6    REGISTER_TEST(PlaceNameToLonLatExampleTest);
     5.7 +  REGISTER_TEST(CoordinateToNameExampleTest);
     5.8  }
     5.9  
    5.10  
    5.11  #undef main
    5.12  #define main PlaceNameToLonLatExampleTest
    5.13  #include "PlaceNameToLonLatExample.cxx"
    5.14 +#define main CoordinateToNameExampleTest
    5.15 +#include "CoordinateToNameExample.cxx"
    5.16 \ No newline at end of file