arcpy.conversion.KMLToLayer(in_kml_file, output_folder, {output_data}, {include_groundoverlay}, {out_suffix})
|
名称
|
说明
|
数据类型
|
|
in_kml_file
|
将转换为地理数据库数据集的 .kml 或 .kmz 文件。
|
File; KML Layer
|
|
output_folder
|
将在其中创建输出地理数据库和图层文件 (.lyrx) 的目标文件夹。
|
Folder
|
|
output_data
(可选)
|
将同时用于输出地理数据库和图层文件 (.lyrx) 的名称。 默认值为输入文件的名称。
可以指定目标文件夹中现有地理数据库的名称,并且转换会将新数据集写入现有地理数据库。 如果指定名称的地理数据库不存在,则将在目标文件夹中创建该地理数据库。
|
String
|
|
include_groundoverlay
(可选)
|
指定是否在输出中包含来自 KML 的地面叠加层。
如果 KMZ 指向提供影像的服务,请谨慎使用。 该工具将尝试按所有可用比例转换栅格影像。 此过程也许会较漫长,并且可能会导致服务器不堪重负或超时。
|
Boolean
|
|
out_suffix
(可选)
|
将添加至所有输出要素数据集、要素类、镶嵌数据集和图层文件的名称的后缀。 如果未指定任何后缀,则输出地理数据库中的要素数据集名称将为 Placemarks。
|
String
|
派生输出
|
名称
|
说明
|
数据类型
|
|
output_layer
|
输出图层文件。
|
Group Layer
|
|
out_geodatabase
|
包含要素数据集中的要素类的输出地理数据库,以及镶嵌数据集(如果包括了地面叠加层)。
|
Workspace
|
代码示例
KMLToLayer 示例 1(Python 窗口)
在 Python 窗口中,将 .kmz 文件转换为文件地理数据库。
import arcpy
arcpy.conversion.KMLToLayer(r'C:\kmls\earthquakes.kml', r'C:\gisdata\fromkmls',
'earthquake_09')
以下脚本会将 .kmz 和 .kml 文件的文件夹转换为其各自的文件地理数据库。 然后,会将该文件地理数据库内的要素类合并到单个文件地理数据库中。
注:
此脚本不维护 KMLToLayer 函数中的图层文件。
# Name: BatchKML_to_GDB.py
# Description: Convert a directory of KMLs and copy the output to a single
# fGDB. A 2-step process: first convert the KML files; then
# copy the feature classes.
# Import system modules
import arcpy
import os
# Set workspace (where all the KMLs are)
arcpy.env.workspace = "C:/VancouverData/KML"
# Set local variables and location for the consolidated file geodatabase
out_location = "C:/WorkingData/fGDBs"
gdb = 'AllKMLLayers.gdb'
gdb_location = os.path.join(out_location, gdb)
# Create the primary file geodatabase
arcpy.management.CreateFileGDB(out_location, gdb)
# Convert all KMZ and KML files found in the current workspace
for kmz in arcpy.ListFiles('*.KM*'):
print("CONVERTING: {0}".format(os.path.join(arcpy.env.workspace, kmz)))
arcpy.conversion.KMLToLayer(os.path.join(arcpy.env.workspace, kmz), out_location)
# Change the workspace to fGDB location
arcpy.env.workspace = out_location
# Loop through all the file geodatabases in the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the primary GDB
wks.remove(gdb_location)
for fgdb in wks:
# Change the workspace to the current file geodatabase
arcpy.env.workspace = fgdb
# For every feature class inside, copy it to the primary and use the name
# from the original fGDB
feature_classes = arcpy.ListFeatureClasses('*', '', 'Placemarks')
for fc in feature_classes:
print("COPYING: {} FROM: {}".format(fc, fgdb))
fcCopy = os.path.join(fgdb, 'Placemarks', fc)
arcpy.conversion.FeatureClassToFeatureClass(
fcCopy, gdb_location, fgdb[fgdb.rfind(os.sep) + 1:-4])
以下脚本将转换 .kmz 文件并将 HTML 弹出数据提取到字段属性。
注:
此脚本不维护 KMLToLayer 函数中的图层文件。
# Name: Extract_KML_popup_table.py
# Description: Convert a .kmz file and transfer a 2-column table from the pop-up
# to the field attributes.
# Import system modules
import arcpy
import os
# Set local variables and location for the consolidated file geodatabase
out_location = "C:/WorkingData/fGDBs"
gdb = 'SCP BoardDistricts.gdb'
gdb_location = os.path.join(out_location, gdb)
# Set kmz path
kmz = os.path.join(out_location, "SCP BoardDistricts.kmz")
# Convert all KMZ files to feature in the current workspace
arcpy.conversion.KMLToLayer(kmz, out_location)
# Change the workspace to fGDB location
arcpy.env.workspace = gdb_location
# Loop through all the file geodatabases in the workspace
feature_classes = arcpy.ListFeatureClasses('*', '', 'Placemarks')
for fc in feature_classes:
popup_info_field_name = 'PopupInfo'
field_names = [['gs_guid', 'TEXT', '#', 255, None, ''],
['gs_vc_revision', 'TEXT', '#', 255, None, ''],
['gs_vc_modified_sw', 'TEXT', '#', 255, None, ''],
['gs_old_objectid', 'TEXT', '#', 255, None, ''],
['gs_date_created', 'TEXT', '#', 255, None, ''],
['gs_date_modified', 'TEXT', '#', 255, None, ''],
['gs_code', 'TEXT', '#', 255, None, ''],
['gs_reference_scale', 'TEXT', '#', 255, None, ''],
['gs_description', 'TEXT', '#', 255, None, ''],
['gs_plot_scale', 'TEXT', '#', 255, None, ''],
['gs_nisc_guid', 'TEXT', '#', 255, None, ''],
['gs_mapped_by', 'TEXT', '#', 255, None, ''],
['gs_district', 'TEXT', 'Name', 255, None, ''],
['gs_boardmember', 'TEXT', '#', 255, None, ''],
['gs_servicetype', 'TEXT', 'Name', 255, None, ''],
['gs_legacy_futuraguid', 'TEXT', '#', 255, None, ''],
['gs_last_modified_by', 'TEXT', '#', 255, None, ''],
['gs_shape_from_gps_sw', 'TEXT', '#', 255, None, ''],
['gs_district_code', 'TEXT', '#', 255, None, ''],
['gs_district_name', 'TEXT', '#', 255, None, '']]
arcpy.management.AddFields(fc, field_names, None)
field_calculation = [[calc[0], f"extract_field(!{popup_info_field_name}!, '{calc[0]}')"] for calc in field_names]
arcpy.management.CalculateFields(
fc, "PYTHON3", field_calculation,
"""from lxml import etree as _etree
def extract_field(s, field):
''' Extract fields from pop-up from each record in the calculate fields tool '''
html = _etree.HTML(s)
# Get all 'td'
rows = html.xpath('//table/tr/td')
next_value = False
for row in rows:
c = row.text
if next_value:
return c
if c == field:
next_value = True
return None""",
"NO_ENFORCE_DOMAINS")