1 /****************************************************************************** 2 * Copyright (c) 2015, Hobu Inc. (hobu@hobu.co) 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. nor the names of its contributors 17 * may be used to endorse or promote products derived from this 18 * software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 * OF SUCH DAMAGE. 32 ****************************************************************************/ 33 34 #pragma once 35 36 #include <pdal/PDALUtils.hpp> 37 #include <pdal/Scaling.hpp> 38 #include <pdal/Writer.hpp> 39 40 namespace pdal 41 { 42 43 class PDAL_DLL FlexWriter : public Writer 44 { 45 protected: FlexWriter()46 FlexWriter() : m_filenum(1) 47 {} 48 49 std::string m_filename; 50 Scaling m_scaling; 51 validateFilename(PointTableRef table)52 void validateFilename(PointTableRef table) 53 { 54 if (!table.supportsView() && 55 (m_filename.find('#') != std::string::npos)) 56 { 57 std::ostringstream oss; 58 oss << getName() << ": Can't write with template-based " 59 "filename using streaming point table."; 60 throw pdal_error(oss.str()); 61 } 62 } 63 64 private: 65 std::string::size_type m_hashPos; 66 l_initialize(PointTableRef table)67 virtual void l_initialize(PointTableRef table) final 68 { 69 Writer::l_initialize(table); 70 try { 71 m_hashPos = handleFilenameTemplate(m_filename); 72 } 73 catch (const pdal_error& err) 74 { 75 throwError(err.what()); 76 } 77 } 78 generateFilename()79 std::string generateFilename() 80 { 81 std::string filename = m_filename; 82 if (m_hashPos != std::string::npos) { 83 std::string fileCount = std::to_string(m_filenum++); 84 filename.replace(m_hashPos, 1, fileCount); 85 } 86 return filename; 87 } 88 89 #if (__GNUG__ < 4 || (__GNUG__ == 4 && __GNUG_MINOR__ < 7)) 90 #define final final 91 #endif 92 srsOverridden() const93 virtual bool srsOverridden() const 94 { return false; } 95 ready(PointTableRef table)96 virtual void ready(PointTableRef table) final 97 { 98 readyTable(table); 99 100 // Ready the file if we're writing a single file. 101 if (m_hashPos == std::string::npos) 102 { 103 if (!table.spatialReferenceUnique() && !srsOverridden()) 104 log()->get(LogLevel::Error) << getName() << 105 ": Attempting to write '" << m_filename << 106 "' with multiple point spatial references." << std::endl; 107 readyFile(generateFilename(), table.spatialReference()); 108 } 109 } 110 prerun(const PointViewSet & views)111 virtual void prerun(const PointViewSet& views) final 112 { 113 // If the output is a consolidation of all views, call 114 // prerun with all views. 115 if (m_hashPos == std::string::npos) 116 prerunFile(views); 117 } 118 119 // This essentially moves ready() and done() into write(), which means 120 // that they get executed once for each view. The check for m_hashPos 121 // is a test to see if the filename specification is a template. If it's 122 // not a template, ready() and done() are taken care of in the ready() 123 // and done() functions in this class. write(const PointViewPtr view)124 virtual void write(const PointViewPtr view) final 125 { 126 if (m_hashPos != std::string::npos) 127 { 128 if (view->size() == 0) 129 return; 130 // Ready the file - we're writing each view separately. 131 readyFile(generateFilename(), view->spatialReference()); 132 prerunFile({view}); 133 } 134 writeView(view); 135 if (m_hashPos != std::string::npos) 136 doneFile(); 137 } 138 done(PointTableRef table)139 virtual void done(PointTableRef table) final 140 { 141 if (m_hashPos == std::string::npos) 142 doneFile(); 143 doneTable(table); 144 } 145 146 #undef final 147 readyTable(PointTableRef table)148 virtual void readyTable(PointTableRef table) 149 {} 150 doneTable(PointTableRef table)151 virtual void doneTable(PointTableRef table) 152 {} 153 154 virtual void readyFile(const std::string& filename, 155 const SpatialReference& srs) = 0; prerunFile(const PointViewSet & pvSet)156 virtual void prerunFile(const PointViewSet& pvSet) 157 {} 158 virtual void writeView(const PointViewPtr view) = 0; doneFile()159 virtual void doneFile() 160 {} 161 162 size_t m_filenum; 163 164 FlexWriter& operator=(const FlexWriter&); // not implemented 165 FlexWriter(const FlexWriter&); // not implemented 166 }; 167 168 } // namespace pdal 169 170