点集转线python最优代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 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() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理