2

I am working with the below code for ArcGIS and what I am guessing is a simple error I am making. The aim is the find the tangent to the midpoint of several line segments and then plot a line perpendicular to these points. The final step of plotting the new point the join the line to is giving me the error 'Point' object has no attribute 'point'. I understand what the error is saying but am having trouble fixing it.

from math import atan2, pi
import arcpy as arc
input_shp = 'C:/Users/agood/OneDrive - USN/Python/Paper 1 Analysis/Python/Meltwater Tran_SplitLineAtPo.shp'

e = 1e-10 #epsilon

Cursor = arcpy.SearchCursor(input_shp)
for feature in Cursor:
    Midpoint = feature.shape.positionAlongLine(0.5,True).firstPoint #find midpoint along each line segment
   
    
    #getpoints immediately before and after midpoint
    before = feature.shape.positionAlongLine(0.5-e,True).firstPoint 
    after = feature.shape.positionAlongLine(0.5+e,True).firstPoint
    
    dX = after.X - before.X
    dY = after.Y - before.Y
    
    #return angle of midpoint section. We will plot a line at 90 degrees from this angle. 
    angle = atan2(dX, dY)* 180/pi
    
    newpoint=Midpoint.point.pointFromAngleAndDistance(angle + 90, 400)
2
  • 1
    is Midpoint already a Point object? try remove the .point and go for Midpoint.pointFromAngleAndDistance(angle + 90, 400) Nov 22, 2022 at 9:32
  • 2
    APointGeomerty has that method, not a Point.
    – Vince
    Nov 22, 2022 at 12:38

1 Answer 1

8

There's a bunch of problems here, and the first is failure to use a DA SearchCursor (old-style cursors are kludgey and not to be used in new code). Your variable naming also violates best practice (as defined by PEP8) -- Only classes should be named in Uppercase (using lowercase for instance variables makes the code more readable to others).

If your features are single-part Polyline objects, you can use the .centroid property to obtain the midpoint (which returns a Point).

Point is just a wrapper class for X/Y/Z/M values. The PointGeometry class (which is a subclass of Geometry) supports the method .pointFromAngleAndDistance().

The PointGeometry constructor requires a SpatialReference, which can be extracted from the source feature class.

Putting it all together (but untested):

from math import atan2, pi
import arcpy as arc
input_shp = 'C:/Users/agood/OneDrive - USN/Python/Paper 1 Analysis/Python/Meltwater Tran_SplitLineAtPo.shp'

e = 1e-10 #epsilon

sr = arcpy.Describe(input_shp).spatialReference

with arcpy.da.SearchCursor(input_shp,['SHAPE@']) as cursor:
    for row in cursor:
        polyline = row[0]
        midpoint = arcpy.PointGeometry(polyline.positionAlongLine(0.5,True).firstPoint,sr) #find midpoint along each line segment
      
        #getpoints immediately before and after midpoint
        before = polyline.positionAlongLine(0.5-e,True).firstPoint 
        after = polyline.positionAlongLine(0.5+e,True).firstPoint
    
        dX = after.X - before.X
        dY = after.Y - before.Y
    
        #return angle of midpoint section. We will plot a line at 90 degrees from this angle. 
        angle = atan2(dX, dY)* 180/pi
    
        newpoint = midpoint.pointFromAngleAndDistance(angle + 90, 400)

Note that it may be unwise to assume that the epsilon offsets from the midpoint are within the segment containing the midpoint (which could mess up your angle calculation), and that Cartesian constructions with angular units (as in a geographic coordinate system) are likely to produce incorrect results.

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.