jiayuasu commented on code in PR #612:
URL: https://github.com/apache/incubator-sedona/pull/612#discussion_r853631322


##########
sql/src/main/scala/org/apache/spark/sql/sedona_sql/io/GeotiffFileFormat.scala:
##########
@@ -101,4 +126,123 @@ private[spark] class GeotiffFileFormat extends FileFormat 
with DataSourceRegiste
       }
     }
   }
+
+  private def isValidGeoTiffSchema(options: Map[String, String], dataSchema: 
StructType): Boolean = {
+    val colImage = options.getOrElse("key_image", "image")
+    val fields = dataSchema.fieldNames
+    if (fields.contains(colImage) ){
+      val schemaFields = 
dataSchema.fields(dataSchema.fieldIndex(colImage)).dataType.asInstanceOf[StructType]
+      if (schemaFields.fieldNames.length != 6) return false
+    }
+    else {
+      if (fields.length != 6) return false
+    }
+    true
+  }
+
+}
+
+// class for writing geoTiff images
+private class GeotiffFileWriter(savePath: String,
+                                options: Map[String, String],
+                                dataSchema: StructType,
+                                context: TaskAttemptContext) extends 
OutputWriter {
+
+  // set writing parameters
+  private val DEFAULT_WRITE_PARAMS: GeoTiffWriteParams = new 
GeoTiffWriteParams()
+  DEFAULT_WRITE_PARAMS.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
+  DEFAULT_WRITE_PARAMS.setCompressionType("LZW")
+  DEFAULT_WRITE_PARAMS.setCompressionQuality(0.75F)
+  DEFAULT_WRITE_PARAMS.setTilingMode(ImageWriteParam.MODE_EXPLICIT)
+  DEFAULT_WRITE_PARAMS.setTiling(512, 512)
+
+  private val hfs = new Path(savePath).getFileSystem(context.getConfiguration)
+
+  override def write(row: InternalRow): Unit = {
+    // retrieving the metadata of a geotiff image
+    var rowFields: InternalRow = row
+    var schemaFields: StructType = dataSchema
+    val fields = dataSchema.fieldNames
+
+    val colImage = options.getOrElse("key_image", "image")
+    val colOrigin = options.getOrElse("key_origin", "origin")
+    val colBands = options.getOrElse("key_n_bands", "nBands")
+    val colWidth = options.getOrElse("key_width", "width")
+    val colHeight = options.getOrElse("key_height", "height")
+    val colWkt = options.getOrElse("key_wkt", "wkt")
+    val colData = options.getOrElse("key_data", "data")
+
+    if (fields.contains(colImage)) {
+      schemaFields = 
dataSchema.fields(dataSchema.fieldIndex(colImage)).dataType.asInstanceOf[StructType]
+      rowFields = row.getStruct(dataSchema.fieldIndex(colImage), 6)
+    }
+
+    val tiffOrigin = rowFields.getString(schemaFields.fieldIndex(colOrigin))
+    val tiffBands = rowFields.getInt(schemaFields.fieldIndex(colBands))
+    val tiffWidth = rowFields.getInt(schemaFields.fieldIndex(colWidth))
+    val tiffHeight = rowFields.getInt(schemaFields.fieldIndex(colHeight))
+    val tiffGeometry = rowFields.getString(schemaFields.fieldIndex(colWkt))
+    val tiffData = 
rowFields.getArray(schemaFields.fieldIndex(colData)).toDoubleArray()
+
+    // create a writable raster object
+    val raster = RasterFactory.createBandedRaster(DataBuffer.TYPE_DOUBLE, 
tiffWidth, tiffHeight, tiffBands, null)
+
+    // extract the pixels of the geotiff image and write to the writable raster
+    val pixelVal = Array.ofDim[Double](tiffBands)
+    for (i <- 0 until tiffHeight) {
+      for (j <- 0 until tiffWidth) {
+        for (k <- 0 until tiffBands) {
+          pixelVal(k) = tiffData(tiffHeight*tiffWidth*k + i * tiffWidth + j)
+        }
+        raster.setPixel(j, i, pixelVal)
+      }
+    }
+
+    // CRS is always fixed to EPSG:4326
+    val crs = CRS.decode("EPSG:4326", true)

Review Comment:
   @kanchanchy 
   
   In my opinion,
   
   For reader, both "ReadFromCRS" and "ReadToCRS" are optional. We use The 
following combinations:
   
   1. No "ReadFromCRS" and "ReadToCRS" are provided: we don't perform CRS 
transformation even if the raw data has CRS info. We just drop the CRS info.
   2. "ReadFromCRS" NOT provided, "ReadToCRS" are provided: we use the CRS 
available in the GeoTiff as source, use "ReadToCRS" as the target. If the 
GeoTiff does not have CRS info, we throw an exception.
   3. "ReadFromCRS" are provided, "ReadToCRS" are  NOT provided: we don't 
perform CRS transformation even if the raw data has CRS info. We just drop the 
CRS info.
   4. Both "ReadFromCRS" and "ReadToCRS" are provided: we use the "ReadFromCRS" 
and "ReadToCRS" to perform the transformation, ignoring the CRS in the GeoTiff.
   
   For writer, "writeToCRS" is optional.
   
   1. "writeToCRS" is provided: store CRS info in GeoTiff metadata. No CRS 
transformation is needed.
   2. "writeToCRS" is NOT provided: don't store CRS info in GeoTiff metadata, 
or store WGS84 (aka epsg:4326) in the metadata. No CRS transformation is needed.



-- 
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]

Reply via email to