jiayuasu commented on code in PR #2673: URL: https://github.com/apache/sedona/pull/2673#discussion_r2851276883
########## spark/common/src/main/scala/org/apache/spark/sql/sedona_sql/io/raster/RasterPartitionReader.scala: ########## @@ -0,0 +1,213 @@ +/* + * 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.raster + +import org.apache.hadoop.conf.Configuration +import org.apache.hadoop.fs.Path +import org.apache.sedona.common.raster.RasterConstructors +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.codegen.UnsafeRowWriter +import org.apache.spark.sql.connector.read.PartitionReader +import org.apache.spark.sql.execution.datasources.PartitionedFile +import org.apache.spark.sql.sedona_sql.UDT.RasterUDT +import org.apache.spark.sql.sedona_sql.io.raster.RasterPartitionReader.rasterToInternalRows +import org.apache.spark.sql.sedona_sql.io.raster.RasterTable.{MAX_AUTO_TILE_SIZE, RASTER, RASTER_NAME, TILE_X, TILE_Y} +import org.apache.spark.sql.types.StructType +import org.geotools.coverage.grid.GridCoverage2D + +import java.net.URI +import scala.collection.JavaConverters._ + +class RasterPartitionReader( + configuration: Configuration, + partitionedFiles: Array[PartitionedFile], + dataSchema: StructType, + rasterOptions: RasterOptions) + extends PartitionReader[InternalRow] { + + // Track the current file index we're processing + private var currentFileIndex = 0 + + // Current raster being processed + private var currentRaster: GridCoverage2D = _ + + // Current row + private var currentRow: InternalRow = _ + + // Iterator for the current file's tiles + private var currentIterator: Iterator[InternalRow] = Iterator.empty + + override def next(): Boolean = { + // If current iterator has more elements, return true + if (currentIterator.hasNext) { + currentRow = currentIterator.next() + return true + } + + // If current iterator is exhausted, but we have more files, load the next file + if (currentFileIndex < partitionedFiles.length) { + loadNextFile() + if (currentIterator.hasNext) { + currentRow = currentIterator.next() + return true + } + } + + // No more data + false + } + + override def get(): InternalRow = { + currentRow + } + + override def close(): Unit = { + if (currentRaster != null) { + currentRaster.dispose(true) + currentRaster = null + } + } + + private def loadNextFile(): Unit = { + // Clean up previous raster if exists + if (currentRaster != null) { + currentRaster.dispose(true) + currentRaster = null + } + + if (currentFileIndex >= partitionedFiles.length) { + currentIterator = Iterator.empty + return + } + + val partition = partitionedFiles(currentFileIndex) + val path = new Path(new URI(partition.filePath.toString())) + + try { + // Read file bytes from Hadoop FS + val fs = path.getFileSystem(configuration) + val fileStatus = fs.getFileStatus(path) + val fileLength = fileStatus.getLen.toInt + val bytes = new Array[Byte](fileLength) + val inputStream = fs.open(path) + try { + org.apache.hadoop.io.IOUtils.readFully(inputStream, bytes, 0, fileLength) + } finally { + inputStream.close() + } + + // Create in-db GridCoverage2D from GeoTiff bytes. The RenderedImage is lazy - + // pixel data will only be decoded when accessed via image.getData(Rectangle). + currentRaster = RasterConstructors.fromGeoTiff(bytes) + currentIterator = rasterToInternalRows(currentRaster, dataSchema, rasterOptions, path) Review Comment: Fixed. -- 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]
