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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
.. _cpptutorial:
*********************************************
C++ Tutorial
*********************************************
:Author: Mateusz Loskot
:Contact: mateusz at loskot dot net
:Author: Howard Butler
:Contact: hobu.inc at gmail dot com
:Date: 2/22/2011
.. contents::
:depth: 2
:backlinks: none
This basic tutorial explains how to use libLAS to read and write LIDAR data
encoded in LAS File Format. For more examples in C++, check source of `unit
tests package`_ for libLAS.
==============================================================================
Reading LAS data using ``liblas::Reader``
==============================================================================
1. Include required header files from libLAS and C++ Standard Library
.. code-block:: cpp
#include <liblas/liblas.hpp>
#include <fstream> // std::ifstream
#include <iostream> // std::cout
2. Create input stream and associate it with .las file opened to read in
**binary** mode
.. code-block:: cpp
std::ifstream ifs;
ifs.open("file.las", std::ios::in | std::ios::binary);
3. Create a ReaderFactory and instantiate a new liblas::Reader using
the stream.
.. code-block:: cpp
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
.. note::
It is possible to use the basic liblas::Reader constructor that
takes in a std::istream, but it will not be able to account for the fact
that the file might be compressed. Using the ReaderFactory will take
care of all of this for you.
4. After the reader has been created, you can access members of the Public
Header Block
.. code-block:: cpp
liblas::Header const& header = reader.GetHeader();
std::cout << "Compressed: " << (header.Compressed() == true) ? "true":"false";
std::cout << "Signature: " << header.GetFileSignature() << '\n';
std::cout << "Points count: " << header.GetPointRecordsCount() << '\n';
.. note::
It's correct to assume that after successful instantiation of reader, the
public header block is automatically accessible. In other words, there
never be a reader that can not serve a header data.
5. Iterate through point records
.. code-block:: cpp
while (reader.ReadNextPoint())
{
liblas::Point const& p = reader.GetPoint();
std::cout << p.GetX() << ", " << p.GetY() << ", " << p.GetZ() << "\n";
}
6. Randomly read point 2
.. code-block:: cpp
reader.ReadPointAt(2);
liblas::Point const& p = reader.GetPoint();
.. note::
A `ReadPointAt` method call implies an individual seek in the file per
point read. Use ``liblas::Reader::Seek`` if you wish to read a run
of points starting at a specified location in the file.
.. warning::
If the file is a compressed LAS file, ReadPointAt is going to be much
slower than randomly reading through an uncompressed file. The `LASzip`_
compression as of LASzip 1.0.1 is sequential in nature, and asking for the
2nd point in the file means decompressing both the 0th and 1st points.
7. Seek to the 10th point to start reading. A ``liblas::Reader`` provides a
`Seek` method to allow you to start reading from a specified location for
however many points you wish. This functionality is useful for reading
strips out of files.
.. code-block:: cpp
reader.Seek(10);
while (reader.ReadNextPoint())
...
==============================================================================
Writing using ``liblas::Writer``
==============================================================================
1. Include required header files from libLAS and C++ Standard Library
.. code-block:: cpp
#include <liblas/liblas.hpp>
#include <fstream> // std::ofstream
#include <iostream> // std::cout
2. Create output stream and associate it with .las file opened to write data
in **binary** mode
.. code-block:: cpp
std::ofstream ofs;
ofs.open("file.las", ios::out | ios::binary);
.. note::
It is also possible to open the stream in append mode to add data
to an existing file. You should first instantiate a ``liblas::Reader``
and fetch the ``liblas::Header`` from the file, and then create the
``liblas::Writer`` with that header and the ofstream opened in append
mode.
.. code-block:: cpp
liblas::Header header = reader.GetHeader();
std::ios::openmode m = std::ios::out | std::ios::in | std::ios::binary | std::ios::ate;
ofs.open("file.las", m);
liblas::Writer writer(ofs, header);
3. Create instance of ``liblas::Header`` class to define Public Header Block and set some values
.. code-block:: cpp
liblas::Header header;
header.SetDataFormatId(liblas::ePointFormat1); // Time only
// Set coordinate system using GDAL support
liblas::SpatialReference srs;
srs.SetFromUserInput("EPSG:4326");
header.SetSRS(srs);
// fill other header members
.. note::
Simply setting ``header.SetCompressed(true)`` on the header will be
sufficient to output a compressed file when the ``liblas::Writer`` is
created if `LASzip`_ support is enabled in libLAS, but it is up to the
user to specify the proper file name extension, `.laz`, when writing the
file.
.. note::
The default constructed header object can be used as perfectly valid
header. It will define LAS file according to LAS 1.2 and Point Data Format
3.
4. Create LAS file writer object attached to the output stream and the based
on the header object.
.. code-block:: cpp
liblas::Writer writer(ofs, header);
// here the header has been serialized to disk into the *file.las*
5. Write some point records
.. code-block:: cpp
liblas::Point point;
point.SetCoordinates(10, 20, 30);
// fill other properties of point record
writer.WritePoint(point);
==============================================================================
Copying an .las file
==============================================================================
Below, two simple examples present how to rewrite content from one LAS file to
another, in two different ways.
--------------------------------------------------
Using interface of Reader and Writer classes
--------------------------------------------------
.. code-block:: cpp
#include <liblas/lasreader.hpp>
#include <liblas/laswriter.hpp>
#include <exception>
#include <iostream>
int main()
{
try
{
std::ifstream ifs("input.las", ios::in | ios::binary);
std::ofstream ofs("output.las", ios::out | ios::binary);
liblas::Reader reader(ifs);
liblas::Writer writer(ofs, reader.GetHeader());
while (reader.ReadNextPoint())
{
writer.WritePoint(reader.GetPoint());
}
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}
----------------------------------------------------
Using iterator classes defined for reader and writer
----------------------------------------------------
The library provides two `iterators <http://en.wikipedia.org/wiki/Iterator>`__
compatible with `iterator concept
<http://www.sgi.com/tech/stl/Iterators.html>`__ defined in `C++ Standard
Template Library <http://en.wikipedia.org/wiki/C%2B%2B_standard_library>`__.
* lasreader_iterator - (`input iterator
<http://www.sgi.com/tech/stl/InputIterator.html>`__) used to read data from
LAS file attached to Reader object
* laswriter_iterator - (`output iterator
<http://www.sgi.com/tech/stl/OutputIterator.html>`__) used to write data into
LAS file attached to Writer object.
.. code-block:: cpp
#include <liblas/liblas.hpp>
#include <algorithm>
#include <exception>
#include <iostream>
using namespace liblas;
int main()
{
try
{
std::ifstream ifs("input.las", std::ios::in | std::ios::binary);
std::ofstream ofs("output.las", std::ios::out | std::ios::binary);
Reader reader(ifs);
Writer writer(ofs, reader.GetHeader());
std::copy(lasreader_iterator(reader), lasreader_iterator(),
laswriter_iterator(writer));
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}
==============================================================================
Creating a compressed .laz file from a .las file
==============================================================================
Using the `LASzip`_ library in combination with libLAS, you can write compressed
data that will be much smaller in size than regular LAS data. Of course, there
is some CPU cost to doing so, but its use can be a big win in the right situations.
The following example reads a file, `file.las`, and writes a file, `file.laz`,
that is an exact copy of the file except for it contains compressed data.
.. code-block:: cpp
#include <liblas/liblas.hpp>
#include <fstream> // std::ofstream
#include <algorithm> // std::copy
#include <exception> // std::exception
int main()
{
try
{
std::ifstream ifs("file.las", std::ios::in | std::ios::binary);
std::ofstream ofs("file.laz", std::ios::out | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
liblas::Header header = reader.GetHeader();
header.SetCompressed(true);
liblas::Writer writer(ofs, header);
std::copy(lasreader_iterator(reader), lasreader_iterator(),
laswriter_iterator(writer));
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}
==============================================================================
Applying filters to a reader to extract specified classes
==============================================================================
The following example demonstrates how to use the built-in ClassificationFilter
to filter for classes that match the specified values. You can also provide
your own filters by subclassing liblas::FilterI and providing a filter()
function.
.. note::
Filters are applied in the order they are read from the vector that is
given to the Reader/Writer in the SetFilters call.
.. code-block:: cpp
#include <liblas/liblas.hpp>
#include <vector>
#include <iostream>
class LastReturnFilter: public liblas::FilterI
{
public:
LastReturnFilter();
bool filter(const liblas::Point& point);
private:
LastReturnFilter(LastReturnFilter const& other);
LastReturnFilter& operator=(LastReturnFilter const& rhs);
};
LastReturnFilter::LastReturnFilter( ) : liblas::FilterI(eInclusion) {}
bool LastReturnFilter::filter(const liblas::Point& p)
{
// If the GetReturnNumber equals the GetNumberOfReturns,
// we're a last return
bool output = false;
if (p.GetReturnNumber() == p.GetNumberOfReturns())
{
output = true;
}
// If the type is switched to eExclusion, we'll throw out all last returns.
if (GetType() == eExclusion && output == true)
{
output = false;
} else {
output = true;
}
return output;
}
int main(int argc, char* argv[])
{
std::ifstream ifs;
if (argc < 3)
{
std::cerr << "not all arguments specified. Usage: 'filter input.las output.las'"
<< std::endl;
exit(1);
}
std::string in_file(argv[1]);
std::string out_file(argv[2]);
if (!liblas::Open(ifs, in_file.c_str()))
{
std::cout << "Can not open file" << std::endl;;
exit(1);
}
std::vector<liblas::Classification> classes;
classes.push_back(liblas::Classification(2)); // ground
classes.push_back(liblas::Classification(9)); // water
classes.push_back(liblas::Classification(6)); // building
std::vector<liblas::FilterPtr> filters;
liblas::FilterPtr class_filter = liblas::FilterPtr(new liblas::ClassificationFilter(classes));
// eInclusion means to keep the classes that match. eExclusion would
// throw out those that matched
class_filter->SetType(liblas::FilterI::eInclusion);
filters.push_back(class_filter);
liblas::FilterPtr return_filter = liblas::FilterPtr(new LastReturnFilter());
filters.push_back(return_filter);
liblas::ReaderFactory f;
f.CreateWithStream(ifs);
reader.SetFilters(&filters);
std::ofstream ofs;
if (!liblas::Create(ofs, out_file.c_str()))
{
std::cout <<std::string("Can not create \'") + out_file + "\'" << std::endl;;
exit(1);
}
liblas::Writer writer(ofs, reader.GetHeader());
while (reader.ReadNextPoint())
{
liblas::Point const& p = reader.GetPoint();
writer.WritePoint(p);
}
}
.. _`unit tests package`: http://trac.liblas.org/browser/test/unit
.. _`LASzip`: http://laszip.org
.. _`GDAL`: http://gdal.org
|