1

I would like to create in an automatic way a linear feature representing the connections among all points using ArcMap or Python.

That is I would like that each point is connected to the others one like the attached pic.

Linear connection among points


import arcpy
import os
arcpy.env.workspace = r"C:/test/Test.gdb"
fc_list = arcpy.ListFeatureClasses()
array = arcpy.Array()
pnt = arcpy.Point()
for record in fc_list:
    polyline = arcpy.Polyline(pnt)
arcpy.CreateFeatureclass_management("C:/test/Test.gdb", "linea", "POLYLINE")
print("finished")

The script finishes, but the "linea" created is empty. In the "Test.gdb" there is only a point feature class called "gps" which has 17 points that have to be connected each other.

  • 3
    What software API and language are you using to perform this? – Michael Stimson Jun 30 at 7:01
  • I have ArcMap and Python – Michele Papasidero Jun 30 at 8:11
  • This can be done by creating a list of all the point object, iterating frompoints to point cont - 1 then iterating topoints + 1 to point count creating a geometry joining each point to every other point. Read resources.arcgis.com/en/help/main/10.1/index.html#//… which has the fundamentals of creating geometry objects and resources.arcgis.com/en/help/main/10.1/index.html#//… about writing new features to the feature class. – Michael Stimson Jul 1 at 2:15
  • Dear Michael, my knowledge in python code is quite basic. Do you think that is it possible for you to write an example of this code? It will be great. – Michele Papasidero Jul 1 at 7:53
  • Have a read of resources.arcgis.com/en/help/main/10.2/index.html#//… about the construction of geometry objects from arcpy.Array of arcpy.Point(). I only provide specific advice on a defined problem as that is the site policy, as you have no code to start from I can't advise on fixing your code. I would urge you to read the linked pages, look closely at the examples and learn how python/arcpy works, that's how I learned. When you've got some code to start from then post your code and we can see if we can help. – Michael Stimson Jul 1 at 8:13
4

Based on your quesion and code I think you're after something like this:

import arcpy, os, itertools

arcpy.env.workspace = r"C:/test/Test.gdb"
# could be trouble if arcpy.env.overwriteOutput is False and the feature class already exists
arcpy.CreateFeatureclass_management("C:/test/Test.gdb", "linea", "POLYLINE")

# I assume that you want to process all the point feature classes in your workspace
fc_list   = arcpy.ListFeatureClasses(feature_type='Point')
AllPoints = []        # an empty list to store the points
for fc in fc_list:    # loop through each point feature class
    with arcpy.da.SearchCursor(fc,'SHAPE@') as sCur:
        for sRow in sCur:            # looping through each row in the table/feature class
            # add the shape of the point to the list, the object is a PointGeometry
            # but we need a Point object so we take the firstPoint from the PointGeometry
            AllPoints.append(sRow[0].firstPoint)  

# create a list of pairs to iterate so that there are no duplicates
ListOfNumbers = range(len(AllPoints))
UniquePairs   = list(itertools.combinations(ListOfNumbers,2))


# using an insert cursor to write the features
with arcpy.da.InsertCursor("C:/test/Test.gdb/linea","SHAPE@") as ICur:
    for UPair in UniquePairs:
      F = UPair[0]
      T = UPair[1]
      storeLine = arcpy.Polyline(arcpy.Array([AllPoints[F],AllPoints[T]]))
      ICur.insertRow([storeLine]) # insert cursor expects a list or tuple, even if it's one element

The first stage is to digest all the points in all the input point feature classes into a list using an arcpy.da.SearchCursor, the 'SHAPE@' keyword returns PointGeometry objects which aren't the same as Point objects which are what's needed in the array to create a polyline so I use the firstPoint property of the PointGeometry which is a point object.

After all the points are accounted for I use range to create an index for the list then using the answer of this SO post I generate a list of all the unique combinations that can be made with that list.

From there, I have the list of point geometries and unique combinations I then iterate the list and get the points that match the combination into an array then cast to a polyline object which is then inserted into the line feature class with an InsertCursor.

| improve this answer | |
  • May I ask you a mini-question, when you write comments do you perform it while writing your code or afterwards when your code is done and working? – Taras Jul 2 at 6:04
  • It would be unusual to test code, I don't have the required data. If there is an error when you try to run the code post a comment and I'll attempt to correct it. When I was studying programming the lecturer stated that the likelihood of a program working as intended was inversely proportional to the length of the code so I wouldn't be surprised in the least if my code contains errors. – Michael Stimson Jul 2 at 6:14
  • Your script works properly, thanks a lot Michael. I tried to add a code which select not all points, but a subset on the basis of a field value, but this code was deleted. – Michele Papasidero Jul 2 at 17:16
  • The place to add a query filter (where clause) is in the search cursor, the parameters are DataSource, fields, where_clause, however you must work out if the field exists first, you can do this with if len(arcpy.ListFields(fc,'Field_Name')) != 0: before issuing the with arcpy.da.SearchCursor. ListFields returns a list of field objects matching the wildcard, if none match an empty list is returned, using your field name as the wildcard it will return a list of 1 field if a matching name is found or a list of 0 objects if it's not found. – Michael Stimson Jul 2 at 23:57
0
import arcpy, os, itertools

arcpy.env.workspace = r"C:/test/Test.gdb"
whereclause = """{} = 1""".format(arcpy.AddFieldDelimiters("Export_Output", "ses_1"))
# could be trouble if arcpy.env.overwriteOutput is False and the feature class already 
exists
arcpy.CreateFeatureclass_management("C:/test/Test.gdb", "linea_1", "POLYLINE")

# I assume that you want to process all the point feature classes in your workspace
fc_list   = arcpy.ListFeatureClasses(feature_type='Point')
AllPoints = []        # an empty list to store the points
for fc in fc_list:    # loop through each point feature class
    with arcpy.da.SearchCursor("Export_Output", "ID", whereclause) as sCur:
        for sRow in sCur:            # looping through each row in the table/feature class
            # add the shape of the point to the list, the object is a PointGeometry
            # but we need a Point object so we take the firstPoint from the PointGeometry
            AllPoints.append(sRow[0].firstPoint)

# create a list of pairs to iterate so that there are no duplicates
ListOfNumbers = range(len(AllPoints))
UniquePairs   = list(itertools.combinations(ListOfNumbers,2))


# using an insert cursor to write the features
with arcpy.da.InsertCursor("C:/test/Test.gdb/linea","SHAPE@") as ICur:
    for UPair in UniquePairs:
      F = UPair[0]
      T = UPair[1]
      storeLine = arcpy.Polyline(arcpy.Array([AllPoints[F],AllPoints[T]]))
      ICur.insertRow([storeLine]) # insert cursor expects a list or tuple, even if it's one element

I tried, but there is an error: please see the pic attached enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.