Cesium 加载geojson数据类型点线面

1.获取geojson数据,本地新建一个.ts文件放置数据导出,并引入

 获取geojson数据:

DataV.GeoAtlas地理小工具系列

import { scGeojson } from './geojson';

2.加载面

const addPolygonEvt = () => {
	viewer.dataSources.add(
		Cesium.GeoJsonDataSource.load(scGeojson, {
			clampToGround: true, // 贴地
			fill: Cesium.Color.AZURE.withAlpha(0.5),
		}),
	);
};

因为面数据贴地,边界线消失,所以,将面数据转为多线数据,加载一次边界线

3.加载边界线

import { polygonToLine, polygon } from '@turf/turf';
const addPolygonLine = () => {
	// 设置一个空数组,用来放置每个面的边界,geojson数据
	const lineArr: any[] = [];
	scGeojson.features.map((it) => {
		if (it.geometry && it.geometry.coordinates) {
			it.geometry.coordinates.map((item) => {
				let line = polygonToLine(polygon(item)); // 把多面转为多线
				lineArr.push(line);
			});
		}
	});
	// 1.遍历线数组,可以使用加载实体线方式,把每个面的边界线加载出来
	// 2.或则使用加载geojson数据方法,加载出来
	lineArr.map((it) => {
        // 加载实体方式
	    // viewer.entities.add({
		// 	polyline: {
		// 		positions: Cesium.Cartesian3.fromDegreesArray(
		// 			flatten(it.geometry.coordinates),
		// 		),
		// 		material: Cesium.Color.AQUA,
		// 		clampToGround: true,
		// 	},
		// });
        // 加载geojson方式
		viewer.dataSources.add(
			Cesium.GeoJsonDataSource.load(it, {
				clampToGround: true, // 贴地
				stroke: Cesium.Color.AQUA,
				strokeWidth: 1,
			}),
		);
	});
};

4.加载点

import { point, FeatureCollection } from '@turf/turf';
const addGeojsonPoint = () => {
	const pointGeoJson: FeatureCollection = {
		type: 'FeatureCollection',
		features: [],
	};
	scGeojson.features.map((it) => {
		pointGeoJson.features.push(point(it.properties.center, it.properties));
	});
	let data = viewer.dataSources.add(
		Cesium.GeoJsonDataSource.load(pointGeoJson, {}),
	);
	data.then((dataSource: any) => {
		const entities = dataSource.entities.values;
		for (const item in entities) {
			const entity = entities[item];
			// if (entity.point) {
			entity.billboard = {
				image: '/public/images/gg.png',
				color: Cesium.Color.AQUA,
				width: 40,
				height: 40,
				heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地
			};
			entity.label = {
				text: entity.name,
				font: '14px',
				pixelOffset: new Cesium.Cartesian3(0, 30, 0),
				fillColor: Cesium.Color.DARKGREEN,
			};
		}
	});
};

 效果:


hooks.ts代码:

import * as Cesium from 'cesium';
import { popInfo } from './config';
import { scGeojson } from './geojson';
import { polygonToLine, polygon, point, FeatureCollection } from '@turf/turf';
let viewer: any = {};
export function mountedEvt() {
	Cesium.Ion.defaultAccessToken =
		'';
	viewer = new Cesium.Viewer('cesiumContainer', {
		// baseLayerPicker: false,
	});
	mapFlyEvt(104.065735, 30.659462, 2000000);
	addPolygonEvt();
	addPolygonLine();
	addGeojsonPoint();
}
/**
 * @Description 加载geojson点位
 * @Author: wms
 * @Date: 2023-11-21 15:20:34
 */
const addGeojsonPoint = () => {
	const pointGeoJson: FeatureCollection = {
		type: 'FeatureCollection',
		features: [],
	};
	scGeojson.features.map((it) => {
		pointGeoJson.features.push(point(it.properties.center, it.properties));
	});
	let data = viewer.dataSources.add(
		Cesium.GeoJsonDataSource.load(pointGeoJson, {}),
	);
	data.then((dataSource: any) => {
		const entities = dataSource.entities.values;
		for (const item in entities) {
			const entity = entities[item];
			// if (entity.point) {
			entity.billboard = {
				image: '/public/images/gg.png',
				color: Cesium.Color.AQUA,
				width: 40,
				height: 40,
				heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, // 贴地
			};
			entity.label = {
				text: entity.name,
				font: '14px',
				pixelOffset: new Cesium.Cartesian3(0, 30, 0),
				fillColor: Cesium.Color.DARKGREEN,
			};
		}
	});
	addPopEvt();
};
/**
 * @Description 弹窗
 * @Author: wms
 * @Date: 2023-11-17 11:02:33
 */
export const addPopEvt = () => {
	const dom = document.getElementById('popBox');
	let popBox: any = new Cesium.InfoBox(dom as string | Element);
	viewer.screenSpaceEventHandler.setInputAction(function onLeftClick(
		movement: any,
	) {
		let pickedObject = viewer.scene.pick(movement.position);

		if (
			Cesium.defined(pickedObject) &&
			pickedObject.id instanceof Cesium.Entity
		) {
			var entity = pickedObject.id;
			if (entity.position) {
				// 显示弹窗
				popBox.container.style.visibility = 'visible';
				// 获取位置信息
				let entityPosition = entity.position.getValue(
					viewer.clock.currentTime,
				);
				// 传递数据,由于我定义了一个map.js文件,所以没办法把点位数据直接传递给页面,只能用eventBus传递两个文件的数据
				popInfo.value = entity.properties;
				// 监听 Viewer 的 postRender 事件,在地图移动时更新弹窗位置
				viewer.scene.postRender.addEventListener(function () {
					try {
						if (entityPosition !== null) {
							let screenPosition =
								Cesium.SceneTransforms.wgs84ToWindowCoordinates(
									viewer.scene,
									entityPosition,
								);
							if (screenPosition) {
								let leftOffset =
									screenPosition.x -
									popBox.container.clientWidth / 2;
								let topOffset =
									screenPosition.y -
									popBox.container.clientHeight -
									18;
								popBox.container.style.left = leftOffset + 'px';
								popBox.container.style.top = topOffset + 'px';
							}
						}
					} catch (error) {
						console.log(error);
					}
				});
			} else {
				popBox.container.style.visibility = 'hidden';
			}
		} else {
			// 隐藏弹窗
			popBox.container.style.visibility = 'hidden';
		}
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};
/**
 * @Description 加载面数据
 * @Author: wms
 * @Date: 2023-11-21 09:36:22
 */
export const addPolygonEvt = () => {
	viewer.dataSources.add(
		Cesium.GeoJsonDataSource.load(scGeojson, {
			clampToGround: true, // 贴地
			fill: Cesium.Color.AZURE.withAlpha(0.5),
		}),
	);
};
/**
 * @Description 加载面边界
 * @Author: wms
 * @Date: 2023-11-21 14:59:04
 */
const addPolygonLine = () => {
	// 设置一个空数组,用来放置每个面的边界,geojson数据
	const lineArr: any[] = [];
	scGeojson.features.map((it) => {
		if (it.geometry && it.geometry.coordinates) {
			it.geometry.coordinates.map((item) => {
				let line = polygonToLine(polygon(item)); // 把多面转为多线
				lineArr.push(line);
			});
		}
	});
	// 遍历线数组,使用加载实体线方式,把每个面的边界线加载出来
	// 或则使用加载geojson数据方法,加载出来
	lineArr.map((it) => {
		// viewer.entities.add({
		// 	polyline: {
		// 		positions: Cesium.Cartesian3.fromDegreesArray(
		// 			flatten(it.geometry.coordinates),
		// 		),
		// 		material: Cesium.Color.AQUA,
		// 		clampToGround: true,
		// 	},
		// });
		viewer.dataSources.add(
			Cesium.GeoJsonDataSource.load(it, {
				clampToGround: true, // 贴地
				stroke: Cesium.Color.AQUA,
				strokeWidth: 1,
			}),
		);
	});
};
/**
 * @Description 数组扁平化处理
 * @Author: wms
 * @Date: 2023-11-21 14:50:59
 */
const flatten = (arr: any[]) => {
	let result: number[] = [];
	for (let i = 0; i < arr.length; i++) {
		if (Array.isArray(arr[i])) {
			result = result.concat(flatten(arr[i]));
		} else {
			result.push(arr[i]);
		}
	}
	return result;
};
/**
 * @Description 地图飞行动画
 * @Author: wms
 * @Date: 2023-11-21 15:11:14
 */
const mapFlyEvt = (lon: number, lat: number, height: number) => {
	const position = Cesium.Cartesian3.fromDegrees(lon, lat, height);
	// flyTo快速切换视角,带飞行动画,可以设置飞行时长
	viewer.camera.flyTo({
		destination: position,

		orientation: {
			heading: Cesium.Math.toRadians(0),

			pitch: Cesium.Math.toRadians(-90),

			roll: Cesium.Math.toRadians(0),
		},

		duration: 3, // 单位秒
	});
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 要在Cesium加载GeoJSON点数据,您可以按照以下步骤操作: 1. 将GeoJSON文件加载Cesium中。您可以使用Cesium的load方法来加载文件,如下所示: ``` Cesium.load('/path/to/your/geojson/file.geojson').then(function(data) { // Do something with the data }); ``` 2. 解析GeoJSON数据。您可以使用CesiumGeoJsonDataSource来解析数据,如下所示: ``` var dataSource = new Cesium.GeoJsonDataSource(); dataSource.load('/path/to/your/geojson/file.geojson').then(function() { // Do something with the data source }); ``` 3. 将数据源添加到场景中。您可以使用Cesium的viewer对象来添加数据源,如下所示: ``` viewer.dataSources.add(dataSource); ``` 4. 设置点的样式。您可以使用Cesium的点图形来设置点的样式,如下所示: ``` dataSource.entities.point = { pixelSize: 10, color: Cesium.Color.RED }; ``` 以上是加载GeoJSON点数据的基本步骤,您可以根据自己的需求进行调整和修改。 ### 回答2: Cesium是一款开源的地图可视化引擎,能够快速高效地展示地理数据,支持多种数据格式。GeoJSON是一种常用的地理数据格式,可用于描述地图上的点、线和面等要素。Cesium提供了丰富的API,能够便捷地加载GeoJSON点数据,并在地图上展示。 要加载GeoJSON点数据,首先需要将数据转换为Cesium可识别的格式。Cesium支持将GeoJSON转换为Cesium的Entities、Primitives和Imagery Layers等形式,其中Entities是最常用的一种形式。通过CesiumGeoJsonDataSource对象可以实现将GeoJSON点数据加载为Entities,并在地图上展示。 接下来,我们可以通过Cesium内置的Viewer对象,创建一个基础地图,并将GeoJSON点数据添加到地图上。下面是一段示例代码: ```javascript var viewer = new Cesium.Viewer('cesiumContainer'); //创建Viewer对象,将地图加载到'cesiumContainer'元素中 var dataSource = new Cesium.GeoJsonDataSource('myData'); //创建GeoJsonDataSource对象,并指定数据名称为'myData' var promise = dataSource.load('myPoints.geojson'); //通过load方法加载GeoJSON数据文件 viewer.dataSources.add(dataSource); //将数据源添加到Viewer中 promise.then(function() { viewer.zoomTo(dataSource); }); //当数据加载完毕,将Viewer缩放至图层范围 ``` 在上面的代码中,我们首先创建了一个Viewer对象,并将它加载到HTML元素'cesiumContainer'中。然后,创建一个GeoJsonDataSource对象,并通过load方法加载GeoJSON数据文件'myPoints.geojson'。将数据源添加到Viewer中后,使用promise.then()回调函数,在数据加载完成后将Viewer缩放至图层范围。 需要注意的是,GeoJSON点数据中的属性信息将自动转换为Cesium的Entity属性,并可用于自定义标签、颜色和大小等信息。此外,通过调用Entity的show和position属性,可以方便地控制Entity的显示状态和位置。 ### 回答3: Cesium是一款开源的WebGIS平台,支持加载geojson点数据。GeoJson是一种基于JavaScript对象表示法(JSON)的地理数据交换格式,它能够将矢量地理数据以一种简单、轻量级的方式表示出来。 在Cesium中,加载GeoJson点数据可以通过以下步骤实现: 1.创建数据源 首先,我们需要创建一个Cesium.GeoJsonDataSource对象,用于加载GeoJson数据。我们可以使用以下代码创建一个空的GeoJson数据源: var dataSource = new Cesium.GeoJsonDataSource(); 2.加载GeoJson数据 接下来,我们需要读取包含GeoJson数据的URL并加载这些数据。可以使用Cesium的load方法读取GeoJson文件并将其加载到数据源中。以下是加载GeoJson数据并将其添加到数据源的示例代码: Cesium.load('path/to/geojson.json').then(function (geojson) { dataSource.load(geojson); }) 3.显示GeoJson数据 一旦我们将GeoJson数据添加到数据源中,就可以将其添加到场景中显示。以下是将GeoJson数据添加到场景中的示例代码: viewer.dataSources.add(dataSource); 完成这些步骤后,应该可以在Cesium应用程序中看到已加载GeoJson点数据。 总而言之,Cesium支持加载GeoJson点数据,只需要使用Cesium.GeoJsonDataSource对象方法读取、加载和显示GeoJson数据即可。这种方法使得加载大规模点数据的展示和分析变得简单,方便且易于操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小满blue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值