六、geotrellis按时间序列存储至hbase

实现代码如下:

import org.apache.camel.scala.dsl.builder.RouteBuilderSupport
import geotrellis.raster._
import geotrellis.proj4._
import geotrellis.raster.resample.Bilinear
import geotrellis.spark._
import geotrellis.spark.io._
import geotrellis.spark.io.file._
import geotrellis.spark.io.hadoop._
import geotrellis.spark.io.hbase._
import geotrellis.spark.io.index._
import geotrellis.spark.pyramid._
import geotrellis.spark.reproject._
import geotrellis.spark.tiling._
import geotrellis.spark.render._
import geotrellis.vector._
import org.apache.spark._
import org.apache.spark.rdd._
import java.io.{File, FilenameFilter}

import scala.io.StdIn
import com.typesafe.config.ConfigFactory
import com.yykj.chatta.initWriteBackend
import com.yykj.common.comfun
import geotrellis.raster.io.geotiff.{SinglebandGeoTiff, Tags}
import geotrellis.raster.io.geotiff.reader.GeoTiffReader

object makeSlices {

  val config = ConfigFactory.load()

  def makeslice(inputPath:String)={
    val conf =
      new SparkConf()
        .setMaster("local[*]")
        .setAppName("Spark Tiler")
        .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
        .set("spark.kryo.registrator", "geotrellis.spark.io.kryo.KryoRegistrator")

    val sc = new SparkContext(conf)

    runtoHbaseTime(sc,inputPath,config.getString("file.path"))
  }

  def runtoHbaseTime(implicit sc: SparkContext,inputPath:String) = {

    //将时间添加至tif文件的head信息中,并生成新的tif
    val newTiffPath="/data/tmp/t0.tif"

    val time="2019:06:29 00:00:00"
    val geoTiff: SinglebandGeoTiff = GeoTiffReader.readSingleband(inputPath)
    val t=Map(Tags.TIFFTAG_DATETIME -> time)
    val newtiff = new SinglebandGeoTiff(geoTiff.tile, geoTiff.extent, geoTiff.crs, Tags(t, geoTiff.tags.bandTags), geoTiff.options)
    newtiff.write(newTiffPath)

    //生成rdd
    val inputRdd: RDD[(TemporalProjectedExtent, Tile)] =
      sc.hadoopTemporalGeoTiffRDD(newTiffPath)

    //创建元数据信息
    val layoutScheme = FloatingLayoutScheme(512)
    val (_, rasterMetaData: TileLayerMetadata[SpaceTimeKey]) =
      inputRdd.collectMetadata[SpaceTimeKey](layoutScheme)


    val tilerOptions =
      Tiler.Options(
        resampleMethod = Bilinear,
        partitioner = new HashPartitioner(inputRdd.partitions.length)
      )

    //生成SpaceTimeKey RDD
    val tiledRdd:RDD[(SpaceTimeKey, Tile)] with 
    Metadata[TileLayerMetadata[SpaceTimeKey]]=
    inputRdd.tileToLayout[SpaceTimeKey](rasterMetaData, tilerOptions)


    //切片大小
    val tarlayoutScheme = ZoomedLayoutScheme(WebMercator, tileSize = 256)

    val (zoom, reprojected): (Int, RDD[(SpaceTimeKey, Tile)] with 
     Metadata[TileLayerMetadata[SpaceTimeKey]]) =
      TileLayerRDD(tiledRdd, rasterMetaData)
        .reproject(WebMercator, tarlayoutScheme, Bilinear)

    //hbase.config的配置内容,这里示例如下
    //hbase {
    //  zookeepers = "node1,node2,node3,node4"
    //  host       = "localhhost"
    //  port       =2181
    //  master     = "localhost"
    //  tablename  ="tb1"
    //}


    //创建hbase存储对象
    val (writer,attributeStore:HBaseAttributeStore,instance) = {
    val seq = config.getString("hbase.zookeepers").split(",")
        val host=config.getString("hbase.host")
        val port=config.getString("hbase.port")
        val instance:HBaseInstance = HBaseInstance.apply(seq,host,port)
        val tabnename=config.getString("hbase.tablename")
        //instance.withTableConnectionDo(tabnename)
        (HBaseCollectionLayerReader(instance), HBaseValueReader(instance),     
       HBaseAttributeStore(instance))
    }

    
    //创建金字塔并进行切片,保存至hbase
    Pyramid.upLevels(reprojected, tarlayoutScheme, zoom, Bilinear) { (rdd, z) =>
      val layerId = LayerId("testid", z)

      val indexKeyBounds: KeyBounds[SpaceTimeKey] = {
        val KeyBounds(minKey, maxKey) = rdd.metadata.bounds.get // assuming non-empty layer
        //      KeyBounds(
        //        minKey.setComponent[TemporalKey](minDate),
        //        maxKey.setComponent[TemporalKey](maxDate)
        //      )
        KeyBounds(minKey, maxKey)
      }

      val keyIndex =
        ZCurveKeyIndexMethod.byDay
          .createIndex(indexKeyBounds)

      writer.write(layerId, rdd, keyIndex)

    }

    sc.stop()
  }


  def main(args: Array[String]): Unit = {

    inputpath="/data/tmp/t.tif"
    makeSlices.makeslice(inputpath)

  }

}

 

 

你可能感兴趣的:(geotrellis)