GeosparkViz 可视化
生活随笔
收集整理的這篇文章主要介紹了
GeosparkViz 可视化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
title: ‘(五)GeosparkViz 可視化’
date: 2029-01-01 00:00:00
tags: [scala]
published: true
hideInList: false
#feature: /post-images/hello-gridea.png
isTop: false
上一節我們使用GeoSpark SQL簡單進行了空間的一些操作,本節我們繼續利用GeoSpark SQL以及GeoSparkViz將我們的結果進行渲染展示。下面是我們今天要用到的新的6個SQL函數
GeosparkViz 可視化
GeoSparkViz 可視化
上一節我們使用GeoSpark SQL簡單進行了空間的一些操作,本節我們繼續利用GeoSpark SQL以及GeoSparkViz將我們的結果進行渲染展示。下面是我們今天要用到的新的6個SQL函數:
加載數據
SparkSession spark = SparkSession.builder().config("spark.serializer","org.apache.spark.serializer.KryoSerializer").config("spark.kryo.registrator", "org.datasyslab.geospark.serde.GeoSparkKryoRegistrator").master("local[*]").appName("Learn05").getOrCreate();GeoSparkSQLRegistrator.registerAll(spark); GeoSparkVizRegistrator.registerAll(spark);// 加載CSV文件,CSV中的第一列為WKT格式 String inputCSVPath = Learn04.class.getResource("/checkin.csv").toString(); Dataset rawDF = spark.read().format("csv").option("delimiter", ",").option("header", "false").load(inputCSVPath); rawDF.createOrReplaceTempView("pointtable");構建幾何圖形(Geometry)
// 創建Geometry列 String sqlText = "select ST_Point(cast(_c0 as Decimal(24,20)), cast(_c1 as Decimal(24,20))) AS shape, _c2 from pointtable"; Dataset spatialDf = spark.sql(sqlText); spatialDf.createOrReplaceTempView("pointtable"); spatialDf.show(); +--------------------+----------+ | shape| _c2| +--------------------+----------+ |POINT (-88.331492...| hotel| |POINT (-88.175933...| gas| |POINT (-88.388954...| bar| |POINT (-88.221102...|restaurant| +--------------------+----------+渲染
接下來就要對上面四個點進行展示,首先我們要將地理位置轉為屏幕上的像素坐標,首先使用ST_Pixelize,但是ST_Pixelize要求首先提供一個邊界范圍,所以我們先用ST_Envelope_Aggr來生成bound。
sqlText = "SELECT ST_Envelope_Aggr(shape) as bound FROM pointtable"; spatialDf = spark.sql(sqlText); spatialDf.createOrReplaceTempView("boundtable"); spatialDf.show(); +--------------------+ | bound| +--------------------+ |POLYGON ((-88.388...| +--------------------+生成像素
sqlText = "SELECT pixel, shape FROM pointtable " +"LATERAL VIEW ST_Pixelize(ST_Transform(shape, 'epsg:4326','epsg:3857'), 256, 256, (SELECT ST_Transform(bound, 'epsg:4326','epsg:3857') FROM boundtable)) AS pixel"; spatialDf = spark.sql(sqlText); spatialDf.createOrReplaceTempView("pixels"); spatialDf.show(); +--------------------+--------------------+ | pixel| shape| +--------------------+--------------------+ |Pixel(x=69.0, y=0...|POINT (-88.331492...| |Pixel(x=255.0, y=...|POINT (-88.175933...| |Pixel(x=0.0, y=23...|POINT (-88.388954...| |Pixel(x=201.0, y=...|POINT (-88.221102...| +--------------------+--------------------+生成顏色
本次僅僅是展示點,因此可以給每個點固定的顏色,所以這里權重就填1。
sqlText = "SELECT ST_Colorize(1, 1, 'red') as color, pixel FROM pixels"; spatialDf = spark.sql(sqlText); spatialDf.createOrReplaceTempView("pixelaggregates"); spatialDf.show(false); +------+----------------------------------------------------------------------------+ |color |pixel | +------+----------------------------------------------------------------------------+ |-65536|Pixel(x=69.0, y=0.0, width=256, height=256, isDuplicate=false, tileId=-1) | |-65536|Pixel(x=255.0, y=255.0, width=256, height=256, isDuplicate=false, tileId=-1)| |-65536|Pixel(x=0.0, y=230.0, width=256, height=256, isDuplicate=false, tileId=-1) | |-65536|Pixel(x=201.0, y=186.0, width=256, height=256, isDuplicate=false, tileId=-1)| +------+----------------------------------------------------------------------------+渲染
在得到像素點和顏色后,就可以調用ST_Render生成圖片了
sqlText = "SELECT ST_Render(pixel, color) AS image, (SELECT ST_AsText(bound) FROM boundtable) AS boundary FROM pixelaggregates" ; spatialDf = spark.sql(sqlText); spatialDf.createOrReplaceTempView("images"); spatialDf.show(); +--------------------------+--------------------+ | image| boundary| +--------------------------+--------------------+ |Image(width=256height=256)|POLYGON ((-88.388...| +--------------------------+--------------------+保存圖片
Dataset<org.apache.spark.sql.Row> images = spark.table("images"); Row[] take = (Row[])images.take(1); ImageSerializableWrapper image = (ImageSerializableWrapper)take[0].get(0); new ImageGenerator().SaveRasterImageAsLocalFile(image.getImage(),System.getProperty("user.home") + "/point", ImageType.PNG);參考鏈接:https://www.jianshu.com/p/9a0e7b151f49
總結
以上是生活随笔為你收集整理的GeosparkViz 可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Geospark空间查询
- 下一篇: 再见 Jenkins!几行脚本搞定自动化