Skip to content

Commit

Permalink
Allocate byte array per InputStream in LASreadItemCompressed_BYTE14_v3
Browse files Browse the repository at this point in the history
Add test using data obtained from https://www.fisheries.noaa.gov/inport/item/71943
2019_saipan_waveform.laz has waveform written as compressed extra bytes
  • Loading branch information
mreutegg committed Jan 18, 2025
1 parent e843288 commit 8deb2ca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public class LASreadItemCompressed_BYTE14_v3 extends LASreadItemCompressed{

private boolean[] requested_Bytes;

private byte[] bytes;
private int num_bytes_allocated;

private int current_context;
private LAScontextBYTE14[] contexts = new LAScontextBYTE14[4];

Expand Down Expand Up @@ -77,11 +74,6 @@ public LASreadItemCompressed_BYTE14_v3(IByteStreamInProvider instreamProvider, i
}
}

/* init the bytes buffer to zero */

bytes = null;
num_bytes_allocated = 0;

/* mark the four scanner channel contexts as uninitialized */

for (int c = 0; c < contexts.length; c++)
Expand Down Expand Up @@ -134,24 +126,18 @@ public void init(PointDataRecord seedItem, MutableInteger context) {
{
if (requested_Bytes[i]) num_bytes += num_bytes_Bytes[i];
}

/* make sure the buffer is sufficiently large */

if (num_bytes > num_bytes_allocated)
{
bytes = new byte[num_bytes];
num_bytes_allocated = num_bytes;
}

/* load the requested bytes and init the corresponding instreams an decoders */


/* load the requested bytes and init the corresponding instreams and decoders */

byte[] bytes;
num_bytes = 0;
for (i = 0; i < number; i++)
{
if (requested_Bytes[i])
{
if (num_bytes_Bytes[i] != 0)
{
bytes = new byte[num_bytes_Bytes[i]];
instream.getBytes(bytes, num_bytes_Bytes[i]);
instream_Bytes[i].init(bytes, num_bytes_Bytes[i]);
dec_Bytes[i].init(instream_Bytes[i]);
Expand Down
38 changes: 16 additions & 22 deletions src/test/java/com/github/mreutegg/laszip4j/DataFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class DataFiles extends ExternalResource {
// test file contributed via https://github.com/mreutegg/laszip4j/pull/141
public static final String LAZ_14_V3_RGB_NAME = "500m_5460_59370_IM2023_subset.laz";

public static final String LAZ_14_BYTES_V3_COMPRESSED_NAME = "2019_saipan_waveform.laz";
public static final int LAZ_14_BYTES_V3_COMPRESSED_NUM_POINT_RECORDS = 5198;

// file created with txt2las as described here: https://groups.google.com/g/lasroom/c/DWQ2GXKE8f8
public static final String EXTRA_TYPES_NAME = "extra-bytes.las";

Expand All @@ -55,6 +58,7 @@ public class DataFiles extends ExternalResource {
public final File laz14 = new File(target, LAZ_14_NAME);
public final File extraBytes = new File(resources, EXTRA_TYPES_NAME);
public final File laz14v3rgb = new File(resources, LAZ_14_V3_RGB_NAME);
public final File laz14v3bytesCompressed = new File(resources, LAZ_14_BYTES_V3_COMPRESSED_NAME);

@Override
protected void before() throws Throwable {
Expand All @@ -63,32 +67,22 @@ protected void before() throws Throwable {

public void download() throws Exception {
if (!laz.exists()) {
URI url = new URI(LAZ_BASE_URL + "/" + LAZ_NAME);
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
try (OutputStream out = Files.newOutputStream(laz.toPath())) {
response.getEntity().writeTo(out);
}
}
}
download(LAZ_BASE_URL + "/" + LAZ_NAME, laz);
}
if (!las.exists()) {
URI url = new URI(LAS_BASE_URL + "/" + LAS_NAME);
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
try (OutputStream out = Files.newOutputStream(las.toPath())) {
response.getEntity().writeTo(out);
}
}
}
download(LAS_BASE_URL + "/" + LAS_NAME, las);
}
if (!laz14.exists()) {
URI url = new URI(LAZ_14_BASE_URL + "/" + LAZ_14_NAME);
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
try (OutputStream out = Files.newOutputStream(laz14.toPath())) {
response.getEntity().writeTo(out);
}
download(LAZ_14_BASE_URL + "/" + LAZ_14_NAME, laz14);
}
}

private static void download(String uri, File file) throws Exception {
URI url = new URI(uri);
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
try (OutputStream out = Files.newOutputStream(file.toPath())) {
response.getEntity().writeTo(out);
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/github/mreutegg/laszip4j/LASReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,28 @@ public void readLaz14v3RGB() {
assertEquals(21877, minBlue);
assertEquals(63435, maxBlue);
}

@Test
public void read() {
LASReader reader = new LASReader(files.laz14v3bytesCompressed);

int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE;
long numPoints = 0;
for (LASPoint p : reader.getPoints()) {
minX = Math.min(minX, p.getX());
maxX = Math.max(maxX, p.getX());
minY = Math.min(minY, p.getY());
maxY = Math.max(maxY, p.getY());
numPoints++;
}

assertEquals(DataFiles.LAZ_14_BYTES_V3_COMPRESSED_NUM_POINT_RECORDS, numPoints);
assertEquals(7025714, minX);
assertEquals(7031235, maxX);
assertEquals(7520541, minY);
assertEquals(7525854, maxY);
}
}
Binary file added src/test/resources/2019_saipan_waveform.laz
Binary file not shown.

0 comments on commit 8deb2ca

Please sign in to comment.