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. LeetCode691. Stickers to Spell Word

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  2. CVE-2012-0003 Microsoft Windows Media Player ‘winmm.dll’ MIDI文件解析远程代码执行漏洞 分析

    [CNNVD]Microsoft Windows Media Player ‘winmm.dll’ MIDI文件解析远程代码执行漏洞(CNNVD-201201-110)    Microsoft Wi ...

  3. 关于Fuzz——peach的学习

    最近在搞漏洞挖掘,之前写过一个文件格式漏洞挖掘的博文,使用的是光刃牛写的Alpha Fuzz工具.感觉样本生成的质量不是很好,这次打算使用一下老牌的Fuzz工具peach.学长介绍了一下说peach的 ...

  4. 【转载】pygame的斜线运动

    pygame是用来写2D游戏的. 实现斜线运动,无非是在x和y两个方向上分运动的合成.x方向上的运动,当撞到边界的时候取相反速度就好了. 这里是用网球王子中的图片,以及一个网球实现,效果截图: 注意看 ...

  5. ssh连接报错Write failed: Broken pipe Resource temporarily unavailable

    问题描述 使用root连接服务器正常,切换普通用户连接报错 具体报错如下:Write failed: Broken pipe 或者:failed to execute /bin/bash: Resou ...

  6. Keras中RNN不定长输入的处理--padding and masking

    在使用RNN based model处理序列的应用中,如果使用并行运算batch sample,我们几乎一定会遇到变长序列的问题. 通常解决变长的方法主要是将过长的序列截断,将过短序列用0补齐到一个固 ...

  7. 【LOJ】#2244. 「NOI2014」起床困难综合症

    题解 写水题放松一下心情 二进制有个很好的性质是每一位是独立的,我们按位贪心,先看这一位能不能填1,然后看看如果这一位填0那么运算后最后这一位是不是1,是的话就退出,然后看看这一位如果填1最后是1这一 ...

  8. windows svn post-commit 报错解决 error resolving case

    在svn仓库目录下有个hooks目录,下面建一个 post-commit.cmd 文件,有代码提交到仓库,自动checkout到指定目录.   @echo onSET REPOS=%1SET USER ...

  9. 【工具】获取pojo类属性,并写入表格

    1.添加依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <g ...

  10. 第1天:Django框架简介与工程创建

    Django简介 Django特点 环境搭建 创建工程 启动服务 使用pycharm打开工程 创建子应用 Django简介 Django,是用Python语言写的开源web开发框架,并遵循MVC设计. ...