Skip to content

Commit

Permalink
TileFeature Serialization and I/O Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
James McClain committed Apr 6, 2016
1 parent b6e8c18 commit 0ec6d21
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
69 changes: 69 additions & 0 deletions spark/src/test/scala/geotrellis/spark/io/TileFeatureIOSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package geotrellis.spark.io

import geotrellis.raster._
import geotrellis.raster.io.geotiff.SinglebandGeoTiff
import geotrellis.spark._
import geotrellis.spark.io._
import geotrellis.spark.io.file._
import geotrellis.spark.io.index._
import geotrellis.vector.Extent

import org.scalatest.FunSpec


class TileFeatureIOSpec
extends FunSpec
with TestEnvironment {

describe("TileFeature AvroRecordCodec") {
val path = "raster-test/data/aspect.tif"
val gt = SinglebandGeoTiff(path)
val originalRaster = gt.raster.resample(500, 500)
val (_, rdd) = createTileLayerRDD(originalRaster, 5, 5, gt.crs)
val outRdd = rdd.withContext {
_.map({ case (key,tile) => key -> TileFeature(tile,tile) })
}

val toDisk = outRdd
.collect
.sortWith((a,b) => a.toString.compareTo(b.toString) < 0)

val writer = FileLayerWriter(outputLocalPath)
val reader = FileLayerReader(outputLocalPath)
val deleter = FileLayerDeleter(outputLocalPath)

it("should allow proper writing and reading") {
import java.security.MessageDigest
val md = MessageDigest.getInstance("SHA-256");
val layerId = LayerId("TileFeatureLayer", 0)

try {
writer.write[SpatialKey, TileFeature[Tile, Tile], TileLayerMetadata[SpatialKey]](layerId, outRdd, ZCurveKeyIndexMethod)

val fromDisk = reader.read[SpatialKey, TileFeature[Tile, Tile], TileLayerMetadata[SpatialKey]](layerId)
.collect
.sortWith((a,b) => a.toString.compareTo(b.toString) < 0)

toDisk.zip(fromDisk).map({ case (a,b) =>
md.reset
md.update(a._1.toString.getBytes)
md.update(a._2.tile.toBytes)
md.update(a._2.data.toBytes)
val digestA = md.digest

md.reset
md.update(b._1.toString.getBytes)
md.update(b._2.tile.toBytes)
md.update(b._2.data.toBytes)
val digestB = md.digest

MessageDigest.isEqual(digestA, digestB) should be (true)
})
}
finally {
deleter.delete(layerId)
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package geotrellis.spark.io.avro

import geotrellis.raster._
import geotrellis.spark.io._
import org.scalatest._


class TileFeatureCodecSpec extends FunSpec with Matchers with AvroTools {
describe("TileFeatureCodec") {
val tile = IntArrayTile.fill(0, 10, 10)

it("should encode/decode a TileFeature of a Tile and a Tile"){
roundTrip(TileFeature(tile,tile))
}

it("should encode/decode a TileFeature of a Tile and a non-Tile"){
roundTrip(TileFeature(tile,TileFeature(tile,tile)))
}

}
}

0 comments on commit 0ec6d21

Please sign in to comment.