1

I am a novice when it comes to python. I am working on creating a new field that has the distance between two sets of x & y values.

I've joined two tables so I have each of their x y values in the same table. I am using field calculator in ArcMap trying to format this equation and for the life of me cannot figure it out. I'm not using the codeblock, is this necessary? I have been attempting to get the results to compute for days. I've gotten a few different errors. I'm wondering if its an error stemming from the function I'm using I've looked through the forums and can't find an answer.

So far what I have is:
This is the expression I am attempting to use:

math.sqrt(((!x2!-!x1!)**2)-((!y2!-!y1!)**2)))

enter image description here

5
  • 2
    Your title says you're using VBScript, but the code says Python. I'd use a Python function, define dx and dy, then use return math.sqrt(dx*dx + dy*dy)
    – Vince
    Apr 7, 2023 at 17:40
  • Hey Vince, my mistake. I am using python. I'll give that a shot!
    – JTstone
    Apr 7, 2023 at 17:42
  • Use "join field" instead of "add join" and math.hypot(x1-x2, y2-y1). This will make expression easier to type, less errors
    – FelixIP
    Apr 7, 2023 at 20:36
  • I greatly appreciate all of your input. I am still struggling. When using field calculator, should I be defining these variables in the pre-logic script code? Or should I be able to just plug in the fields as necessary in the expression? Thanks again
    – JTstone
    Apr 10, 2023 at 18:40
  • 1
    No need in a block. Just replace x1,x2... by relevant !point_x!, !point_x1!...
    – FelixIP
    Apr 10, 2023 at 20:31

1 Answer 1

2

Inputs:

enter image description here

arcpy.JoinField_management(in_data="SET_A", in_field="J_FIELD", join_table="SET_B", join_field="J_FIELD", fields="POINT_X;POINT_Y")
arcpy.AddField_management(in_table="SET_A", field_name="DISTANCE", field_type="DOUBLE", field_precision="", field_scale="", field_length="", field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")

enter image description here

enter image description here

enter image description here

Control:

arcpy.XYToLine_management(in_table="SET_A", out_featureclass="in_memory/LINES", startx_field="POINT_X", starty_field="POINT_Y", endx_field="POINT_X_1", endy_field="POINT_Y_1", line_type="GEODESIC", id_field="J_FIELD", spatial_reference="PROJCS['NZGD_2000_New_Zealand_Transverse_Mercator',GEOGCS['GCS_NZGD_2000',DATUM['D_NZGD_2000',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',1600000.0],PARAMETER['False_Northing',10000000.0],PARAMETER['Central_Meridian',173.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]];-4020900 1900 1000;-100000 10000;-100000 10000;0.002;0.001;0.001;IsHighPrecision")

enter image description here

3
  • Thank you!!! I appreciate everyone's input. This worked for me. Just for clarification, could this have been done the original way I was working on it? By using the square root function?
    – JTstone
    Apr 11, 2023 at 15:41
  • 1
    Yes, because square root and hypot are identical functions.
    – FelixIP
    Apr 11, 2023 at 19:42
  • Thanks so much for your help!
    – JTstone
    Apr 12, 2023 at 18:50

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.