supertiler优化

    两年前研究过一个supertiler的项目,之前只是简单优化了下,最近有新的思路进行优化。在此总结下

范围过滤

function getExtent(data) {

    let xmin = Infinity;
    let ymin = Infinity;
    let xmax = -Infinity;
    let ymax = -Infinity;
    data.forEach((element) => {
        //console.log(element);
        if (element.geometry !== null) {
            const coords = element.geometry.coordinates;
            const lon = coords[0];
            const lat = coords[1];
            if (lon > xmax) {
                xmax = lon;
            }
            if (lat > ymax) {
                ymax = lat;
            }

            if (lon < xmin) {
                xmin = lon;
            }
            if (lat < ymin) {
                ymin = lat;
            }
        }


    });
    return {xmin, ymin, xmax, ymax};
}

function long2tile(lon, zoom) {
    return (Math.floor((lon + 180) / 360 * Math.pow(2, zoom)));
}

function lat2tile(lat, zoom) {
    return (Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, zoom)));
}

利用流读取

    原先的库中不支持超过512MB的json,使用stream-json就可以读取超大的json.

const fs = require('fs');
const supertiler = require('./index.js');
const StreamObject = require('stream-json/streamers/StreamObject');
const inputpath  = './test/pnt.geojson';
const outputPath = './test/basic.mbtiles';

const pipeline = fs.createReadStream(inputpath).pipe(StreamObject.withParser());
pipeline.on('data', (data) => {
    if (data.key === 'features') {
      
        const extent = getExtent(data.value);
        const option = {
            minZoom: 1,
            maxZoom: 11,
            radius: 1,
            data: data.value,
            ex: extent,
            logPerformance: true,
            input: inputpath,
            output: outputPath,
        };
        supertiler(option);
        //console.timeEnd('Create Tiles Using Time');
    }
});

使用事务提升插入性能

    better-sqlite3拥有更好的性能,同时也支持数据库事务。在较多数据插入时使用事务可以有更好的性能

const insertMany = db.transaction((cats) => {
    for (const cat of cats) insert.run(cat);
});
insertMany(insertItem);
posted @ 2023-02-07 10:52  polong  阅读(18)  评论(0编辑  收藏  举报