Openlayers | Cesium 在线生成随机点线面坐标数据

Posted 非科班Java出身GISer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Openlayers | Cesium 在线生成随机点线面坐标数据相关的知识,希望对你有一定的参考价值。

Openlayers | Cesium 在线生成随机点线面坐标数据

OpenLayers 教程

地图开发工作中,有时候需要模拟一些数据,之前都是手动用循环创建,数据太有规律,不好用,于是搞个在线工具。

工具是使用 turf.js 实现的,效率还可以,仅限于小数据量,大数据量模拟,建议通过后台实现。

这里放上核心代码,完整代码详见在线示例。

在线示例中,可操作生成模拟数据(GeoJson 格式)之后,直接复制使用即可。


使用 Turf.js 模拟随机数据

// 模拟随机点数据
const points = turf.randomPoint(
    // 模拟数量
    25,
    
        // 范围
        bbox: [115.11343196896966, 38.053632016866445, 116.22305110959466, 39.558758969991445]
    )

// 模拟随机线段数据
const lineStrings = turf.randomLineString(
    // 模拟数量
    25,
    
        // 范围
        bbox: [115.11343196896966, 38.053632016866445, 116.22305110959466, 39.558758969991445],
        // 顶点数量
        num_vertices: 10,
        // 最大长度
        max_length: 0.1,
        // 最大角度
        max_rotation: Math.PI / 8
    )

// 模拟随机多边形
const polygons = turf.randomPolygon(
    // 模拟数量
    25,
    
        // 范围
        bbox: [115.11343196896966, 38.053632016866445, 116.22305110959466, 39.558758969991445],
        // 最大辐射长度
        max_radial_length: 0.1,
        // 顶点数量
        num_vertices: 10
    )
// 可以查看一下,这里数据为 geojson 对象数据
console.log(points);
// geojson 创建 feature 对象
console.log(getFeatureByGeoJson(points));

/**
 * @todo 图形对象转化成GeoJson格式数据(postgis)
 * @param string|object geojson geojson字符串或者对象
 * @param string|Projection sourceCode 源投影坐标系
 * @param string|Projection targetCode 目标投影坐标系
 * @returns Feature
 */
function getFeatureByGeoJson(geojson, sourceCode, targetCode) 

    let view = map.getView();

    if (!geojson) 
        return null;
    

    let feature;

    if ((typeof geojson) == 'string') 
        // 替换 null 字符
        while (geojson.indexOf('null') != -1) 
            // geojson = geojson
            geojson = geojson.replace("null", "");
        
    

    feature = (new ol.format.GeoJSON()).readFeatures(geojson, 
        dataProjection: sourceCode || view.getProjection(),    // 设定JSON数据使用的坐标系
        featureProjection: targetCode || view.getProjection() // 设定当前地图使用的feature的坐标系
    );

    return feature;



在线示例

Openlayers 生成随机坐标:Openlayers random feature

Cesium 生成随机坐标:Cesium random feature

以上是关于Openlayers | Cesium 在线生成随机点线面坐标数据的主要内容,如果未能解决你的问题,请参考以下文章

Cesium加载在线地图服务

使用openlayers 3 在线加载天地图及GeoServer发布的地图

openlayers3应用:显示百度在线地图

使用 Cesium 在 Geoserver 上无法获取(找不到正确的 url)Tile 层

Cesium实战项目

Cesium CallbackProperty(十五)