scala - GeoTrellis/Scala:为 Json 解析找到缺失的隐含证据
问题描述
需要哪些导入来定位隐式证据以编译从 GeoTrellis 对 GeoJson.parse 的调用?
geotrellis.vector.io.json.Geometry 使用 spray.json 进行解析,并且必须能够定位 JsonReader 或 JsonFormats 实例,模板化为 WithCrs 和 Geometry 类。
证据在 FeatureFormats 中定义;但是下面的代码片段如何使用它?
以下不能解决证据:
- 导入
geotrellis.vector.io.json.*
包中的所有内容 - 专门导入隐式
import geotrellis.vector.io.json.Implicits
- 直接导入特征格式
import geotrellis.vector.io.json.FeatureFormats
- 确保正确的导入,尤其是没有
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
解决方案
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.
More about package objects:
https://www.scala-lang.org/docu/files/packageobjects/packageobjects.html
推荐阅读
- testing - 如果某个测试失败,有没有办法保释套件?
- python - 通用视图中的成功消息不起作用
- php - Laravel / PHP - 高级多态关系
- r - 如何在调整后的折叠更改代码中使用 is.na 或 is.infinite 覆盖数据帧中的 NA?
- python-3.x - 带有 https 的 Kivy UrlRequest
- r - 使格子图中的某些线条不可见
- paypal - Paypal Sandbox Orders API - 捕获后资金未添加到余额中
- emacs - “选择已删除缓冲区”的“org-agenda-get-day-entries”错误
- html - 使用 Safari 在输入焦点上不需要的自动滚动到顶部
- javascript - 比较日期字符串