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

liblas-commits at liblas.org liblas-commits at liblas.org
Mon Jun 8 21:52:13 EDT 2009


Author: hobu
Date: Tue May 19 16:24:50 2009
New Revision: 1271
URL: http://liblas.org/changeset/1271

Log:
more refactoring, still not close to done though

Modified:
   trunk/include/liblas/lasindex.hpp
   trunk/src/lasindex.cpp

Modified: trunk/include/liblas/lasindex.hpp
==============================================================================
--- trunk/include/liblas/lasindex.hpp	(original)
+++ trunk/include/liblas/lasindex.hpp	Tue May 19 16:24:50 2009
@@ -62,6 +62,12 @@
 {
 public:
 
+    enum IndexType
+    {
+        eMemoryIndex = 1, ///< A transient memory index that will go away
+        eVLRIndex = 2, ///< An index that will store its data in VLR records
+        eExternalIndex = 3 ///< An index that will store its data in files alongside the .las file (.las.dat & .las.idx)
+    };
     
     LASIndex();
     LASIndex(LASDataStream& strm, std::string& filename);
@@ -76,18 +82,50 @@
     /// Comparison operator.
     bool operator==(const LASIndex& other) const;
 
-    SpatialIndex::ISpatialIndex& index() {return *m_rtree;}
     
     void insert(LASPoint& p, int64_t id);
     std::vector<uint32_t>* intersects(double minx, double miny, double maxx, double maxy, double minz, double maxz);
+
+    /// Sets the page size for the index when stored externally
+    /// \param v - page value.  Defaults to 4096.
+    void SetPageSize(uint32_t v) { m_Pagesize = v; }
+
+    /// Get index page size for indexes that are stored externally
+    /// \return index page size.
+    uint32_t GetPageSize() { return m_Pagesize; }
+    
+    /// Sets the index type
+    /// \param v - index type. 
+    void SetIndexType(IndexType v) { m_idxType = v; }
+
+    /// Gets the index type
+    /// \return index type.
+    IndexType GetIndexType() { return m_idxType; }
+    
 private:
 
     SpatialIndex::IStorageManager* m_storage;
     SpatialIndex::StorageManager::IBuffer* m_buffer;
     SpatialIndex::ISpatialIndex* m_rtree;
-    std::string m_indexname;
+
+    uint32_t m_Pagesize;
+    IndexType m_idxType;
+    SpatialIndex::id_type m_idxId;
+    uint32_t m_idxCapacity;
+    uint32_t m_idxLeafCap;
+    uint32_t m_idxDimension;   
+    double m_idxFillFactor; 
+    bool m_idxExternalExists;
+
+
+    uint16_t m_bufferCapacity;
+    bool m_bufferWriteThrough;
     
     void Init();
+    SpatialIndex::IStorageManager* CreateStorage(std::string& filename);
+    SpatialIndex::StorageManager::IBuffer* CreateIndexBuffer(SpatialIndex::IStorageManager& storage);
+    
+    bool ExternalIndexExists(std::string& filename);
 };
 
 class LASVisitor : public SpatialIndex::IVisitor

Modified: trunk/src/lasindex.cpp
==============================================================================
--- trunk/src/lasindex.cpp	(original)
+++ trunk/src/lasindex.cpp	Tue May 19 16:24:50 2009
@@ -54,107 +54,168 @@
 namespace liblas
 {
 
+void LASIndex::Init()
+{   
+
+    m_Pagesize = 4096;
+    m_idxType = eExternalIndex;
+    m_idxId = 1;
+    
+    m_idxCapacity = 100;
+    m_idxLeafCap = 100;
+    m_idxDimension = 3;
+    
+    m_idxFillFactor = 0.7;
+    
+    m_bufferCapacity = 10;
+    m_bufferWriteThrough = false;
+
+    m_idxExternalExists = false;
+}
 
 LASIndex::LASIndex()
 {
     std::cout << "Blank Index Constructor called!" << std::endl;
-    // m_storage = createNewLASStorageManager();
-    // Init();
+    Init();
 }
 
+
 LASIndex::LASIndex(LASDataStream& strm, std::string& filename)
 {
+    using namespace SpatialIndex;
+    
+    Init();
+    
+    m_storage = CreateStorage(filename);
+    m_buffer = CreateIndexBuffer(*m_storage);
 
-    struct stat stats;
-    std::ostringstream os;
-    os << filename << ".dat";
-    std::cout << "LASDataStream index name: " << os.str() << std::endl;
-
-    std::string indexname = os.str();
-    int ret = stat(indexname.c_str(),&stats);
-    if (!ret) {
-        std::cout << "loading existing index from LASReader " << indexname << std::endl;
+    if (m_idxType == eExternalIndex) {
+            
+    if (m_idxExternalExists == true) {
+        std::cout << "loading existing index from LASDataStream " << std::endl;
         try{
-            m_storage = SpatialIndex::StorageManager::loadDiskStorageManager(filename);
-        } catch (Tools::IllegalStateException& e) {
-            std::string s = e.what();
-            std::cout << "error loading index " << s <<std::endl; exit(1);
+            m_rtree = SpatialIndex::RTree::loadRTree(*m_buffer,m_idxId);
+        } catch (Tools::Exception& e) {
+            std::ostringstream os;
+            os << "Spatial Index Error: " << e.what();
+            throw std::runtime_error(os.str());
         }
-        Init();
     }
     else
     {
-        std::cout << "Creating new index from LASReader stream ... " << std::endl;
+        std::cout << "Creating new index from LASDataStream  ... " << std::endl;
         try{
-            m_storage = SpatialIndex::StorageManager::createNewDiskStorageManager(filename, 4096);
-            uint16_t capacity = 10;
-            bool writeThrough = false;
-            // R-Tree parameters
-            double fillFactor = 0.7;
-            uint32_t indexCapacity = 100;
-            uint32_t leafCapacity = 100;
-            uint32_t dimension = 3;
-            SpatialIndex::id_type indexId=1;
-            m_buffer = SpatialIndex::StorageManager::createNewRandomEvictionsBuffer(*m_storage, capacity, writeThrough);
-            m_rtree = SpatialIndex::RTree::createAndBulkLoadNewRTree(   SpatialIndex::RTree::BLM_STR,
-                                                                        strm,
-                                                                        *m_buffer,
-                                                                        fillFactor,
-                                                                        indexCapacity,
-                                                                        leafCapacity,
-                                                                        dimension,
-                                                                        SpatialIndex::RTree::RV_RSTAR,
-                                                                        indexId);
+            m_rtree = RTree::createAndBulkLoadNewRTree(   SpatialIndex::RTree::BLM_STR,
+                                                          strm,
+                                                          *m_buffer,
+                                                          m_idxFillFactor,
+                                                          m_idxCapacity,
+                                                          m_idxLeafCap,
+                                                          m_idxDimension,
+                                                          SpatialIndex::RTree::RV_RSTAR,
+                                                          m_idxId);
         bool ret = m_rtree->isIndexValid();
-        if (ret == false) std::cerr << "ERROR: Structure is invalid!" << std::endl;
-        std::cout << index() << std::endl;
+        if (ret == false) 
+            throw std::runtime_error(   "Spatial index error: index is not"
+                                        " valid after createAndBulkLoadNewRTree");
         } catch (Tools::Exception& e) {
-            std::string s = e.what();
-            std::cout << "error creating index" << s <<std::endl; exit(1);
+            std::ostringstream os;
+            os << "Spatial Index Error: " << e.what();
+            throw std::runtime_error(os.str());
         }
     }        
+    } // eExternalIndex
 }
-void LASIndex::Init()
-{    
-    uint16_t capacity = 10;
-    bool writeThrough = false;
-    m_buffer = SpatialIndex::StorageManager::createNewRandomEvictionsBuffer(*m_storage, capacity, writeThrough);
-    // 
-    // // R-Tree parameters
-    // double fillFactor = 0.7;
-    // uint32_t indexCapacity = 100;
-    // uint32_t leafCapacity = 100;
-    // uint32_t dimension = 3;
-    // SpatialIndex::RTree::RTreeVariant variant = SpatialIndex::RTree::RV_RSTAR;
-    // 
-    // // create R-tree
-    SpatialIndex::id_type indexId=1;
-    m_rtree = SpatialIndex::RTree::loadRTree(*m_buffer,indexId);
-    std::cout << "index is valid? " << m_rtree->isIndexValid() << std::endl;
-    // m_rtree = SpatialIndex::RTree::createNewRTree(*m_buffer, fillFactor, indexCapacity,
-    //                                leafCapacity, dimension, variant, indexId); 
 
 
+SpatialIndex::IStorageManager* LASIndex::CreateStorage(std::string& filename)
+{
+    using namespace SpatialIndex::StorageManager;
+    
+    SpatialIndex::IStorageManager* storage = 0;
+    if (m_idxType == eExternalIndex) {
+
+        if (ExternalIndexExists(filename) && !filename.empty()) {
+            std::cout << "loading existing DiskStorage " << filename << std::endl;
+            try{
+                storage = loadDiskStorageManager(filename);
+                m_idxExternalExists = true;
+                return storage;
+            } catch (Tools::Exception& e) {
+                std::ostringstream os;
+                os << "Spatial Index Error: " << e.what();
+                throw std::runtime_error(os.str());
+            } 
+        } else if (!filename.empty()){
+            try{
+                std::cout << "creating new DiskStorage " << filename << std::endl;            
+                storage = createNewDiskStorageManager(filename, m_Pagesize);
+                m_idxExternalExists = false;
+                return storage;
+            } catch (Tools::Exception& e) {
+                std::ostringstream os;
+                os << "Spatial Index Error: " << e.what();
+                throw std::runtime_error(os.str());
+            }         
+        }
+    }
+    return storage;               
+}
+
+SpatialIndex::StorageManager::IBuffer* LASIndex::CreateIndexBuffer(SpatialIndex::IStorageManager& storage)
+{
+    using namespace SpatialIndex::StorageManager;
+    IBuffer* buffer = 0;
+    try{
+        if ( m_storage == 0 ) throw std::runtime_error("Storage was invalid to create index buffer");
+        buffer = createNewRandomEvictionsBuffer(storage,
+                                                m_bufferCapacity,
+                                                m_bufferWriteThrough);
+    } catch (Tools::Exception& e) {
+        std::ostringstream os;
+        os << "Spatial Index Error: " << e.what();
+        throw std::runtime_error(os.str());
+    }
+    return buffer;
 }
 
-LASIndex::LASIndex(std::string& filename) 
+
+bool LASIndex::ExternalIndexExists(std::string& filename)
 {
-    // FIXME: This is not C, no need for struct.
-    // FIXME: stat is very weak name! There tons of structs in various C libs (ie. struct stat; in Windows C lib)
     struct stat stats;
     std::ostringstream os;
     os << filename << ".dat";
-    std::cout << "index name: " << os.str() << std::endl;
+    
+    if (m_idxExternalExists == true) return true;
 
     std::string indexname = os.str();
     int ret = stat(indexname.c_str(),&stats);
-    if (!ret) {
-        std::cout << "loading existing index " << indexname << std::endl;
+    std::cout << "indexname: " << indexname << " ret: " << ret << std::endl;
+    bool output = false;
+    if (ret == 0) output= true; else output =false;
+    return output;
+}
+LASIndex::LASIndex(std::string& filename) 
+{
+
+    Init();
+    
+    m_storage = CreateStorage(filename);
+    
+    m_buffer = CreateIndexBuffer(*m_storage);
+
+    if (ExternalIndexExists(filename)) {
+        std::cout << "loading existing index " << filename << std::endl;
         try{
-            m_storage = SpatialIndex::StorageManager::loadDiskStorageManager(filename);
-        } catch (Tools::IllegalStateException& e) {
-            std::string s = e.what();
-            std::cout << "error loading index " << s <<std::endl; exit(1);
+            // m_storage = SpatialIndex::StorageManager::loadDiskStorageManager(filename);
+            // m_buffer = SpatialIndex::StorageManager::createNewRandomEvictionsBuffer(*m_storage, 
+            //                                                                         m_bufferCapacity, 
+            //                                                                         m_bufferWriteThrough);
+            m_rtree = SpatialIndex::RTree::loadRTree(*m_buffer,m_idxId);
+        } catch (Tools::Exception& e) {
+                std::ostringstream os;
+                os << "Spatial Index Error: " << e.what();
+                throw std::runtime_error(os.str());
         }
     }
     else{std::cout << "index name does not exist, failing" << std::endl;exit(1);}
@@ -168,8 +229,6 @@
     //         std::cout << "error creating index" << s <<std::endl; exit(1);
     //     }
     // }
-
-    Init();
 }
 
 LASIndex::LASIndex(LASIndex const& other) 


More information about the Liblas-commits mailing list