From ffbc74caa55697019aa45284a91eadaf5783cc71 Mon Sep 17 00:00:00 2001 From: Bertil Chapuis Date: Sat, 12 Oct 2024 13:23:44 +0200 Subject: [PATCH] Fix issues in group --- .../baremaps/geoparquet/GeoParquetGroup.java | 121 ++++++++---------- .../geoparquet/GeoParquetMetadata.java | 24 ++-- .../baremaps/geoparquet/GeoParquetSchema.java | 4 +- .../geoparquet/GeoParquetReaderTest.java | 2 +- .../baremaps/geoparquet/OvertureMapsTest.java | 2 +- 5 files changed, 68 insertions(+), 85 deletions(-) diff --git a/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetGroup.java b/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetGroup.java index 35617447a..38c6dd746 100644 --- a/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetGroup.java +++ b/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetGroup.java @@ -133,10 +133,6 @@ public List getValues(int fieldIndex) { return (List) data[fieldIndex]; } - private GeoParquetGroup getGroup(int fieldIndex) { - return (GeoParquetGroup) data[fieldIndex]; - } - private void addValue(int fieldIndex, Object value) { Object currentValue = data[fieldIndex]; if (currentValue instanceof List) { @@ -225,48 +221,6 @@ private GeoParquetException createGeoParquetException(int fieldIndex, String ele return new GeoParquetException(msg); } - public String toString() { - return toString(""); - } - - public String toString(String indent) { - StringBuilder builder = new StringBuilder(); - appendToString(builder, indent); - return builder.toString(); - } - - private void appendToString(StringBuilder builder, String indent) { - int i = 0; - for (org.apache.parquet.schema.Type field : parquetSchema.getFields()) { - String name = field.getName(); - Object object = data[i]; - ++i; - if (object != null) { - if (object instanceof Listvalues) { - for (Object value : values) { - builder.append(indent).append(name); - if (value == null) { - builder.append(": NULL\n"); - } else if (value instanceof GeoParquetGroup group) { - builder.append('\n'); - group.appendToString(builder, indent + " "); - } else { - builder.append(": ").append(value).append('\n'); - } - } - } else { - builder.append(indent).append(name); - if (object instanceof GeoParquetGroup group) { - builder.append('\n'); - group.appendToString(builder, indent + " "); - } else { - builder.append(": ").append(object).append('\n'); - } - } - } - } - } - public GeoParquetSchema getGeoParquetSchema() { return geoParquetSchema; } @@ -280,30 +234,10 @@ public GeoParquetMetadata getGeoParquetMetadata() { } // Getter methods for different data types - public String getString(int fieldIndex, int index) { + public String getStringValue(int fieldIndex, int index) { return getBinaryValue(fieldIndex, index).toStringUsingUTF8(); } - public int getInteger(int fieldIndex, int index) { - return (int) getValue(fieldIndex, index); - } - - public long getLong(int fieldIndex, int index) { - return (long) getValue(fieldIndex, index); - } - - public double getDouble(int fieldIndex, int index) { - return (double) getValue(fieldIndex, index); - } - - public float getFloat(int fieldIndex, int index) { - return (float) getValue(fieldIndex, index); - } - - public boolean getBoolean(int fieldIndex, int index) { - return (boolean) getValue(fieldIndex, index); - } - public Binary getBinaryValue(int fieldIndex, int index) { return (Binary) getValue(fieldIndex, index); } @@ -343,7 +277,7 @@ public Long getLongValue(int fieldIndex) { } public String getStringValue(int fieldIndex) { - return getString(fieldIndex, 0); + return getStringValue(fieldIndex, 0); } public Geometry getGeometryValue(int fieldIndex) { @@ -514,4 +448,55 @@ public List getEnvelopeValues(String fieldName) { return getEnvelopeValues(parquetSchema.getFieldIndex(fieldName)); } + public String toString() { + return toString(""); + } + + private String toString(String indent) { + StringBuilder builder = new StringBuilder(); + int fieldCount = parquetSchema.getFields().size(); + + for (int i = 0; i < fieldCount; i++) { + String fieldName = parquetSchema.getFieldName(i); + Object fieldValue = data[i]; + if (fieldValue != null) { + appendFieldToString(builder, indent, fieldName, fieldValue); + } + } + + return builder.toString(); + } + + private void appendFieldToString(StringBuilder builder, String indent, String fieldName, + Object fieldValue) { + if (fieldValue instanceof Listvalues) { + for (Object value : values) { + appendValueToString(builder, indent, fieldName, value); + } + } else { + appendValueToString(builder, indent, fieldName, fieldValue); + } + } + + private void appendValueToString(StringBuilder builder, String indent, String fieldName, + Object value) { + builder.append(indent).append(fieldName); + if (value == null) { + builder.append(": NULL\n"); + } else if (value instanceof GeoParquetGroup group) { + builder.append("\n").append(group.toString(indent + " ")); + } else { + String valueString = getValueAsString(value); + builder.append(": ").append(valueString).append("\n"); + } + } + + private String getValueAsString(Object value) { + if (value instanceof Binary binary) { + return binary.toStringUsingUTF8(); + } else { + return value.toString(); + } + } + } diff --git a/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetMetadata.java b/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetMetadata.java index 4e45aaa32..35f32d63d 100644 --- a/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetMetadata.java +++ b/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetMetadata.java @@ -64,7 +64,7 @@ public record Column( @JsonProperty("crs") JsonNode crs, @JsonProperty("orientation") String orientation, @JsonProperty("edges") String edges, - @JsonProperty("bbox") Double[] bbox) { + @JsonProperty("bbox") List bbox) { } /** @@ -94,13 +94,12 @@ public int getSrid(String column) { String code = idNode.get("code").asText(); // Determine SRID based on authority and code - switch (authority) { - case "OGC": - return getOgcSrid(code); // Handle OGC specific SRIDs - case "EPSG": - return getEpsgCode(code); // Handle EPSG SRIDs - default: - return 4326; // Default SRID if authority is unrecognized + if (authority.equals("EPSG")) { + return getEpsgCode(code); + } else if (authority.equals("OGC")) { + return getOgcSrid(code); + } else { + return 4326; // Default SRID if authority is unrecognized } } @@ -111,11 +110,10 @@ public int getSrid(String column) { * @return the SRID, or 0 if the code is unrecognized */ private int getOgcSrid(String code) { - switch (code) { - case "CRS84": - return 4326; - default: - return 0; // Unrecognized OGC code + if ("CRS84".equals(code)) { + return 4326; + } else { + return 0; // Unrecognized OGC code } } diff --git a/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetSchema.java b/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetSchema.java index ef47952a9..f7c1b5454 100644 --- a/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetSchema.java +++ b/baremaps-geoparquet/src/main/java/org/apache/baremaps/geoparquet/GeoParquetSchema.java @@ -59,8 +59,8 @@ public enum Cardinality { * Sealed interfaces were introduced in Java 17 and can be used with pattern matching since Java * 21. */ - sealed - public interface Field { + public sealed + interface Field { String name(); Type type(); diff --git a/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/GeoParquetReaderTest.java b/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/GeoParquetReaderTest.java index 849720a3e..efda0f7a3 100644 --- a/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/GeoParquetReaderTest.java +++ b/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/GeoParquetReaderTest.java @@ -30,7 +30,7 @@ class GeoParquetReaderTest { void read() { URI geoParquet = TestFiles.GEOPARQUET.toUri(); GeoParquetReader geoParquetReader = new GeoParquetReader(geoParquet); - assertEquals(5, geoParquetReader.read().count()); + assertEquals(5, geoParquetReader.read().peek(System.out::println).count()); } @Test diff --git a/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/OvertureMapsTest.java b/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/OvertureMapsTest.java index 1fbe19b8b..eeddfc768 100644 --- a/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/OvertureMapsTest.java +++ b/baremaps-geoparquet/src/test/java/org/apache/baremaps/geoparquet/OvertureMapsTest.java @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test; import org.locationtech.jts.geom.Envelope; -public class OvertureMapsTest { +class OvertureMapsTest { @Disabled("Requires access to the Internet") @Test