六、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)

  }

}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值