轉自原文 Cesium之3D拉伸顯示行政區含GeoJSON數據生成過程GDAL的ogr2ogr
Cesiumjs 是一套javascript庫,用來渲染3D地球,2D區域地圖,和多種GIS要素。不需要安裝任何插件就能在支持最新HTML5標准的瀏覽器上運行。支持WebGL硬件加速,非常適合動態數據在GIS圖層上的展示,是一個跨平台,開源,非常有前途的webgis表現層庫。Cesium 基於 Apache 開源協議,支持商業和非商業免費使用。
背景:
Cesiumjs源自 Analytical Graphics, Inc. (AGI)公司為他們客戶開發一個虛擬地球項目,后來將cesium項目貢獻給開源社區並一直活躍開發中。是一種針對Cesium-應用的流式高分辨率服務器)另一個公司級的貢獻者是NICTA (NationalICT Australia) 澳大利亞最大的信息與通訊技術中心,NICTA的員工們貢獻了瓦片地圖服務、地形處理等cesium的核心功能。並一直使用cesium開發環境監測分析、交通基礎設施建模仿真優化等領域的應用。
Demo需求,想要將北京市行政區划內的要素,用3D表現出來,首先,需要得到北京16個區的面坐標:
Cesium通過GeoJSON(JSON格式)格式的數據展示空間要素,因此,需要得到此種格式的北京行政區划坐標,通過研究,可以使用GDAL的ogr2ogr方法將shp格式的圖層轉成GeoJSON格式
下面說一下GDAL的下載和使用:
需要安裝GDAL,下載路徑:http://www.gisinternals.com/release.php
下載的是第一個:release-1500-gdal-1-11-3-mapserver-6-4-2
解壓縮release-1500-gdal-1-11-3-mapserver-6-4-2.zip
根據里面的read-me.txt,安裝此GDAL需要CMD運行SDKShell.bat命令即可,成功安裝,每次重啟機器后,需要重新執行此程序進行安裝,才能使用
然后cd C:\Users\IBM_ADMIN\Downloads\release-1500-gdal-1-11-3-mapserver-6-4-2\bin\gdal\apps
根據ogr2ogr.exe的工具進行轉換,開始,找到的shp文件轉換失敗,后從此命令轉換的shp修改了下,即可成功轉成json,可能是有的shp文件不支持,具體不詳
命令:
將geoJSON轉成shp格式:
C:\Users\IBM_ADMIN\Downloads\release-1500-gdal-1-11-3-mapserver-6-4-2\bin\gdal\a
pps>ogr2ogr -f "ESRI Shapefile" v.shp geojsonfile.json
將f.shp轉成geoJSON格式:
C:\Users\IBM_ADMIN\Downloads\release-1500-gdal-1-11-3-mapserver-6-4-2\bin\gdal\a
pps>ogr2ogr -f "GeoJSON" o.json f.shp
因為本地沒有現成的北京行政區shp圖層,因此,又發現竟然可以通過ESRI在線地圖服務的行政區圖層轉成GeoJSON格式,開始發現坐標系不是wgs84,Cesium-1.16僅支持WGS84坐標系,因為,加入參數(此處指的是坐標轉換參數,具體的可以通過ArcGIS的spatial Adjust 獲取的校正參數,或者,直接是坐標平移參數。20.3.21 更新),將ESRI的輸出坐標系轉成4326即可。
代碼:
ogr2ogr -f GeoJSON test.json "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_CHN/BeiJing_Community_BaseMap_CHN/MapServer/0/query?where=1%3D1&outfields=*&f=json" OGRGeoJSON 修改下語句即可,ArcGISServer支持輸出不同的坐標系 ogr2ogr -f GeoJSON test.json "http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_CHN/BeiJing_Community_BaseMap_CHN/MapServer/0/query?where=1%3D1&outSR=4326&outfields=*&f=json" OGRGeoJSON
完成導出
這時候終於得到了GeoJSON格式的北京行政區數據文件
下面就是將Cesium現成的例子,改個數據源地址,和高程字段
//Seed the random number generator for repeatable results. Cesium.Math.setRandomNumberSeed(0); var promise = Cesium.GeoJsonDataSource.load('../../SampleData/test.json'); promise.then(function(dataSource) { viewer.dataSources.add(dataSource); //Get the array of entities var entities = dataSource.entities.values; var colorHash = {}; for (var i = 0; i < entities.length; i++) { //For each entity, create a random color based on the state name. //Some states have multiple entities, so we store the color in a //hash so that we use the same color for the entire state. var entity = entities[i]; var name = entity.name; var color = colorHash[name]; if (!color) { color = Cesium.Color.fromRandom({ alpha : 1.0 }); colorHash[name] = color; } //Set the polygon material to our random color. entity.polygon.material = color; //Remove the outlines. entity.polygon.outline = false; //Extrude the polygon based on the state's population. Each entity //stores the properties for the GeoJSON feature it was created from //Since the population is a huge number, we divide by 50. entity.polygon.extrudedHeight = entity.properties.Shape_Area / 100000.0; } } viewer.zoomTo(promise);
最終效果如上圖所示。