Compute angles between intersecting roads

585
8
08-14-2017 06:38 PM
ErinKoletsis1
New Contributor

I'm trying to compute the angles between intersecting roads using dkwiens script found at: https://community.esri.com/message/582193?q=calculate%20angle%20of but am receiving the following error:

Runtime error  Traceback (most recent call last):   File "<string>", line 53, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 3246, in AddField     raise e ExecuteError: ERROR 000732: Input Table: Dataset in_memory\points does not exist or is not supported

My code is:

>>> import arcpy
... import os
... fc = "NYC_Roads_Extent50"
... sr = arcpy.Describe(fc).spatialReference
... radius = 3
... out_fc = r'in_memory\points'
... int_pt = r'in_memory\int_pt'
... diss_fc = 'NYC_Roads_Extent50'
... arcpy.Dissolve_management(fc,diss_fc,["name"],"","SINGLE_PART")
... arcpy.Intersect_analysis(diss_fc,int_pt,output_type='POINT')
... diss_int_pt = r'in_memory\diss_int_pt'
... arcpy.Dissolve_management(int_pt,diss_int_pt,'#',[["OBJECTID","MIN"]],"SINGLE_PART")
... buff = r'in_memory\buff'
... arcpy.Buffer_analysis(diss_int_pt,buff,str(radius) + ' METERS')
... buff_line_int = r'in_memory\buff_line_int'
... arcpy.Intersect_analysis([buff,fc],buff_line_int,output_type='POINT')
... sing_buff_line_int = r'in_memory\sing_buff_line_int'
... arcpy.MultipartToSinglepart_management(buff_line_int,sing_buff_line_int)
... new_points = {}
... with arcpy.da.SearchCursor(diss_int_pt,['OID@','SHAPE@'],spatial_reference=sr) as cursor1:
... for row1 in cursor1:
... cent_pt = row1[1].centroid
... angs = []
... with arcpy.da.SearchCursor(sing_buff_line_int,'SHAPE@','\"FID_buff\" = ' + str(row1[0]),spatial_reference=sr) as cursor2:
... for row2 in cursor2:
... buff_pt = row2[0].centroid
... dx = cent_pt.X - buff_pt.X
... dy = cent_pt.Y - buff_pt.Y
... if dx < 0 and dy <= 0:
... ang = math.degrees(math.atan(abs(dy/dx)))
... if dx <= 0 and dy > 0:
... ang = math.degrees(math.atan(abs(dx/dy))) + 270
... if dx > 0 and dy >= 0:
... ang = math.degrees(math.atan(abs(dy/dx))) + 180
... if dx >= 0 and dy < 0:
... ang = math.degrees(math.atan(abs(dx/dy))) + 90
... angs.append(ang)
... angs.sort()
... for i in range(1,len(angs)):
... mid_ang = ((angs + angs[i-1])/2)
... ang_diff = angs - angs[i-1]
... new_x = cent_pt.X + (radius * math.cos(math.radians(mid_ang)))
... new_y = cent_pt.Y + (radius * math.sin(math.radians(mid_ang)))
... new_point = arcpy.PointGeometry(arcpy.Point(new_x, new_y),sr)
... new_points[str(row1[0]) + '_' + str(i)] = [ang_diff,new_point]
... mid_ang = (((360-angs[-1]) + angs[0])/2) - (360-angs[-1])
... ang_diff = (360-angs[-1]) + angs[0]
... new_x = cent_pt.X + (radius * math.cos(math.radians(mid_ang)))
... new_y = cent_pt.Y + (radius * math.sin(math.radians(mid_ang)))
... new_point = arcpy.PointGeometry(arcpy.Point(new_x, new_y),sr)
... new_points[str(row1[0]) + '_0'] = [ang_diff,new_point]
... arcpy.CreateFeatureclass_management(os.path.dirname(out_fc),os.path.basename(out_fc),'POINT',spatial_reference=sr)
... arcpy.AddField_management(out_fc,'FID_buff',"LONG")
... arcpy.AddField_management(out_fc,'ANGLE',"DOUBLE")
... iCursor = arcpy.da.InsertCursor(out_fc,['SHAPE@','FID_buff','ANGLE'])
... for k,v in new_points.iteritems():
... row = [v[1],k.split('_')[0],v[0]]
... iCursor.insertRow(row)

Can someone please help? Apologies, I am a novice Python user.

0 Kudos
8 Replies
DanPatterson_Retired
MVP Esteemed Contributor

could you copy and paste that into a script and run it from an IDE rather than the builtin python one.

You can format your code appropriately by doing so

DuncanHornby
MVP Frequent Contributor

Erin,

As Dan Patterson‌ hints you need to edit your question and ensure your code is formatted correctly, otherwise no one can read your code, especially as it is python and indentation is everything for that language. Learn how to format your code correctly here.

ErinKoletsis1
New Contributor

Thanks Hornbydd My code is as follows: 

>>> import arcpy
... import os  
... fc = "DubaiRoads_Extent"  
... sr = arcpy.Describe(fc).spatialReference 
... radius = 3  
... out_fc = r'J:\research\MapLiteracyStudy\Dubai.gdb\points'  
... int_pt = r'J:\research\MapLiteracyStudy\Dubai.gdb\int_pt'
... arcpy.Intersect_analysis(fc,int_pt,output_type='POINT')  
... diss_int_pt = r'J:\research\MapLiteracyStudy\Dubai.gdb\diss_int_pt'  
... arcpy.Dissolve_management(int_pt,diss_int_pt,'#',[["OBJECTID","MIN"]],"SINGLE_PART")  
... buff = r'J:\research\MapLiteracyStudy\Dubai.gdb\buff'  
... arcpy.Buffer_analysis(diss_int_pt,buff,str(radius) + ' METERS')  
... buff_line_int = r'J:\research\MapLiteracyStudy\Dubai.gdb\buff_line_int'  
... arcpy.Intersect_analysis([buff,fc],buff_line_int,output_type='POINT')  
... sing_buff_line_int = r'J:\research\MapLiteracyStudy\Dubai.gdb\sing_buff_line_int'  
... arcpy.MultipartToSinglepart_management(buff_line_int,sing_buff_line_int)  
... new_points = {}  
... with arcpy.da.SearchCursor(diss_int_pt,['OID@','SHAPE@'],spatial_reference=sr) as cursor1:  
...    for row1 in cursor1:  
...        cent_pt = row1[1].centroid  
...        angs = []  
...        with arcpy.da.SearchCursor(sing_buff_line_int,'SHAPE@','\"FID_buff\" = ' + str(row1[0]),spatial_reference=sr) as cursor2:  
...            for row2 in cursor2:  
...                buff_pt = row2[0].centroid  
...                dx = cent_pt.X - buff_pt....                dy = cent_pt.Y - buff_pt....                if dx < 0 and dy <= 0:  
...                    ang = math.degrees(math.atan(abs(dy/dx)))  
...                if dx <= 0 and dy > 0:  
...                    ang = math.degrees(math.atan(abs(dx/dy))) + 270  
...                if dx > 0 and dy >= 0:  
...                    ang = math.degrees(math.atan(abs(dy/dx))) + 180  
...                if dx >= 0 and dy < 0:  
...                    ang = math.degrees(math.atan(abs(dx/dy))) + 90  
...                angs.append(ang)  
...        angs.sort()  
...        for i in range(1,len(angs)):  
...            mid_ang = ((angs[i] + angs[i-1])/2)  
...            ang_diff = angs[i] - angs[i-1]  
...            new_x = cent_pt.X + (radius * math.cos(math.radians(mid_ang)))  
...            new_y = cent_pt.Y + (radius * math.sin(math.radians(mid_ang)))  
...            new_point = arcpy.PointGeometry(arcpy.Point(new_x, new_y),sr)  
...            new_points[str(row1[0]) + '_' + str(i)] = [ang_diff,new_point]  
...        mid_ang = (((360-angs[-1]) + angs[0])/2) - (360-angs[-1])  
...        ang_diff = (360-angs[-1]) + angs[0]  
...        new_x = cent_pt.X + (radius * math.cos(math.radians(mid_ang)))  
...        new_y = cent_pt.Y + (radius * math.sin(math.radians(mid_ang)))  
...        new_point = arcpy.PointGeometry(arcpy.Point(new_x, new_y),sr)  
...        new_points[str(row1[0]) + '_0'] = [ang_diff,new_point]  
... arcpy.CreateFeatureclass_management(os.path.dirname(out_fc),os.path.basename(out_fc),'POINT',spatial_reference=sr)  
... arcpy.AddField_management(out_fc,'FID_buff',"LONG")  
... arcpy.AddField_management(out_fc,'ANGLE',"DOUBLE")  
... iCursor = arcpy.da.InsertCursor(out_fc,['SHAPE@','FID_buff','ANGLE'])  
... for k,v in new_points.iteritems():  
...    row = [v[1],k.split('_')[0],v[0]]  
...    iCursor.insertRow(row) 
...    

The script runs, with the resulting image:

And the buffers look like:

However, in the example provided by Darren Wiens, it the result should be:

I can't figure out why all of my calculated points seem to be in a circle around my study area, as opposed to on the buffers between the intersecting lines.

Any help would be greatly appreciated.

Erin

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

looks like a projection issue perhaps.  Are the source files in projected units? I suspect that inputs and outputs are expected in those and not in degrees

DuncanHornby
MVP Frequent Contributor

It's good that you now have worked out how to format your code, ideally you should have gone back and edited your original question rather than add another reply to the thread, stops the thread becoming long and unreadable.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Actually Duncan... it is a Discussion rather than a question (could be a bloop)... so droning ad nauseum is permissible

DuncanHornby
MVP Frequent Contributor

Good point but I do find the distinction between the two blurred as visually the posting types look almost identical unless you pick up that tiny blue icon (which hands up I did not notice!). Just trying to encourage people who post to format their code as that is such a trivial thing to do but can make such a visual impact, especially with python.

Now I'm just going off to sharpen my pointy stick ready for some more droning on! 

0 Kudos
MattheusPrudêncio
New Contributor

I'm having the same issue, someone can help???

0 Kudos