Cesium学习笔记5——加载topojson和Geojson
1.Cesium学习笔记1——安装配置2.Cesium学习笔记2——第一个Cesium程序3.地图服务器GeoServer4.Geoserver发布地图服务5.Cesium学习笔记3——调用Geoserver发布的WMS服务6.Cesium学习笔记4——加载天地图地图服务
7.Cesium学习笔记5——加载topojson和Geojson
8.Cesium学习笔记6——加载倾斜摄影模型9.Cesium开发遇到的问题解决10.Cesium学习笔记7——几何体绘制11.Cesium学习笔记8——加载城市建筑物火柴盒模型12.Cesium学习笔记9——鼠标交互绘制13.Cesium学习笔记10——通过WFS服务实现交互式属性查询14.Cesium学习笔记11——坐标量测15.Cesium学习笔记12——基于Cesium的分层分户模型构建与可视化在根目录下新建bucket.css
@import"../Build/CesiumUnminified/Widgets/widgets.css";@import"../Build/CesiumUnminified/Widgets/lighter.css";html{height:100%}body{background:#000;color:#eee;font-family:sans-serif;font-size:9pt;padding:0;margin:0;width:100%;height:100%;overflow:hidden}.fullSize{display:block;position:absolute;top:0;left:0;border:none;width:100%;height:100%}#loadingOverlay{position:absolute;top:0;left:0;opacity:.9;width:100%;height:100%;display:none}#loadingOverlay h1{text-align:center;position:relative;top:50%;margin-top:-.5em}.sandcastle-loading #loadingOverlay{display:block}.sandcastle-loading #toolbar{display:none}#toolbar{margin:5px;padding:2px 5px;position:absolute}.infoPanel{background:rgba(42,42,42,.8);padding:4px;border:1px solid #444;border-radius:4px}
新建代码:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8" /> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge" /> 6 <meta 7 name="viewport" 8 content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" 9 /> 10 <meta name="description" content="Load GeoJSON or TopoJSON data and apply custom styling."> 11 <meta name="cesium-sandcastle-labels" content="Showcases, Tutorials, DataSources"> 12 <title>Cesium Demo</title> 13 <script type="text/javascript" src="../Sandcastle-header.js"></script> 14 <script type="module" src="../load-cesium-es6.js"></script> 15 </head> 16 <body 17 class="sandcastle-loading" 18 data-sandcastle-bucket="bucket-requirejs.html" 19 > 20 <style> 21 @import url(bucket.css); 22 html, 23 body, 24 #cesiumContainer { 25 width: 100%; 26 height: 100%; 27 margin: 0; 28 padding: 0; 29 overflow: hidden; 30 } 31 </style> 32 <div id="cesiumContainer" class="fullSize"></div> 33 <div id="loadingOverlay"><h1>Loading...</h1></div> 34 <div id="toolbar"></div> 35 <script id="cesium_sandcastle_script"> 36 window.startup = async function (Cesium) { 37 'use strict'; 38 //Sandcastle_Begin 39 const viewer = new Cesium.Viewer("cesiumContainer", { 40 timeline:false, 41 animation:false, 42 infoBox:false, 43 imageryProvider: new Cesium.WebMapTileServiceImageryProvider({ 44 url: "http://t0.tianditu.gov.cn/vec_w/wmts?tk=你的Token" , 45 layer: "vec", 46 style: "default", 47 tileMatrixSetID: "w", 48 format: "tiles", 49 maximumLevel: 18, 50 }), 51 }); 52 53 //Example 1: Load with default styling. 54 Sandcastle.addDefaultToolbarButton("Default styling", function () { 55 viewer.dataSources.add( 56 Cesium.GeoJsonDataSource.load( 57 "../ne_10m_us_states.topojson" 58 ) 59 ); 60 }); 61 62 //Example 2: Load with basic styling options. 63 Sandcastle.addToolbarButton("Basic styling", function () { 64 viewer.dataSources.add( 65 Cesium.GeoJsonDataSource.load( 66 "../ne_10m_us_states.topojson", 67 { 68 stroke: Cesium.Color.HOTPINK, 69 fill: Cesium.Color.PINK.withAlpha(0.5), 70 strokeWidth: 3, 71 } 72 ) 73 ); 74 }); 75 76 //Example 3: Apply custom graphics after load. 77 Sandcastle.addToolbarButton("Custom styling", function () { 78 //Seed the random number generator for repeatable results. 79 Cesium.Math.setRandomNumberSeed(0); 80 81 const promise = Cesium.GeoJsonDataSource.load( 82 "../ne_10m_us_states.topojson" 83 ); 84 promise 85 .then(function (dataSource) { 86 viewer.dataSources.add(dataSource); 87 88 //Get the array of entities 89 const entities = dataSource.entities.values; 90 91 const colorHash = {}; 92 for (let i = 0; i < entities.length; i++) { 93 //For each entity, create a random color based on the state name. 94 //Some states have multiple entities, so we store the color in a 95 //hash so that we use the same color for the entire state. 96 const entity = entities[i]; 97 const name = entity.name; 98 let color = colorHash[name]; 99 if (!color) { 100 color = Cesium.Color.fromRandom({ 101 alpha: 1.0, 102 }); 103 colorHash[name] = color; 104 } 105 106 //Set the polygon material to our random color. 107 entity.polygon.material = color; 108 //Remove the outlines. 109 entity.polygon.outline = false; 110 111 //Extrude the polygon based on the state's population. Each entity 112 //stores the properties for the GeoJSON feature it was created from 113 //Since the population is a huge number, we divide by 50. 114 entity.polygon.extrudedHeight = 115 entity.properties.Population / 50.0; 116 } 117 }) 118 .catch(function (error) { 119 //Display any errrors encountered while loading. 120 window.alert(error); 121 }); 122 }); 123 124 //Reset the scene when switching demos. 125 Sandcastle.reset = function () { 126 viewer.dataSources.removeAll(); 127 128 //Set the camera to a US centered tilted view and switch back to moving in world coordinates. 129 viewer.camera.lookAt( 130 Cesium.Cartesian3.fromDegrees(-98.0, 40.0), 131 new Cesium.Cartesian3(0.0, -4790000.0, 3930000.0) 132 ); 133 viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY); 134 }; 135 //Sandcastle_End 136 Sandcastle.finishedLoading(); 137 }; 138 if (typeof Cesium !== 'undefined') { 139 window.startupCalled = true; 140 window.startup(Cesium).catch((error) => { 141 "use strict"; 142 console.error(error); 143 }); 144 } 145 </script> 146 </body> 147 </html>
效果:
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了