1 /****************************************************************************
2 * Copyright (c) 2012, Michael P. Gerlek (mpg@flaxen.com)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. or Flaxen Consulting LLC nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include <memory>
36 #include <vector>
37 
38 #include "NitfWriter.hpp"
39 
40 #include <pdal/PointView.hpp>
41 #include <pdal/private/gdal/GDALUtils.hpp>
42 
43 #ifndef IMPORT_NITRO_API
44 #define IMPORT_NITRO_API
45 #endif
46 #include <nitro/c++/import/nitf.hpp>
47 #include "tre_plugins.hpp"
48 
49 // NOTES
50 //
51 // is it legal to write a LAZ file?
52 // syntactically, how do we name all the LAS writer options that we will
53 // pass to the las writer?
54 
55 namespace pdal
56 {
57 
58 static PluginInfo const s_info
59 {
60     "writers.nitf",
61     "NITF Writer",
62     "http://pdal.io/stages/writers.nitf.html"
63 };
64 
CREATE_SHARED_STAGE(NitfWriter,s_info)65 CREATE_SHARED_STAGE(NitfWriter, s_info)
66 
67 std::string NitfWriter::getName() const { return s_info.name; }
68 
reprojectBoxToDD(const SpatialReference & reference,const BOX3D & box)69 BOX3D NitfWriter::reprojectBoxToDD(const SpatialReference& reference,
70     const BOX3D& box)
71 {
72     if (reference.empty())
73         return BOX3D();
74 
75     BOX3D output(box);
76     if (!gdal::reprojectBounds(output, reference.getWKT(), "EPSG:4326"))
77         throwError("Couldn't reproject corner points to geographic: " +
78             gdal::lastError());
79     return output;
80 }
81 
82 
NitfWriter()83 NitfWriter::NitfWriter()
84 {
85     try
86     {
87         m_nitf.initialize();
88     }
89     catch (const NitfFileWriter::error& err)
90     {
91         throwError(err.what());
92     }
93 }
94 
95 
addArgs(ProgramArgs & args)96 void NitfWriter::addArgs(ProgramArgs& args)
97 {
98     LasWriter::addArgs(args);
99     m_nitf.addArgs(args);
100 }
101 
102 
writeView(const PointViewPtr view)103 void NitfWriter::writeView(const PointViewPtr view)
104 {
105     LasWriter::writeView(view);
106 }
107 
108 
readyFile(const std::string & filename,const SpatialReference & srs)109 void NitfWriter::readyFile(const std::string& filename,
110     const SpatialReference& srs)
111 {
112     m_nitf.setFilename(filename);
113 
114     Utils::writeProgress(m_progressFd, "READYFILE", filename);
115     prepOutput(&m_oss, srs);
116 }
117 
118 
doneFile()119 void NitfWriter::doneFile()
120 {
121     finishOutput();
122 
123     std::streambuf *buf = m_oss.rdbuf();
124     std::streamoff size = buf->pubseekoff(0, m_oss.end);
125     buf->pubseekoff(0, m_oss.beg);
126 
127     std::vector<char> bytes(size);
128     buf->sgetn(bytes.data(), size);
129     m_oss.clear();
130     m_nitf.wrapData(bytes.data(), size);
131     m_nitf.setBounds(reprojectBoxToDD(m_srs, m_lasHeader.getBounds()));
132 
133     try
134     {
135         m_nitf.write();
136     }
137     catch (const NitfFileWriter::error& err)
138     {
139         throwError(err.what());
140     }
141 }
142 
143 } // namespaces
144