How to connect points with lines using Python?

10476
3
03-05-2018 12:01 PM
YuriAndrade
New Contributor

Hello Esri Community,

I have a bunch of points close to each other in a sequence, and I would like to create a line from point to point, connecting all of them.

After that I would like to "densify" the number of points on the whole line created. For example, I have a sequence of 3 points, but after linking all of them with a line, I want to insert a new point on that line between each original point, making it a line with 5 points (on that case).

Is there any way I could do that using Python code on ArcMap? Thank you in advance.

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Points to line... for the points to line

Points along line...  one alternative to densifry

Densify by Factor.... another alternative if you have PRO or want to repurpose some of the code

RandyBurton
MVP Alum

After you run a tool  (like Points to line) in the ArcMap and get results you like, click on Geoprocessing >> Results and you should see the results of that tool.  When you right click, you can "Copy as Python Snippet".  This will show you the parameters used when the tool was run, and makes it easy to paste into a Python script.

XanderBakker
Esri Esteemed Contributor

In addition to what has been mentioned above, see the snippet below:

def main():
    import arcpy

    # inputs (will normally be extracted from existign featureclass or table)
    sr = arcpy.SpatialReference(4326)
    pnts = [arcpy.Point(1, 2), arcpy.Point(3, 4), arcpy.Point(5, 6)]

    # Modify the list and add intermediate points
    pnts2 = []
    for i in range(len(pnts)-1):
        pnt1 = pnts[i]
        pnt2 = pnts[i+1]
        pnt_mid = GetMidPoint(pnt1, pnt2)
        pnts2.append(pnt1)
        pnts2.append(pnt_mid)

    # add last point and print result
    pnts2.append(pnt2)
    print pnts2

    # create polyline from original list and report length of polyline
    polyline1 = arcpy.Polyline(arcpy.Array(pnts), sr)
    print polyline1.length

    # create polyline from result list and report lenth of polyline
    polyline2 = arcpy.Polyline(arcpy.Array(pnts2), sr)
    print polyline2.length


def GetMidPoint(pnt1, pnt2):
    return arcpy.Point((pnt1.X + pnt2.X)/2.0, (pnt1.Y + pnt2.Y)/2.0)

if __name__ == '__main__':
    main()

... which will yield:

[<Point (1.0, 2.0, #, #)>, <Point (2.0, 3.0, #, #)>, <Point (3.0, 4.0, #, #)>, <Point (4.0, 5.0, #, #)>, <Point (5.0, 6.0, #, #)>]
5.65685424949
5.65685424949

As you can see additional points are added and the length of the polyline remains the same.