Skip to content

Commit 38178ed

Browse files
committed
Updated Class 9
1 parent 6adb138 commit 38178ed

5 files changed

Lines changed: 225 additions & 2 deletions

File tree

Classes/09_Cursors/ALL_FILES.zip

3.97 KB
Binary file not shown.

Classes/09_Cursors/Step_1.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
print("There are " + str(line_count) + " csv records for crane 100840.")
2323

24-
# Uncomment the below, now let's now do the same using arcpy.da (arcpy data access module):
24+
# Uncomment the below, now let's now do the same using arcpy.da (arcpy data access module), you could of course
25+
# use select and get count but we are doing it the hard way!:
2526

2627
# import arcpy
2728
# line_count_da = 0

Classes/09_Cursors/Step_1_Answers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#
2222
# print("There are " + str(line_count) + " csv records for crane 100840.")
2323

24-
# Uncomment the below, now let's now do the same using arcpy.da (arcpy data access module):
24+
# Uncomment the below, now let's now do the same using arcpy.da (arcpy data access module), you could of course
25+
# use select and get count but we are doing it the hard way!:
2526

2627
# import arcpy
2728
# line_count_da = 0

Classes/09_Cursors/Step_4.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
print(angles)
41+
42+
43+
for ang in angles:
44+
# calculate offsets with trig
45+
angle = float(int(ang))
46+
(disp_x, disp_y) = (distance * sin(radians(angle)), distance * cos(radians(angle)))
47+
(end_x, end_y) = (origin_x + disp_x, origin_y + disp_y)
48+
(end2_x, end2_y) = (origin_x + disp_x, origin_y + disp_y)
49+
50+
cur = arcpy.InsertCursor(OutputFeature)
51+
lineArray = arcpy.Array()
52+
53+
# start point
54+
start = arcpy.Point()
55+
(start.ID, start.X, start.Y) = (1, origin_x, origin_y)
56+
lineArray.add(start)
57+
58+
# end point
59+
end = arcpy.Point()
60+
(end.ID, end.X, end.Y) = (2, end_x, end_y)
61+
lineArray.add(end)
62+
63+
# write our fancy feature to the shapefile
64+
feat = cur.newRow()
65+
feat.shape = lineArray
66+
cur.insertRow(feat)
67+
68+
# yes, this shouldn't really be necessary...
69+
lineArray.removeAll()
70+
del cur
71+
72+
73+
# 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
74+
# 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
75+
# for your input locations: input_locations = [(-71.42, 41.47), (-72.42, 42.47), (-73.42, 43.47)]
76+
input_locations = [(-71.42, 41.47), (-72.42, 42.47), (-73.42, 43.47)]
77+
78+
for location in input_locations:
79+
location == (-71.42, 41.47)
80+
location[0] == -71.42
81+
location[1] == 41.47
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)