利用rayrender動態(tài)可視化全球人口密度

介紹

rayrender 是一個R語言編寫的開源包,用于創(chuàng)建光線跟蹤場景螟深。這個包為用 C++ 構(gòu)建的光線追蹤器提供了一個整潔的 R API小槐,以渲染由一組基元構(gòu)建的場景污秆。 rayrender 使用可管道化的迭代界面構(gòu)建場景皂股,并支持漫反射墅茉、金屬、電介質(zhì)(玻璃)呜呐、發(fā)光材料就斤,以及程序和用戶指定的圖像紋理和 HDR 環(huán)境照明。 rayrender 包括通過 RcppThread 的多核支持(帶有進度條)蘑辑、通過 PCG RNG 的隨機數(shù)生成以及通過 TinyObrjLoader 的 .obj 文件支持战转。

官網(wǎng)鏈接
Build and Raytrace 3D Scenes ? rayrender

數(shù)據(jù)源

NASA社會數(shù)據(jù)應(yīng)用中心的世界格網(wǎng)人口數(shù)據(jù)V4版本,2010年30km級別數(shù)據(jù)

下載鏈接
https://sedac.ciesin.columbia.edu/data/set/gpw-v4-basic-demographic-characteristics-rev11/data-download

可視化過程

  1. 安裝R語言環(huán)境

  2. 安裝rayrender,rayshader,rgdal,magick等包

  3. 運行腳本

  4. 利用Python腳本拼接單獨生成的PNG格式的圖片為GIF格式的圖片

可視化腳本

 library(rayshader)
 library(rayrender)
 
 popdata = raster::raster("gpw_v4_basic_demographic_characteristics_rev11_atotpopbt_2010_dens_15_min.tif")
 
 population_mat = rayshader:::flipud(raster_to_matrix(popdata))
 
 above1 = population_mat > 1
 above5 = population_mat > 5
 above10 = population_mat > 10
 above50 = population_mat > 50
 above100 = population_mat > 100
 above500 = population_mat > 500
 above1000 = population_mat > 1000
 
 above1[is.na(above1)] = 0
 above5[is.na(above5)] = 0
 above10[is.na(above10)] = 0
 above50[is.na(above50)] = 0
 above100[is.na(above100)] = 0
 above500[is.na(above500)] = 0
 above1000[is.na(above1000)] = 0
 
 
 turbocols = viridis::turbo(7)
 wc = 0.4
 
 chart_items = 
  xy_rect(x=-1,y=-1.4,z=1,xwidth=wc,ywidth=0.2, 
  material=diffuse(color="grey30")) %>% 
  add_object(text3d(label = "0", x=-1,y=-1.4,z=1.01, text_height = 0.1, 
  material=diffuse(color="black"))) %>% 
  add_object(xy_rect(x=-0.6,y=-1.4,z=1,xwidth=wc,ywidth=0.2, 
  material=diffuse(color=turbocols[1]))) %>% 
  add_object(text3d(label = "1>", x=-0.6,y=-1.4,z=1.01, text_height = 0.1, 
  material=diffuse(color="black"))) %>% 
  add_object(xy_rect(x=-0.2,y=-1.4,z=1,xwidth=wc,ywidth=0.2, 
  material=diffuse(color=turbocols[2]))) %>% 
  add_object(text3d(label = "5>", x=-0.2,y=-1.4,z=1.01, text_height = 0.1, 
  material=diffuse(color="black"))) %>% 
  add_object(xy_rect(x=0.2,y=-1.4,z=1,xwidth=wc,ywidth=0.2, 
  material=diffuse(color=turbocols[3]))) %>% 
  add_object(text3d(label = "10>", x=0.2,y=-1.4,z=1.01, text_height = 0.1, 
  material=diffuse(color="black"))) %>% 
  add_object(xy_rect(x=0.6,y=-1.4,z=1,xwidth=wc,ywidth=0.2, 
  material=diffuse(color=turbocols[4]))) %>% 
  add_object(text3d(label = "50>", x=0.6,y=-1.4,z=1.01, text_height = 0.1, 
  material=diffuse(color="black"))) %>% 
  add_object(xy_rect(x=1.0,y=-1.4,z=1,xwidth=wc,ywidth=0.2, 
  material=diffuse(color=turbocols[5]))) %>% 
  add_object(text3d(label = "100>", x=1.0,y=-1.4,z=1.01, text_height = 0.1,
  material=diffuse(color="black"))) %>%
  add_object(xy_rect(x=1.4,y=-1.4,z=1,xwidth=wc,ywidth=0.2,
  material=diffuse(color=turbocols[6]))) %>%
  add_object(text3d(label = "500>", x=1.4,y=-1.4,z=1.01, text_height = 0.1,
  material=diffuse(color="black"))) %>%
  add_object(xy_rect(x=1.8,y=-1.4,z=1,xwidth=wc,ywidth=0.2,
  material=diffuse(color=turbocols[7]))) %>%
  add_object(text3d(label = "1000>", x=1.8,y=-1.4,z=1.01, text_height = 0.1,
  material=diffuse(color="black"))) %>%
  add_object(text3d(label = "People per 30km^2", x=-0.55,y=-1.2,z=1.01, text_height = 0.15,
  material=diffuse(color="white"))) %>%
  group_objects(group_translate = c(-0.4,0,0),group_scale=c(0.85,0.85,0.85))
 
 radm = 1.2
 for(i in 1:720) {
  chart_items %>% 
  add_object(group_objects(
  sphere(radius=0.99*radm,material=diffuse(color="grey20")) %>% 
  add_object(sphere(radius=1.0*radm,material= diffuse(color=turbocols[1],alpha_texture = above1))) %>% 
  add_object(sphere(radius=1.02*radm,material=diffuse(color=turbocols[2],alpha_texture = above5))) %>% 
  add_object(sphere(radius=1.03*radm,material=diffuse(color=turbocols[3],alpha_texture = above10))) %>% 
  add_object(sphere(radius=1.04*radm,material=diffuse(color=turbocols[4],alpha_texture = above50))) %>% 
  add_object(sphere(radius=1.05*radm,material=diffuse(color=turbocols[5],alpha_texture = above100))) %>% 
  add_object(sphere(radius=1.06*radm,material=diffuse(color=turbocols[6],alpha_texture = above500))) %>% 
  add_object(sphere(radius=1.07*radm,material=diffuse(color=turbocols[7],alpha_texture = above1000))),
  group_angle = c(0,-i/2,0))) %>% 
  add_object(sphere(y=10,z=5,radius=3,material=light(intensity = 20))) %>%
  add_object(sphere(y=0,z=20,radius=3,material=light(intensity = 20))) %>%
  render_scene(width=1000,height=1000,samples=128,rotate_env = 180,clamp_value = 10,
  aperture=0,
  filename=sprintf("worldpopfocus%i.png",i), lookat=c(0,-0.2,0))
 }

單幀生成的結(jié)果圖

worldpopfocus1.png

利用Python腳本拼接多張PNG圖像

 import imageio
 
 def create_gif(image_list, gif_name):
  frames = []
  for image_name in image_list:
  frames.append(imageio.imread(image_name))
  imageio.mimsave(gif_name, frames, 'GIF', duration=0.1)
  return
 
 
 def main():
  image_list = ["rayrender\worldpopfocus" +
  str(x)+".png" for x in range(1, 200)]
  gif_name = 'rayrender\created_gif.gif'
  create_gif(image_list, gif_name)
 
 
 if __name__ == "__main__":
  main()

動態(tài)可視化結(jié)果

部分地區(qū)人口密度動態(tài)圖.gif

總結(jié)

rayrender提供了非常方便簡潔的調(diào)用方式,即可實現(xiàn)很強的光線渲染效果以躯。在GIS領(lǐng)域,如何實現(xiàn)好的光線渲染效果是一個比較熱門的研究方向啄踊,而rayrender的渲染結(jié)果就非常出色忧设,可以用于數(shù)字地形渲染、動態(tài)制圖等方向颠通。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末址晕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子顿锰,更是在濱河造成了極大的恐慌谨垃,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件硼控,死亡現(xiàn)場離奇詭異刘陶,居然都是意外死亡,警方通過查閱死者的電腦和手機牢撼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門匙隔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人熏版,你說我怎么就攤上這事纷责。” “怎么了撼短?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵再膳,是天一觀的道長。 經(jīng)常有香客問我曲横,道長喂柒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮胳喷,結(jié)果婚禮上湃番,老公的妹妹穿的比我還像新娘。我一直安慰自己吭露,他們只是感情好吠撮,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著讲竿,像睡著了一般泥兰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上题禀,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音削彬,去河邊找鬼。 笑死融痛,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的神僵。 我是一名探鬼主播雁刷,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼沛励!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起目派,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎铝阐,沒想到半個月后址貌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡徘键,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了螟凭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡螺男,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出下隧,到底是詐尸還是另有隱情,我是刑警寧澤淆院,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站支救,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏各墨。R本人自食惡果不足惜启涯,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望结洼。 院中可真熱鬧,春花似錦补君、人聲如沸昧互。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至更扁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赫冬,已是汗流浹背浓镜。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工劲厌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人补鼻。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像咨跌,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子锌半,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容