Arcgis10.5 python按属性分割图层,属性相同分为一个图层
# coding=utf-8 """ Source code for potential gp tool to create outputs based on attributes of an input. """ import arcpy import numbers import sys try: unicode except: unicode = str def get_unique_values(in_data, fields): """ Identify all unique values for field(s) in a data source :param in_data: Input data source :param fields: Field name :return: A list of unique values """ # Respect extent environment where possible if arcpy.env.extent: lyr_name = 'sbyloc_extent' try: lyr = arcpy.MakeFeatureLayer_management(in_data, lyr_name)[0] arcpy.SelectLayerByLocation_management(lyr, 'INTERSECT', arcpy.env.extent.polygon) in_data = lyr_name except arcpy.ExecuteError: pass table_name = arcpy.CreateUniqueName('freq', 'in_memory') arcpy.Frequency_analysis(in_data, table_name, fields) atts = [r for r in arcpy.da.SearchCursor(table_name, fields)] try: arcpy.Delete_management(table_name) except arcpy.ExecuteError: # Should delete, but don't fail for intermediate data issues pass return atts def create_name(workspace, name, extension): """ Create a unique name :param workspace: The workspace that an expected output will be written to :param name: Base name of the output (list) :param extension: Extension including the leading period :return: A unique name, including pathname """ name = u'_'.join([unicode(i) for i in name]) name = name.replace('"', '') name = name.replace("'", "") if name == '': # name of '' validated to '' name = 'T' validated_name = u'{}{}'.format( arcpy.ValidateTableName(name, workspace), extension) unique_name = arcpy.CreateUniqueName(validated_name, workspace) return unique_name def create_expression(in_data, field_name, value): """ Create a SQL Expression :param in_data: Input data source :param field_name: The field name that will be queried :param value: The value in the field that will be queried for :return: SQL expression """ delimited_field = arcpy.AddFieldDelimiters(in_data, field_name) if isinstance(value, numbers.Number): return u'{} = {}'.format(delimited_field, value) elif isinstance(value, type(None)): return u'{} IS NULL'.format(delimited_field) else: return u''' %s = '%s' ''' % ( delimited_field, value.replace("'", "'\'").replace('"', '\"') ) def select(datatype, *args): """ Data type non-specific Select tool handling :param datatype: arcpy.Describe datatype keyword :param args: arguments for Select/TableSelect tools :return: """ feature_data = datatype in ['FeatureClass', 'FeatureLayer'] tool_name = 'Select' if feature_data else 'TableSelect' eval('arcpy.analysis.{}'.format(tool_name))(*args) def split_by_atts(in_data, out_workspace, fields): """ Split a feature class include a series of feature classes based on unique values in a field. :param in_data: The input data source :param out_workspace: The output workspace that data will be written to :param fields: Unique values in these fields will be used to split the data :return: A list of output pathnames (output has been created) """ try: from itertools import izip except ImportError: izip = zip datatype = arcpy.Describe(in_data).datatype unique_values = get_unique_values(in_data, fields) workspace_type = arcpy.Describe(out_workspace).datatype # If output workspace is a folder add a .shp extension extension = '' if workspace_type == 'Folder': extension = '.shp' arcpy.SetProgressor('STEP', '', 0, len(unique_values), 1) outputs = list() for i in unique_values: output = create_name(out_workspace, i, extension) expression = u' AND '.join([ create_expression(in_data, j[0], j[1]) for j in izip(fields, i)]) select(datatype, in_data, output, expression) values_string = u', '.join([unicode(v) for v in i]) arcpy.AddIDMessage('INFORMATIVE', 86245, values_string, output) outputs.append(output) arcpy.SetProgressorPosition() return outputs if __name__ == '__main__': in_data = arcpy.GetParameterAsText(0) out_workspace = arcpy.GetParameterAsText(1) fields = [i.value for i in arcpy.GetParameter(2)] try: split_by_atts(in_data, out_workspace, fields) arcpy.SetParameterAsText(3, out_workspace) except Exception as err: arcpy.AddError(str(err)) sys.exit(1)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2015-09-18 arcgis安装路径的获得
2015-09-18 Add map surrounds using the SymbologyControl
2015-09-18 ArcGis实现画矩形(RectangleFeedBack)
2012-09-18 公共卫生研究院派员参加“GIS数据处理与建模”高级培训班