Skip to content

Commit

Permalink
Fix SentinelXMLMetadataRasterSource again. #177
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileSonneveld committed Aug 4, 2023
1 parent aee01fc commit e3ada2d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.openeo.geotrellis.layers

import geotrellis.proj4.CRS
import geotrellis.raster.io.geotiff.OverviewStrategy
import geotrellis.raster.{CellSize, CellType, FloatConstantNoDataCellType, FloatConstantTile, GridBounds, GridExtent, MultibandTile, Raster, RasterMetadata, RasterSource, ResampleMethod, ResampleTarget, SourceName, TargetCellType}
import geotrellis.raster.{CellSize, CellType, FloatConstantNoDataCellType, FloatConstantTile, GridBounds, GridExtent, MultibandTile, Raster, RasterMetadata, RasterSource, ResampleMethod, ResampleTarget, SourceName, SourcePath, TargetCellType}
import geotrellis.vector.Extent
import org.openeo.opensearch.OpenSearchResponses.CreoFeatureCollection

Expand All @@ -13,9 +13,6 @@ object SentinelXMLMetadataRasterSource {

/**
* Returns SAA,SZA,VAA,VZA selected by the bands argument.
* @param path
* @param bands
* @return
*/
def apply(path:String, bands:Seq[Int]=Seq(0,1,2,3)): Seq[SentinelXMLMetadataRasterSource] = {
val xmlDoc = XML.load(CreoFeatureCollection.loadMetadata(path))
Expand All @@ -33,16 +30,22 @@ object SentinelXMLMetadataRasterSource {
val ulx = (position \ "ULX").text.toDouble
val uly = (position \ "ULY").text.toDouble
// TODO: Looking at the cloud locations in the accompanying cloud mask file
// TODO: It appears that the extent should be: (ulx, uly-(10*10980), ulx+(10*10980), uly)
val extent = Extent(ulx-(10*10980),uly-(10*10980),ulx,uly)
val extent = Extent(ulx, uly - (10 * 10980), ulx + (10 * 10980), uly)
val gridExtent = GridExtent[Long](extent,CellSize(10,10))
val allBands = Seq(mSAA,mSZA,mVAA,mVZA)
bands.map(b => allBands(b)).map(new SentinelXMLMetadataRasterSource(_,crs,gridExtent))
bands.map(b => allBands(b)).map(new SentinelXMLMetadataRasterSource(_, crs, gridExtent, OpenEoSourcePath(path)))

}
}

class SentinelXMLMetadataRasterSource(value: Float, theCrs:CRS, theGridExtent:GridExtent[Long]) extends RasterSource{
// SourcePath is a trait, so we need to subclass it to instantiate.
case class OpenEoSourcePath(value: String) extends SourcePath

class SentinelXMLMetadataRasterSource(value: Float,
theCrs: CRS,
theGridExtent: GridExtent[Long],
sourcePathName: OpenEoSourcePath,
) extends RasterSource {

val targetCellType = None
val gridSize = 23
Expand All @@ -59,13 +62,13 @@ class SentinelXMLMetadataRasterSource(value: Float, theCrs:CRS, theGridExtent:Gr
}

override def read(bounds: GridBounds[Long], bands: Seq[Int]): Option[Raster[MultibandTile]] = {

Some(Raster(MultibandTile(FloatConstantTile(value,bounds.width.intValue(),bounds.height.intValue())),null))
val extent = gridExtent.extentFor(bounds, clamp = false)
Some(Raster(MultibandTile(FloatConstantTile(value, bounds.width.intValue(), bounds.height.intValue())), extent))
}

override def convert(targetCellType: TargetCellType): RasterSource = ???

override def name: SourceName = null
override def name: SourceName = sourcePathName

override def crs: CRS = theCrs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ class SentinelXMLMetadataRasterSourceTest {

@Test
def testReadAngles(): Unit = {
val source: Seq[SentinelXMLMetadataRasterSource] = SentinelXMLMetadataRasterSource(getClass.getResource("/org/openeo/geotrellis/layers/MTD_TL.xml").getPath)
val path = getClass.getResource("/org/openeo/geotrellis/layers/MTD_TL.xml").getPath
val source: Seq[SentinelXMLMetadataRasterSource] = SentinelXMLMetadataRasterSource(path)
assertEquals( 171.800, source(0).read().get.tile.band(0).getDouble(0,0),0.001)
assertEquals( 65.707, source(1).read().get.tile.band(0).getDouble(0,0),0.001)
assertEquals( 251.333, source(2).read().get.tile.band(0).getDouble(0,0),0.001)
assertEquals(source.head.name.toString, path)
}
}

0 comments on commit e3ada2d

Please sign in to comment.