KML 转为图层 (转换)

许可等级:BasicStandardAdvanced

摘要

将 KML 或 KMZ 文件转换为要素类和图层文件。图层文件用于维护在原始 KML 或 KMZ 文件中找到的符号。

了解有关 ArcGIS 中 KML 支持的详细信息

用法

语法

KMLToLayer_conversion (in_kml_file, output_folder, {output_data}, {include_groundoverlay})
参数说明数据类型
in_kml_file

要转换的 KML 或 KMZ 文件。

File
output_folder

文件地理数据库和图层 (.lyr) 文件的目标文件夹。

Folder
output_data
(可选)

输出文件地理数据库和图层文件 (.lyr) 的名称。默认为输入 KML 文件的名称。

String
include_groundoverlay
(可选)

包括地面叠加层(栅格、航空照片等)。KMZ 指向提供栅格影像的服务时务必要谨慎。该工具将尝试按所有可用比例转换栅格影像。此过程可能会很长且可能超出服务的能力。

  • GROUNDOVERLAY地面叠加层包括在输出中。
  • NO_GROUNDOVERLAY地面叠加层不包括在输出中。这是默认设置。
Boolean

代码实例

KMLToLayer 示例 1(Python 窗口)

在 Python 窗口中,将 KMZ 文件转换为 FGDB。

import arcpy

arcpy.KMLToLayer_conversion(r'C:\kmls\earthquakes.kml',r'C:\gisdata\fromkmls','earthquake_09')
KMLToLayer 示例 2(独立脚本)

以下脚本会将 KMZ 和 KML 文件的文件夹转换为其各自的文件地理数据库。然后,会将这些文件地理数据库内的要素类合并到单个文件地理数据库中。

注:此脚本不维护“KML 转图层”工具中的图层文件。

# Name: BatchKML_to_GDB.py
# Description: Converts a directory of KMLs and copies the output into a single fGDB.
#              A 2 step process: first convert the KML files, and then copy the featureclases

# Import system models
import arcpy, os

# Set workspace (where all the KMLs are)
arcpy.env.workspace="C:/VancouverData/KML"

# Set local variables and location for the consolidated file geodatabase
outLocation = "C:/WorkingData/fGDBs"
MasterGDB = 'AllKMLLayers.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)

# Create the master FileGeodatabase
arcpy.CreateFileGDB_management(outLocation, MasterGDB)

# Convert all KMZ and KML files found in the current workspace
for kmz in arcpy.ListFiles('*.KM*'):
  print "CONVERTING: " + os.path.join(arcpy.env.workspace,kmz)
  arcpy.KMLToLayer_conversion(kmz, outLocation)


# Change the workspace to fGDB location
arcpy.env.workspace = outLocation

# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the Master GDB
wks.remove(MasterGDBLocation)

for fgdb in wks:  
     
  # Change the workspace to the current FileGeodatabase
  arcpy.env.workspace = fgdb    

  # For every Featureclass inside, copy it to the Master and use the name from the original fGDB  
  featureClasses = arcpy.ListFeatureClasses('*', '', 'Placemarks')
  for fc in featureClasses:
    print "COPYING: " + fc + " FROM: " + fgdb    
    fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc    
    arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])
  

# Clean up
del kmz, wks, fc, featureClasses, fgdb

环境

相关主题

许可信息

ArcGIS for Desktop Basic:是
ArcGIS for Desktop Standard:是
ArcGIS for Desktop Advanced:是
9/15/2013