Skip to content

Commit

Permalink
Add support for colormap with floating point keys (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
moradology committed Feb 18, 2020
1 parent 3a223d1 commit 8c21b44
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated MAML up to 0.6.0 [#199](https://github.com/geotrellis/geotrellis-server/pull/199)

### Fixed
- Fixed color interpolation bug related to constructing a range when the step is 0 [#111](https://github.com/geotrellis/geotrellis-server/issues/111)
- Non-integer (floating point) ColorMap keys now work with or without being quoted [#187](https://github.com/geotrellis/geotrellis-server/issues/187)
- Missing `<ows:Title>` and `<ows:Abstract>` elements in WCS GetCapabilities response [#114](https://github.com/geotrellis/geotrellis-server/issues/114)
- Layer definition elements unused in WMS GetCapabilities response [#115](https://github.com/geotrellis/geotrellis-server/issues/115)
- Bad assembly strategy [#142](https://github.com/geotrellis/geotrellis-server/issues/142)
Expand Down
23 changes: 23 additions & 0 deletions ogc-example/src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ layers = {
type = "colorrampconf"
colors = ${color-ramps.red-to-blue}
stops = 64
},
{
name = "ndvi"
title = "NDVI colors (for demonstration only)"
type = "colormapconf"
color-map = ${color-maps.ndvi}
}
]
}
Expand Down Expand Up @@ -369,4 +375,21 @@ color-maps = {
2500: 0xD7D7D7FF,
3000: 0xFFFFFFFF
}
"ndvi": {
-1.0: 0x1947B0FF,
-0.7: 0x3961B7FF,
-0.5: 0x5A7BBFFF,
-0.4: 0x7B95C6FF,
-0.3: 0x9CB0CEFF,
-0.2: 0xBDCAD5FF,
-0.1: 0xDEE4DDFF,
"0": 0xFFFFE5FF,
"0.1": 0xDAE4CAFF,
"0.2": 0xB6C9AFFF,
"0.3": 0x91AF94FF,
"0.4": 0x6D9479FF,
"0.5": 0x487A5EFF,
"0.7": 0x245F43FF,
"1.0": 0x004529FF
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import geotrellis.raster.render.{ColorMap, ColorRamp}

import com.azavea.maml.ast._
import com.azavea.maml.ast.codec.tree._
import com.typesafe.config.{ConfigRenderOptions, ConfigValue}
import com.typesafe.config._
import io.circe._
import io.circe.parser._
import pureconfig._
Expand All @@ -33,6 +33,8 @@ import pureconfig.generic.FieldCoproductHint
import pureconfig.generic.auto._

import scala.util.Try
import scala.collection.JavaConverters._


/** A grab bag of [[ConfigReader]] instances necessary to read the configuration */
package object conf {
Expand Down Expand Up @@ -67,9 +69,31 @@ package object conf {
ColorRamp(colors.map(java.lang.Long.decode(_).toInt))
}

/**
* HOCON doesn't naturally handle unquoted strings which contain decimals ('.') very well.
* As a result, some special configuration handling is required here to allow unquoted
* strings specifically when we know we're trying to decode a ColorMap.
*/
implicit def colormapReader: ConfigReader[ColorMap] =
ConfigReader[Map[String, String]].map { cmap =>
ColorMap(cmap.map { case (k, v) => (k.toDouble -> java.lang.Long.decode(v).toInt) }.toMap)
ConfigReader[Map[String, ConfigValue]].map { cmap =>
val numericMap = cmap.map({ case (k, v) =>
v.valueType match {
case ConfigValueType.OBJECT =>
val confmap = v.asInstanceOf[ConfigObject].asScala
val fixedKey: String = k + "." + confmap.keys.head
val fixedValue: String = confmap.values.head.unwrapped.asInstanceOf[String]
fixedKey -> fixedValue
case ConfigValueType.STRING =>
k -> v.unwrapped.asInstanceOf[String]
case _ =>
k -> v.toString
}
}).map({ case (k, v) =>
val key = k.toDouble
val value = java.lang.Long.decode(v).toInt
key -> value
}).toMap
ColorMap(numericMap)
}

implicit def keywordConfigReader: ConfigReader[opengis.wms.Keyword] =
Expand Down

0 comments on commit 8c21b44

Please sign in to comment.