五、geotrellis实现栅格切片并存储至hbase

部署环境可以参考本栏目的其他章节。

1、maven代码如下:



    
      org.apache.camel
      camel-core
    
    
      org.apache.camel
      camel-scala
    

    
    
      org.scala-lang
      scala-library
      2.11.7
    
    
      org.scala-lang.modules
      scala-xml_2.11
      1.0.6
    

    
    
      org.apache.logging.log4j
      log4j-api
      runtime
    
    
      org.apache.logging.log4j
      log4j-core
      runtime
    
    
      org.apache.logging.log4j
      log4j-slf4j-impl
      runtime
    

    
    
      org.apache.camel
      camel-test
      test
    

    
      com.typesafe.akka
      akka-http_2.11
      10.0.6
    
    
      com.typesafe.akka
      akka-stream_2.11
      2.4.18
    
    
      com.typesafe.akka
      akka-actor_2.11
      2.4.18
    
    
      com.typesafe.akka
      akka-http-core_2.11
      10.0.6
    
    
      com.typesafe.akka
      akka-http-spray-json_2.11
      10.0.2
    
    
      com.typesafe.akka
      akka-parsing_2.11
      10.0.6
    

    
      org.locationtech.geotrellis
      geotrellis-spark_2.11
      2.1.0
    
    
      org.locationtech.geotrellis
      geotrellis-raster_2.11
      2.1.0
    
    
      org.locationtech.geotrellis
      geotrellis-vector_2.11
      2.1.0
      
        
          io.spray
          spray-json_2.11
        
      
    
    
      org.locationtech.geotrellis
      geotrellis-proj4_2.11
      2.1.0
    
    
      org.locationtech.geotrellis
      geotrellis-util_2.11
      2.1.0
    
    
      org.locationtech.geotrellis
      geotrellis-macros_2.11
      2.1.0
    
    
      org.locationtech.geotrellis
      geotrellis-spark-etl_2.11
      2.1.0
    
    
      org.locationtech.geotrellis
      geotrellis-cassandra_2.11
      2.1.0
    

    
      org.locationtech.geotrellis
      geotrellis-shapefile_2.11
      2.1.0
      
        
          org.geotools
          gt-shapefile
        
        
          javax.media
          jai_core
        
      
    



    
      org.apache.spark
      spark-core_2.11
      2.3.1
    

    
      com.fasterxml.jackson.core
      jackson-core
      2.6.7
    
    
      com.fasterxml.jackson.core
      jackson-databind
      2.6.7
    
    
      com.fasterxml.jackson.core
      jackson-annotations
      2.6.7
    

    
      io.spray
      spray-json_2.11
      1.3.5
    

    
      org.scala-lang.modules
      scala-parser-combinators_2.11
      1.0.5
    

    
        org.opengis
        geoapi
        3.0.1
    

    
      org.scalanlp
      breeze_2.11
      1.0-RC4
    


  

2、scala代码如下:

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


import scala.io.StdIn
import com.typesafe.config.ConfigFactory
import com.yykj.chatta.initWriteBackend

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)

    run(sc,inputPath)
  }

  def run(implicit sc: SparkContext,inputPath:String) = {
    //创建rdd
    val inputRdd: RDD[(ProjectedExtent, Tile)] =
    sc.hadoopGeoTiffRDD(inputPath)

    //创建tile的元数据对象
    val (_, rasterMetaData) =
    TileLayerMetadata.fromRDD(inputRdd, FloatingLayoutScheme(512))


    val tiled: RDD[(SpatialKey, Tile)] =
    inputRdd
      .tileToLayout(rasterMetaData.cellType, rasterMetaData.layout, Bilinear)
      .repartition(50)

    //设置裁剪的tile大小,越小数据量越大,分层越多
    val layoutScheme = ZoomedLayoutScheme(WebMercator, tileSize = 64)

    //墨卡托投影
    val (zoom, reprojected): (Int, RDD[(SpatialKey, Tile)] with Metadata[TileLayerMetadata[SpatialKey]]) =
      TileLayerRDD(tiled, rasterMetaData)
        .reproject(WebMercator, layoutScheme, Bilinear)


    val (writer,attributeStore:HBaseAttributeStore,instance) = initWriteBackend(config)

    val filename=new File(inputPath).getName()
    val lyid=filename.substring(0,filename.lastIndexOf("."));

    Pyramid.upLevels(reprojected, layoutScheme, zoom, Bilinear) { (rdd, z) =>
      val layerId = LayerId(lyid, z)
      //删除重复图层
      if(attributeStore.layerExists(layerId)) {
        new HBaseLayerManager(attributeStore,instance).delete(layerId)
      }
      writer.write(layerId, rdd, ZCurveKeyIndexMethod)
    }

    sc.stop()
  }
}

 

你可能感兴趣的:(geotrellis,geotrellis)