Global

Methods

classifyFeature(feature, result)

Description:
  • 將單一 Feature 依幾何類型分類到結果物件中

Source:
Parameters:
Name Type Description
feature Object

輸入 GeoJSON Feature

result Object

輸入分類結果物件(包含 points、lines、polygons)

createEmptyResult() → {Object}

Description:
  • 建立空的分類結果物件

Source:
Returns:

回傳包含 points、lines、polygons 三個空 FeatureCollection 的物件

Type
Object

ensureRingClosed(ring) → {Array}

Description:
  • 修復 Polygon ring 閉合(首尾座標相同)

Source:
Parameters:
Name Type Description
ring Array

輸入 ring 座標陣列

Returns:

回傳已閉合的 ring 座標陣列

Type
Array

fixCloseMultiPolygonCoords(coordinates) → {Array}

Description:
  • 修復 MultiPolygon 的所有 ring 閉合

Source:
Parameters:
Name Type Description
coordinates Array

MultiPolygon 的 coordinates(三維陣列)

Returns:

回傳已修復閉合的 coordinates

Type
Array

fixClosePolygonCoords(coordinates) → {Array}

Description:
  • 修復 Polygon 的所有 ring 閉合

Source:
Parameters:
Name Type Description
coordinates Array

Polygon 的 coordinates(二維陣列,每個元素為一個 ring)

Returns:

回傳已修復閉合的 coordinates

Type
Array

procSpecPolygon(polygonsFC) → {Object}

Description:
  • 處理特殊 Polygon 數據,讓 MapLibre GL JS 可正確渲染

    此函數的設計目的是處理 splitGeoJSON 回傳的 polygons FeatureCollection 中, 含有「多層套疊 ring」的 Polygon / MultiPolygon 數據。

    Leaflet 利用 SVG 的 evenodd fill rule 可直接繪製多層套疊的 ring, 但 MapLibre GL JS 僅依賴 winding order(外環 CCW、洞環 CW)來決定填色/挖洞。 因此,必須將多層套疊結構轉換為符合 RFC 7946 的標準 MultiPolygon 格式。

    處理邏輯:

    1. 遍歷所有 Feature
    2. 對於 Polygon 類型:
      • ring 數量 <= 2 時,使用 flattenMultiPolygon 修正 winding order
      • ring 數量 > 2 時(可能為多層套疊),使用 flattenMultiPolygon 搭配 supposeType='ringStrings' 模式,透過 XOR 運算將套疊結構轉為標準 MultiPolygon
    3. 對於 MultiPolygon 類型:
      • 將每個子 polygon 各自透過 flattenMultiPolygon 處理後合併
    4. 處理完畢後,若 MultiPolygon 只含一個 polygon,降級為 Polygon 類型
    5. 深拷貝輸出,不汙染原始資料
    6. 保留所有 properties
Source:
Example
// 三層套疊 Polygon
let input = {
    type: 'FeatureCollection',
    features: [{
        type: 'Feature',
        properties: { name: '三層' },
        geometry: {
            type: 'Polygon',
            coordinates: [
                [[0, 0], [20, 0], [20, 20], [0, 20], [0, 0]],
                [[4, 4], [16, 4], [16, 16], [4, 16], [4, 4]],
                [[8, 8], [12, 8], [12, 12], [8, 12], [8, 8]],
            ],
        },
    }],
}
let result = procSpecPolygon(input)
console.log(result.features[0].geometry.type) // 'MultiPolygon'
Parameters:
Name Type Description
polygonsFC Object | null

輸入 polygons FeatureCollection(來自 splitGeoJSON 的 polygons 欄位)

Returns:

回傳處理後的 FeatureCollection

Type
Object

processPolygonCoords(coordinates) → {Object}

Description:
  • 處理單一 Polygon 的 coordinates

Source:
Parameters:
Name Type Description
coordinates Array

Polygon 的 coordinates(二維陣列,每個元素為一個 ring)

Returns:

回傳 GeoJSON Geometry 物件(Polygon 或 MultiPolygon)

Type
Object

splitAndProcGeoJSON(geoIn) → {Object}

Description:
  • 將任意 GeoJSON 資料拆分為依幾何類型分類的多個 FeatureCollection, 並對其中的 Polygon 數據進行特殊處理(多層套疊 ring 轉換為標準 MultiPolygon)

    此函數結合了 splitGeoJSON 與 procSpecPolygon 的功能:

    1. splitGeoJSON:將混合幾何類型的 GeoJSON 拆分為 points / lines / polygons
    2. procSpecPolygon:將 polygons 中含有多層套疊 ring 的 Polygon/MultiPolygon 轉換為符合 RFC 7946 規範的標準格式,確保 winding order 正確

    此函數設計為方便日後 MapLibre GL JS 渲染使用的一站式前處理函數。

Source:
Example
let input = {
    type: 'FeatureCollection',
    features: [
        { type: 'Feature', properties: { name: 'pt' }, geometry: { type: 'Point', coordinates: [121, 25] } },
        { type: 'Feature', properties: { name: 'ls' }, geometry: { type: 'LineString', coordinates: [[121, 25], [122, 26]] } },
        {
            type: 'Feature',
            properties: { name: '三層' },
            geometry: {
                type: 'Polygon',
                coordinates: [
                    [[0, 0], [20, 0], [20, 20], [0, 20], [0, 0]],
                    [[4, 4], [16, 4], [16, 16], [4, 16], [4, 4]],
                    [[8, 8], [12, 8], [12, 12], [8, 12], [8, 8]],
                ],
            },
        },
    ],
}
let result = splitAndProcGeoJSON(input)
console.log(result.points.features.length)   // 1
console.log(result.lines.features.length)    // 1
console.log(result.polygons.features.length) // 1(polygon 已被處理為 MultiPolygon)
Parameters:
Name Type Description
geoIn Object | String | null

輸入 GeoJSON 資料,可為 FeatureCollection、Feature、裸 Geometry 物件或 JSON 字串

Returns:

回傳分類結果物件,結構為 { points: FeatureCollection, lines: FeatureCollection, polygons: FeatureCollection }

Type
Object

splitGeoJSON(geoIn) → {Object}

Description:
  • 將 GeoJSON 資料拆分成依幾何類型分類的多個 FeatureCollection

    此函數的設計目的是針對 MapLibre GL JS 的 layer type 限制, 將混合不同幾何類型的 GeoJSON 資料預先拆分成:

    • points:包含 Point / MultiPoint 的 FeatureCollection
    • lines:包含 LineString / MultiLineString 的 FeatureCollection
    • polygons:包含 Polygon / MultiPolygon 的 FeatureCollection

    額外處理:

    1. GeometryCollection 會被遞迴拆解成獨立的 Feature,properties 繼承父 Feature
    2. Polygon / MultiPolygon 的 ring 自動修復閉合(首尾座標相同)
    3. 支援多種輸入格式:FeatureCollection、Feature、裸 Geometry、JSON 字串
    4. 深拷貝輸出,不汙染原始資料
    5. 空輸入 / 無效輸入回傳空的分類結果
Source:
Example
// 混合類型 FeatureCollection
let input = {
    type: 'FeatureCollection',
    features: [
        { type: 'Feature', properties: { name: 'pt' }, geometry: { type: 'Point', coordinates: [121, 25] } },
        { type: 'Feature', properties: { name: 'ls' }, geometry: { type: 'LineString', coordinates: [[121, 25], [122, 26]] } },
        { type: 'Feature', properties: { name: 'pg' }, geometry: { type: 'Polygon', coordinates: [[[0, 0], [4, 0], [4, 4], [0, 4], [0, 0]]] } },
    ],
}
let result = splitGeoJSON(input)
console.log(result.points.features.length)   // 1
console.log(result.lines.features.length)    // 1
console.log(result.polygons.features.length) // 1
Parameters:
Name Type Description
geoIn Object | String | null

輸入 GeoJSON 資料,可為 FeatureCollection、Feature、裸 Geometry 物件或 JSON 字串

Returns:

回傳分類結果物件,結構為 { points: FeatureCollection, lines: FeatureCollection, polygons: FeatureCollection }

Type
Object