dayjournal memo

Total 974 articles!!

Turf.js #028 – 切り取ったポリゴン生成2

Yasunori Kirimoto's avatar


画像



切り取ったポリゴンを生成するメモ2。



画像



script.js

// MIERUNE MONO読み込み
var map = new mapboxgl.Map({
    container: "map",
    style: {
        version: 8,
        sources: {
            m_mono: {
                type: "raster",
                tiles: ["https://tile.mierune.co.jp/mierune_mono/{z}/{x}/{y}.png"],
                tileSize: 256
            }
        },
        layers: [{
            id: "m_mono",
            type: "raster",
            source: "m_mono",
            minzoom: 0,
            maxzoom: 18
        }]
    },
    center: [139.770, 35.676],
    zoom: 13
});


map.on("load", function () {
    // ポリゴンを取得
    var polygon1 = turf.polygon([[
        [139.76793766021726, 35.687452668555544],
        [139.77004051208496, 35.683060650398176],
        [139.77875232696533, 35.6796793288464],
        [139.78068351745605, 35.685361261493114],
        [139.76793766021726, 35.687452668555544]
    ]]);

    // ポリゴン表示
    map.addSource("PolygonSample", {
        type: "geojson",
        data: polygon1
    });
    map.addLayer({
        id: "PolygonSample",
        type: "fill",
        source: "PolygonSample",
        layout: {},
        paint: {
            "fill-color": "#FFD464",
            "fill-opacity": 0.5
        }
    });
    map.addLayer({
        id: "PolygonLineSample",
        type: "line",
        source: "PolygonSample",
        layout: {
            "line-join": "round",
            "line-cap": "round"
        },
        paint: {
            "line-color": "#FFD464",
            "line-width": 10,
            "line-opacity": 0.8
        }
    });

    // ポリゴンを取得
    var polygon2 = turf.polygon([[
        [139.7661, 35.6759],
        [139.7718, 35.6741],
        [139.7722, 35.6745],
        [139.7786, 35.6837],
        [139.7734, 35.6843],
        [139.7709, 35.6846],
        [139.7687, 35.6799],
        [139.7661, 35.6759]
    ]]);

    // ポリゴン表示
    map.addSource("PolygonSample2", {
        type: "geojson",
        data: polygon2
    });
    map.addLayer({
        id: "PolygonSample2",
        type: "fill",
        source: "PolygonSample2",
        layout: {},
        paint: {
            "fill-color": "#1253A4",
            "fill-opacity": 0.5
        }
    });
    map.addLayer({
        id: "PolygonLineSample2",
        type: "line",
        source: "PolygonSample2",
        layout: {
            "line-join": "round",
            "line-cap": "round"
        },
        paint: {
            "line-color": "#1253A4",
            "line-width": 10,
            "line-opacity": 0.8
        }
    });

    // 切り取ったポリゴン生成(1番目ポリゴンと2番目ポリゴンの重なった部分を切り取り)
    var intersection = turf.intersect(polygon1, polygon2);

    // ポリゴン表示
    map.addSource("PolygonSample3", {
        type: "geojson",
        data: intersection
    });
    map.addLayer({
        id: "PolygonSample3",
        type: "fill",
        source: "PolygonSample3",
        layout: {},
        paint: {
            "fill-color": "#58BE89",
            "fill-opacity": 0.5
        }
    });
    map.addLayer({
        id: "PolygonLineSample3",
        type: "line",
        source: "PolygonSample3",
        layout: {
            "line-join": "round",
            "line-cap": "round"
        },
        paint: {
            "line-color": "#58BE89",
            "line-width": 5,
            "line-opacity": 0.8
        }
    });

});

// コントロール関係表示
map.addControl(new mapboxgl.NavigationControl());



Turf.jsを手軽に始めるビルド環境公開しています。
turfjs-starter


book

Q&A