<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal>liblas-devs,<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>I believe I&#8217;ve isolated a bug that I was having a hard
time tracking down. It seems to require a fairly narrow set of circumstances for
it to appear. The particular circumstances that will cause the bug are when you
have an existing LAS file with a data offset larger than the header size(Such
as when you have VLRs), but there are zero points in the file. In my particular
case, I was writing out the header in one method, then populating the points in
another existing method designed to append points to existing files.(I have
sample code demonstrating the problem below) In the writer/header.cpp file, at
the end of the write() method, this block of code appears to be the problem.<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New";color:blue'>    if</span><span style='font-size:10.0pt;
font-family:"Courier New"'> (!bAppendMode) <o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>    {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>        WriteVLRs();<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>        <span style='color:green'>// Write the 1.0
pad signature if we need to.<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>        WriteLAS10PadSignature(); <o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>    }           <o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>    <span style='color:green'>// If we already have
points, we're going to put it at the end of the file.  <o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>    <span style='color:green'>// If we don't have
any points,  we're going to leave it where it is.<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>    <span style='color:blue'>if</span> (m_pointCount
!= 0)<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>       
m_ofs.seekp(0, std::ios::end);<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal>Before entering this section, the m_ofs stream is positioned
at the end of the file header. Since the file existed before being opened to
write the points, the &#8220;bAppendMode&#8221; variable is true, skipping that
block. However, there are no points in the file, so the stream position remains
unchanged. Any VLRs after the header will be overridden when points are added
to the file. Later, when the header write() method is called when the writer&#8217;s
destructor is called, the method uses the file size and data offset to
calculate the number of points. Because the point data started before the data
offset mark, the resulting file size is off by the difference between the header
size and the offset. This results in the number of points reported by the
header being changed to a lower figure than the number of points you&#8217;ve
actually written. For a quick fix here, I&#8217;ve add an &#8220;else&#8221;
block locally:<o:p></o:p></p>

<p class=MsoNormal style='text-indent:.5in;text-autospace:none'><span
style='font-size:10.0pt;font-family:"Courier New";color:blue'>if</span><span
style='font-size:10.0pt;font-family:"Courier New"'> (m_pointCount != 0)<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>        m_ofs.seekp(0, std::ios::end);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      }<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      <span style='color:blue'>else<o:p></o:p></span></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>        m_ofs.seekp(0, m_header.getDataOffset();<o:p></o:p></span></p>

<p class=MsoNormal><span style='font-size:10.0pt;font-family:"Courier New"'>      }</span><o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>I used the following code to duplicate the problem. If you
comment out the line setting the data offset, the resulting file will have 500k
points. With that line in place, the point count will be 499981.<o:p></o:p></p>

<p class=MsoNormal>                <o:p></o:p></p>

<p class=MsoNormal>                std::string path( &#8220;MyTestFile.las&#8221;
);<o:p></o:p></p>

<p class=MsoNormal style='text-indent:.5in;text-autospace:none'><span
style='font-size:10.0pt;font-family:"Courier New"'>std::fstream out;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      out.open( path.c_str(), std::ios::out |
std::ios::binary );<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      liblas::LASHeader newHeader;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      newHeader.SetPointRecordsCount( 500000 );<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      newHeader.SetDataOffset(759);//Toggle to see
the differences<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      newHeader.SetDataFormatId(
liblas::ePointFormat1 );<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>            liblas::LASWriter testWriter( out,
newHeader );<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      }<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      liblas::LASWriter test2Writer( out,
newHeader);<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      <span style='color:blue'>for</span> ( size_t i
= 0; i &lt; 500000 ; i++ )<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      {<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>            liblas::LASPoint point;<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>            point.SetCoordinates( 10 + i, 20 + i, 30
+ i );<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>            test2Writer.WritePoint( point );<o:p></o:p></span></p>

<p class=MsoNormal style='text-autospace:none'><span style='font-size:10.0pt;
font-family:"Courier New"'>      }</span><o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>Tim Black<o:p></o:p></p>

<p class=MsoNormal>Ball Aerospace<o:p></o:p></p>

<p class=MsoNormal>571-357-2158<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

</body>

</html>


<pre>

This message and any enclosures are intended only for the addressee.  Please  
notify the sender by email if you are not the intended recipient.  If you are  
not the intended recipient, you may not use, copy, disclose, or distribute this  
message or its contents or enclosures to any other person and any such actions  
may be unlawful.  Ball reserves the right to monitor and review all messages  
and enclosures sent to or from this email address.</pre>