# 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)

Arcgis10.5 python按属性分割图层,属性相同分为一个图层的更多相关文章

  1. python中string模块各属性以及函数的用法

    任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作.     python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...

  2. 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西

    http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...

  3. python中类的三种属性

    python中的类有三种属性:字段.方法.特性 字段又分为动态字段和静态字段 类 class Province: #静态字段 memo = 'listen' #动态字段 def __init__(se ...

  4. 【Python】[面性对象编程] 获取对象信息,实例属性和类属性

    获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...

  5. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解

     1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...

  6. python动态获取对象的属性和方法 (转载)

    首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): def __init__ ...

  7. python基础——实例属性和类属性

    python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...

  8. Python类属性,实例属性

    1.Python类数据属性:定义在类里面但在函数外面的变量,它们都是静态的. #一段很简单的代码,但反应了很多 >>> class A(): a=1 #一个类里面有个属性a > ...

  9. python动态获取对象的属性和方法

    http://blog.csdn.net/kenkywu/article/details/6822220首先通过一个例子来看一下本文中可能用到的对象和相关概念.01     #coding: UTF- ...

随机推荐

  1. MySQL学习笔记:字符串前后补全0

    遇到一个需求:不足6位的需要自动补全6位,使用函数LPAD()和RPAD()补全. LPAD(str, len, padstr) 用字符串padstr对str进行左边填充补全直至它的长度达到len个字 ...

  2. MySQL学习笔记:生成一个时间序列

    今天遇到一个需求是生成以下表格的数据,一整天24小时,每秒一行数据. 寻找颇旧,找到另外两个实现的例子,暂且学习一翻.另一个见另外一篇. DAY) AS DATE FROM ( ) AS tmp, ( ...

  3. MySQL学习笔记:limit

    MySQL支持用limit语句来设置查询的行数范围,同Hive. LIMIT a, b; a:从a+1开始: b:取b行数据: 1.取出第一行记录: ,; 2.取出第二行记录: ,; 3.取出从11开 ...

  4. MySQL学习笔记:case when

    一.MySQL case when的三种用法: 1.case 字段 when, 字段的具体值: select a.*, case sex when '1' then '男' else '女' end ...

  5. 树莓派3B更换源为阿里源

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #科大源 sudo nano /etc/apt/sources.list deb htt ...

  6. Hive(八)Hive的Shell操作与压缩存储

    一.Hive的命令行 1.Hive支持的一些命令 Command Description quit Use quit or exit to leave the interactive shell. s ...

  7. LogStash plugins-inputs-file介绍(三)

    官方文档 https://www.elastic.co/guide/en/logstash/current/plugins-inputs-file.html 重要参数: path # 文件路径 sin ...

  8. customPage.class.php可添加js事件的分页类

    用于ajax动态加载数据的分页类,分页事件可以动态添加,去除了a链接中的href地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  9. thinkphp3.2路由美化,url简化

    thinkphp的路由功能很实用也很强大,可以简化url,有强大的正则匹配,可以做成任何想要的url样式. 在前台的config.php配置文件中: 1.首先开启路由 1 'URL_ROUTER_ON ...

  10. 【原创】获取MySQL crash 时的core file

    最近有台服务器的MySQL经常crash,为了进一步定位问题,开启了mysql core file功能,开启步骤如下,供参考 [开启步骤] 1. my.cnf文件中增加2个配置选项 [mysqld] ...