[Liblas-commits] hg: add a SetFromUserInput function to the LASSpatialReference o...

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Oct 1 13:01:24 EDT 2009


changeset b959e0a380d7 in /home/www/liblas.org/hg
details: http://hg.liblas.org/main/hg?cmd=changeset;node=b959e0a380d7
summary: add a SetFromUserInput function to the LASSpatialReference object to allow us to piggyback on GDAL for setting the SRS from a file or whatnot

diffstat:

 include/liblas/capi/liblas.h           |   1 +
 include/liblas/lasspatialreference.hpp |   7 ++++++-
 python/liblas/core.py                  |   3 +++
 python/liblas/srs.py                   |   3 +++
 python/tests/SRS.txt                   |  16 ++++++++++++++++
 src/las_c_api.cpp                      |  16 ++++++++++++++++
 src/lasspatialreference.cpp            |  28 +++++++++++++++++++++++++++-
 7 files changed, 72 insertions(+), 2 deletions(-)

diffs (144 lines):

diff -r 94e8c45b4cb1 -r b959e0a380d7 include/liblas/capi/liblas.h
--- a/include/liblas/capi/liblas.h	Thu Oct 01 11:33:34 2009 -0500
+++ b/include/liblas/capi/liblas.h	Thu Oct 01 11:58:43 2009 -0500
@@ -1109,6 +1109,7 @@
 LAS_DLL const GTIF* LASSRS_GetGTIF(LASSRSH hSRS);
 LAS_DLL char* LASSRS_GetWKT(LASSRSH hSRS);
 LAS_DLL LASError LASSRS_SetWKT(LASSRSH hSRS, const char* value);
+LAS_DLL LASError LASSRS_SetFromUserInput(LASSRSH hSRS, const char* value);
 LAS_DLL char* LASSRS_GetProj4(LASSRSH hSRS);
 LAS_DLL LASError LASSRS_SetProj4(LASSRSH hSRS, const char* value);
 LAS_DLL LASSRSH LASHeader_GetSRS(const LASHeaderH hHeader);
diff -r 94e8c45b4cb1 -r b959e0a380d7 include/liblas/lasspatialreference.hpp
--- a/include/liblas/lasspatialreference.hpp	Thu Oct 01 11:33:34 2009 -0500
+++ b/include/liblas/lasspatialreference.hpp	Thu Oct 01 11:58:43 2009 -0500
@@ -122,7 +122,12 @@
     /// operation has no effect.
     /// \param v - a string containing the WKT string.  
     void SetWKT(std::string const& v);
-    
+
+    /// Sets the SRS using GDAL's SetFromUserInput function. If GDAL is not linked, this 
+    /// operation has no effect.
+    /// \param v - a string containing the definition (filename, proj4, wkt, etc).  
+    void SetFromUserInput(std::string const& v);
+        
     /// Returns the Proj.4 string describing the Spatial Reference System.
     /// If GDAL is linked, it uses GDAL's operations and methods to determine 
     /// the Proj.4 string -- otherwise, if libgeotiff is linked, it uses 
diff -r 94e8c45b4cb1 -r b959e0a380d7 python/liblas/core.py
--- a/python/liblas/core.py	Thu Oct 01 11:33:34 2009 -0500
+++ b/python/liblas/core.py	Thu Oct 01 11:58:43 2009 -0500
@@ -629,6 +629,9 @@
 las.LASSRS_SetWKT.restype = ctypes.c_int
 las.LASSRS_SetWKT.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
 las.LASSRS_SetWKT.errcheck = check_return
+las.LASSRS_SetFromUserInput.restype = ctypes.c_int
+las.LASSRS_SetFromUserInput.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
+las.LASSRS_SetFromUserInput.errcheck = check_return
 
 las.LASSRS_AddVLR.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
 las.LASSRS_AddVLR.errcheck = check_value
diff -r 94e8c45b4cb1 -r b959e0a380d7 python/liblas/srs.py
--- a/python/liblas/srs.py	Thu Oct 01 11:33:34 2009 -0500
+++ b/python/liblas/srs.py	Thu Oct 01 11:58:43 2009 -0500
@@ -62,6 +62,9 @@
         return core.las.LASSRS_SetWKT(self.handle, value)
     wkt = property(get_wkt, set_wkt)
     
+    def set_userinput(self, value):
+        return core.las.LASSRS_SetFromUserInput(self.handle, value)
+    
     def get_proj4(self):
         return core.las.LASSRS_GetProj4(self.handle)
     def set_proj4(self, value):
diff -r 94e8c45b4cb1 -r b959e0a380d7 python/tests/SRS.txt
--- a/python/tests/SRS.txt	Thu Oct 01 11:33:34 2009 -0500
+++ b/python/tests/SRS.txt	Thu Oct 01 11:58:43 2009 -0500
@@ -18,6 +18,22 @@
   >>> test_srs()
   True
   
+  >>> s = srs.SRS()
+  >>> s.set_userinput('EPSG:4326')
+  True
+  
+  >>> def test_userinput():
+  ...     if not liblas.HAVE_LIBGEOTIFF:
+  ...         return True
+  ...     if not liblas.HAVE_GDAL: 
+  ...         return s.proj4 == ''
+  ...     if liblas.HAVE_GDAL:
+  ...         return s.proj4 == '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '
+  ...     return False
+  
+  >>> test_userinput()
+  True
+  
   >>> from liblas import file
   >>> f = file.File('../test/data/1.2_3.las',mode='r')
   >>> s = f.header.srs
diff -r 94e8c45b4cb1 -r b959e0a380d7 src/las_c_api.cpp
--- a/src/las_c_api.cpp	Thu Oct 01 11:33:34 2009 -0500
+++ b/src/las_c_api.cpp	Thu Oct 01 11:58:43 2009 -0500
@@ -1851,6 +1851,22 @@
     return LE_None;
 }
 
+LAS_DLL LASErrorEnum LASSRS_SetFromUserInput(LASSRSH hSRS, const char* value)
+{
+    VALIDATE_LAS_POINTER1(hSRS, "LASSRS_SetFromUserInput", LE_Failure);
+    VALIDATE_LAS_POINTER1(value, "LASSRS_SetFromUserInput", LE_Failure);
+
+    try {
+         ((LASSpatialReference*) hSRS)->SetFromUserInput(value);
+    }
+    catch (std::exception const& e) {
+        LASError_PushError(LE_Failure, e.what(), "LASSRS_SetFromUserInput");
+        return LE_Failure;
+    }
+
+    return LE_None;
+}
+
 LAS_DLL LASErrorEnum LASSRS_AddVLR(LASSRSH hSRS, const LASVLRH hVLR) {
     
     VALIDATE_LAS_POINTER1(hSRS, "LASSRS_AddVLR", LE_Failure);
diff -r 94e8c45b4cb1 -r b959e0a380d7 src/lasspatialreference.cpp
--- a/src/lasspatialreference.cpp	Thu Oct 01 11:33:34 2009 -0500
+++ b/src/lasspatialreference.cpp	Thu Oct 01 11:58:43 2009 -0500
@@ -382,7 +382,33 @@
     return std::string();
 #endif
 }
-       
+
+void LASSpatialReference::SetFromUserInput( std::string const& v)
+{
+#ifdef HAVE_GDAL
+
+    char* poWKT = 0;
+    const char* input = v.c_str();
+    OGRSpatialReference* poSRS = new OGRSpatialReference();
+    if (OGRERR_NONE != poSRS->SetFromUserInput((char *) input))
+    {
+        delete poSRS;
+        throw std::invalid_argument("could not import coordinate system into OSRSpatialReference SetFromUserInput");
+    }
+    
+    poSRS->exportToWkt(&poWKT);
+    delete poSRS;
+    
+    std::string tmp(poWKT);
+    std::free(poWKT);
+    
+    SetWKT(tmp);
+#else
+    detail::ignore_unused_variable_warning(v);
+    throw std::runtime_error("GDAL is not available, LASSpatialReference could not be set from WKT");
+#endif
+}
+
 void LASSpatialReference::SetWKT(std::string const& v)
 {
     if (!m_gtiff)


More information about the Liblas-commits mailing list