0

需要哪些导入来定位隐式证据以编译从 GeoTrellis 对 GeoJson.parse 的调用?

geotrellis.vector.io.json.Geometry 使用 spray.json 进行解析,并且必须能够定位 JsonReader 或 JsonFormats 实例,模板化为 WithCrs 和 Geometry 类。

证据在 FeatureFormats 中定义;但是下面的代码片段如何使用它?

以下不能解决证据:

  1. 导入geotrellis.vector.io.json.*包中的所有内容
  2. 专门导入隐式import geotrellis.vector.io.json.Implicits
  3. 直接导入特征格式import geotrellis.vector.io.json.FeatureFormats
  4. 确保正确的导入,尤其是没有com.vividsolutions.jts.Geometry会掩盖目标对象的导入

这是有问题的代码

import geotrellis.vector.Geometry
import geotrellis.proj4.CRS
import geotrellis.vector.io.json.*
import geotrellis.vector.io.json.{GeoJson, WithCrs}
import org.json4s.{DefaultFormats, Formats}
import scala.util.{Failure, Success, Try}
val exampleQueryJson =
  """
|{
|   "type": "Polygon",
|   "crs": {
|       "type": "name",
|       "properties": {
|           "name": "EPSG:4326"
|       }
|   },
|   "coordinates": [
|       [
|           [....]
|       ]
|   ]
|}
  """.stripMargin

class GeometryReader extends FeatureFormats {
  implicit val jsonFormats: Formats = DefaultFormats
}

object GeometryReader {

  def parseGeometry(request: String): Geometry = {

    GeoJson.parse[Geometry](request)
  }
}

val g = GeometryReader.parseGeometry(exampleQueryJson)

编译错误显示在当前可用的情况下无法找到正确的证据

[error] /path/redacted/GeometryReader.scala:19: Cannot find JsonReader or JsonFormat type class for geotrellis.vector.io.json.WithCrs[geotrellis.vector.Geometry]
[error]       val geometryWithCrs: WithCrs[Geometry] = GeoJson.parse[WithCrs[Geometry]](request)
[error]                                                                                ^
[error] /path/redacted/GeometryReader.scala:25: Cannot find JsonReader or JsonFormat type class for geotrellis.vector.Geometry
[error]       Try(GeoJson.parse[Geometry](request)) match {
[error]                                  ^
[error] two errors found
[error] (compile:compileIncremental) Compilation failed
4

1 回答 1

0

Short answer: Add

import geotrellis.vector.io._

The creators of this library made use of package objects to publish these implicits. The package object (source code below) extends g.io.json.Implicits, and that brings them into scope.

https://github.com/locationtech/geotrellis/blob/master/vector/src/main/scala/geotrellis/vector/io/package.scala

More about package objects:

https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html

于 2018-09-18T13:34:51.910 回答