首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoTrellis/Scala:查找缺少Json解析的隐式证据

GeoTrellis/Scala:查找缺少Json解析的隐式证据
EN

Stack Overflow用户
提问于 2018-09-17 18:02:31
回答 1查看 234关注 0票数 0

要从GeoJson.parse编译对GeoTrellis的调用,需要哪些导入才能找到隐式证据?

geotrellis.vector.io.json.Geometry使用spray.json来解析,并且必须能够找到JsonReader或JsonFormats实例,模板化到WithCrs和几何类。

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

以下内容无法解决证据问题:

  1. 导入geotrellis.vector.io.json.*包中的所有内容
  2. 导入Implicits特定的import geotrellis.vector.io.json.Implicits
  3. 直接导入FeatureFormats import geotrellis.vector.io.json.FeatureFormats
  4. 确保正确的导入,特别是不导入将掩盖目标对象的com.vividsolutions.jts.Geometry

这是有问题的代码

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
[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
EN

回答 1

Stack Overflow用户

发布于 2018-09-18 13:34:51

简短回答:加

import geotrellis.vector.io._

这个库的创建者使用包对象来发布这些关联。包对象(下面的源代码)扩展了g.io.json.Implicits,并将它们引入范围。

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

有关包对象的更多信息:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52373431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档