Skip to content

Commit 393db1b

Browse files
committed
Updated Class 10
1 parent 3868322 commit 393db1b

2 files changed

Lines changed: 217 additions & 0 deletions

File tree

Classes/10_Cursors/Step_4.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
#####
3+
# Step 4 - Data Access module - Adding new records using an InsertCursor.
4+
#####
5+
6+
# Insert Cursors are uised to add new data to shapefile or feature class. It is almost exactly the same
7+
# as the other Cursors that we have used. In this example, we will construct a line feature class.
8+
9+
import arcpy, os
10+
from math import radians, sin, cos
11+
12+
# Create an empty Shapefile
13+
arcpy.env.workspace = r"Z:\Andy's Documents\Teaching and students\URI\NRS - GIS Python Course\Github\Course_ArcGIS_Python\Classes\10_Cursors"
14+
15+
# Set local variables
16+
out_path = arcpy.env.workspace
17+
out_name = "radiating_line.shp"
18+
geometry_type = "POLYLINE"
19+
template = "#"
20+
has_m = "DISABLED"
21+
has_z = "DISABLED"
22+
23+
# Use Describe to get a SpatialReference object
24+
spatial_ref = 4326
25+
26+
# Execute CreateFeatureclass
27+
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template,
28+
has_m, has_z, spatial_ref)
29+
30+
31+
# From a point (origin), let's create some radiating lines and add them to the new shapefile
32+
33+
origin_x, origin_y = (-71.42, 41.47)
34+
distance = 1
35+
angle = 10 # in degrees
36+
37+
OutputFeature = os.path.join(out_path, out_name)
38+
39+
#create list of bearings
40+
angles = range(0, 360,angle)
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+
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
#####
3+
# Step 4 - Data Access module - Adding new records using an InsertCursor.
4+
#####
5+
6+
# Insert Cursors are uised to add new data to shapefile or feature class. It is almost exactly the same
7+
# as the other Cursors that we have used. In this example, we will construct a line feature class.
8+
9+
import arcpy, os
10+
from math import radians, sin, cos
11+
12+
# Create an empty Shapefile
13+
arcpy.env.workspace = r"Z:\Andy's Documents\Teaching and students\URI\NRS - GIS Python Course\Github\Course_ArcGIS_Python\Classes\10_Cursors"
14+
15+
# Set local variables
16+
out_path = arcpy.env.workspace
17+
out_name = "radiating_line.shp"
18+
geometry_type = "POLYLINE"
19+
template = "#"
20+
has_m = "DISABLED"
21+
has_z = "DISABLED"
22+
23+
# Use Describe to get a SpatialReference object
24+
spatial_ref = 4326
25+
26+
# Execute CreateFeatureclass
27+
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template,
28+
has_m, has_z, spatial_ref)
29+
30+
31+
# From a point (origin), let's create some radiating lines and add them to the new shapefile
32+
33+
origin_x, origin_y = (-71.42, 41.47)
34+
distance = 1
35+
angle = 10 # in degrees
36+
37+
OutputFeature = os.path.join(out_path, out_name)
38+
39+
#create list of bearings
40+
angles = range(0, 360,angle)
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+
77+
import arcpy, os
78+
from math import radians, sin, cos
79+
80+
# Create an empty Shapefile
81+
arcpy.env.workspace = r"Z:\Andy's Documents\Teaching and students\URI\NRS - GIS Python Course\Github\Course_ArcGIS_Python\Classes\10_Cursors"
82+
83+
# Set local variables
84+
out_path = arcpy.env.workspace
85+
out_name = "radiating_line_2.shp"
86+
geometry_type = "POLYLINE"
87+
template = "#"
88+
has_m = "DISABLED"
89+
has_z = "DISABLED"
90+
91+
# Use Describe to get a SpatialReference object
92+
spatial_ref = 4326
93+
94+
# Execute CreateFeatureclass
95+
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, template,
96+
has_m, has_z, spatial_ref)
97+
98+
99+
input_locations = [(-71.42, 41.47), (-72.42, 42.47), (-73.42, 43.47)]
100+
101+
for i in input_locations:
102+
103+
104+
origin_x, origin_y = i[0], i[1]
105+
distance = 1
106+
angle = 10 # in degrees
107+
108+
OutputFeature = os.path.join(out_path, out_name)
109+
110+
#create list of bearings
111+
angles = range(0, 360,angle)
112+
113+
114+
for ang in angles:
115+
# calculate offsets with trig
116+
angle = float(int(ang))
117+
(disp_x, disp_y) = (distance * sin(radians(angle)), distance * cos(radians(angle)))
118+
(end_x, end_y) = (origin_x + disp_x, origin_y + disp_y)
119+
(end2_x, end2_y) = (origin_x + disp_x, origin_y + disp_y)
120+
121+
cur = arcpy.InsertCursor(OutputFeature)
122+
lineArray = arcpy.Array()
123+
124+
# start point
125+
start = arcpy.Point()
126+
(start.ID, start.X, start.Y) = (1, origin_x, origin_y)
127+
lineArray.add(start)
128+
129+
# end point
130+
end = arcpy.Point()
131+
(end.ID, end.X, end.Y) = (2, end_x, end_y)
132+
lineArray.add(end)
133+
134+
# write our fancy feature to the shapefile
135+
feat = cur.newRow()
136+
feat.shape = lineArray
137+
cur.insertRow(feat)
138+
139+
# yes, this shouldn't really be necessary...
140+
lineArray.removeAll()
141+
del cur

0 commit comments

Comments
 (0)