矢量线数据(GeoJson)生成代码

概要

小工具(TypeScript+Vue3+Turf),可生成指定数量的GeoJson格式矢量线(LineString)数据,属性包括name、color、opacity等(随机值),需要引入Turf。


<script setup lang="ts">
import { onMounted, ref } from "vue"
import * as turf from '@turf/turf'
// 期望生成的数量
const inputValue = ref(10000)
// GeoJson对象
let geo = ref({
	type: "FeatureCollection",
	features: []
})
// 入口
function start() {
	const data = turf.randomLineString(inputValue.value, { bbox: [-180, -90, 180, 90], max_length: 1 })
	data.features.forEach(creatGeo)
	downloadFile(geo.value)
}
// 数据生成
function creatGeo(turfFeature: turf.helpers.Feature<turf.helpers.Geometry>) {
	let coordinate = turfFeature.geometry.coordinates
	// 透明度,精确到 1 位小数
	const opacity = parseFloat(Math.random().toFixed(1))
	// 随机名称
	function generateRandomName() {
		const adjectives = ["Big", "Small", "Crazy", "Brave", "Wild", "Gentle", "Silly", "Happy", "Lucky", "Wise"];
		const nouns = ["Elephant", "Tiger", "Lion", "Monkey", "Dolphin", "Kangaroo", "Giraffe", "Penguin", "Owl", "Turtle"];
		const randomAdjective = adjectives[Math.floor(Math.random() * adjectives.length)];
		const randomNoun = nouns[Math.floor(Math.random() * nouns.length)];
		return randomAdjective + " " + randomNoun;
	}
	const name = generateRandomName();
	// 随机颜色
	function getRandomColor() {
		const letters = "0123456789ABCDEF";
		let color = "#";
		for (let i = 0; i < 6; i++) {
			color += letters[Math.floor(Math.random() * 16)];
		}
		return color;
	}
	const color = getRandomColor()

	let obj = {
		"type": "Feature",
		"properties": {
			"name": name,
			"opacity": opacity,
			"color": color
		},
		"geometry": {
			"coordinates": coordinate,
			"type": "LineString"
		}
	}
	geo.value.features.push(obj)
}
//下载数据
function downloadFile(json: any) {
	let jsonString = JSON.stringify(json)
	// 创建一个 Blob 对象
	var blob = new Blob([jsonString], { type: "application/json" });
	// 创建一个下载链接
	var downloadLink = document.createElement("a");
	downloadLink.href = URL.createObjectURL(blob);
	downloadLink.download = "geoJson.json";
	// 点击链接进行下载
	document.body.appendChild(downloadLink);
	downloadLink.click();
	// 清理下载链接
	document.body.removeChild(downloadLink);
}
onMounted(() => {
	start()
})
</script>

数据样例:

{
    "type": "FeatureCollection",
    "features": [
               {
            "type": "Feature",
            "properties": {
                "name": "Wild Monkey",
                "opacity": 0.9,
                "color": "#B011B9"
            },
            "geometry": {
                "coordinates": [
                    [
                        -49.96371765347669,
                        41.845725108200355
                    ],
                    [
                        -50.009347363386,
                        41.86656844603469
                    ],
                    [
                        -49.813249591882986,
                        41.718594794912484
                    ]
                ],
                "type": "LineString"
            }
        },
    ]
}

加载效果:

请添加图片描述

下载地址(包含1W、10W、50W矢量线的GeoJson文件):

https://download.csdn.net/download/qq_40236953/88642487

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在Cesium中使用GroundPrimitive加载GeoJSON线数据,可以按照以下步骤进行操作: 1. 准备数据:将GeoJSON线数据准备好,并确保其符合GeoJSON规范。 2. 创建GroundPrimitive:使用Cesium的GroundPrimitive类创建一个实例。GroundPrimitive类可以用于在地面上呈现几何图形,例如线、面等。 3. 加载数据:使用Cesium的GeoJsonDataSource类将GeoJSON数据加载到GroundPrimitive中。 4. 显示数据:将GroundPrimitive添加到Cesium的场景中即可显示数据。 以下是一个示例代码,演示如何加载GeoJSON线数据: ```javascript // 准备数据 var geojsonData = { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [-74.0059, 40.7128], [-77.0369, 38.9072] ] }, "properties": { "name": "New York to Washington DC" } } ] }; // 创建GroundPrimitive var groundPrimitive = new Cesium.GroundPrimitive({ geometryInstances: [] }); // 加载数据 var dataSource = Cesium.GeoJsonDataSource.load(geojsonData); dataSource.then(function(dataSource) { var entities = dataSource.entities.values; for (var i = 0; i < entities.length; i++) { var entity = entities[i]; var geometryInstance = new Cesium.GeometryInstance({ geometry: new Cesium.PolylineGeometry({ positions: entity.polyline.positions.getValue(), width: 5.0 }) }); groundPrimitive.geometryInstances.push(geometryInstance); } }); // 添加到场景中 viewer.scene.primitives.add(groundPrimitive); ``` 在该示例代码中,我们首先准备了一个包含一条线GeoJSON数据。然后创建了一个GroundPrimitive实例,并使用GeoJsonDataSource类将数据加载到其中。最后,将GroundPrimitive添加到场景中即可显示数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

無可言喻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值