使用從應用程式匯出的原始 kml/kmz 檔案的範例。

本頁提供您可能會覺得有用的 kmz/kml 檔案後處理範例。

準備:

  1. 從 GPS Camera 匯出: 將點匯出為 KMZ/KML
  2. 提取 KML: 如有需要解壓縮 KMZ

從 imageNotes 更新 KML 地標名稱

本指南示範如何透過從 imageNotes 欄位提取資訊來自動更新 KML 檔案中的地標名稱。

概述

從 GPS Camera 匯出點時,應用程式會根據照片和點標題建立帶有地標名稱的 KML 檔案。註記內容(如「Note 1」、「Note 2」等)會以分隔符號分隔的值儲存在 imageNotes 欄位中。此腳本允許您自動提取該註記資訊,並在需要時將其用作地標名稱。

最好的做法可能是在拍照時直接將您的註記放入標題欄位中。此腳本適用於您沒有這樣做的情況,也可作為 kml 後處理的範例。

使用案例

之前:

  • 地標名稱顯示時間戳記: 15Nov25 16:25, 12Nov25 17:24
  • 實際註記隱藏在 imageNotes 欄位中: Note 1, Note 2

之後:

  • 地標名稱顯示有意義的註記: Note 1, Note 2
  • 在地圖應用程式中一目了然地輕鬆識別點

KML 檔案結構

匯出的 KML 檔案包含具有以下結構的地標:

<Placemark>
  <name>15Nov25 16:25</name>
  <ExtendedData>
    <SchemaData schemaUrl="#blcPointSchema">
      <SimpleData name="imageNotes"><![CDATA[15Nov25 16:25	Note 1		Ad-hoc	V Třešňovce 227/12...]]></SimpleData>
    </SchemaData>
  </ExtendedData>
  <Point>
    <coordinates>14.502834,50.095184,239.0</coordinates>
  </Point>
</Placemark>

imageNotes 欄位包含分隔符號分隔的值:

  • 欄位 0: 時間戳記 (15Nov25 16:25)
  • 欄位 1: 註記文字 (Note 1) ← 這是我們提取的內容
  • 欄位 2: 空白
  • 欄位 3: 類型 (Ad-hoc)
  • 欄位 4: 地址

解決方案: Python 腳本

我們建立了一個 Python 腳本,可自動處理 KML 檔案並根據 imageNotes 內容更新地標名稱。

腳本功能

  • 自動提取: 解析分隔符號分隔的 imageNotes 欄位
  • 基於位置: 從特定欄位位置提取註記(預設: 位置 1)
  • 通用: 適用於遵循此結構的任何 KML 檔案
  • 安全: 在覆寫檔案之前建立備份
  • 靈活: 可以輸出到新檔案或就地更新

運作方式

  1. 解析 KML 檔案: 讀取 XML 結構
  2. 尋找地標: 定位所有 <Placemark> 元素
  3. 提取註記: 透過分隔符號字元分割 imageNotes 並取得位置 1 的註記
  4. 更新名稱: 將整個 <name> 元素替換為提取的註記
  5. 儲存輸出: 將更新的 KML 寫入檔案

安裝與使用

先決條件

  • Python 3 (macOS 上預先安裝,Windows 請從 python.org 下載)
  • 不需要額外的程式庫(僅使用標準程式庫)

逐步指南

1. 下載腳本

下載 update_kml_names.py 並將其放在與您的 KML 檔案相同的資料夾中。

2. 執行腳本

macOS / Linux:

# 導航到包含您的 KML 檔案的資料夾
cd ~/Downloads/Points_20251115_163211

# 執行腳本(建立備份,更新原始檔案)
python3 update_kml_names.py doc.kml

# 或建立新的輸出檔案(保留原始檔案)
python3 update_kml_names.py doc.kml updated_points.kml

Windows:

# 導航到包含您的 KML 檔案的資料夾
cd %USERPROFILE%\Downloads\Points_20251115_163211

# 執行腳本(建立備份,更新原始檔案)
python update_kml_names.py doc.kml

# 或建立新的輸出檔案(保留原始檔案)
python update_kml_names.py doc.kml updated_points.kml

3. 檢視結果

腳本將顯示更新:

Updated: '15Nov25 16:25' -> 'Note 1'
Updated: '12Nov25 17:24' -> 'Note 2'

Completed! Updated 2 placemark(s).
Output saved to: doc.kml

進階使用

自訂註記位置

如果您的 imageNotes 欄位具有不同的結構且註記在不同位置,您可以指定它:

# 從位置 2 提取註記(第三個欄位,從 0 開始索引)
python3 update_kml_names.py doc.kml output.kml 2

批次處理

您可以使用簡單的腳本處理多個 KML 檔案:

macOS / Linux:

for file in *.kml; do
  python3 update_kml_names.py "$file"
done

Windows (PowerShell):

Get-ChildItem -Filter *.kml | ForEach-Object {
  python update_kml_names.py $_.Name
}

範例: 之前和之後

處理前

<Placemark>
  <name>15Nov25 16:25</name>
  <SimpleData name="imageNotes">15Nov25 16:25	Note 1		Ad-hoc	Address...</SimpleData>
</Placemark>

<Placemark>
  <name>12Nov25 17:24</name>
  <SimpleData name="imageNotes">12Nov25 17:24	Note 2		Ad-hoc	Address...</SimpleData>
</Placemark>

處理後

<Placemark>
  <name>Note 1</name>
  <SimpleData name="imageNotes">15Nov25 16:25	Note 1		Ad-hoc	Address...</SimpleData>
</Placemark>

<Placemark>
  <name>Note 2</name>
  <SimpleData name="imageNotes">12Nov25 17:24	Note 2		Ad-hoc	Address...</SimpleData>
</Placemark>

技術細節

腳本實作

腳本使用 Python 的內建 XML 解析器(xml.etree.ElementTree)來:

  • 使用適當的命名空間處理解析 KML 檔案
  • 導航 XML 結構以尋找地標元素
  • 提取和處理分隔符號分隔的值
  • 在保留檔案結構的同時更新元素

相容性

  • KML 版本: 2.2(標準格式)
  • Python 版本: 3.6+
  • 作業系統: macOS, Windows, Linux
  • 應用程式: 與 Google Earth, QGIS, ArcGIS 及其他支援 KML 的應用程式相容

疑難排解

問題: 找不到腳本

原因: 未安裝 Python 或不在系統 PATH 中

解決方案:

  • macOS: Python 3 應該已預先安裝
  • Windows: 從 python.org 下載並確保安裝時勾選「Add to PATH」

問題: 更新後名稱為空

原因: 註記欄位為空或位置錯誤

解決方案: 腳本將顯示警告。檢查您的 imageNotes 格式並調整位置參數。

與您的工作流程整合

此腳本可以整合到自動化工作流程中:

  1. 從 GPS Camera 匯出: 將點匯出為 KMZ/KML
  2. 提取 KML: 如有需要解壓縮 KMZ
  3. 執行腳本: 處理 KML 以更新名稱
  4. 匯入到 GIS: 在 Google Earth, QGIS 或其他工具中使用

原始碼

#!/usr/bin/env python3
"""
Script to update KML placemark names with Note from imageNotes field.
Extracts the note from the tab-separated imageNotes field (position 2) and replaces the placemark name.
"""

import xml.etree.ElementTree as ET
import sys
import os

def update_kml_with_notes(input_file, output_file=None, note_position=1):
    """
    Update KML placemark names by extracting Note from tab-separated imageNotes field.

    Args:
        input_file: Path to input KML file
        output_file: Path to output KML file (if None, overwrites input)
        note_position: Position of the note in tab-separated imageNotes (0-indexed, default 1)
    """
    # Parse the KML file
    tree = ET.parse(input_file)
    root = tree.getroot()

    # Define namespace
    ns = {'kml': 'http://www.opengis.net/kml/2.2'}

    # Find all Placemark elements
    placemarks = root.findall('.//kml:Placemark', ns)

    updated_count = 0

    for placemark in placemarks:
        # Get the current name
        name_elem = placemark.find('kml:name', ns)
        if name_elem is None:
            continue

        current_name = name_elem.text

        # Find the imageNotes field
        image_notes_elem = placemark.find('.//kml:SimpleData[@name="imageNotes"]', ns)

        if image_notes_elem is not None and image_notes_elem.text:
            image_notes = image_notes_elem.text

            # Split by tab to get the note (tab-separated format)
            parts = image_notes.split('\t')

            if len(parts) > note_position and parts[note_position].strip():
                note_text = parts[note_position].strip()

                # Update the name - replace entirely with the note
                name_elem.text = note_text

                print(f"Updated: '{current_name}' -> '{note_text}'")
                updated_count += 1
            else:
                print(f"Warning: No note found at position {note_position} in imageNotes for placemark '{current_name}'")
                print(f"  imageNotes content: {image_notes}")
        else:
            print(f"Warning: No imageNotes found for placemark '{current_name}'")

    # Determine output file
    if output_file is None:
        output_file = input_file

    # Write the updated KML
    tree.write(output_file, encoding='utf-8', xml_declaration=True)

    print(f"\nCompleted! Updated {updated_count} placemark(s).")
    print(f"Output saved to: {output_file}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python3 update_kml_names.py <input_kml_file> [output_kml_file] [note_position]")
        print("\nArguments:")
        print("  input_kml_file:  Path to the KML file to process")
        print("  output_kml_file: (Optional) Output file path. If not provided, input will be overwritten.")
        print("  note_position:   (Optional) 0-indexed position of note in tab-separated imageNotes (default: 1)")
        print("\nExample:")
        print("  python3 update_kml_names.py doc.kml")
        print("  python3 update_kml_names.py doc.kml doc_updated.kml")
        print("  python3 update_kml_names.py doc.kml doc_updated.kml 1")
        sys.exit(1)

    input_file = sys.argv[1]
    output_file = sys.argv[2] if len(sys.argv) > 2 else None
    note_position = int(sys.argv[3]) if len(sys.argv) > 3 else 1

    if not os.path.exists(input_file):
        print(f"Error: Input file '{input_file}' not found.")
        sys.exit(1)

    # Create backup if overwriting
    if output_file is None:
        backup_file = input_file + '.backup'
        import shutil
        shutil.copy2(input_file, backup_file)
        print(f"Backup created: {backup_file}\n")

    update_kml_with_notes(input_file, output_file, note_position)

支援

如有疑問或問題:

  • 驗證 Python 3 已正確安裝
  • 檢查 imageNotes 是否包含分隔符號分隔的值

授權

此腳本按現狀提供以供應用程式匯出使用。請隨意修改並調整以適應您的特定需求。


相關文件

匯出與匯入

使用 Google Earth

相關工作流程


最後更新: 2025年11月