0

I have been struggling to figure out how to build a Python script with the following utility using ArcPy for ArcGIS Pro: Taking two user-supplied point feature classes (A and B) and creating a third, polyline feature class C, that contains the lines connecting each point in feature class A to the nearest point in feature class B, while also recording the distance between the points in the layers’ properties table.

I am new to Python programming in this context.

5
  • Hi, and welcome. Do you have any code at all you can share? If so, please paste it between two triple ticks like this:code
    – auslander
    Commented Feb 24, 2020 at 17:31
  • Hi there auslander, thank you very much for your readiness to help. I don't even manage to figure out how to even compose the code, not list compose it. Most of my prior work has focused on manipulating existing layers of information
    – B. Emental
    Commented Feb 24, 2020 at 17:35
  • The code itself is not hard and honestly could be done in ModelBuilder probably more quickly. But the problem needs to be more clearly expressed - would this be a potential one-to-many relationship? Could for example many points in FCA be connected to the same single point in FCB?
    – auslander
    Commented Feb 24, 2020 at 17:41
  • There are no restrictions on multiple lines using the same point in FCB
    – B. Emental
    Commented Feb 24, 2020 at 17:45
  • Add geometry attributes xy to A. Apply Near with location. Use Xy to line on A table which will have 2 xy pairs.
    – FelixIP
    Commented Feb 24, 2020 at 19:37

1 Answer 1

1

Here's some very rough code that obtained results for me that sound like what you may be looking for. In pseudo code what I am doing is:

  1. set up the environment variables
  2. identify the two feature classes
  3. set up the Near analysis parameters
  4. write the starting and ending x and y to a csv file
  5. use that csv file as input to the XY To Line tool

This assumes a GCS; if you are using local projection/datum you will want to use the PLANAR method in your Near analysis.

import arcpy
import csv
import os

# set up environment
arcpy.env.workspace = "path to your geodatabase or folder"
arcpy.env.overwriteOutput = True
sr = arcpy.SpatialReference(4326)  # WGS84, change as necessary

# get feature classes
fca = "points_a"  # first point feature class
fcb = "points_b"  # second point feature class

# set up Near analysis parameters
location = "LOCATION"
angle = "NO_ANGLE"
method = "GEODESIC"

near_result = arcpy.Near_analysis(fca, fcb, "#", location, angle, method)

# set up csv output
csv_out_file = "to_lines.csv"
csv_out_dir = "C:/Temp"
csv_out = os.path.join(csv_out_dir, csv_out_file)

# prep fields from Near output
fields = ["SHAPE@XY", "NEAR_X", "NEAR_Y"]

# now write the csv
with open(csv_out, "w", newline='') as output:
    c = csv.writer(output)
    c.writerow(["startx", "starty", "endx", "endy"])  # header row
    for r in arcpy.da.SearchCursor(fca, fields):  # read the geoms from Near output
        sx, sy = r[0]
        ex, ey = r[1], r[2]
        row = [sx, sy, ex, ey]
        c.writerow(row)

outfc = "closest_lines"  # name of new line feature class

arcpy.XYToLine_management(csv_out, outfc, "startx", "starty", "endx", "endy", spatial_reference=sr)

One thing Esri is well-known for is their extremely thorough documentation. I encourage you not to just copy-paste what I've given you here, but also read up on the documentation for both the Near tool, the XY to Line tool, and the da.SearchCursor class. Good luck!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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