Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply_dimension(dim=bands) => do not filter out all nodata tiles #322

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,15 @@ class OpenEOProcesses extends Serializable {
return ContextRDD(new org.apache.spark.rdd.PairRDDFunctions[K,MultibandTile](datacube).mapValues(tile => {
if (!tile.isInstanceOf[EmptyMultibandTile]) {
val resultTiles = function(tile.bands)
MultibandTile(resultTiles)
if(resultTiles.exists(!_.isNoDataTile)){
MultibandTile(resultTiles)
}else{
new EmptyMultibandTile(tile.cols,tile.rows,tile.cellType,resultTiles.length)
}
}else{
tile
tile//at this point, the band count of the empty tile may be wrong
}
}).filter(_._2.bands.exists(!_.isNoDataTile)),datacube.metadata.copy(cellType = scriptBuilder.getOutputCellType()))
}),datacube.metadata.copy(cellType = scriptBuilder.getOutputCellType()))
}

def filterEmptyTile[K:ClassTag](datacube:MultibandTileLayerRDD[K]): RDD[(K, MultibandTile)] with Metadata[TileLayerMetadata[K]]={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaPairRDD$;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.rdd.RDD;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import scala.Function1;
import scala.Option;
import scala.Tuple2;
import scala.collection.JavaConversions;
Expand Down Expand Up @@ -54,24 +57,6 @@ public static void shutDownSparkContext() {
}


@DisplayName("Test combining all bands.")
@Test
public void testMapBands() {
OpenEOProcessScriptBuilder processBuilder = TestOpenEOProcessScriptBuilder.createNormalizedDifferenceProcess10AddXY();
assertEquals(FloatConstantNoDataCellType$.MODULE$,processBuilder.getOutputCellType());
Tile tile0 = new UByteConstantTile((byte)3,256,256, (UByteCells) CellType$.MODULE$.fromName("uint8"));
Tile tile1 = new UByteConstantTile((byte)1,256,256, (UByteCells) CellType$.MODULE$.fromName("uint8"));
RDD<Tuple2<SpatialKey, MultibandTile>> datacube = TileLayerRDDBuilders$.MODULE$.createMultibandTileLayerRDD(SparkContext.getOrCreate(), new ArrayMultibandTile(new Tile[]{tile0,tile1}), new TileLayout(1, 1, 256, 256));
ClassTag<SpatialKey> tag = scala.reflect.ClassTag$.MODULE$.apply(SpatialKey.class);
ContextRDD<SpatialKey, MultibandTile, TileLayerMetadata<SpatialKey>> ndviDatacube = (ContextRDD<SpatialKey, MultibandTile, TileLayerMetadata<SpatialKey>>) new OpenEOProcesses().<SpatialKey>mapBandsGeneric(datacube, processBuilder, new HashMap<>(), tag);
assertEquals(FloatConstantNoDataCellType$.MODULE$,ndviDatacube.metadata().cellType());
List<Tuple2<SpatialKey, MultibandTile>> result = ndviDatacube.toJavaRDD().collect();
assertEquals(FloatConstantNoDataCellType$.MODULE$,result.get(0)._2.cellType());
System.out.println("result = " + result);
assertEquals(1, result.get(0)._2().bandCount());
double[] doubles = result.get(0)._2().band(0).toArrayDouble();
assertEquals(0.5,doubles[0],0.0);
}

@Test
public void testMapToIntervals() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.apache.hadoop.security.UserGroupInformation
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
import org.junit.Assert._
import org.junit.jupiter.api.{AfterAll, BeforeAll}
import org.junit.jupiter.api.{AfterAll, BeforeAll, DisplayName}
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments.arguments
import org.junit.jupiter.params.provider.{Arguments, EnumSource, MethodSource}
Expand Down Expand Up @@ -598,4 +598,34 @@ class OpenEOProcessesSpec extends RasterMatchers {

}

@DisplayName("Test combining all bands.")
@Test def testMapBands(): Unit = {
val processBuilder = TestOpenEOProcessScriptBuilder.createNormalizedDifferenceProcess10AddXY
assertEquals(FloatConstantNoDataCellType, processBuilder.getOutputCellType)
val inputCellType = UByteConstantNoDataCellType
val tile0 = new UByteConstantTile(3.toByte, 256, 256, inputCellType)
val tile1 = new UByteConstantTile(1.toByte, 256, 256, inputCellType)
val datacube = TileLayerRDDBuilders.createMultibandTileLayerRDD(SparkContext.getOrCreate, new ArrayMultibandTile(Array[Tile](tile0, tile1)), new TileLayout(4, 4, 64, 64))
val withEmptyTiles = datacube.withContext{_.map((v1: Tuple2[SpatialKey, MultibandTile]) => {
if (v1._1 == new SpatialKey(1, 0)) {
val tile = v1._2
new Tuple2[SpatialKey, MultibandTile](v1._1, new EmptyMultibandTile(tile.cols, tile.rows, tile.cellType, tile.bandCount))
}
else v1
})}

val ndviDatacube = new OpenEOProcesses().mapBandsGeneric[SpatialKey](withEmptyTiles, processBuilder, new util.HashMap[String, Any])
assertEquals(FloatConstantNoDataCellType, ndviDatacube.metadata.cellType)
val result = ndviDatacube.collectAsMap()
val key0 = SpatialKey(0, 0)
assertEquals(FloatConstantNoDataCellType, result(key0).cellType)
System.out.println("result = " + result)
assertEquals(1, result(key0).bandCount)
val doubles = result(key0).band(0).toArrayDouble
assertEquals(0.5, doubles(0), 0.0)
assertTrue(result(SpatialKey(1,0)).isInstanceOf[EmptyMultibandTile])
// this test fails, because ndvi function doesn't really know the band count
//assertEquals(1,result(SpatialKey(1,0)).bandCount)
}

}