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- ...
随机推荐
- (转载)solr实现满足指定距离范围条件的搜索
配置schema.xml <?xml version="1.0" encoding="UTF-8" ?> <schema name=" ...
- Spring Boot使用JavaMailSender发送邮件
http://www.cnblogs.com/wxc-xiaohuang/p/9532631.html https://blog.csdn.net/icannotdebug/article/detai ...
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- HBase(三)HBase架构与工作原理
一.系统架构 注意:应该是每一个 RegionServer 就只有一个 HLog,而不是一个 Region 有一个 HLog. 从HBase的架构图上可以看出,HBase中的组件包括Client.Zo ...
- LoadRunner11-遇到问题及解决办法(汇总)
LoadRunner11-遇到问题及解决办法 1.LoadRunner超时错误:在录制Web服务器端,如果超过120秒服务器协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同.错 ...
- 决策树 (decision tree)
内容学习于 ApacheCN github 定义: 分类决策树模型是一种描述对实例进行分类的树形结构.决策树由结点(node)和有向边(directed edge)组成.结点有两种类型:内部结点(in ...
- Java 集合之 Map
Map 就是另一个顶级接口了,总感觉 Map 是 Collection 的子接口呢.Map 主要用于表示那些含有映射关系的数据,存储的是一组一组的键值对.Map 是允许你将某些对象与其它一些对象关联起 ...
- 整理之DOM事件阶段、冒泡与捕获、事件委托、ie事件和dom模型事件、鼠标事件
整理之DOM事件阶段 本文主要解决的问题: 事件流 DOM事件流的三个阶段 先理解流的概念 在现今的JavaScript中随处可见.比如说React中的单向数据流,Node中的流,又或是今天本文所讲的 ...
- PHP 二维数组根据某个字段排序 复制代码 array_multisort
//二维数组,按照里面的age从大到小降序,代码如下 <?php header('Content-Type:text/html;Charset=utf-8'); $arrUsers = arra ...
- 汉化 的 空指针 bug
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha nulljava.lang.NullPointerException at com.an ...