Skip to content

Commit

Permalink
Add apply overloads with noDataValue to ArrayTile subtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
echeipesh committed Oct 26, 2016
1 parent 6305be3 commit b9c3a05
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 22 deletions.
26 changes: 26 additions & 0 deletions raster/src/main/scala/geotrellis/raster/ByteArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ object ByteArrayTile {
new ByteUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

/**
* Create a new [[ByteArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of bytes
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new ByteArrayTile
*/
def apply(arr: Array[Byte], cols: Int, rows: Int, noDataValue: Option[Byte]): ByteArrayTile =
apply(arr, cols, rows, ByteCells.withNoData(noDataValue))

/**
* Create a new [[ByteArrayTile]] from an array of bytes, a
* number of columns, and a number of rows.
*
* @param arr An array of bytes
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new ByteArrayTile
*/
def apply(arr: Array[Byte], cols: Int, rows: Int, noDataValue: Byte): ByteArrayTile =
apply(arr, cols, rows, Some(noDataValue))

/**
* Produce a [[ByteArrayTile]] of the specified dimensions.
*
Expand Down
67 changes: 56 additions & 11 deletions raster/src/main/scala/geotrellis/raster/CellType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,17 @@ sealed abstract class DataType extends Serializable { self: CellType =>
val bits: Int
val isFloatingPoint: Boolean
val name: String

/** Determine if two [[CellType]] instances have equal [[DataType]] component */
def equalDataType(other: DataType): Boolean

/** Creates CellType with requested NoData semantics.
* In case where [[DataType]] is not Double noDataValue will be coerced to that type.
* This may lead to loss of precision but will leave NoData consistent with tile cells.
*
* @param noDataValue Optional NoData Value
* @return [[DataType]] unchanged but with [[NoDataHandling]] implied by the value of the parameter
*/
def withNoData(noDataValue: Option[Double]): CellType

def bytes = bits / 8
Expand Down Expand Up @@ -124,11 +134,16 @@ sealed trait ByteCells extends DataType { self: CellType =>
val name = "int8"
def equalDataType(other: DataType) = other.isInstanceOf[ByteCells]
def withNoData(noDataValue: Option[Double]): ByteCells with NoDataHandling =
ByteCells.withNoData(noDataValue.map(_.toByte))
}

object ByteCells {
def withNoData(noDataValue: Option[Byte]): ByteCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.toInt == Byte.MinValue =>
case Some(nd) if nd == Byte.MinValue =>
ByteConstantNoDataCellType
case Some(nd) =>
ByteUserDefinedNoDataCellType(nd.toByte)
ByteUserDefinedNoDataCellType(nd)
case None =>
ByteCellType
}
Expand All @@ -143,11 +158,16 @@ sealed trait UByteCells extends DataType { self: CellType =>
val name = "uint8"
def equalDataType(other: DataType) = other.isInstanceOf[UByteCells]
def withNoData(noDataValue: Option[Double]): UByteCells with NoDataHandling =
UByteCells.withNoData(noDataValue.map(_.toByte))
}

object UByteCells {
def withNoData(noDataValue: Option[Byte]): UByteCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.toInt == 0 =>
case Some(nd) if nd == 0 =>
UByteConstantNoDataCellType
case Some(nd) =>
UByteUserDefinedNoDataCellType(nd.toByte)
UByteUserDefinedNoDataCellType(nd)
case None =>
UByteCellType
}
Expand All @@ -162,11 +182,16 @@ sealed trait ShortCells extends DataType { self: CellType =>
val name = "int16"
def equalDataType(other: DataType) = other.isInstanceOf[ShortCells]
def withNoData(noDataValue: Option[Double]): ShortCells with NoDataHandling =
ShortCells.withNoData(noDataValue.map(_.toShort))
}

object ShortCells {
def withNoData(noDataValue: Option[Short]): ShortCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.toInt == Short.MinValue =>
case Some(nd) if nd == Short.MinValue =>
ShortConstantNoDataCellType
case Some(nd) =>
ShortUserDefinedNoDataCellType(nd.toShort)
ShortUserDefinedNoDataCellType(nd)
case None =>
ShortCellType
}
Expand All @@ -181,11 +206,16 @@ sealed trait UShortCells extends DataType { self: CellType =>
val name = "uint16"
def equalDataType(other: DataType) = other.isInstanceOf[UShortCells]
def withNoData(noDataValue: Option[Double]): UShortCells with NoDataHandling =
UShortCells.withNoData(noDataValue.map(_.toShort))
}

object UShortCells {
def withNoData(noDataValue: Option[Short]): UShortCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.toInt == 0 =>
case Some(nd) if nd == 0 =>
UShortConstantNoDataCellType
case Some(nd) =>
UShortUserDefinedNoDataCellType(nd.toShort)
UShortUserDefinedNoDataCellType(nd)
case None =>
UShortCellType
}
Expand All @@ -200,11 +230,16 @@ sealed trait IntCells extends DataType { self: CellType =>
val name = "int32"
def equalDataType(other: DataType) = other.isInstanceOf[IntCells]
def withNoData(noDataValue: Option[Double]): IntCells with NoDataHandling =
IntCells.withNoData(noDataValue.map(_.toInt))
}

object IntCells {
def withNoData(noDataValue: Option[Int]): IntCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.toInt == Int.MinValue =>
case Some(nd) if nd == Int.MinValue =>
IntConstantNoDataCellType
case Some(nd) =>
IntUserDefinedNoDataCellType(nd.toInt)
IntUserDefinedNoDataCellType(nd)
case None =>
IntCellType
}
Expand All @@ -216,11 +251,16 @@ sealed trait FloatCells extends DataType { self: CellType =>
val name = "float32"
def equalDataType(other: DataType) = other.isInstanceOf[FloatCells]
def withNoData(noDataValue: Option[Double]): FloatCells with NoDataHandling =
FloatCells.withNoData(noDataValue.map(_.toFloat))
}

object FloatCells {
def withNoData(noDataValue: Option[Float]): FloatCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.isNaN =>
FloatConstantNoDataCellType
case Some(nd) =>
FloatUserDefinedNoDataCellType(nd.toFloat)
FloatUserDefinedNoDataCellType(nd)
case None =>
FloatCellType
}
Expand All @@ -234,6 +274,11 @@ sealed trait DoubleCells extends DataType { self: CellType =>
val isFloatingPoint: Boolean = true
val name = "float64"
def equalDataType(other: DataType) = other.isInstanceOf[DoubleCells]
def withNoData(noDataValue: Option[Double]): DoubleCells with NoDataHandling =
DoubleCells.withNoData(noDataValue)
}

object DoubleCells {
def withNoData(noDataValue: Option[Double]): DoubleCells with NoDataHandling =
noDataValue match {
case Some(nd) if nd.isNaN =>
Expand Down
27 changes: 27 additions & 0 deletions raster/src/main/scala/geotrellis/raster/DoubleArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,33 @@ object DoubleArrayTile {
new DoubleUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

/**
* Create a new [[DoubleArrayTile]] from an array of doubles, a
* number of columns, and a number of rows.
*
* @param arr An array of doubles
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new DoubleArrayTile
*/
def apply(arr: Array[Double], cols: Int, rows: Int, noDataValue: Option[Double]): DoubleArrayTile =
apply(arr, cols, rows, DoubleCells.withNoData(noDataValue))

/**
* Create a new [[DoubleArrayTile]] an array of doubles, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new DoubleArrayTile
*/
def apply(arr: Array[Double], cols: Int, rows: Int, noDataValue: Double): DoubleArrayTile =
apply(arr, cols, rows, Some(noDataValue))


/**
* Produce a [[DoubleArrayTile]] of the specified dimensions.
*
Expand Down
26 changes: 26 additions & 0 deletions raster/src/main/scala/geotrellis/raster/FloatArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,32 @@ object FloatArrayTile {
new FloatUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

/**
* Create a new [[DoubleArrayTile]] from an array of floats, a
* number of columns, and a number of rows.
*
* @param arr An array of floats
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new DoubleArrayTile
*/
def apply(arr: Array[Float], cols: Int, rows: Int, noDataValue: Option[Float]): FloatArrayTile =
apply(arr, cols, rows, FloatCells.withNoData(noDataValue))

/**
* Create a new [[DoubleArrayTile]] an array of floats, a
* number of columns, and a number of rows.
*
* @param arr An array of floats
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new DoubleArrayTile
*/
def apply(arr: Array[Float], cols: Int, rows: Int, noDataValue: Float): FloatArrayTile =
apply(arr, cols, rows, Some(noDataValue))

/**
* Produce a [[FloatArrayTile]] of the specified dimensions.
*
Expand Down
35 changes: 24 additions & 11 deletions raster/src/main/scala/geotrellis/raster/IntArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -220,18 +220,31 @@ object IntArrayTile {
new IntUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

def apply(arr: Array[Int], cols: Int, rows: Int, noData: Option[Int]): IntArrayTile =
noData match {
case Some(nd) if isNoData(nd) =>
new IntConstantNoDataArrayTile(arr, cols, rows)
case Some(nd) =>
new IntUserDefinedNoDataArrayTile(arr, cols, rows, IntUserDefinedNoDataCellType(nd))
case None =>
new IntRawArrayTile(arr, cols, rows)
}
/**
* Create a new [[IntArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new IntArrayTile
*/
def apply(arr: Array[Int], cols: Int, rows: Int, noDataValue: Option[Int]): IntArrayTile =
apply(arr, cols, rows, IntCells.withNoData(noDataValue))

def apply(arr: Array[Int], cols: Int, rows: Int, noData: Int): IntArrayTile =
apply(arr, cols, rows, Some(noData))
/**
* Create a new [[IntArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new IntArrayTile
*/
def apply(arr: Array[Int], cols: Int, rows: Int, noDataValue: Int): IntArrayTile =
apply(arr, cols, rows, Some(noDataValue))

/**
* Produce a [[IntArrayTile]] of the specified dimensions.
Expand Down
26 changes: 26 additions & 0 deletions raster/src/main/scala/geotrellis/raster/ShortArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ object ShortArrayTile {
new ShortUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

/**
* Create a new [[ShortArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new ShortArrayTile
*/
def apply(arr: Array[Short], cols: Int, rows: Int, noDataValue: Option[Short]): ShortArrayTile =
apply(arr, cols, rows, ShortCells.withNoData(noDataValue))

/**
* Create a new [[ShortArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new ShortArrayTile
*/
def apply(arr: Array[Short], cols: Int, rows: Int, noDataValue: Short): ShortArrayTile =
apply(arr, cols, rows, Some(noDataValue))

/**
* Produce a [[ShortArrayTile]] of the specified dimensions.
*
Expand Down
27 changes: 27 additions & 0 deletions raster/src/main/scala/geotrellis/raster/UByteArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ object UByteArrayTile {
new UByteUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

/**
* Create a new [[UByteArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of bytes
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new UByteArrayTile
*/
def apply(arr: Array[Byte], cols: Int, rows: Int, noDataValue: Option[Byte]): UByteArrayTile =
apply(arr, cols, rows, UByteCells.withNoData(noDataValue))

/**
* Create a new [[UByteArrayTile]] from an array of bytes, a
* number of columns, and a number of rows.
*
* @param arr An array of bytes
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new UByteArrayTile
*/
def apply(arr: Array[Byte], cols: Int, rows: Int, noDataValue: Byte): UByteArrayTile =
apply(arr, cols, rows, Some(noDataValue))


/**
* Produce a [[UByteArrayTile]] of the specified dimensions.
*
Expand Down
26 changes: 26 additions & 0 deletions raster/src/main/scala/geotrellis/raster/UShortArrayTile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,32 @@ object UShortArrayTile {
new UShortUserDefinedNoDataArrayTile(arr, cols, rows, udct)
}

/**
* Create a new [[UShortArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue Optional NODATA value
* @return A new UShortArrayTile
*/
def apply(arr: Array[Short], cols: Int, rows: Int, noDataValue: Option[Short]): UShortArrayTile =
apply(arr, cols, rows, UShortCells.withNoData(noDataValue))

/**
* Create a new [[UShortArrayTile]] from an array of integers, a
* number of columns, and a number of rows.
*
* @param arr An array of integers
* @param cols The number of columns
* @param rows The number of rows
* @param noDataValue NODATA value
* @return A new UShortArrayTile
*/
def apply(arr: Array[Short], cols: Int, rows: Int, noDataValue: Short): UShortArrayTile =
apply(arr, cols, rows, Some(noDataValue))

/**
* Produce a [[UShortArrayTile]] of the specified dimensions.
*
Expand Down

0 comments on commit b9c3a05

Please sign in to comment.