You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sedona.apache.org by GitBox <gi...@apache.org> on 2022/04/19 19:08:04 UTC

[GitHub] [incubator-sedona] jiayuasu commented on a diff in pull request #612: [SEDONA-108] Write Support for GeoTiff Raster Images

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


##########
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:
   This should NOT be always fixed to EPSG:4326. The user will use ST_Transform in their DF to convert coordinates to whatever CRS they like. Then call df.write.save to save GeoTiff. GeoTiff meta data has a place for storing the name of the CRS.



-- 
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-unsubscribe@sedona.apache.org

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