要从GeoJson.parse编译对GeoTrellis的调用,需要哪些导入才能找到隐式证据?
geotrellis.vector.io.json.Geometry使用spray.json来解析,并且必须能够找到JsonReader或JsonFormats实例,模板化到WithCrs和几何类。
证据是在FeatureFormats中定义的;但是下面的代码段如何使用它呢?
以下内容无法解决证据问题:
geotrellis.vector.io.json.*包中的所有内容import geotrellis.vector.io.json.Implicitsimport geotrellis.vector.io.json.FeatureFormatscom.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发布于 2018-09-18 13:34:51
简短回答:加
import geotrellis.vector.io._
这个库的创建者使用包对象来发布这些关联。包对象(下面的源代码)扩展了g.io.json.Implicits,并将它们引入范围。
有关包对象的更多信息:
https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html
https://stackoverflow.com/questions/52373431
复制相似问题