File: lasindex.cpp

package info (click to toggle)
liblas 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,888 kB
  • ctags: 4,614
  • sloc: cpp: 31,630; xml: 4,195; python: 2,928; ansic: 2,439; cs: 2,411; sh: 143; makefile: 37
file content (156 lines) | stat: -rwxr-xr-x 3,889 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <liblas/liblas.hpp>

#include <cstring>
#include <iterator>
#include <iterator>
#include <fstream>
#include <vector>
#include <sstream>

#ifdef _WIN32
#define compare_no_case(a,b,n)  _strnicmp( (a), (b), (n) )
#else
#define compare_no_case(a,b,n)  strncasecmp( (a), (b), (n) )
#endif

std::istream* OpenInput(std::string filename) 
{
    std::ios::openmode const mode = std::ios::in | std::ios::binary;
    std::istream* istrm;
    if (compare_no_case(filename.c_str(),"STDIN",5) == 0)
    {
        istrm = &std::cin;
    }
    else 
    {
        istrm = new std::ifstream(filename.c_str(), mode);
    }
    
    if (!istrm->good())
    {
        delete istrm;
        throw std::runtime_error("Reading stream was not able to be created");
    }
    return istrm;
}

void usage() {}

using namespace liblas;

void LoadIndex (LASIndex* index, liblas::Reader* reader, long dimension) 
{

    bool read = reader->ReadNextPoint();
    liblas::int64_t id = 0;
    if (read) {
        const liblas::Point& p = reader->GetPoint();
        index->insert(const_cast<liblas::Point&>(p), id);
    }
    while (reader->ReadNextPoint()) {
        id += 1;
        const liblas::Point& p = reader->GetPoint();
        index->insert(const_cast<liblas::Point&>(p), id);
    }

	boost::ignore_unused_variable_warning(dimension);
}
int main(int argc, char* argv[])
{
    std::string input;
    
    long dimension = 3;
    long capacity = 10000;
    bool bBulkLoad = true;
    
    for (int i = 1; i < argc; i++)
    {
        if (    std::strcmp(argv[i],"-h") == 0 ||
                std::strcmp(argv[i],"--help") == 0
            )
        {
            usage();
            exit(0);
        }
        else if (   std::strcmp(argv[i],"--input") == 0  ||
                    std::strcmp(argv[i],"-input") == 0   ||
                    std::strcmp(argv[i],"-i") == 0       ||
                    std::strcmp(argv[i],"-in") == 0
                )
        {
            i++;
            input = std::string(argv[i]);
        }
        else if (   std::strcmp(argv[i],"--dimension") == 0  ||
                    std::strcmp(argv[i],"-dim") == 0     ||
                    std::strcmp(argv[i],"-d") == 0       
                )
        {
            i++;
            dimension = atoi(argv[i]);
        }
        
        else if (   std::strcmp(argv[i],"--capacity") == 0  ||
                    std::strcmp(argv[i],"-cap") == 0     ||
                    std::strcmp(argv[i],"-c") == 0       
                )
        {
            i++;
            capacity = atoi(argv[i]);
        }
        else if (   std::strcmp(argv[i],"--individual") == 0  ||
                    std::strcmp(argv[i],"--non-bulk") == 0     ||
                    std::strcmp(argv[i],"-g") == 0       
                )
        {

            bBulkLoad = false;
        }
        else if (input.empty())
        {
            input = std::string(argv[i]);
        }

        else 
        {
            usage();
            exit(1);
        }
    }
    
    if (input.empty()) {
        usage();
        exit(-1);
    }

    // FIXME: Missing RAII and try-catch, no LAS throws exceptions.
    std::istream* istrm = OpenInput(input);
    liblas::Reader* reader = new liblas::Reader(*istrm);
    std::cout << "Indexing " << input<< " "<<std::endl;

    LASIndexDataStream* idxstrm = 0;
    
    LASIndex* idx = new LASIndex(input);
    idx->SetType(LASIndex::eExternalIndex);
    idx->SetLeafCapacity(capacity);
    idx->SetFillFactor(0.99);
    idx->SetDimension(dimension);

    if (bBulkLoad) {
        idxstrm = new LASIndexDataStream(reader, dimension);
        idx->Initialize(*idxstrm);
    } else {
        idx->Initialize();
        LoadIndex(idx, reader, dimension);
    }

    if (idx)
        delete idx;
    
    if (idxstrm)
        delete idxstrm;
    if (reader)
        delete reader;
    
    delete istrm;
}