Skip to content

Commit

Permalink
support construction of GDALMetadata #317
Browse files Browse the repository at this point in the history
  • Loading branch information
bossie committed Oct 25, 2024
1 parent 4f1ede1 commit 4c4275e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class GTiffOptions extends Serializable {
var resampleMethod:String = "near"
var separateAssetPerBand = false

private val xmlTags = collection.mutable.Buffer[String]() // TODO: improve

def setFilenamePrefix(name: String): Unit = this.filenamePrefix = name

def setSeparateAssetPerBand(value: Boolean): Unit = this.separateAssetPerBand = value
Expand Down Expand Up @@ -47,20 +49,42 @@ class GTiffOptions extends Serializable {
})
}

def addHeadTag(tagName:String,value:String): Unit = {
def addHeadTag(tagName:String, value:String): Unit = {
tags = Tags(tags.headTags + (tagName -> value), tags.bandTags)

xmlTags append asItemElement(tagName, value)
}

def addBandTag(bandIndex:Int, tagName:String,value:String): Unit = {
def addBandTag(bandIndex: Int, tagName:String, value:String, role: Option[String]): Unit = {
val emptyMap = Map.empty[String, String]
var newBandTags = Vector.fill[Map[String,String]](math.max(bandIndex+1,tags.bandTags.size))(emptyMap)
newBandTags = newBandTags.zipAll(tags.bandTags,emptyMap,emptyMap).map(elem => elem._1 ++ elem._2)
newBandTags = newBandTags.updated(bandIndex, newBandTags(bandIndex) + (tagName -> value))
tags = Tags(tags.headTags ,newBandTags.toList)

xmlTags append asItemElement(tagName, value, Some(bandIndex), role)
}

def addBandTag(bandIndex: Int, tagName: String, value: String): Unit =
addBandTag(bandIndex, tagName, value, role = None)

def setBandTags(newBandTags: List[Map[String, String]]): Unit = {
tags = Tags(tags.headTags, newBandTags)

xmlTags.clear()
for ((tagName, value) <- tags.headTags) addHeadTag(tagName, value)
for {
(band, bandIndex) <- newBandTags.zipWithIndex
(tagName, value) <- band
} addBandTag(bandIndex, tagName, value)
}

def toGdalMetadataXml: String = {
val buffer = new StringBuilder("<GDALMetadata>")
for (xmlTag <- xmlTags) buffer.appendAll(xmlTag)
buffer.appendAll("</GDALMetadata>")

buffer.toString
}

/**
Expand All @@ -78,4 +102,11 @@ class GTiffOptions extends Serializable {
val ois = new java.io.ObjectInputStream(bais)
ois.readObject().asInstanceOf[GTiffOptions]
}

private def asItemElement(name: String, value: Any, index: Option[Int] = None,
role: Option[String] = None): String = {
val sampleAttribute = index.map(i => s"""sample="$i"""").getOrElse("")
val roleAttribute = role.map(r => s"""role="$r"""").getOrElse("")
s"""<Item name="$name" $sampleAttribute $roleAttribute>$value</Item>"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package org.openeo.geotrellis.geotiff
import org.junit.Assert.assertEquals
import org.junit.Test

import scala.xml.XML
import scala.xml.Utility.trim

class GTiffOptionsTest {

@Test
Expand All @@ -13,7 +16,7 @@ class GTiffOptionsTest {
val bandNames = Seq("VV", "VH", "mask", "local_incidence_angle")

for ((bandName, index) <- bandNames.zipWithIndex) {
options.addBandTag(index, "DESCRIPTION", bandName)
options.addBandTag(index, "DESCRIPTION", bandName, role = Some("description"))
}

assertEquals(Map("PROCESSING_SOFTWARE" -> "0.6.1a1"), options.tags.headTags)
Expand All @@ -23,5 +26,16 @@ class GTiffOptionsTest {
Map("DESCRIPTION" -> "mask"),
Map("DESCRIPTION" -> "local_incidence_angle")
), options.tags.bandTags)

val expectedGdalMetadataXml =
<GDALMetadata>
<Item name="PROCESSING_SOFTWARE">0.6.1a1</Item>
<Item name="DESCRIPTION" sample="0" role="description">VV</Item>
<Item name="DESCRIPTION" sample="1" role="description">VH</Item>
<Item name="DESCRIPTION" sample="2" role="description">mask</Item>
<Item name="DESCRIPTION" sample="3" role="description">local_incidence_angle</Item>
</GDALMetadata>

assertEquals(trim(expectedGdalMetadataXml), trim(XML.loadString(options.toGdalMetadataXml)))
}
}

0 comments on commit 4c4275e

Please sign in to comment.