使用ArcPy从点对坐标创建线?


11

我有一些点对坐标(起点和终点),必须将它们转换为线。到现在为止,我曾经在这两个坐标的追加pippo.Point(),一个pippo.CalculateGeometry()定义每个PIONT的几何形状,并pippo.append(defined geometry)找出一对点,然后PointsToLine获得我行。对于数百行而言,这是相当昂贵的时间。

有更短的方法吗?

例如,将每条线的起点和终点放置在单个表的不同字段中,然后直接导入线而无需传递点几何。

Answers:


8

这将读取一个看起来像这样的表(在这种情况下为Excel工作表,但可以是任何表类型):

在此处输入图片说明

S_X是起点X点,E_X是终点X点,与Y相同。我们遍历输入表,然后对于每一行,将开始/结束X / Y设置为一个点,将该点添加到数组中,然后从两个点的数组中创建一条折线。然后插入要素类。冲洗并重复。

import arcpy

in_rows = arcpy.SearchCursor(r"D:\Temp\Lines.xls\Sheet1$")

point = arcpy.Point()
array = arcpy.Array()

featureList = []
cursor = arcpy.InsertCursor(r"D:\Temp\Lines.shp")
feat = cursor.newRow()

for in_row in in_rows:
    # Set X and Y for start and end points
    point.X = in_row.S_X
    point.Y = in_row.S_Y
    array.add(point)
    point.X = in_row.E_X
    point.Y = in_row.E_Y
    array.add(point)   
    # Create a Polyline object based on the array of points
    polyline = arcpy.Polyline(array)
    # Clear the array for future use
    array.removeAll()
    # Append to the list of Polyline objects
    featureList.append(polyline)
    # Insert the feature
    feat.shape = polyline
    cursor.insertRow(feat)
del feat
del cursor

然后你得到了台词:

在此处输入图片说明


谢谢,我将尝试估计分析的持续时间..这正是我正在尝试做的事情:-)
Annalisa Minelli

对于线point.X = in_row.S_X,它返回一条错误,指出输入值不是数字。我试图将其设置为int或float或什至数值,但由于字段不是数字为Nonetype而无法正常工作。有什么帮助吗?
FedericoGómez'19

5

我上周创建了一个python脚本(尽管未使用ArcPy),该脚本采用的点是根据序列号字段(“ SEQ”)创建总线几何形状的(点shp)。您可以轻松地对其进行调整,以从同一要素的字段中获取坐标(使用字段值而不是几何图形)。

# -*- coding: utf-8 -*-
###############################################################################
from sys import argv
import osgeo.ogr
import os, os.path
###############################################################################

script, srcSHP = argv

#-- Open source shapefile
shapefile = osgeo.ogr.Open(srcSHP)
layer = shapefile.GetLayer(0)
spatialRef = layer.GetSpatialRef()

#-- Output directory
outDir = os.path.dirname(srcSHP)
outDirName = os.path.basename(outDir)

driver = osgeo.ogr.GetDriverByName("ESRI Shapefile")
outFile = driver.CreateDataSource(os.path.join(outDir,outDirName + "_lines.shp"))
outLayer = outFile.CreateLayer("layer", spatialRef)

#-- Adding fields to the output shapefile
fieldDef = osgeo.ogr.FieldDefn("line_no", osgeo.ogr.OFTString)
fieldDef.SetWidth(12)
outLayer.CreateField(fieldDef)

fieldDef = osgeo.ogr.FieldDefn("From_SEQ", osgeo.ogr.OFTReal)
outLayer.CreateField(fieldDef)

fieldDef = osgeo.ogr.FieldDefn("To_SEQ", osgeo.ogr.OFTReal)
outLayer.CreateField(fieldDef)

#-- Going through each feature, one by one
#-- The last point is the end of the line so I don't want to iterate through that one
for i in range(layer.GetFeatureCount()-1):
    lString = osgeo.ogr.Geometry(osgeo.ogr.wkbLineString)  

    feature1 = layer.GetFeature(i)
    feature2 = layer.GetFeature(i+1)

    # When it's a new line, the sequential number restart to 1, so we don't want that line
    if feature1.GetField("SEQ") < feature2.GetField("SEQ"):
        geom1 = feature1.GetGeometryRef()
        geom2 = feature2.GetGeometryRef()

        geom1x = geom1.GetX()
        geom1y = geom1.GetY()
        geom2x = geom2.GetX()
        geom2y = geom2.GetY()

        lString.AddPoint(geom1x, geom1y)
        lString.AddPoint(geom2x, geom2y)     # Adding the destination point

        #-- Adding the information from the source file to the output
        feat = osgeo.ogr.Feature(outLayer.GetLayerDefn())
        feat.SetGeometry(lString)
        feat.SetField("line_no", feature1.GetField("line_no"))
        feat.SetField("From_SEQ", feature1.GetField("SEQ"))
        feat.SetField("To_SEQ", feature2.GetField("SEQ"))
        outLayer.CreateFeature(feat)

print "The End"

每对点将创建一条直线。可能有一种更优雅的方法,但是它在大约15秒内创建了3900条线,因此对我有用。


谢谢,看起来就像是详尽的阐述..这对我来说真的很有用。我将尝试,然后提供反馈。现在谢谢。
Annalisa Minelli


1

这只是@ChadCooper答案的更新,因为“ da”游标现在可以方便地替换以前的游标:

with arcpy.da.SearchCursor(input_table,[orig_namefield,x1,y1,x2,y2] ) as in_rows:
    with arcpy.da.InsertCursor(output_lines,["SHAPE@",name_field]) as cursor:
        for row in in_rows:
            # build array for line segment
            array = arcpy.Array([arcpy.Point(row[1],row[2]),arcpy.Point(row[3],row[4])])
            # Create a Polyline object based on the array of points
            polyline = arcpy.Polyline(array)
            # Insert the feature
            cursor.insertRow([polyline,row[0]])
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.