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


##########
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:
   @jiayuasu When writing a GeoTiff, we usually need to pass the CRS as a 
parameter when creating the reference envelop. It indicates the CRS of the 
polygon representing the image source. So, we need to pass the correct CRS info 
in line 208 
[here](https://github.com/apache/incubator-sedona/pull/612/files/6fe92ec5c2736f8cbd6d9dd381380a1f5ac6aab8#diff-9afdfa8529cd2458278c7f0a2abb09bfaaa0599fe312b02ff6085d72c49fdda2).



-- 
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: dev-unsubscr...@sedona.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to