[pdal] How to get las header info?

Peder Axensten Peder.Axensten at slu.se
Wed Jul 6 04:30:16 PDT 2022


Thank you very much, Mateusz.
I should’ve suspected it. :-)
My own convention is to give such classes a “_view” suffix.

I attach my solution in case someone else has a similar problem. Since a LasHeader object is contained in LasReader it is better just to save the LasReader object and ask for header() when needed. But I had constructed my code around a header type, so I implemented a LasHeader subclass with a shared_ptr to the LasReader object.


class LasHeader_stable : public pdal::LasHeader {
using Reader_ptr = std::shared_ptr< pdal::LasReader >;

static Reader_ptr get_reader_ptr( const std::filesystem::path & path_ ) {
//From https://pdal.io/api/transition/index.html
pdal::Optionslas_opts;
las_opts.add( pdal::Option( "filename", path_.native() ) );

Reader_ptrreader_ptr{ std::make_shared< pdal::LasReader >() };
reader_ptr->setOptions( las_opts );
pdal::PointTabletable;
reader_ptr->prepare( table );

return reader_ptr;
}

Reader_ptrm_reader_ptr;

public:
LasHeader_stable( Reader_ptr reader_ptr ) :
pdal::LasHeader{ reader_ptr->header() },
m_reader_ptr{ reader_ptr }
{}

LasHeader_stable( const std::filesystem::path & path_ ) :
LasHeader_stable( get_reader_ptr( path_ ) )
{}

LasHeader_stable( const LasHeader_stable & )= default;
LasHeader_stable( LasHeader_stable && )= default;
LasHeader_stable & operator=( const LasHeader_stable & )= default;
LasHeader_stable & operator=( LasHeader_stable && )= default;
};


inline auto get_header( const std::filesystem::path & path_ ) {
//In Port, to list all installed PDAL versions, use "port installed pdal".
//In port, to change the PDAL version, use e.g. "sudo port activate PDAL @2.3.0_2+postgresql13+python39".
#if ( PDAL_VERSION_MAJOR == 2 ) && ( PDAL_VERSION_MINOR >= 4 )
return LasHeader_stable{ path_ };
#else
pdal::LasHeaderheader{};
pdal::ILeStreaminput{ path_.native() };
input >> header;
return header;
#endif
}



Best regards,

Peder Axensten
Systems Developer

Remote Sensing
Department of Forest Resource Management
Swedish University of Agricultural Sciences
SE-901 83 Umeå
Visiting address: Skogsmarksgränd
Phone: +46 90 786 85 00
peder.axensten at slu.se, www.slu.se/srh

The Department of Forest Resource Management is environmentally certified in accordance with ISO 14001.

> On 6 Jul 2022, at 00:05, Mateusz Loskot <mateusz at loskot.net> wrote:
>
> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
>
>
> On Tue, 5 Jul 2022 at 19:06, Peder Axensten <Peder.Axensten at slu.se> wrote:
>>
>> I tried that too. :-)
>> I tried the following, inspired by code on https://pdal.io/api/transition/index.html:
>>
>>    pdal::LasHeader get_header( const std::filesystem::path & path_ ) {
>>        pdal::Option              las_opt( "filename", path_.native() );
>>        pdal::Options             las_opts;
>>        las_opts.add( las_opt );
>>
>>        pdal::PointTable          table;
>>        pdal::LasReader           las_reader;
>>        las_reader.setOptions( las_opts );
>>        las_reader.prepare( table );
>>
>>        pdal::PointViewSet        point_view_set = las_reader.execute( table );
>>        pdal::PointViewPtr        point_view = *point_view_set.begin();
>>        pdal::Dimension::IdList   dims = point_view->dims();
>>        return las_reader.header();
>
> You observe garbage header because it actually is destroyed at exit
> from get_header.
> You must keep LasReader object alive for as long as you refer to its
> LasHeader object.
> The LasHeader is NOT a value type, but a thin 'view' over internal object.
>
> The bug in your code is somewhat equivalent to this
>
> auto foo()
> {
>    std::vector<int> v;
>    return v.begin();
> }
>
> Best regards,
> --
> Mateusz Loskot, http://mateusz.loskot.net
> _______________________________________________
> pdal mailing list
> pdal at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/pdal

---
När du skickar e-post till SLU så innebär detta att SLU behandlar dina personuppgifter. För att läsa mer om hur detta går till, klicka här <https://www.slu.se/om-slu/kontakta-slu/personuppgifter/>
E-mailing SLU will result in SLU processing your personal data. For more information on how this is done, click here <https://www.slu.se/en/about-slu/contact-slu/personal-data/>


More information about the pdal mailing list