import arcpy
import os
import types def convertPoints():
arcpy.env.overwriteOutput = True inPts = arcpy.GetParameterAsText(0) # Input point FC
outFeatures = arcpy.GetParameterAsText(1) # Output FC
IDField = arcpy.GetParameterAsText(2) # Feature Field
sortField = arcpy.GetParameterAsText(3) # Sort Field
closeLine = arcpy.GetParameterAsText(4) # Close Line or Leave Open if IDField in ["", "#"]: IDField = None if sortField in ["", "#"]:
cursorSort = IDField
else:
if IDField:
cursorSort = IDField + ";" + sortField
else:
cursorSort = sortField if not isinstance(closeLine, types.BooleanType):
if closeLine.lower() == "false":
close = False
else:
close = True convertPointsToLine(inPts, outFeatures, IDField, cursorSort, close) def getZM(propType, hasMZ):
envValue = getattr(arcpy.env, propType).upper() if envValue in ['ENABLED', 'DISABLED']:
return envValue
else:
if hasMZ:
return "ENABLED"
else:
return "DISABLED" def convertPointsToLine(inPts, outFeatures, IDField, cursorSort, close):
try:
# Assign empty values to cursor and row objects
iCur, sRow, feat = None, None, None desc = arcpy.Describe(inPts)
shapeName = desc.shapeFieldName # Create the output feature class
outPath, outFC = os.path.split(outFeatures)
arcpy.CreateFeatureclass_management(outPath, outFC, "POLYLINE", "",
getZM("outputMFlag", desc.hasM),
getZM("outputZFlag", desc.hasZ),
inPts) outShapeName = arcpy.Describe(outFeatures).shapeFieldName # If there is an IDField, add the equivalent to the output
if IDField:
f = arcpy.ListFields(inPts, IDField)[0]
fName = arcpy.ValidateFieldName(f.name, outPath)
arcpy.AddField_management(outFeatures, fName, f.type, f.precision, f.scale, f.length,
f.aliasName, f.isNullable, f.required, f.domain) # Open an insert cursor for the new feature class
iCur = arcpy.InsertCursor(outFeatures) # Create an array needed to create features
array = arcpy.Array() # Initialize a variable for keeping track of a feature's ID.
ID = -1
fields = shapeName
if cursorSort:
fields += ";" + cursorSort for sRow in arcpy.gp.SearchCursor(inPts, "", None, fields, cursorSort, arcpy.env.extent):
pt = sRow.getValue(shapeName).getPart(0)
if IDField:
currentValue = sRow.getValue(IDField)
else:
currentValue = None if ID == -1:
ID = currentValue if ID <> currentValue:
if array.count >= 2: # To close, add first point to the end
#
if close:
array.add(array.getObject(0)) feat = iCur.newRow()
if IDField:
if ID: #in case the value is None/Null
feat.setValue(IDField, ID)
feat.setValue(outShapeName, array)
iCur.insertRow(feat)
else:
arcpy.AddIDMessage("WARNING", 1059, unicode(ID)) array.removeAll() array.add(pt)
ID = currentValue # Add the last feature
if array.count > 1:
# To close, add first point to the end
if close:
array.add(array.getObject(0)) feat = iCur.newRow()
if IDField:
if ID: #in case the value is None/Null
feat.setValue(IDField, currentValue)
feat.setValue(outShapeName, array)
iCur.insertRow(feat)
else:
arcpy.AddIDMessage("WARNING", 1059, unicode(ID))
array.removeAll() except Exception as err:
import traceback
arcpy.AddError(
traceback.format_exception_only(type(err), err)[0].rstrip()) finally:
if iCur:
del iCur
if sRow:
del sRow
if feat:
del feat try:
# Update the spatial index(es)
#
r = arcpy.CalculateDefaultGridIndex_management(outFeatures)
arcpy.AddSpatialIndex_management(outFeatures, r.getOutput(0), r.getOutput(1), r.getOutput(2))
except:
pass if __name__ == '__main__':
convertPoints()

点集转线python最优代码的更多相关文章

  1. python 常忘代码查询 和autohotkey补括号脚本和一些笔记和面试常见问题

    笔试一些注意点: --,23点43 今天做的京东笔试题目: 编程题目一定要先写变量取None的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...

  2. <转>机器学习系列(9)_机器学习算法一览(附Python和R代码)

    转自http://blog.csdn.net/han_xiaoyang/article/details/51191386 – 谷歌的无人车和机器人得到了很多关注,但我们真正的未来却在于能够使电脑变得更 ...

  3. 机器学习算法的基本知识(使用Python和R代码)

    本篇文章是原文的译文,然后自己对其中做了一些修改和添加内容(随机森林和降维算法).文章简洁地介绍了机器学习的主要算法和一些伪代码,对于初学者有很大帮助,是一篇不错的总结文章,后期可以通过文中提到的算法 ...

  4. Python 坑爹之 代码缩进

    建议:统一使用空格!!!!!!!!!不要Tab Python代码缩进   这两天python-cn邮件列表有一条thread发展的特别长,题目是<python的代码缩进真是坑爹>(地址), ...

  5. Python第一行代码

    Python版本:Python 3.6.1 0x01 命令行交互 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计 ...

  6. python的PEP8 代码风格指南

    PEP8 代码风格指南 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 知识点 代码排版 字符串引号 表达式和语句中的空格 注释 版本 ...

  7. 用python处理html代码的转义与还原

    用python处理html代码的转义与还原   转义 escape: import cgi s = cgi.escape("""& < >" ...

  8. 【转】利用Boost.Python将C++代码封装为Python模块

    用Boost.Python将C++代码封装为Python模块 一.     基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...

  9. python爬虫小说代码,可用的

    python爬虫小说代码,可用的,以笔趣阁为例子,python3.6以上,可用 作者的QQ:342290433,汉唐自远工程师 import requests import refrom lxml i ...

随机推荐

  1. Introduction to MWB Minor Mode

    Introduction to MWB Minor Mode */--> Table of Contents 1. Introduction 2. Usage 1 Introduction MW ...

  2. 经典面试题:n个数字(0,1,…,n-1)形成一个圆圈

    题目: n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始, 每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字). 当一个数字删除后,从被删除数字的下一个继续删 ...

  3. MEF实现设计上的“松耦合”(三)

    1.面向接口编程:有一定编程经验的博友应该都熟悉或者了解这种编程思想,层和层之间通过接口依赖,下层不是直接给上层提供服务,而是定义一组接口供上层调用.至于具体的业务实现,那是开发中需要做的事情,在项目 ...

  4. 【Sequel Pro】下载查询结果乱码问题处理方式

    1.下载查询结果已CSV格式保存 2.出现乱码问题样式如下截图: 3. 右键下载的CSV文件,选择用文本编辑打开 4.全选打开的页面内容,重新新建“文本编辑”并进行“储存” 5.打开 1.CSV,则看 ...

  5. ABP zero 3.2 发布

    v3.2.0 (2017-03-07) Common Tenant based UI customizations (allow tenants to upload custom CSS and lo ...

  6. Java学习(异常类)

    一.什么是异常: 异常就是在运行时产生的问题.通常用Exception描述. 在java中,把异常封装成了一个类,当出现问题时,就会创建异常类对象并抛出异常相关的信息(如详细信息,名称以及异常所处的位 ...

  7. TCP、UDP、HTTP、SOCKET之间的区别与联系

    IP:网络层协议: TCP和UDP:传输层协议: HTTP:应用层协议: SOCKET:TCP/IP网络的API. TCP/IP代表传输控制协议/网际协议,指的是一系列协议. TCP和UDP使用IP协 ...

  8. 20169211《Linux内核原理与分析》课程总结

    第一周作业:linux入门学习:熟悉操作linux的基础命令 第二周作业:实验反汇编一个简答的C程序,学习汇编代码的工作过程 第三周作业:学习linux内核的启动过程 第四周作业:学习linux内核进 ...

  9. 历史文章分类汇总-Anaconda安装第三方包(whl文件)

    本文主要是对公众号之前发布的文章进行分类整理,方面大家查阅,以后会不定期对文章汇总进行更新与发布.   一.推荐阅读: Anaconda安装第三方包(whl文件) 福布斯系列之数据分析思路篇 福布斯系 ...

  10. Eclipse插件安装出现Duplicate location错误

    一.原因 1.曾今安装过此插件 2.曾今安装此插件的时候出现错误 二.解决方法[eclipse] - Help - Install new software - Available Software ...