jiayuasu commented on code in PR #2846: URL: https://github.com/apache/sedona/pull/2846#discussion_r3126784793
########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/io/geotiffmetadata/GeoTiffMetadataPartitionReader.scala: ########## @@ -0,0 +1,452 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.spark.sql.sedona_sql.io.geotiffmetadata + +import org.apache.hadoop.conf.Configuration +import org.apache.hadoop.fs.Path +import org.apache.sedona.common.raster.RasterAccessors +import org.apache.sedona.common.raster.RasterBandAccessors +import org.apache.sedona.common.raster.inputstream.HadoopImageInputStream +import org.apache.sedona.common.utils.RasterUtils +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.GenericInternalRow +import org.apache.spark.sql.catalyst.util.GenericArrayData +import org.apache.spark.sql.connector.read.PartitionReader +import org.apache.spark.sql.execution.datasources.PartitionedFile +import org.apache.spark.sql.types.StructType +import org.apache.spark.unsafe.types.UTF8String +import org.geotools.coverage.grid.GridCoverage2D +import org.geotools.gce.geotiff.GeoTiffReader +import org.geotools.referencing.crs.DefaultEngineeringCRS + +import java.net.URI +import scala.collection.mutable +import scala.util.Try + +class GeoTiffMetadataPartitionReader( + configuration: Configuration, + partitionedFiles: Array[PartitionedFile], + readDataSchema: StructType) + extends PartitionReader[InternalRow] { + + private var currentFileIndex = 0 + private var currentRow: InternalRow = _ + + override def next(): Boolean = { + if (currentFileIndex < partitionedFiles.length) { + currentRow = readFileMetadata(partitionedFiles(currentFileIndex)) + currentFileIndex += 1 + true + } else { + false + } + } + + override def get(): InternalRow = currentRow + + override def close(): Unit = {} + + // Fields requiring reader.read() to build a GridCoverage2D + private val COVERAGE_FIELDS = Set( + "width", + "height", + "numBands", + "srid", + "crs", + "geoTransform", + "cornerCoordinates", + "bands") + + private def readFileMetadata(partition: PartitionedFile): InternalRow = { + val requested = readDataSchema.fieldNames.toSet + val needCoverage = requested.exists(COVERAGE_FIELDS.contains) + val needIsTiled = requested.contains("isTiled") + val needBands = requested.contains("bands") + val needTiffMetadata = requested.contains("metadata") + val needCompression = requested.contains("compression") + val needReader = + needCoverage || needIsTiled || needBands || needTiffMetadata || needCompression || + requested.contains("overviews") + + val path = new Path(new URI(partition.filePath.toString())) + + // Skip all I/O if only cheap fields (path, driver, fileSize) are requested + if (!needReader) { + return buildRow(path, partition, null, null, false, -1, Map.empty, null) + } + + val imageStream = new HadoopImageInputStream(path, configuration) + var reader: GeoTiffReader = null + var raster: GridCoverage2D = null + try { + reader = new GeoTiffReader( + imageStream, + new org.geotools.util.factory.Hints( + org.geotools.util.factory.Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, + java.lang.Boolean.TRUE)) + + // Extract TIFF IIO metadata BEFORE read() to avoid stream state issues. + // Only extract fields actually requested. + val isTiled = + if (needIsTiled) GeoTiffMetadataPartitionReader.hasTiffTag(reader, 322) else false + val photometric = Review Comment: Fixed in 9ab55f8210. The metadata root node is now fetched once per file via `reader.getMetadata().getRootNode()` and passed to the helpers (`hasTiffTagInRoot`, `extractPhotometricFromRoot`, `extractMetadataFromRoot`, `extractCompressionFromRoot`). Each file's DOM tree is walked at most once per helper invocation, and `reader.getMetadata` is no longer called repeatedly when multiple tag-dependent columns are selected. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
