1+ #####
2+ # Step 4 - Data Access module - Adding new records using an InsertCursor.
3+ #####
4+
5+ # Insert Cursors are used to add new data to shapefile or feature class. It is almost exactly the same
6+ # as the other Cursors that we have used. In this example, we will construct a line feature class.
7+
8+ import arcpy , os
9+ from math import radians , sin , cos
10+
11+ # # Create an empty Shapefile
12+ # arcpy.env.workspace = r"C:\Course_ArcGIS_Python\Classes\9_Cursors"
13+ #
14+ # # Set local variables
15+ # out_path = arcpy.env.workspace
16+ # out_name = "radiating_line.shp"
17+ # geometry_type = "POLYLINE"
18+ # template = "#"
19+ # has_m = "DISABLED"
20+ # has_z = "DISABLED"
21+ #
22+ # # Use Describe to get a SpatialReference object
23+ # spatial_ref = 4326
24+ #
25+ # # Execute CreateFeatureclass
26+ # arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template,
27+ # has_m, has_z, spatial_ref)
28+ #
29+ #
30+ # # From a point (origin), let's create some radiating lines and add them to the new shapefile
31+ #
32+ # origin_x, origin_y = (-71.42, 41.47)
33+ # distance = 1
34+ # angle = 10 # in degrees
35+ #
36+ # OutputFeature = os.path.join(out_path, out_name)
37+ #
38+ # #create list of bearings
39+ # angles = range(0, 360,angle)
40+ #
41+ #
42+ # for ang in angles:
43+ # # calculate offsets with trig
44+ # angle = float(int(ang))
45+ # (disp_x, disp_y) = (distance * sin(radians(angle)), distance * cos(radians(angle)))
46+ # (end_x, end_y) = (origin_x + disp_x, origin_y + disp_y)
47+ # (end2_x, end2_y) = (origin_x + disp_x, origin_y + disp_y)
48+ #
49+ # cur = arcpy.InsertCursor(OutputFeature)
50+ # lineArray = arcpy.Array()
51+ #
52+ # # start point
53+ # start = arcpy.Point()
54+ # (start.ID, start.X, start.Y) = (1, origin_x, origin_y)
55+ # lineArray.add(start)
56+ #
57+ # # end point
58+ # end = arcpy.Point()
59+ # (end.ID, end.X, end.Y) = (2, end_x, end_y)
60+ # lineArray.add(end)
61+ #
62+ # # write our fancy feature to the shapefile
63+ # feat = cur.newRow()
64+ # feat.shape = lineArray
65+ # cur.insertRow(feat)
66+ #
67+ # # yes, this shouldn't really be necessary...
68+ # lineArray.removeAll()
69+ # del cur
70+
71+
72+ # Task - Using the above code, amend it so you can do multiple origin_x and origin_y. Note that you don't have to do
73+ # too much to the code, so think of the steps you need to take before you touch this. BTW this is a hard one. Use this
74+ # for your input locations: input_locations = [(-71.42, 41.47), (-72.42, 42.47), (-73.42, 43.47)]
75+
76+ import arcpy , os
77+ from math import radians , sin , cos
78+
79+ # Create an empty Shapefile
80+ arcpy .env .workspace = r"C:\Course_ArcGIS_Python\Classes\9_Cursors"
81+
82+ # Set local variables
83+ out_path = arcpy .env .workspace
84+ out_name = "radiating_line_2.shp"
85+ geometry_type = "POLYLINE"
86+ template = "#"
87+ has_m = "DISABLED"
88+ has_z = "DISABLED"
89+
90+ # Use Describe to get a SpatialReference object
91+ spatial_ref = 4326
92+
93+ # Execute CreateFeatureclass
94+ arcpy .CreateFeatureclass_management (out_path , out_name , geometry_type , template ,
95+ has_m , has_z , spatial_ref )
96+
97+
98+ input_locations = [(- 71.42 , 41.47 ), (- 72.42 , 42.47 ), (- 73.42 , 43.47 )]
99+
100+ for i in input_locations :
101+
102+
103+ origin_x , origin_y = i [0 ], i [1 ]
104+ distance = 1
105+ angle = 10 # in degrees
106+
107+ OutputFeature = os .path .join (out_path , out_name )
108+
109+ #create list of bearings
110+ angles = range (0 , 360 ,angle )
111+
112+
113+ for ang in angles :
114+ # calculate offsets with trig
115+ angle = float (int (ang ))
116+ (disp_x , disp_y ) = (distance * sin (radians (angle )), distance * cos (radians (angle )))
117+ (end_x , end_y ) = (origin_x + disp_x , origin_y + disp_y )
118+ (end2_x , end2_y ) = (origin_x + disp_x , origin_y + disp_y )
119+
120+ cur = arcpy .InsertCursor (OutputFeature )
121+ lineArray = arcpy .Array ()
122+
123+ # start point
124+ start = arcpy .Point ()
125+ (start .ID , start .X , start .Y ) = (1 , origin_x , origin_y )
126+ lineArray .add (start )
127+
128+ # end point
129+ end = arcpy .Point ()
130+ (end .ID , end .X , end .Y ) = (2 , end_x , end_y )
131+ lineArray .add (end )
132+
133+ # write our fancy feature to the shapefile
134+ feat = cur .newRow ()
135+ feat .shape = lineArray
136+ cur .insertRow (feat )
137+
138+ # yes, this shouldn't really be necessary...
139+ lineArray .removeAll ()
140+ del cur
0 commit comments