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)
Arcgis10.5 python按属性分割图层,属性相同分为一个图层的更多相关文章
- python中string模块各属性以及函数的用法
任何语言都离不开字符,那就会涉及对字符的操作,尤其是脚本语言更是频繁,不管是生产环境还是面试考验都要面对字符串的操作. python的字符串操作通过2部分的方法函数基本上就可以解决所有的字符串 ...
- 牛人总结python中string模块各属性以及函数的用法,果断转了,好东西
http://blog.chinaunix.net/uid-25992400-id-3283846.html http://blog.csdn.net/xiaoxiaoniaoer1/article/ ...
- python中类的三种属性
python中的类有三种属性:字段.方法.特性 字段又分为动态字段和静态字段 类 class Province: #静态字段 memo = 'listen' #动态字段 def __init__(se ...
- 【Python】[面性对象编程] 获取对象信息,实例属性和类属性
获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- python动态获取对象的属性和方法 (转载)
首先通过一个例子来看一下本文中可能用到的对象和相关概念. #coding:utf-8 import sys def foo():pass class Cat(object): def __init__ ...
- python基础——实例属性和类属性
python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...
- Python类属性,实例属性
1.Python类数据属性:定义在类里面但在函数外面的变量,它们都是静态的. #一段很简单的代码,但反应了很多 >>> class A(): a=1 #一个类里面有个属性a > ...
- python动态获取对象的属性和方法
http://blog.csdn.net/kenkywu/article/details/6822220首先通过一个例子来看一下本文中可能用到的对象和相关概念.01 #coding: UTF- ...
随机推荐
- 浅谈C、C++及其区别、兼容与不兼容
一.闲说C C语言之所以命名为C,是因为C语言源自Ken Thompson发明的B语言,而 B语言则源自BCPL语言. 1967年,剑桥大学的Martin Richards对CPL语言进行了简化,于是 ...
- day1作业二:多级菜单操作
作业二:多级菜单 (1)三级菜单 (2)可以次选择进入各子菜单 (3)所需新知识点:列表.字典 要求:输入back返回上一层,输入quit退出整个程序 思路: (1)首先定义好三级菜单字典: (2)提 ...
- python使用cookie登陆网页
python2: Python 爬虫入门六之 Cookie 的使用 python3: Python3 网络爬虫 (六):Python3 使用 Cookie - 模拟登陆获取妹子联系方式 python ...
- MapReduce的原理及执行过程
MapReduce简介 MapReduce是一种分布式计算模型,是Google提出的,主要用于搜索领域,解决海量数据的计算问题. MR有两个阶段组成:Map和Reduce,用户只需实现map()和re ...
- network出错
1.更改IP之后,执行service network restart时出现 shutting down interface eth0:Device state :3(disconnected)的问题时 ...
- C#基础语法补充
[学习笔记]前接:https://www.cnblogs.com/aland-1415/p/7360509.html 一.异常处理 1.格式 try{ } catch{ } catch{ } ... ...
- Python爬虫个人记录(二) 获取fishc 课件下载链接
参考: Python爬虫个人记录(一)豆瓣250 (2017.9.6更新,通过cookie模拟登陆方法,已成功实现下载文件功能!!) 一.目的分析 获取http://bbs.fishc.com/for ...
- 线程池--ThreadPoolExecutor
线程池的实现原理 1)如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(注意,执行这一步骤 需要获取全局锁). 2)如果运行的线程等于或多于corePoolSize,则将任务加入 ...
- HTTP/FTP压力测试工具siege
HTTP/FTP压力测试工具siege 压力测试可以检测服务器的承载能力.针对HTTP和FTP服务,Kali Linux提供专项工具siege.该工具可以模拟多个用户同时访问同一个网站的多个网页, ...
- Django快速创建博客,包含了整个框架使用过程,简单易懂
创建工程 ...