1 /******************************************************************************
2  * $Id$
3  *
4  * Project:  libLAS - http://liblas.org - A BSD library for LAS format data.
5  * Purpose:  LAS 1.0 writer implementation for C++ libLAS
6  * Author:   Mateusz Loskot, mateusz@loskot.net
7  *
8  ******************************************************************************
9  * Copyright (c) 2008, Mateusz Loskot
10  *
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following
15  * conditions are met:
16  *
17  *     * Redistributions of source code must retain the above copyright
18  *       notice, this list of conditions and the following disclaimer.
19  *     * Redistributions in binary form must reproduce the above copyright
20  *       notice, this list of conditions and the following disclaimer in
21  *       the documentation and/or other materials provided
22  *       with the distribution.
23  *     * Neither the name of the Martin Isenburg or Iowa Department
24  *       of Natural Resources nor the names of its contributors may be
25  *       used to endorse or promote products derived from this software
26  *       without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
35  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
39  * OF SUCH DAMAGE.
40  ****************************************************************************/
41 
42 #include <liblas/liblas.hpp>
43 #include <liblas/detail/writer/writer.hpp>
44 #include <liblas/detail/writer/header.hpp>
45 #include <liblas/detail/writer/point.hpp>
46 #include <liblas/detail/private_utility.hpp>
47 // std
48 #include <vector>
49 #include <fstream>
50 #include <stdexcept>
51 #include <cstdlib> // std::size_t
52 #include <cassert>
53 
54 namespace liblas { namespace detail {
55 
WriterImpl(std::ostream & ofs)56 WriterImpl::WriterImpl(std::ostream& ofs) :
57     m_ofs(ofs)
58     , m_point_writer(PointWriterPtr( ))
59     , m_header_writer(HeaderWriterPtr())
60     , m_header(HeaderPtr())
61     , m_pointCount(0)
62 {
63 }
64 
65 
WriteHeader()66 void WriterImpl::WriteHeader()
67 {
68     m_header_writer = HeaderWriterPtr(new writer::Header(m_ofs, m_pointCount, *m_header) );
69 
70     m_header_writer->write();
71 
72     m_header = HeaderPtr(new liblas::Header(m_header_writer->GetHeader()));
73 }
74 
UpdatePointCount(boost::uint32_t count)75 void WriterImpl::UpdatePointCount(boost::uint32_t count)
76 {
77     boost::uint32_t out = m_pointCount;
78 
79     if ( count != 0 ) { out = count; }
80 
81     m_header->SetPointRecordsCount(out);
82 
83     if (!m_ofs.good() ) return;
84     // Skip to first byte of number of point records data member
85     std::streamsize const dataPos = 107;
86     m_ofs.seekp(dataPos, std::ios::beg);
87     detail::write_n(m_ofs, out , sizeof(out));
88 }
89 
WritePoint(liblas::Point const & point)90 void WriterImpl::WritePoint(liblas::Point const& point)
91 {
92     if (m_point_writer.get() == 0) {
93         m_point_writer = PointWriterPtr(new writer::Point(m_ofs, m_pointCount, m_header));
94     }
95     m_point_writer->write(point);
96 
97 }
98 
~WriterImpl()99 WriterImpl::~WriterImpl()
100 {
101     // Try to update the point count on our way out, but we don't really
102     // care if we weren't able to write it.
103     try
104     {
105         UpdatePointCount(m_pointCount);
106 
107     } catch (std::runtime_error const&)
108     {
109 
110     }
111 
112 }
113 
SetFilters(std::vector<liblas::FilterPtr> const & filters)114 void WriterImpl::SetFilters(std::vector<liblas::FilterPtr> const& filters)
115 {
116     m_filters = filters;
117 }
GetFilters() const118 std::vector<liblas::FilterPtr>  WriterImpl::GetFilters() const
119 {
120     return m_filters;
121 }
122 
SetTransforms(std::vector<liblas::TransformPtr> const & transforms)123 void WriterImpl::SetTransforms(std::vector<liblas::TransformPtr> const& transforms)
124 {
125     m_transforms = transforms;
126 }
GetTransforms() const127 std::vector<liblas::TransformPtr>  WriterImpl::GetTransforms() const
128 {
129     return m_transforms;
130 }
131 
GetHeader() const132 liblas::Header& WriterImpl::GetHeader() const
133 {
134     return *m_header;
135 }
SetHeader(liblas::Header const & header)136 void WriterImpl::SetHeader(liblas::Header const& header)
137 {
138     m_header = HeaderPtr(new liblas::Header(header));
139 }
140 
141 
142 }} // namespace liblas::detail
143