[Liblas-commits] r1056 - in trunk: include/liblas include/liblas/detail src src/detail

liblas-commits at liblas.org liblas-commits at liblas.org
Fri Feb 20 14:14:39 EST 2009


Author: hobu
Date: Fri Feb 20 14:14:39 2009
New Revision: 1056
URL: http://liblas.org/changeset/1056

Log:
add the ability to have Reader/Writer project on the way out #93

Modified:
   trunk/include/liblas/detail/reader.hpp
   trunk/include/liblas/detail/writer.hpp
   trunk/include/liblas/lasreader.hpp
   trunk/include/liblas/laswriter.hpp
   trunk/src/detail/reader.cpp
   trunk/src/detail/writer.cpp
   trunk/src/lasreader.cpp
   trunk/src/laswriter.cpp

Modified: trunk/include/liblas/detail/reader.hpp
==============================================================================
--- trunk/include/liblas/detail/reader.hpp	(original)
+++ trunk/include/liblas/detail/reader.hpp	Fri Feb 20 14:14:39 2009
@@ -43,7 +43,13 @@
 #define LIBLAS_DETAIL_READER_HPP_INCLUDED
 
 #include <liblas/cstdint.hpp>
+#include <liblas/lassrs.hpp>
 #include <liblas/detail/fwd.hpp>
+
+#ifndef HAVE_GDAL
+    typedef struct OGRCoordinateTransformationHS *OGRCoordinateTransformationH;
+#endif
+
 // std
 #include <iosfwd>
 
@@ -65,6 +71,7 @@
     std::istream& GetStream() const;
     bool ReadVLR(LASHeader& header);
     bool ReadGeoreference(LASHeader& header);
+    void SetSRS(const LASSRS& srs);
     
 protected:
     
@@ -75,6 +82,12 @@
     uint32_t m_recordlength;
 
     void FillPoint(PointRecord& record, LASPoint& point);
+    void Project(LASPoint& point);
+    
+    LASSRS m_out_srs;
+    LASSRS m_in_srs;
+    
+    OGRCoordinateTransformationH m_transform;
 
 private:
 

Modified: trunk/include/liblas/detail/writer.hpp
==============================================================================
--- trunk/include/liblas/detail/writer.hpp	(original)
+++ trunk/include/liblas/detail/writer.hpp	Fri Feb 20 14:14:39 2009
@@ -42,9 +42,14 @@
 #ifndef LIBLAS_DETAIL_WRITER_HPP_INCLUDED
 #define LIBLAS_DETAIL_WRITER_HPP_INCLUDED
 
+#include <liblas/lassrs.hpp>
 #include <liblas/detail/fwd.hpp>
 #include <liblas/detail/utility.hpp>
 
+#ifndef HAVE_GDAL
+    typedef struct OGRCoordinateTransformationHS *OGRCoordinateTransformationH;
+#endif
+
 // std
 #include <iosfwd>
 
@@ -63,19 +68,25 @@
     std::ostream& GetStream() const;
     void WriteVLR(LASHeader const& header);
 
+    void SetSRS(const LASSRS& srs);
+    
 protected:
     PointRecord m_record;
     std::ostream& m_ofs;
 
     void FillPointRecord(PointRecord& record, const LASPoint& point, const LASHeader& header);
 
-
+    void Project(PointRecord& point);      
+    LASSRS m_out_srs;
+    LASSRS m_in_srs;
+    
+    OGRCoordinateTransformationH m_transform;
+    
 private:
 
     // Blocked copying operations, declared but not defined.
     Writer(Writer const& other);
     Writer& operator=(Writer const& rhs);
-
     
 };
 

Modified: trunk/include/liblas/lasreader.hpp
==============================================================================
--- trunk/include/liblas/lasreader.hpp	(original)
+++ trunk/include/liblas/lasreader.hpp	Fri Feb 20 14:14:39 2009
@@ -46,6 +46,7 @@
 #include <liblas/lasheader.hpp>
 #include <liblas/laspoint.hpp>
 #include <liblas/lasrecordheader.hpp>
+#include <liblas/lassrs.hpp>
 #include <liblas/detail/fwd.hpp>
 // std
 #include <iosfwd>
@@ -72,6 +73,10 @@
     bool ReadPointAt(std::size_t n);
     bool ReadVLR();
 
+    // Reproject data as they are written if the LASWriter's reference is
+    // different than the LASHeader's    
+    bool SetSRS(const LASSRS& ref);
+
     // The operator is not const because it updates file stream position.
     LASPoint const& operator[](std::size_t n);
 

Modified: trunk/include/liblas/laswriter.hpp
==============================================================================
--- trunk/include/liblas/laswriter.hpp	(original)
+++ trunk/include/liblas/laswriter.hpp	Fri Feb 20 14:14:39 2009
@@ -74,6 +74,11 @@
     
     // Allow in-place writing of header
     void WriteHeader(LASHeader& header);
+
+    // Reproject data as they are written if the LASWriter's reference is
+    // different than the LASHeader's
+    bool SetSRS(const LASSRS& ref);
+    
     
 private:
     
@@ -85,6 +90,7 @@
 
     LASHeader m_header;
     detail::PointRecord m_record;
+
 };
 
 } // namespace liblas

Modified: trunk/src/detail/reader.cpp
==============================================================================
--- trunk/src/detail/reader.cpp	(original)
+++ trunk/src/detail/reader.cpp	Fri Feb 20 14:14:39 2009
@@ -45,15 +45,19 @@
 #include <liblas/detail/reader12.hpp>
 #include <liblas/lasheader.hpp>
 #include <liblas/laspoint.hpp>
-
-// GeoTIFF
-#ifdef HAVE_LIBGEOTIFF
-#include <geotiff.h>
-#include <geo_simpletags.h>
-#include "geo_normalize.h"
-#include "geo_simpletags.h"
-#include "geovalues.h"
-#endif // HAVE_LIBGEOTIFF
+// 
+// // GeoTIFF
+// #ifdef HAVE_LIBGEOTIFF
+// #include <geotiff.h>
+// #include <geo_simpletags.h>
+// #include "geo_normalize.h"
+// #include "geo_simpletags.h"
+// #include "geovalues.h"
+// #endif // HAVE_LIBGEOTIFF
+
+#ifdef HAVE_GDAL
+#include <ogr_srs_api.h>
+#endif
 
 // std
 #include <fstream>
@@ -63,12 +67,17 @@
 
 namespace liblas { namespace detail {
 
-Reader::Reader(std::istream& ifs) : m_ifs(ifs), m_offset(0), m_current(0)
+Reader::Reader(std::istream& ifs) : m_ifs(ifs), m_offset(0), m_current(0), m_transform(0)
 {
 }
 
 Reader::~Reader()
 {
+#ifdef HAVE_GDAL
+    if (m_transform) {
+        OCTDestroyCoordinateTransformation(m_transform);
+    }
+#endif
 }
 
 std::istream& Reader::GetStream() const
@@ -81,6 +90,8 @@
     point.SetX(record.x);
     point.SetY(record.y);
     point.SetZ(record.z);
+    
+    if (m_transform) Project(point);
     point.SetIntensity(record.intensity);
     point.SetScanFlags(record.flags);
     point.SetClassification(record.classification);
@@ -127,11 +138,6 @@
 
 bool Reader::ReadGeoreference(LASHeader& header)
 {
-// #ifndef HAVE_LIBGEOTIFF
-//     UNREFERENCED_PARAMETER(header);
-// #else
-
-
 
     std::vector<LASVLR> vlrs;
     for (uint16_t i = 0; i < header.GetRecordsCount(); ++i)
@@ -143,22 +149,67 @@
     LASSRS srs(vlrs);
     
     header.SetSRS(srs);
+
+    // keep a copy on the reader in case we're going to reproject data 
+    // on the way out.
+    m_in_srs = srs;
     
     if (vlrs.size()) {
         if (srs.GetProj4().size())
             header.SetProj4(srs.GetProj4());
     }
     
-    // std::cout << srs.GetWKT() << std::endl;
+    return true;
+
+}
+
+void Reader::SetSRS(const LASSRS& srs)
+{
+    m_out_srs = srs;
+#ifdef HAVE_GDAL
+    OGRSpatialReferenceH in_ref = OSRNewSpatialReference(NULL);
+    OGRSpatialReferenceH out_ref = OSRNewSpatialReference(NULL);
+
+    const char* in_wkt = m_in_srs.GetWKT().c_str();
+    if (OSRImportFromWkt(in_ref, (char**) &in_wkt) != OGRERR_NONE) 
+    {
+        throw std::runtime_error("Could not import input spatial reference for Reader::");
+    }
     
-        
-        return true;
+    const char* out_wkt = m_out_srs.GetWKT().c_str();
+    if (OSRImportFromWkt(out_ref, (char**) &out_wkt) != OGRERR_NONE) 
+    {
+        throw std::runtime_error("Could not import output spatial reference for Reader::");
+    }
 
-//#endif /* def HAVE_LIBGEOTIFF */
+    m_transform = OCTNewCoordinateTransformation( in_ref, out_ref);
+    
+#endif
+}
 
-//    return false;
+void Reader::Project(LASPoint& point)
+{
+#ifdef HAVE_GDAL
+    
+    int ret = 0;
+    double x = point.GetX();
+    double y = point.GetY();
+    double z = point.GetZ();
+    
+    ret = OCTTransform(m_transform, 1, &x, &y, &z);
+    
+    if (ret != OGRERR_NONE) {
+        throw std::runtime_error("could not project point!");
+    }
+    
+    point.SetX(x);
+    point.SetY(y);
+    point.SetZ(z);
+    
+#endif
 }
 
+
 Reader* ReaderFactory::Create(std::istream& ifs)
 {
     if (!ifs)

Modified: trunk/src/detail/writer.cpp
==============================================================================
--- trunk/src/detail/writer.cpp	(original)
+++ trunk/src/detail/writer.cpp	Fri Feb 20 14:14:39 2009
@@ -45,6 +45,13 @@
 #include <liblas/detail/writer12.hpp>
 #include <liblas/lasheader.hpp>
 #include <liblas/laspoint.hpp>
+#include <liblas/lassrs.hpp>
+
+#ifdef HAVE_GDAL
+#include <ogr_srs_api.h>
+#endif
+
+
 // std
 #include <fstream>
 #include <cassert>
@@ -53,12 +60,17 @@
 
 namespace liblas { namespace detail {
 
-Writer::Writer(std::ostream& ofs) : m_ofs(ofs)
+Writer::Writer(std::ostream& ofs) : m_ofs(ofs), m_transform(0)
 {
 }
 
 Writer::~Writer()
 {
+#ifdef HAVE_GDAL
+    if (m_transform) {
+        OCTDestroyCoordinateTransformation(m_transform);
+    }    
+#endif
 }
 
 std::ostream& Writer::GetStream() const
@@ -72,6 +84,9 @@
     record.x = static_cast<int32_t>((point.GetX() - header.GetOffsetX()) / header.GetScaleX());
     record.y = static_cast<int32_t>((point.GetY() - header.GetOffsetY()) / header.GetScaleY());
     record.z = static_cast<int32_t>((point.GetZ() - header.GetOffsetZ()) / header.GetScaleZ());
+
+    if (m_transform) Project(record);
+
     record.intensity = point.GetIntensity();
     record.flags = point.GetScanFlags();
     record.classification = point.GetClassification();
@@ -99,6 +114,51 @@
     }
 }
 
+void Writer::SetSRS(const LASSRS& srs)
+{
+    m_out_srs = srs;
+#ifdef HAVE_GDAL
+    OGRSpatialReferenceH in_ref = OSRNewSpatialReference(NULL);
+    OGRSpatialReferenceH out_ref = OSRNewSpatialReference(NULL);
+
+    const char* in_wkt = m_in_srs.GetWKT().c_str();
+    if (OSRImportFromWkt(in_ref, (char**) &in_wkt) != OGRERR_NONE) 
+    {
+        throw std::runtime_error("Could not import input spatial reference for Reader::");
+    }
+    
+    const char* out_wkt = m_out_srs.GetWKT().c_str();
+    if (OSRImportFromWkt(out_ref, (char**) &out_wkt) != OGRERR_NONE) 
+    {
+        throw std::runtime_error("Could not import output spatial reference for Reader::");
+    }
+
+    m_transform = OCTNewCoordinateTransformation( in_ref, out_ref);
+    
+#endif
+}
+
+void Writer::Project(PointRecord& point)
+{
+#ifdef HAVE_GDAL
+    
+    int ret = 0;
+    double x = point.x;
+    double y = point.y;
+    double z = point.z;
+    
+    ret = OCTTransform(m_transform, 1, &x, &y, &z);
+    
+    if (ret != OGRERR_NONE) {
+        throw std::runtime_error("could not project point!");
+    }
+    
+    point.x = x;
+    point.y = y;
+    point.z = z;
+    
+#endif
+}
 Writer* WriterFactory::Create(std::ostream& ofs, LASHeader const& header)
 {
     if (!ofs)

Modified: trunk/src/lasreader.cpp
==============================================================================
--- trunk/src/lasreader.cpp	(original)
+++ trunk/src/lasreader.cpp	Fri Feb 20 14:14:39 2009
@@ -157,5 +157,12 @@
     return GetStream().eof();
 }
 
+bool LASReader::SetSRS(const LASSRS& srs)
+{
+    m_pimpl->SetSRS(srs);
+    return true;
+}
+
+
 } // namespace liblas
 

Modified: trunk/src/laswriter.cpp
==============================================================================
--- trunk/src/laswriter.cpp	(original)
+++ trunk/src/laswriter.cpp	Fri Feb 20 14:14:39 2009
@@ -97,5 +97,12 @@
     m_header = header;
 }
 
+bool LASWriter::SetSRS(const LASSRS& srs)
+{
+    m_pimpl->SetSRS(srs);
+    return true;
+}
+
+
 } // namespace liblas
 


More information about the Liblas-commits mailing list