Looking to find Distance along a line(road) by a user input(Mouse Click). Here is my python code TESTED and works

def onMouseDownMap(self, x, y, button, shift):
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
pointGeom = arcpy.PointGeometry(arcpy.Point(x, y), mxd.activeDataFrame.spatialReference)
road_layer = arcpy.mapping.ListLayers(mxd, "narr", df)[0]
arcpy.SelectLayerByLocation_management(road_layer, "WITHIN_A_DISTANCE", pointGeom, "50 FEET")
output_layer = arcpy.CopyFeatures_management(road_layer, arcpy.Geometry())
dist_along_road = output_layer[0].measureOnLine(pointGeom)
pythonaddins.MessageBox(dist_along_road / 5280, "Address", 1)

  • 1
    what are the errors? – Midavalo Feb 12 '16 at 20:54
  • MeasureOnLine function can not be called against the layer – Gene Wyllson Feb 12 '16 at 22:47
  • is Measures enabled for your feature class? – Midavalo Feb 12 '16 at 22:51
  • I need some sample code for the "measureOnLine" function – Gene Wyllson Feb 16 '16 at 20:11
  • These code might be closer. The error now is "object has no attribute 'measureOnLine'" – Gene Wyllson Feb 16 '16 at 22:41

First of all does your output_layer actually contain any geometries? I ask this as your select by location is set to INTERSECT. No one can ever click on a line, so may be change that to closest or create a buffer around your point and use that as the selecting geometry.

Secondly measureOnLine is a method for Polyline geometry, your code is trying to run that on your cursor cursorRoad, you need to get the row from the cursor then the geometry. Suggest you read this page and study the code.

  • Thanks for your help Hornbydd.. I have posted the working code... Now I can convert this arcpy to pyQGIS – Gene Wyllson Feb 17 '16 at 0:37

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.