bool ReaderImpl::ReadPointAt(std::size_t n, LASPoint& point, const LASHeader& header) { // Read point data record format 0 // TODO: Replace with compile-time assert double t=0; detail::PointRecord record; assert(LASHeader::ePointSize0 == sizeof(record)); if (m_size <= n) return false; std::streamsize pos = (static_cast<std::streamsize>(n) * m_recordlength) + m_offset; m_ifs.clear(); m_ifs.seekg(pos, std::ios::beg); detail::read_n(record, m_ifs, sizeof(record)); Reader::FillPoint(record, point); point.SetCoordinates(header, point.GetX(), point.GetY(), point.GetZ()); if (header.GetDataFormatId() == LASHeader::ePointFormat1) { detail::read_n(t, m_ifs, sizeof(double)); point.SetTime(t); } return true; }
bool ReaderImpl::ReadNextPoint(LASPoint& point, const LASHeader& header) { // Read point data record format 0 // TODO: Replace with compile-time assert detail::PointRecord record; double t=0; assert(LASHeader::ePointSize0 == sizeof(record)); if (0 == m_current) { m_ifs.clear(); m_ifs.seekg(m_offset, std::ios::beg); } if (m_current < m_size) { try { detail::read_n(record, m_ifs, sizeof(PointRecord)); ++m_current; } catch (std::out_of_range const& e) // we reached the end of the file { std::cerr << e.what() << std::endl; return false; } Reader::FillPoint(record, point); point.SetCoordinates(header, point.GetX(), point.GetY(), point.GetZ()); if (header.GetDataFormatId() == LASHeader::ePointFormat1) { detail::read_n(t, m_ifs, sizeof(double)); point.SetTime(t); } return true; } return false; }
void WriterImpl::WriteHeader(LASHeader& header) { uint8_t n1 = 0; uint16_t n2 = 0; uint32_t n4 = 0; // Rewrite the georeference VLR entries if they exist header.SetGeoreference(); // Seek to the beginning m_ofs.seekp(0, std::ios::beg); std::ios::pos_type beginning = m_ofs.tellp(); // Seek to the end m_ofs.seekp(0, std::ios::end); std::ios::pos_type end = m_ofs.tellp(); // Figure out how many points we already have. Each point record // should be 20 bytes long, and header.GetDataOffset tells // us the location to start counting points from. // This test should only be true if we were opened in both // std::ios::in *and* std::ios::out, otherwise it should return false // and we won't adjust the point count. if ((beginning != end) && ((uint32_t)end != 0)) { m_pointCount = ((uint32_t) end - header.GetDataOffset())/header.GetDataRecordLength(); // Position to the beginning of the file to start writing the header m_ofs.seekp(0, std::ios::beg); } // 1. File Signature std::string const filesig(header.GetFileSignature()); assert(filesig.size() == 4); detail::write_n(m_ofs, filesig, 4); // 2. Reserved n4 = header.GetReserved(); detail::write_n(m_ofs, n4, sizeof(n4)); // 3-6. GUID data uint32_t d1 = 0; uint16_t d2 = 0; uint16_t d3 = 0; uint8_t d4[8] = { 0 }; liblas::guid g = header.GetProjectId(); g.output_data(d1, d2, d3, d4); detail::write_n(m_ofs, d1, sizeof(d1)); detail::write_n(m_ofs, d2, sizeof(d2)); detail::write_n(m_ofs, d3, sizeof(d3)); detail::write_n(m_ofs, d4, sizeof(d4)); // 7. Version major n1 = header.GetVersionMajor(); assert(1 == n1); detail::write_n(m_ofs, n1, sizeof(n1)); // 8. Version minor n1 = header.GetVersionMinor(); assert(0 == n1); detail::write_n(m_ofs, n1, sizeof(n1)); // 9. System ID std::string sysid(header.GetSystemId(true)); assert(sysid.size() == 32); detail::write_n(m_ofs, sysid, 32); // 10. Generating Software ID std::string softid(header.GetSoftwareId(true)); assert(softid.size() == 32); detail::write_n(m_ofs, softid, 32); // 11. Flight Date Julian n2 = header.GetCreationDOY(); detail::write_n(m_ofs, n2, sizeof(n2)); // 12. Year n2 = header.GetCreationYear(); detail::write_n(m_ofs, n2, sizeof(n2)); // 13. Header Size n2 = header.GetHeaderSize(); assert(227 <= n2); detail::write_n(m_ofs, n2, sizeof(n2)); // 14. Offset to data n4 = header.GetDataOffset(); detail::write_n(m_ofs, n4, sizeof(n4)); // 15. Number of variable length records // TODO: This value must be updated after new variable length record is added. n4 = header.GetRecordsCount(); detail::write_n(m_ofs, n4, sizeof(n4)); // 16. Point Data Format ID n1 = static_cast<uint8_t>(header.GetDataFormatId()); detail::write_n(m_ofs, n1, sizeof(n1)); // 17. Point Data Record Length n2 = header.GetDataRecordLength(); detail::write_n(m_ofs, n2, sizeof(n2)); // 18. Number of point records // This value is updated if necessary, see UpdateHeader function. n4 = header.GetPointRecordsCount(); detail::write_n(m_ofs, n4, sizeof(n4)); // 19. Number of points by return std::vector<uint32_t>::size_type const srbyr = 5; std::vector<uint32_t> const& vpbr = header.GetPointRecordsByReturnCount(); assert(vpbr.size() <= srbyr); uint32_t pbr[srbyr] = { 0 }; std::copy(vpbr.begin(), vpbr.end(), pbr); detail::write_n(m_ofs, pbr, sizeof(pbr)); // 20-22. Scale factors detail::write_n(m_ofs, header.GetScaleX(), sizeof(double)); detail::write_n(m_ofs, header.GetScaleY(), sizeof(double)); detail::write_n(m_ofs, header.GetScaleZ(), sizeof(double)); // 23-25. Offsets detail::write_n(m_ofs, header.GetOffsetX(), sizeof(double)); detail::write_n(m_ofs, header.GetOffsetY(), sizeof(double)); detail::write_n(m_ofs, header.GetOffsetZ(), sizeof(double)); // 26-27. Max/Min X detail::write_n(m_ofs, header.GetMaxX(), sizeof(double)); detail::write_n(m_ofs, header.GetMinX(), sizeof(double)); // 28-29. Max/Min Y detail::write_n(m_ofs, header.GetMaxY(), sizeof(double)); detail::write_n(m_ofs, header.GetMinY(), sizeof(double)); // 30-31. Max/Min Z detail::write_n(m_ofs, header.GetMaxZ(), sizeof(double)); detail::write_n(m_ofs, header.GetMinZ(), sizeof(double)); WriteVLR(header); uint8_t const sgn1 = 0xCC; uint8_t const sgn2 = 0xDD; detail::write_n(m_ofs, sgn1, sizeof(uint8_t)); detail::write_n(m_ofs, sgn2, sizeof(uint8_t)); // If we already have points, we're going to put it at the end of the file. // If we don't have any points, we're going to leave it where it is. if (m_pointCount != 0) m_ofs.seekp(0, std::ios::end); }