# 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学习笔记:upper、lower、ucase、lacase——字符串函数

    在MySQL中,通过利用upper.lower.ucase.lacase几个函数对字符串进行大小写转换. upper(str)——根据当前字符集映射返回字符串str,并将所有字符更改为大写.默认值是l ...

  2. day6 xml文件格式的处理

        XML处理模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公 ...

  3. HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException

    在使用Maven工程管理工具整合SSM框架时,Mybatis使用逆向工程生成的pojo,mapper接口及映射文件,把mapper接口和映射文件放在DAO工程的同一级src/main/java目录下. ...

  4. 关于ecshop的mobile里user.php登录和注册验证码不显示

    在做ecshop模板的时候由于user.php里的登录和注册是在一个页面里切换的,这就致使这里的登录和注册里的验证码不显示 找到mobile/themesmobile/ecshoptemplate_m ...

  5. js判断一个字符串是否是数字

    function isNumber(val) { var regPos = /^\d+(\.\d+)?$/; //非负浮点数 var regNeg = /^(-(([0-9]+\.[0-9]*[1-9 ...

  6. ref:下一个项目为什么要用 SLF4J

    ref:http://blog.mayongfa.cn/267.html 阿里巴巴 Java 开发手册 前几天阿里巴巴在云栖社区首次公开阿里官方Java代码规范标准,就是一个PDF手册,有命名规范,让 ...

  7. 洛谷P2525 Uim的情人节礼物·其之壱 [康托展开]

    题目传送门 Uim的情人节礼物·其之壱 题目描述 情人节到了,Uim打算给他的后宫们准备情人节礼物.UIm一共有N(1<=N<=9)个后宫妹子(现充去死 挫骨扬灰!). 为了维护他的后宫的 ...

  8. Java—集合工具类

    集合中的元素工具类排序: Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对 ...

  9. Docker应用系列(六)| 如何去掉sudo及避免权限问题

    一.如何在使用docker时去掉sudo 1.添加账户 $ sudo groupadd docker 2.授权给docker账户 sudo gpasswd -a yourname docker 3.重 ...

  10. 【基本功】深入剖析Swift性能优化

    简介 2014年,苹果公司在WWDC上发布Swift这一新的编程语言.经过几年的发展,Swift已经成为iOS开发语言的“中流砥柱”,Swift提供了非常灵活的高级别特性,例如协议.闭包.泛型等,并且 ...