geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探

在上篇博客(geotrellis使用初探)中简单介绍了geotrellis-chatta-demo的大致工作流程,但是有一个重要的问题就是此demo如何调取数据进行瓦片切割分析处理等并未说明,经过几天的调试、分析、源代码研读终于大致搞明白了其数据调取方式,下面简单介绍。

def apply(name: String): RasterSource =
RasterSource(LoadRasterDefinition(LayerId(name)), None)
此方法其实调用了另一个方法
def apply(rasterDef: Op[RasterDefinition], targetExtent: Option[RasterExtent]): RasterSource = {
val (rd, tileOps) =
    targetExtent match {
case reOp @ Some(re) =>
        ( rasterDef.map(_.withRasterExtent(re)),
          rasterDef.map { rd =>
Seq(LoadRaster(rd.layerId, reOp))
          }
        )
case None =>
        ( rasterDef,
          rasterDef.map { rd =>
            (for(tileRow <- 0 until rd.tileLayout.layoutRows;
              tileCol <- 0 until rd.tileLayout.layoutCols) yield {
LoadTile(rd.layerId, tileCol, tileRow)
            })
          }
        )
    }

new RasterSource(rd, tileOps)
}
由此可以看出LoadRasterDefinition(LayerId(name))完成的就是获取一个Op[RasterDefinition]对象。
通过此方法经过N步的追踪之后终于在DataSource类中找到了这么一个方法
def getRasterLayer(name:String):Option[RasterLayer] = layers.get(name)
有戏,看方法名字就知道是获得栅格层,那么主要就在layers身上了,layers怎么来的呢,上面有定义
private def initDirectory(d:File) {
val skipDirectories = mutable.Set[String]()
for(f <- d.listFiles
            .filter(_.isFile)
            .filter(_.getPath.endsWith(".json"))) {
// It's a JSON file
    // which may contain layer metadata,
    // or we just ignore it.
RasterLayer.fromFile(f) match {
case Success(layer) =>
layers(layer.info.id.name) = layer
// Skip the tile directory if it's a tiled raster.
layer match {
case tl:TileSetRasterLayer =>
            skipDirectories.add(new File(tl.tileDirPath).getAbsolutePath)
case _ =>
        }
case Failure(e) =>
        System.err.println(s"[ERROR] Skipping ${f.getPath}: $e")
    }
  }

// Recurse through subdirectories. If a directory was marked
  // as containing a tile set, skip it.
for(subdir <- d.listFiles
                 .filter(_.isDirectory)
                 .filter(f => !skipDirectories.contains(f.getAbsolutePath))) {
    initDirectory(subdir)
  }
}
看到这个是不是就豁然开朗了,原来这里是直接扫描给定的文件夹下的所有json文件,那么这个路径是怎么传进来的呢?找了半天未能找到何时传入了d(即数据路径),不过改变demo中的data文件夹的名字发现报错,并未能成功加载数据,说明是某个地方传入了该文件夹,然后通过查找log发现是GeoTrellis类中报的错,通过分析可以看出其默认获取resource文件夹中的application.conf中的
geotrellis.catalog配置信息,该信息的值为data/catalog.json,此文件具体存在,其中内容如下
{
 "catalog": "Catalog of Chattanooga data",
 "stores": [
  {
   "store": "chatta:albers",
   "params": {
     "type": "fs",
     "path": "arg_albers",
     "cacheAll": "yes"
    }
  },
  {
   "store": "chatta:wm",
   "params": {
     "type": "fs",
     "path": "arg_wm",
     "cacheAll": "yes"
    }
  }
 ]
}

由此可以看出该文件完成了Catalog类和DataSource类定义的实例,而上文中讲到框架正是通过此类来加载数据。
这应当就是GeoTrellis框架读取数据的方式,即在application.conf配置一个catalog.json文件的地址,然后在catalog.json文件记录具体的DataSource信息,通过此信息来获取数据。
通过分析使用GeoTrellis框架下的多个
demo可以发现均有catalog的踪迹,这应当是GeoTrellis读取数据的机制,catlog具体的工作模式还需后续继续研读源代码。
本文讲的比较乱,只是读demo的一点心得,后续如果有更好的想法也会随时进行修改完善。
下一步准备在此demo的基础上实现实时切割dem数据进行显示,后续心得会在总结之后继续发布到博客中。


相关链接:
一、geotrellis使用初探
二、geotrellis使用(二)geotrellis-chatta-demo以及geotrellis框架数据读取方式初探

转载自:http://www.cnblogs.com/shoufengwei/p/5398923.html

You may also like...

退出移动版