Skip to main content
added 1 character in body
Source Link
Emil Brundage
  • 13.9k
  • 3
  • 27
  • 64

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line hashave a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line have a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

edited body
Source Link
Emil Brundage
  • 13.9k
  • 3
  • 27
  • 64

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry  ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description hereenter image description here

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry  ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

added 9 characters in body
Source Link
Emil Brundage
  • 13.9k
  • 3
  • 27
  • 64

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry  ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry  ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

For this task, you'll want to create a dictionary of line numbers as the dictionary's key and point XYs as their value. Then create your new line feature class with the same spatial reference as your point feature class. Iterate through your dictionary, sort your XYs, and then create a polyline geometry object from your sorted points.

This method assumes that all points that should be connected in a single line has a shared ID value.

Given:

enter image description here

Try this script:

import os
import arcpy

#point feature class
pntFc = r"C:\Path\To\PntFc"
#id field
idFld = "Line"
#line feature class
lineFc = r"Path\To\Output\LineFc"

#create point dictionary
di = {}
with arcpy.da.SearchCursor (pntFc, [idFld, "SHAPE@XY"]) as curs:
     for line, xy in curs:
         try: di [line] += [xy]
         except KeyError: di [line] = [xy]

#create new line feature class
sr = arcpy.Describe (pntFc).spatialReference
linePath, lineName = os.path.split (lineFc)
arcpy.CreateFeatureclass_management (linePath, lineName, "POLYLINE", spatial_reference = sr)

#add line ID field to line feature class
fldType = [f.type for f in arcpy.ListFields (pntFc) if f.name == idFld][0]
arcpy.AddField_management (lineFc, idFld, fldType)

#insert cursor
with arcpy.da.InsertCursor (lineFc, [idFld, "SHAPE@"]) as iCurs:
    #iterate dictionary
    for lineId in di:
        #get point xys
        points = di [lineId]
        #create array
        array = arcpy.Array ()
        for x, y in sorted (points):
            array.add (arcpy.Point (x, y))
        #create geometry
        geom = arcpy.Geometry  ("polyline", array, sr)
        #insert row
        row = (lineId, geom)
        iCurs.insertRow (row)
        

Result:

enter image description here

Source Link
Emil Brundage
  • 13.9k
  • 3
  • 27
  • 64
Loading