ColinOL

A Small Website

用户工具

站点工具


wiki:gis:arcpy-get-unique-values

使用ArcPy获得矢量数据字段唯一值

按照字段属性筛选,通常要先获取字段唯一值: Unique Values

下面提供两种方法。

Example 1: Arcpy Data Access Module using SearchCursor and Set Comprehension

getuvalue.py
import arcpy
import timeit
 
def unique_values(table, field):  ##uses list comprehension
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})
 
try:
    arcpy.env.workspace = r"C:\MTBS_Wildfire\mtbs_perimeter_data"
 
    start = timeit.default_timer()
    vals = unique_values("mtbs_perims_1984-2015_DD_20170501.shp","Fir_eType")
 
    stop = timeit.default_timer()
    total_time = stop - start
    print(total_time)
    print(vals)
 
except Exception as e:
    print("Error: " + e.args[0])

Example 2: Using Numpy with Arcpy

getuvalue2.py
import arcpy
import numpy
import timeit
 
def unique_values(table, field):  ##uses numpy
    data = arcpy.da.TableToNumPyArray(table, [field])
    return numpy.unique(data[field])
 
try:
    # mtbs_perims_1984-2015_DD_20170501.shp  - 25,388 recs
    arcpy.env.workspace = r"C:\MTBS_Wildfire\mtbs_perimeter_data"
 
    start = timeit.default_timer()
    vals = unique_values("mtbs_perims_1984-2015_DD_20170501.shp", "Fi_reType")
 
    stop = timeit.default_timer()
    total_time = stop - start
    print(total_time)
    print(vals)
 
except Exception as e:
    print("Error: " + e.args[0])

参考:

wiki/gis/arcpy-get-unique-values.txt · 最后更改: 2020/06/27 20:52 由 colin