ENH: preparing the name info in viewer
authorEmmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Thu Jul 02 17:41:46 2009 +0800 (14 months ago)
changeset 5867b4b2fcdc50fa
parent 5866 8e0668f2ed2a
child 5868 38ff65becdca
ENH: preparing the name info in viewer
Code/Projections/CMakeLists.txt
Code/Projections/otbCoordinateToName.cxx
Code/Projections/otbCoordinateToName.h
Code/Projections/otbCoordinateToName.txx
Code/Visualization/otbImageLayer.h
Code/Visualization/otbImageLayer.txx
     1.1 --- a/Code/Projections/CMakeLists.txt	Thu Jul 02 17:07:08 2009 +0800
     1.2 +++ b/Code/Projections/CMakeLists.txt	Thu Jul 02 17:41:46 2009 +0800
     1.3 @@ -1,10 +1,14 @@
     1.4  FILE(GLOB OTBProjections_SRCS "*.cxx" )
     1.5  
     1.6 +IF( NOT OTB_USE_CURL )
     1.7 +    LIST(REMOVE_ITEM OTBProjections_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/otbCoordinateToName.cxx" )
     1.8 +ENDIF( NOT OTB_USE_CURL )
     1.9 +
    1.10  ADD_LIBRARY(OTBProjections ${OTBProjections_SRCS})
    1.11  # TARGET_LINK_LIBRARIES(OTBProjections OTBIO otbossimplugins)
    1.12  TARGET_LINK_LIBRARIES(OTBProjections OTBIO)
    1.13  IF( OTB_USE_CURL )
    1.14 -    TARGET_LINK_LIBRARIES (OTBProjections ${CURL_LIBRARY})
    1.15 +    TARGET_LINK_LIBRARIES (OTBProjections ${CURL_LIBRARY} tinyXML)
    1.16  ENDIF( OTB_USE_CURL )
    1.17  IF(OTB_LIBRARY_PROPERTIES)
    1.18    SET_TARGET_PROPERTIES(OTBProjections PROPERTIES ${OTB_LIBRARY_PROPERTIES})
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Code/Projections/otbCoordinateToName.cxx	Thu Jul 02 17:41:46 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/Code/Projections/otbCoordinateToName.h	Thu Jul 02 17:07:08 2009 +0800
     3.2 +++ b/Code/Projections/otbCoordinateToName.h	Thu Jul 02 17:41:46 2009 +0800
     3.3 @@ -76,8 +76,5 @@
     3.4  
     3.5  } // namespace otb
     3.6  
     3.7 -#ifndef OTB_MANUAL_INSTANTIATION
     3.8 -#include "otbCoordinateToName.txx"
     3.9 -#endif
    3.10  
    3.11  #endif
     4.1 --- a/Code/Projections/otbCoordinateToName.txx	Thu Jul 02 17:07:08 2009 +0800
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,146 +0,0 @@
     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 -#ifndef __otbCoordinateToName_txx
    4.22 -#define __otbCoordinateToName_txx
    4.23 -
    4.24 -#include "otbCoordinateToName.h"
    4.25 -#include "tinyxml.h"
    4.26 -#include <curl/curl.h>
    4.27 -#include "otbMacro.h"
    4.28 -
    4.29 -namespace otb
    4.30 -{
    4.31 -
    4.32 -/**
    4.33 -   * Constructor
    4.34 -   */
    4.35 -
    4.36 -CoordinateToName::CoordinateToName()
    4.37 -{
    4.38 -  m_Lon = -1000.0;
    4.39 -  m_Lat = -1000.0;
    4.40 -  m_PlaceName = "";
    4.41 -  m_CountryName = "";
    4.42 -}
    4.43 -
    4.44 -/**
    4.45 -   *
    4.46 -   */
    4.47 -
    4.48 -void
    4.49 -CoordinateToName
    4.50 -::PrintSelf(std::ostream& os, itk::Indent indent) const
    4.51 -{
    4.52 -  this->Superclass::PrintSelf(os,indent);
    4.53 -  os << indent << " m_Lon "  << m_Lon << std::endl;
    4.54 -  os << indent << " m_Lat "  << m_Lat << std::endl;
    4.55 -  os << indent << " m_PlaceName "  << m_PlaceName << std::endl;
    4.56 -}
    4.57 -
    4.58 -
    4.59 -bool CoordinateToName::Evaluate()
    4.60 -{
    4.61 -
    4.62 -  std::ostringstream urlStream;
    4.63 -  urlStream << "http://ws.geonames.org/findNearbyPlaceName?lat=";
    4.64 -  urlStream << m_Lat;
    4.65 -  urlStream << "&lng=";
    4.66 -  urlStream << m_Lon;
    4.67 -  otbMsgDevMacro("CoordinateToName: retrieve url " << urlStream.str());
    4.68 -  RetrieveXML(urlStream);
    4.69 -  ParseXMLGeonames();
    4.70 -
    4.71 -  return true;
    4.72 -}
    4.73 -
    4.74 -/*
    4.75 -//This method will be necessary to process the file directly in memory
    4.76 -//without writing it to the disk. Waiting for the xml lib to handle that
    4.77 -//also
    4.78 -static size_t
    4.79 -curlHandlerWriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
    4.80 -  void *data)
    4.81 -{
    4.82 -  register int realsize = (int)(size * nmemb);
    4.83 -
    4.84 -  std::vector<char> *vec
    4.85 -    = static_cast<std::vector<char>*>(data);
    4.86 -  const char* chPtr = static_cast<char*>(ptr);
    4.87 -  vec->insert(vec->end(), chPtr, chPtr + realsize);
    4.88 -
    4.89 -  return realsize;
    4.90 -}
    4.91 -*/
    4.92 -
    4.93 -void CoordinateToName::RetrieveXML(std::ostringstream& urlStream)
    4.94 -{
    4.95 -
    4.96 -  CURL *curl;
    4.97 -  CURLcode res;
    4.98 -
    4.99 -  FILE* output_file = fopen("out.xml","w");
   4.100 -  curl = curl_easy_init();
   4.101 -
   4.102 -//   std::cout << "URL data " << urlStream.str().data() << std::endl;
   4.103 -
   4.104 -
   4.105 -  char url[256];
   4.106 -  strcpy(url,urlStream.str().data());
   4.107 -
   4.108 -//   std::cout << url << std::endl;
   4.109 -  if (curl)
   4.110 -  {
   4.111 -    std::vector<char> chunk;
   4.112 -    curl_easy_setopt(curl, CURLOPT_URL, url);
   4.113 -    /*
   4.114 -    //Step needed to handle curl without temporary file
   4.115 -    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,this->curlHandlerWriteMemoryCallback);
   4.116 -    curl_easy_setopt(curl, CURLOPT_FILE, (void *)&chunk);
   4.117 -    */
   4.118 -    curl_easy_setopt(curl, CURLOPT_WRITEDATA, output_file);
   4.119 -    res = curl_easy_perform(curl);
   4.120 -
   4.121 -    fclose(output_file);
   4.122 -    /* always cleanup */
   4.123 -    curl_easy_cleanup(curl);
   4.124 -  }
   4.125 -
   4.126 -}
   4.127 -
   4.128 -
   4.129 -void CoordinateToName::ParseXMLGeonames()
   4.130 -{
   4.131 -  TiXmlDocument doc( "out.xml" );
   4.132 -  doc.LoadFile();
   4.133 -  TiXmlHandle docHandle( &doc );
   4.134 -
   4.135 -  TiXmlElement* childName = docHandle.FirstChild( "geonames" ).FirstChild( "geoname" ).FirstChild( "name" ).Element();
   4.136 -  if ( childName )
   4.137 -  {
   4.138 -    m_PlaceName=childName->GetText();
   4.139 -  }
   4.140 -  TiXmlElement* childCountryName = docHandle.FirstChild( "geonames" ).FirstChild( "geoname" ).FirstChild( "countryName" ).Element();
   4.141 -  if ( childCountryName )
   4.142 -  {
   4.143 -    m_CountryName=childCountryName->GetText();
   4.144 -  }
   4.145 -}
   4.146 -
   4.147 -} // namespace otb
   4.148 -
   4.149 -#endif
     5.1 --- a/Code/Visualization/otbImageLayer.h	Thu Jul 02 17:07:08 2009 +0800
     5.2 +++ b/Code/Visualization/otbImageLayer.h	Thu Jul 02 17:41:46 2009 +0800
     5.3 @@ -207,7 +207,6 @@
     5.4    unsigned int PixelSize(ImagePointerType image, RGBPixelType* v) const;
     5.5    unsigned int PixelSize(ImagePointerType image, RGBAPixelType* v) const;
     5.6  
     5.7 -
     5.8  private:
     5.9    ImageLayer(const Self&);     // purposely not implemented
    5.10    void operator=(const Self&); // purposely not implemented
    5.11 @@ -236,6 +235,10 @@
    5.12    /** Coordinate transform */
    5.13    TransformType::Pointer m_Transform;
    5.14  
    5.15 +  /** General info about the image*/
    5.16 +  std::string m_PlaceName;//FIXME the call should be done by a more general method outside of the layer
    5.17 +  std::string m_CountryName;//which would also handle the dependance to curl
    5.18 +
    5.19  }; // end class
    5.20  } // end namespace otb
    5.21  
     6.1 --- a/Code/Visualization/otbImageLayer.txx	Thu Jul 02 17:07:08 2009 +0800
     6.2 +++ b/Code/Visualization/otbImageLayer.txx	Thu Jul 02 17:41:46 2009 +0800
     6.3 @@ -24,6 +24,7 @@
     6.4  #include "otbStandardRenderingFunction.h"
     6.5  
     6.6  #include "otbImageKeywordlist.h"
     6.7 +// #include "otbCoordinateToName.h"
     6.8  
     6.9  namespace otb
    6.10  {
    6.11 @@ -58,6 +59,9 @@
    6.12    m_ScaledExtractRenderingFilter->SetInput(m_ScaledExtractFilter->GetOutput());
    6.13  
    6.14    m_Transform = TransformType::New();
    6.15 +
    6.16 +  m_PlaceName = "";
    6.17 +  m_CountryName = "";
    6.18  }
    6.19  
    6.20  template <class TImage, class TOutputImage>
    6.21 @@ -243,6 +247,19 @@
    6.22        oss<< setiosflags(ios::fixed) << setprecision(6) << "Lon: " << point[0] << " Lat: "<< point[1] << std::endl;
    6.23        if (m_Transform->GetTransformAccuracy() == PRECISE) oss<< "(precise location)" << std::endl;
    6.24        if (m_Transform->GetTransformAccuracy() == ESTIMATE) oss<< "(estimated location)" << std::endl;
    6.25 +
    6.26 +//       if ((m_PlaceName == "") && (m_CountryName == ""))
    6.27 +//       {
    6.28 +//         CoordinateToName::Pointer conv = CoordinateToName::New();
    6.29 +//         conv->SetLon(point[0]);
    6.30 +//         conv->SetLat(point[1]);
    6.31 +//         conv->Evaluate();
    6.32 +//
    6.33 +//         m_PlaceName = conv->GetPlaceName();
    6.34 +//         m_CountryName = conv->GetCountryName();
    6.35 +//       }
    6.36 +//       if (m_PlaceName != "") oss << "Near " << m_PlaceName;
    6.37 +//       if (m_CountryName != "") oss << " in " << m_CountryName;
    6.38      }
    6.39      else
    6.40      {
    6.41 @@ -263,6 +280,7 @@
    6.42    return m_Transform->TransformPoint(inputPoint);
    6.43  }
    6.44  
    6.45 +
    6.46  template <class TImage, class TOutputImage>
    6.47  void
    6.48  ImageLayer<TImage,TOutputImage>