0.22.0 • Published 5 months ago

maptalks.tileclip v0.22.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

maptalks.tileclip

maptalks TileLayer tiles merge and clip tool

  • This plugin requires the runtime environment to support OffscreenCanvas. Pay attention to relevant compatibility

  • Considering performance, all operations are completed within the web worker

  • If you are familiar with other map engines, you can also apply them to other map engines leaflet demo

Examples

Install

NPM

npm i maptalks
#or
# npm i maptalks-gl
npm i maptalks.tileclip

CDN

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/maptalks-gl/dist/maptalks-gl.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/maptalks.tileclip/dist/maptalks.tileclip.js"></script>

API

getTileActor()

return TileActor instance

import {
    getTileActor
} from 'maptalks.tileclip'

const tileActor = getTileActor();

TileActor class

Tile clip worker interaction class. about maptalks. Actor details

import {
    getTileActor
} from 'maptalks.tileclip'

const tileActor = getTileActor();

methods

  • getTile(options) get tile ImageBitmap by fetch in worker, return Promise
    • options.url:tile url orl tiles urls
    • options.filter:CanvasRenderingContext2D.filter
    • options.headers:fetch headers params. if need
    • options.fetchOptions:fetch options. if need, If it exists, headers will be ignored
    • options.opacity: tile opacity if need
tileActor.getTile({
    url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/12/1663/3425',
    //or url:[ur1,ur2],
    fetchOptions: {
        referrer: document.location.href,
        headers: {
            ...
        }
        ...
    }
}).then(imagebitmap => {
    consle.log(imagebitmap);
}).catch(error => {
    //do some things
})
const {
    x,
    y,
    z
} = tile;
const urlTemplate = baseLayer.options.urlTemplate;
const maxAvailableZoom = 18;

tileActor.getTileWithMaxZoom({
    x,
    y,
    z,
    urlTemplate,
    //or urlTemplate:[urlTemplate1,urlTemplate2],
    maxAvailableZoom,
    fetchOptions: {
        referrer: document.location.href,
        headers: {
            ...
        }
        ...
    }
}).then(imagebitmap => {
    consle.log(imagebitmap);
}).catch(error => {
    //do some things
})
  • injectMask(maskId,Polygon/MultiPolygon) inject Mask(GeoJSON. Polygon) for clip tiles . return Promise

    • maskId: mask id, Cache mask data in the worker
    • Polygon/MultiPolygon GeoJSON Polygon/MultiPolygon GeoJSON SPEC
const maskId = 'china';

const polygon = {
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": []
    }
}

tileActor.injectMask(maskId, polygon).then(data => {
    // baseLayer.addTo(map);
}).catch(error => {
    console.error(error);
})
  • removeMask(maskId) remove Mask from cache . return Promise

    • maskId: mask id
const maskId = 'china';

tileActor.removeMask(maskId).then(data => {

}).catch(error => {
    console.error(error);
})
  • maskHasInjected(maskId) Has the mask been injected . return Boolean

    • maskId: mask id
const maskId = 'china';
const result = tileActor.maskHasInjected(maskId);
  • clipTile(options) clip tile by mask . return Promise
    • options.tile:tile ImageBitmap data
    • options.tileBBOX:tile BBOX [minx,miny,maxx,maxy]
    • options.projection: Projection code, such as : EPSG:3857
    • options.tileSize:tile size
    • options.maskId:mask key
    • options.returnBlobURL: to return Blob URL by createObjectURL() ? When the blob URL is no longer in use, be sure to destroy its value revokeObjectURL()
import * as maptalks from 'maptalks-gl';
import {
    getTileActor
} from 'maptalks.tileclip';

const tileActor = getTileActor();
const maskId = 'china';

const baseLayer = new maptalks.TileLayer('base', {
    debug: true,
    urlTemplate: '/arcgisonline/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
    subdomains: ["a", "b", "c", "d"],
    // bufferPixel: 1
})

baseLayer.on('renderercreate', function(e) {
    //load tile image
    //   img(Image): an Image object
    //   url(String): the url of the tile
    e.renderer.loadTileBitmap = function(url, tile, callback) {
        //get Tile data
        tileActor.getTile({
            url: maptalks.Util.getAbsoluteURL(url)
        }).then(imagebitmap => {

            //clip tile
            tileActor.clipTile({
                tile: imagebitmap,
                tileBBOX: baseLayer._getTileBBox(tile),
                projection: baseLayer.getProjection().code,
                tileSize: baseLayer.getTileSize().width,
                maskId,
            }).then(image => {
                callback(image);
            }).catch(error => {
                //do some things
                console.error(error);
            })
        }).catch(error => {
            //do some things
            console.error(error);
        })
    };
});

const polygon = {
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": []
    }
}

tileActor.injectMask(maskId, polygon).then(data => {
    baseLayer.addTo(map);
}).catch(error => {
    console.error(error);
})
0.10.0

9 months ago

0.21.0

6 months ago

0.20.0

6 months ago

0.19.0

7 months ago

0.22.0

5 months ago

0.11.0

9 months ago

0.12.0

9 months ago

0.13.0

9 months ago

0.14.0

9 months ago

0.15.0

9 months ago

0.16.0

9 months ago

0.17.0

7 months ago

0.18.0

7 months ago

0.9.0

10 months ago

0.8.0

10 months ago

0.7.0

10 months ago

0.6.0

10 months ago

0.5.0

10 months ago

0.4.0

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

0.1.0

10 months ago