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

liblas-commits at liblas.org liblas-commits at liblas.org
Fri May 15 00:46:59 EDT 2009


Author: hobu
Date: Fri May 15 00:46:59 2009
New Revision: 1265
URL: http://liblas.org/changeset/1265

Log:
more indexing stuff, including a data stream interface for SpatialIndex::

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

Modified: trunk/apps/lasindex.cpp
==============================================================================
--- trunk/apps/lasindex.cpp	(original)
+++ trunk/apps/lasindex.cpp	Fri May 15 00:46:59 2009
@@ -8,6 +8,10 @@
 #include <liblas/lasheader.hpp>
 #include <liblas/lasindex.hpp>
 
+#ifdef HAVE_SPATIALINDEX
+#include <spatialindex/SpatialIndex.h>
+
+#endif
 #include <iostream>
 #include <fstream>
 #include <vector>
@@ -97,6 +101,10 @@
         }
     }
     
+    if (input.empty()) {
+        usage();
+        exit(-1);
+    }
     std::cout << "input: " << input<<  " output: " <<output<<std::endl;
 
     std::istream* istrm = OpenInput(input);
@@ -107,16 +115,31 @@
     std::cout << "number of points: " << header.GetPointRecordsCount() << std::endl;
     
     for (int i=0; i< header.GetPointRecordsCount(); i++) {
-        bool read = reader->ReadPointAt(i);
-        LASPoint p = reader->GetPoint();
+        
+        try{
+            bool read = reader->ReadPointAt(i);
+            LASPoint p = reader->GetPoint();
+        } catch (Tools::IllegalStateException& e) {
+            std::string s = e.what();
+            std::cout << "error creating index" << s <<std::endl; exit(1);
+        }        
+        
         std::cout.precision(2);
         std::cout.setf(std::ios_base::fixed);
         // std::cout << "x: " << p.GetX() << " y: " << p.GetY() << std::endl;
     }
     
     LASIndex* idx = reader->GetIndex();
+
+    std::vector<liblas::uint32_t>* ids = 0;
+    try{
+        ids = idx->intersects(289815.12,4320979.06, 289818.01,4320982.59,46.83,170.65);
+    } catch (Tools::IllegalArgumentException& e) {
+        std::string s = e.what();
+        std::cout << "error querying index value" << s <<std::endl; exit(1);
+    }
+        
     
-    std::vector<liblas::uint32_t>* ids = idx->intersects(289815.12,4320979.06, 289818.01,4320982.59);
     
     std::cout << "Vec length" << ids->size() << std::endl;
     delete reader;

Modified: trunk/include/liblas/lasindex.hpp
==============================================================================
--- trunk/include/liblas/lasindex.hpp	(original)
+++ trunk/include/liblas/lasindex.hpp	Fri May 15 00:46:59 2009
@@ -45,6 +45,7 @@
 #include <liblas/lasvariablerecord.hpp>
 #include <liblas/laspoint.hpp>
 
+
 #ifdef HAVE_SPATIALINDEX
 #include <spatialindex/SpatialIndex.h>
 
@@ -66,7 +67,7 @@
 
     
     LASIndex();
-    LASIndex(std::string const& filename);
+    LASIndex(std::string& filename);
     /// Copy constructor.
     LASIndex(LASIndex const& other);
     ~LASIndex();
@@ -80,7 +81,7 @@
     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);
+    std::vector<uint32_t>* intersects(double minx, double miny, double maxx, double maxy, double minz, double maxz);
 private:
 
     SpatialIndex::IStorageManager* m_storage;
@@ -96,45 +97,45 @@
 public:
     // size_t m_indexIO;
     // size_t m_leafIO;
-	
+    
     std::vector<uint32_t>* m_vector;
-	
+    
 
 public:
     LASVisitor(std::vector<uint32_t>* vect){m_vector = vect;}
 
-	void visitNode(const SpatialIndex::INode& n)
-	{
+    void visitNode(const SpatialIndex::INode& n)
+    {
         //         std::cout << "visitNode" << std::endl;
         // if (n.isLeaf()) m_leafIO++;
         // else m_indexIO++;
-	}
+    }
 
-	void visitData(const SpatialIndex::IData& d)
-	{
-		SpatialIndex::IShape* pS;
-		d.getShape(&pS);
-			// do something.
-		delete pS;
-
-		// data should be an array of characters representing a Region as a string.
-		uint8_t* pData = 0;
-		size_t cLen = 0;
-		d.getData(cLen, &pData);
-		// do something.
-		//string s = reinterpret_cast<char*>(pData);
-		//cout << s << endl;
-		delete[] pData;
+    void visitData(const SpatialIndex::IData& d)
+    {
+        SpatialIndex::IShape* pS;
+        d.getShape(&pS);
+            // do something.
+        delete pS;
+
+        // data should be an array of characters representing a Region as a string.
+        uint8_t* pData = 0;
+        size_t cLen = 0;
+        d.getData(cLen, &pData);
+        // do something.
+        //string s = reinterpret_cast<char*>(pData);
+        //cout << s << endl;
+        delete[] pData;
 
         // std::cout << d.getIdentifier() << std::endl;
         m_vector->push_back(d.getIdentifier());
-			// the ID of this data entry is an answer to the query. I will just print it to stdout.
-	}
+            // the ID of this data entry is an answer to the query. I will just print it to stdout.
+    }
 
-	void visitData(std::vector<const SpatialIndex::IData*>& v)
-	{
-		std::cout << v[0]->getIdentifier() << " " << v[1]->getIdentifier() << std::endl;
-	}
+    void visitData(std::vector<const SpatialIndex::IData*>& v)
+    {
+        std::cout << v[0]->getIdentifier() << " " << v[1]->getIdentifier() << std::endl;
+    }
 };
 
 } // namespace liblas
@@ -143,43 +144,67 @@
 {
 
 
-	namespace StorageManager
-	{
-		extern IStorageManager* returnLASStorageManager(Tools::PropertySet& in);
-		extern IStorageManager* createNewLASStorageManager();
-		
-		class LASStorageManager : public SpatialIndex::IStorageManager
-		{
-		public:
-			LASStorageManager(Tools::PropertySet&);
-
-			virtual ~LASStorageManager();
-
-			virtual void loadByteArray(const id_type id, size_t& len, uint8_t** data);
-			virtual void storeByteArray(id_type& id, const size_t len, const uint8_t* const data);
-			virtual void deleteByteArray(const id_type id);
-
-		private:
-			class Entry
-			{
-			public:
-				byte* m_pData;
-				size_t m_length;
-
-				Entry(size_t l, const uint8_t* const d) : m_pData(0), m_length(l)
-				{
-					m_pData = new uint8_t[m_length];
-					memcpy(m_pData, d, m_length);
-				}
-
-				~Entry() { delete[] m_pData; }
-			}; // Entry
-
-			std::vector<Entry*> m_buffer;
-			std::stack<id_type> m_emptyPages;
-		}; // MemoryStorageManager
-	}
+    namespace StorageManager
+    {
+        extern IStorageManager* returnLASStorageManager(Tools::PropertySet& in);
+        extern IStorageManager* createNewLASStorageManager();
+        
+        class LASStorageManager : public SpatialIndex::IStorageManager
+        {
+        public:
+            LASStorageManager(Tools::PropertySet&);
+
+            virtual ~LASStorageManager();
+
+            virtual void loadByteArray(const id_type id, size_t& len, uint8_t** data);
+            virtual void storeByteArray(id_type& id, const size_t len, const uint8_t* const data);
+            virtual void deleteByteArray(const id_type id);
+
+        private:
+            class Entry
+            {
+            public:
+                byte* m_pData;
+                size_t m_length;
+
+                Entry(size_t l, const uint8_t* const d) : m_pData(0), m_length(l)
+                {
+                    m_pData = new uint8_t[m_length];
+                    memcpy(m_pData, d, m_length);
+                }
+
+                ~Entry() { delete[] m_pData; }
+            }; // Entry
+
+            std::vector<Entry*> m_buffer;
+            std::stack<id_type> m_emptyPages;
+        }; // MemoryStorageManager
+    }
 }
 
+namespace liblas
+{
+class LASDataStream : public SpatialIndex::IDataStream
+{
+public:
+    LASDataStream(LASReader* reader);
+
+    ~LASDataStream()
+    {
+        if (m_pNext != 0) delete m_pNext;
+    };
+
+    SpatialIndex::IData* getNext();
+    bool hasNext() throw (Tools::NotSupportedException);
+
+    size_t size() throw (Tools::NotSupportedException);
+    void rewind() throw (Tools::NotSupportedException);
+
+    bool readPoint();
+    liblas::LASReader* m_reader;
+    SpatialIndex::RTree::Data* m_pNext;
+    SpatialIndex::id_type m_id;
+};
+}
 
 #endif // LIBLAS_LASINDEX_HPP_INCLUDED

Modified: trunk/src/lasindex.cpp
==============================================================================
--- trunk/src/lasindex.cpp	(original)
+++ trunk/src/lasindex.cpp	Fri May 15 00:46:59 2009
@@ -42,12 +42,14 @@
 #include <liblas/lasindex.hpp>
 #include <liblas/cstdint.hpp>
 #include <liblas/guid.hpp>
+#include <liblas/lasreader.hpp>
 
 #include <cstddef>
 #include <iostream>
 #include <sstream>
 #include <string>
 #include <vector>
+#include <stdexcept>
 
 namespace liblas
 {
@@ -67,9 +69,9 @@
 
     // R-Tree parameters
     double fillFactor = 0.7;
-    uint32_t indexCapacity = 10;
-    uint32_t leafCapacity = 10;
-    uint32_t dimension = 2;
+    uint32_t indexCapacity = 100;
+    uint32_t leafCapacity = 100;
+    uint32_t dimension = 3;
     SpatialIndex::RTree::RTreeVariant variant = SpatialIndex::RTree::RV_RSTAR;
 
     // create R-tree
@@ -80,7 +82,7 @@
 
 }
 
-LASIndex::LASIndex(std::string const& filename) 
+LASIndex::LASIndex(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)
@@ -97,14 +99,14 @@
             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);
+            std::cout << "error loading index " << s <<std::endl; exit(1);
         }
     }
     else
     {
         std::cout << "Creating new index ... " << std::endl;
         try{
-            m_storage = SpatialIndex::StorageManager::createNewDiskStorageManager(filename, 3);
+            m_storage = SpatialIndex::StorageManager::createNewDiskStorageManager(filename, 4096);
         } catch (Tools::IllegalStateException& e) {
             std::string s = e.what();
             std::cout << "error creating index" << s <<std::endl; exit(1);
@@ -148,30 +150,35 @@
 
 void LASIndex::insert(LASPoint& p, int64_t id) 
 {
-    double min[2];
-    double max[2];
+    double min[3];
+    double max[3];
     
-    min[0] = p.GetX(); min[1] = p.GetY();
-    max[0] = p.GetX(); max[1] = p.GetY();
-    index().insertData(0, 0, SpatialIndex::Region(min, max, 2), id);
+    min[0] = p.GetX(); min[1] = p.GetY(); min[2] = p.GetZ();
+    max[0] = p.GetX(); max[1] = p.GetY(); max[2] = p.GetZ();
+    try{
+        index().insertData(0, 0, SpatialIndex::Region(min, max, 3), id);
+    } catch (Tools::IllegalArgumentException& e) {
+        std::string s = e.what();
+        std::cout << "error inserting index value" << s <<std::endl; exit(1);
+    }
 }
 
-std::vector<uint32_t>* LASIndex::intersects(double minx, double miny, double maxx, double maxy)
+std::vector<uint32_t>* LASIndex::intersects(double minx, double miny, double maxx, double maxy, double minz, double maxz)
 {
-    double min[2];
-    double max[2];
+    double min[3];
+    double max[3];
     
-    min[0] = minx; min[1] = miny;
-    max[0] = maxx; max[1] = maxy;
+    min[0] = minx; min[1] = miny; min[2] = minz;
+    max[0] = maxx; max[1] = maxy; max[2] = maxz;
     
     std::cout.setf(std::ios_base::fixed);
     
-    std::cout << "minx: " << min[0] << " miny: "<<min[1] << " maxx: " <<max[0] << " maxy: " << max[1] << std::endl;
+    // std::cout << "minx: " << min[0] << " miny: "<<min[1] << " maxx: " <<max[0] << " maxy: " << max[1] << " minz: " << min[2] << " maxz: " << max[2] <<std::endl;
     if (min[0] > max[0] || min[1] > max[1]) {std::cout << "epic fail!" << std::endl;};
     std::vector<uint32_t>* vect = new std::vector<uint32_t>;
     LASVisitor* visitor = new LASVisitor(vect);
     
-    const SpatialIndex::Region *region = new SpatialIndex::Region(min, max, 2);
+    const SpatialIndex::Region *region = new SpatialIndex::Region(min, max, 3);
     std::cout << *region << std::endl;
     index().intersectsWithQuery(*region, *visitor);
     
@@ -183,11 +190,7 @@
 } // namespace liblas
 
 
-#include <stdexcept>
-
 
-using namespace SpatialIndex;
-using namespace SpatialIndex::StorageManager;
 
 SpatialIndex::IStorageManager* SpatialIndex::StorageManager::returnLASStorageManager(Tools::PropertySet& ps)
 {
@@ -201,16 +204,16 @@
 	return returnLASStorageManager(ps);
 }
 
-LASStorageManager::LASStorageManager(Tools::PropertySet& ps)
+SpatialIndex::StorageManager::LASStorageManager::LASStorageManager(Tools::PropertySet& ps)
 {
 }
 
-LASStorageManager::~LASStorageManager()
+SpatialIndex::StorageManager::LASStorageManager::~LASStorageManager()
 {
 	for (std::vector<Entry*>::iterator it = m_buffer.begin(); it != m_buffer.end(); it++) delete *it;
 }
 
-void LASStorageManager::loadByteArray(const id_type id, std::size_t& len, uint8_t** data)
+void SpatialIndex::StorageManager::LASStorageManager::loadByteArray(const id_type id, size_t& len, uint8_t** data)
 {
 	Entry* e;
 	try
@@ -229,7 +232,7 @@
 	memcpy(*data, e->m_pData, len);
 }
 
-void LASStorageManager::storeByteArray(id_type& id, const std::size_t len, const uint8_t* const data)
+void SpatialIndex::StorageManager::LASStorageManager::storeByteArray(id_type& id, const size_t len, const uint8_t* const data)
 {
 	if (id == NewPage)
 	{
@@ -266,7 +269,7 @@
 	}
 }
 
-void LASStorageManager::deleteByteArray(const id_type id)
+void SpatialIndex::StorageManager::LASStorageManager::deleteByteArray(const id_type id)
 {
 	Entry* e;
 	try
@@ -283,4 +286,82 @@
 	m_emptyPages.push(id);
 
 	delete e;
-}
\ No newline at end of file
+}
+
+
+namespace liblas
+{
+
+
+
+LASDataStream::LASDataStream(LASReader *reader) : m_reader(reader), m_pNext(0), m_id(0)
+{
+    bool read = readPoint();
+    
+    if (read)
+        m_id = 0;
+
+    
+}
+
+bool LASDataStream::readPoint()
+{
+    double min[3], max[3];
+    
+    bool doRead = m_reader->ReadNextPoint();
+    LASPoint* p;
+    if (doRead)
+        p = (LASPoint*) &(m_reader->GetPoint());
+    else
+        return false;
+//        throw Tools::IllegalStateException("Unable to read first point for LASReader!");
+    
+    double x = p->GetX();
+    double y = p->GetY();
+    double z = p->GetZ();
+
+    min[0] = x; min[1] = y; min[2] = z;
+    max[0] = x; max[1] = y; max[2] = z;
+    
+    m_id = 0;
+    SpatialIndex::Region r = SpatialIndex::Region(min, max, 3);
+    m_pNext = new SpatialIndex::RTree::Data(0, 0, r, m_id);
+    return true;
+}
+
+
+SpatialIndex::IData* LASDataStream::getNext()
+{
+    if (m_pNext == 0) return 0;
+
+    SpatialIndex::RTree::Data* ret = m_pNext;
+    m_pNext = 0;
+    readPoint();
+    m_id +=1;
+    return ret;
+}
+
+bool LASDataStream::hasNext() throw (Tools::NotSupportedException)
+{
+    return (m_pNext != 0);
+}
+
+size_t LASDataStream::size() throw (Tools::NotSupportedException)
+{
+    throw Tools::NotSupportedException("Operation not supported.");
+}
+
+void LASDataStream::rewind() throw (Tools::NotSupportedException)
+{
+
+    if (m_pNext != 0)
+    {
+     delete m_pNext;
+     m_pNext = 0;
+    }
+    
+    m_reader->Reset();
+    readPoint();
+}
+
+} // namespace liblas
\ No newline at end of file


More information about the Liblas-commits mailing list