|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# # Amarachi Akunna Onyekachi |
| 5 | +# # GEOG 592 Lab 4 |
| 6 | + |
| 7 | +# ### Question 1 |
| 8 | +# Create a point feature class (shapefile – q1.shp) from the locations.txt file. (4 Points) |
| 9 | +# |
| 10 | + |
| 11 | +# In[ ]: |
| 12 | + |
| 13 | +import arcpy |
| 14 | +arcpy.env.workspace = "T:\\Students\\akunna1\\submissions\\Lab_4\\" #workplace location |
| 15 | +arcpy.env.overwriteOutput = True |
| 16 | + |
| 17 | +locationFile = open('locations.txt', 'r') #opening text file for the purpose of reading it and giving it a variable name |
| 18 | +allLines = locationFile.readlines()[1:] #reading all the lines except the header line |
| 19 | + |
| 20 | +#creating a new shapefile/ feature class 'q1.shp' and adding the field (column) names |
| 21 | +# arcpy.CreateFeatureclass_management('outpath','new feature class name','Point') |
| 22 | +arcpy.CreateFeatureclass_management(arcpy.env.workspace, 'q1.shp', 'point')#creates a new feature in a geodatabase |
| 23 | +arcpy.AddField_management('q1.shp', 'LocationID', 'Text', '','',10) #to add a new field to a feature class or table in a geodatabase |
| 24 | + |
| 25 | +#The insertCursor module function allows you to insert new rows into a feature class or table |
| 26 | +#syntax = arcpy.da.InsertCursor(in_table, field_names) |
| 27 | +locationCursor = arcpy.da.InsertCursor('q1.shp',['LocationID', 'SHAPE@XY']) #set the template of the shapefile to insert values |
| 28 | + |
| 29 | +for aLine in allLines: |
| 30 | + aListValues = aLine.split(',') #split the columns in the text files with comma ',' |
| 31 | + locationID = aListValues[0] |
| 32 | + x_coordinate = float(aListValues[1]) |
| 33 | + y_coordinate = float(aListValues[2]) |
| 34 | + |
| 35 | + #inserting the new row for all rows in the text file |
| 36 | + locationCursor.insertRow([LocationID,(x_coordinate,y_coordinate)]) #inserting rows under the field |
| 37 | + |
| 38 | + |
| 39 | +#delete locationCursor |
| 40 | +del locationCursor #deleting the cursor to release any locks on the data |
| 41 | + |
| 42 | +#close textfile |
| 43 | +locationFile.close() |
| 44 | + |
| 45 | + |
| 46 | +# ### Question 2 |
| 47 | +# In q2.py, calculate the mean center for q1.shp and add the point to q1.shp, save the new shapefile as q2.shp. (4 Points) |
| 48 | + |
| 49 | +import arcpy |
| 50 | +arcpy.env.workspace = "T:\\Students\\akunna1\\submissions\\Lab_4\\" #workplace location |
| 51 | +arcpy.env.overwriteOutput = True |
| 52 | + |
| 53 | +locationFile = open('locations.txt', 'r') #opening text file for the purpose of reading it and giving it a variable nam |
| 54 | +textLines = locationFile.readlines()[1:] #reading all the lines except the header line |
| 55 | + |
| 56 | +arcpy.CreateFeatureclass_management(arcpy.env.workspace, 'q2.shp', 'point') #creates a new feature in a geodatabase |
| 57 | +# arcpy.CreateFeatureclass_management('outpath','new feature class name','Point') |
| 58 | + |
| 59 | +arcpy.AddField_management('q2.shp', 'LocationID', 'Text', '','',10)#to add a new field to a feature class or table in a geodatabase |
| 60 | + |
| 61 | +#The insertCursor module function allows you to insert new rows into a feature class or table |
| 62 | +#syntax = arcpy.da.InsertCursor(in_table, field_names) |
| 63 | +locationCursor = arcpy.da.InsertCursor('q2.shp',['LocationID', 'SHAPE@XY']) #set the template of the shapefile to insert values |
| 64 | + |
| 65 | +x = 0 |
| 66 | +y = 0 |
| 67 | +count = 0 |
| 68 | +for aLine in textLines: |
| 69 | + aListValues = aLine.split(',') |
| 70 | + LocationID = aListValues[0] |
| 71 | + x_coordinate = float(aListValues[1]) |
| 72 | + y_coordinate = float(aListValues[2]) |
| 73 | + x=x_coordinate + x |
| 74 | + y=y_coordinate + y |
| 75 | + count = count + 1 |
| 76 | + |
| 77 | + locationCursor.insertRow([LocationID,(x_coordinate,y_coordinate)]) #inserting rows under the field, LocationID |
| 78 | + |
| 79 | +x_mean_center = x/count |
| 80 | +y_mean_center = y/count |
| 81 | +locationCursor.insertRow(['MeanCenter',(x_mean_center,y_mean_center)]) |
| 82 | + |
| 83 | +del locationCursor |
| 84 | +locationFile.close() |
| 85 | + |
| 86 | + |
| 87 | +# ### Question 3 |
| 88 | +# Write a script (q3.py) to convert oneline.txt into polyline feature class (shapefile). Oneline.txt includes multiple vertices for one polyline object. Each row represents one vertex – |
| 89 | +# 177030005600 904 2.5 206 904 29.409337 -92.888029………... |
| 90 | +# The first column represents the polyline’s ID, and last two columns represent a vertex’s latitude and longitude. (4 Points) |
| 91 | +# |
| 92 | + |
| 93 | +# In[ ]: |
| 94 | + |
| 95 | + |
| 96 | +import arcpy |
| 97 | +arcpy.env.workspace = "T:\\Students\\akunna1\\submissions\\Lab_4\\" #workplace location |
| 98 | +arcpy.env.overwriteOutput = True |
| 99 | + |
| 100 | +oneline_file = open('oneline.txt', 'r') |
| 101 | +text_lines = oneline_file.readlines() |
| 102 | + |
| 103 | +#creating a new polyline feature shapefile |
| 104 | +arcpy.CreateFeatureclass_management(arcpy.env.workspace, 'q3.shp', 'polyline') |
| 105 | + |
| 106 | +#adding new field and adding it to the new feature class |
| 107 | +arcpy.AddField_management('q3.shp', 'polylineID', 'Text', '','',10) |
| 108 | +#Using the InsertCursor tool and adding new fields into the shapefile |
| 109 | +polylineCursor = arcpy.da.InsertCursor('q3.shp',['polylineID', 'SHAPE@']) #set the template of the shapefile to insert values |
| 110 | + |
| 111 | +#Creating an empty array |
| 112 | +array = arcpy.Array() |
| 113 | +points = arcpy.Point() |
| 114 | + |
| 115 | +#looping through each line in the .txt file to get the x,y coordinate and the lineIDs in order to add them to the empty array created |
| 116 | +for a_line in text_lines: |
| 117 | + a_list_values = a_line.split() |
| 118 | + lineID = a_list_values[0] |
| 119 | + x_coordinate = float(a_list_values[6]) |
| 120 | + y_coordinate = float(a_list_values[5]) |
| 121 | + points.X = x_coordinate |
| 122 | + points.Y = y_coordinate |
| 123 | + array.add(points) |
| 124 | + |
| 125 | +#creating the polylne with the array points |
| 126 | +polyline = arcpy.Polyline(array) |
| 127 | + |
| 128 | + |
| 129 | +# ### Question 4 |
| 130 | +# Write a script (q4.py) to convert test.txt into a polyline feature class and save the result to your Lab6 folder. Test.txt contains more than one polyline object. Each polyline object has a unique line ID, such as 177030005600, 177030009200, etc. Please use the Line ID to identify each line segment. You can save the result as a shapefile. Once succeeded, apply your code to allLines.txt, which has thousands of records – more than 20 Mbs. (4 Points) |
| 131 | + |
| 132 | +# In[ ]: |
| 133 | + |
| 134 | + |
| 135 | +#For test.txt file |
| 136 | +import arcpy |
| 137 | +arcpy.env.workspace = "T:\\Students\\akunna1\\submissions\\Lab_4\\" #workplace location |
| 138 | +arcpy.env.overwriteOutput = True |
| 139 | + |
| 140 | +test_file = open('test.txt', 'r') |
| 141 | +test_file_lines = test_file.readlines() |
| 142 | + |
| 143 | +#creating a polyline shapefile |
| 144 | +arcpy.CreateFeatureclass_management(arcpy.env.workspace, 'q4.shp', 'polyline') |
| 145 | +#Adding a new field called 'polylineID' |
| 146 | +arcpy.AddField_management('q4.shp', 'polylineID', 'Text', '','',10) |
| 147 | + |
| 148 | +#Calling the InsertCursor tool and setting the template to allow addition of new record into the new shapefile |
| 149 | +polylineCursor = arcpy.da.InsertCursor('q4.shp',['polylineID', 'SHAPE@']) #set the template of the shapefile to insert values |
| 150 | + |
| 151 | +#creating an empty array |
| 152 | +points = arcpy.Point() |
| 153 | +array = arcpy.Array() |
| 154 | + |
| 155 | +#looping through each line in the .txt file and getting the x,y coordinates and the lineIDs |
| 156 | +# Then adding them to the empty array |
| 157 | +pastID = test_file_lines[0].split()[0] #get first row and first value in the row |
| 158 | + |
| 159 | +for aLine in test_file_lines: |
| 160 | + aList_values = aLine.split() |
| 161 | + lineID =aList_values[0] |
| 162 | + x_coordinate = float(aList_values[6]) |
| 163 | + y_coordinate = float(aList_values[5]) |
| 164 | + points.X = x_coordinate |
| 165 | + points.Y = y_coordinate |
| 166 | + array.add(points) |
| 167 | + if lineID != pastID: |
| 168 | + polyline = arcpy.Polyline(array) |
| 169 | + polylineCursor.insertRow([lineID, polyline]) |
| 170 | + pastID = lineID |
| 171 | + array.removeAll() |
| 172 | + |
| 173 | +del polylineCursor |
| 174 | +test_file.close() |
| 175 | + |
| 176 | + |
| 177 | +# In[ ]: |
| 178 | + |
| 179 | + |
| 180 | +#for allLines.txt file |
| 181 | +import arcpy |
| 182 | +arcpy.env.workspace = "T:\\Students\\akunna1\\submissions\\Lab_4\\" #workplace location |
| 183 | +arcpy.env.overwriteOutput = True |
| 184 | + |
| 185 | +allLines_file = open('allLines.txt', 'r') |
| 186 | +allLines_file_lines = allLines_file.readlines() |
| 187 | + |
| 188 | +#creating a polyline shapefile |
| 189 | +arcpy.CreateFeatureclass_management(arcpy.env.workspace, 'q4_allLines.shp', 'polyline') |
| 190 | +#Adding a new field called 'polylineID' |
| 191 | +arcpy.AddField_management('q4_allLines.shp', 'polylineID', 'Text', '','',10) |
| 192 | + |
| 193 | +#Calling the InsertCursor tool and setting the template to allow addition of new record into the new shapefile |
| 194 | +polylineCursor = arcpy.da.InsertCursor('q4_allLines.shp',['polylineID', 'SHAPE@']) |
| 195 | + |
| 196 | +#creating an empty array |
| 197 | +points = arcpy.Point() |
| 198 | +array = arcpy.Array() |
| 199 | + |
| 200 | +#looping through each line in the .txt file and getting the x,y coordinates and the lineIDs |
| 201 | +# Then adding them to the empty array |
| 202 | +pastID = allLines_file_lines[0].split()[0] |
| 203 | +for aLine in allLines_file_lines: |
| 204 | + aList_values = aLine.split() |
| 205 | + lineID =aList_values[0] |
| 206 | + x_coordinate = float(aList_values[6]) |
| 207 | + y_coordinate = float(aList_values[5]) |
| 208 | + points.X = x_coordinate |
| 209 | + points.Y = y_coordinate |
| 210 | + array.add(points) |
| 211 | + if lineID != pastID: |
| 212 | + polyline = arcpy.Polyline(array) |
| 213 | + polylineCursor.insertRow([lineID, polyline]) |
| 214 | + pastID = lineID |
| 215 | + array.removeAll() |
| 216 | + |
| 217 | +del polylineCursor |
| 218 | +allLines_file.close() |
| 219 | + |
| 220 | + |
| 221 | +# ### Question 5 |
| 222 | +# write a script (q5.py) to perform the following procedures: (4 Points) |
| 223 | +# |
| 224 | +# a) List all numerical fields from states2010.shp for a user to choose. For example, <1>POP2000 <2>POP2010 <3>POP00_SQMI …, and the user can choose POP2000 by typing 1 and hit “enter” key. |
| 225 | +# |
| 226 | +# ) Once your script receives the input from the user, compute (1) total value of the selected field (2) maximum value of the chosen field (3) average value of the chosen field |
| 227 | +# |
| 228 | +# c) Prompt the results to the user. |
| 229 | +# |
| 230 | +# d) If there is no “popchg” field, create a “popchg” field for states2010.shp, then calculate population change percentage from 2000 to 2010 for each state and populate this field. To add a field to a feature class, you can use AddField_management function. For examples of using this function, you can search ArcGIS10 desktop help. |
| 231 | +# |
| 232 | + |
| 233 | +# In[ ]: |
| 234 | + |
| 235 | + |
| 236 | +import arcpy |
| 237 | +arcpy.env.workspace = "T:\\Students\\akunna1\\submissions\\Lab_4\\" #workplace location |
| 238 | +arcpy.env.overwriteOutput = True |
| 239 | + |
| 240 | +states2010_file = "states2010.shp" |
| 241 | + |
| 242 | +numerical_fields = arcpy.ListFields(states2010_file, "", 'Double') + arcpy.ListFields(states2010_file, "", 'Integer') + arcpy.ListFields(states2010_file, "", 'Float') |
| 243 | +#arcpy.ListFields is used to list the fields in a table or feature class. All the numerical fields --> Double, Integer, Float |
| 244 | + |
| 245 | +count = 0 |
| 246 | + |
| 247 | +for afield in numerical_fields: |
| 248 | + print("<%d>" % count + afield.name) |
| 249 | + count+=1 |
| 250 | + |
| 251 | +user_input = int(input("Choose a field by typing the corresponding index number.\n")) |
| 252 | +stat_cursor = arcpy.da.SearchCursor(states2010_file, [numerical_fields[user_input].name]) # used to iterate over rows in a table or feature class, and extract specific field values for each row. |
| 253 | +total = 0 |
| 254 | +maximum = -9999999999 |
| 255 | +count = 0 |
| 256 | + |
| 257 | +for aRow in stat_cursor: |
| 258 | + # Calculate total and maximum in the loop |
| 259 | + total=int(aRow[0])+total |
| 260 | + count = count+1 |
| 261 | + if aRow[0] > maximum: |
| 262 | + maximum = aRow[0] |
| 263 | +average = total/count |
| 264 | + |
| 265 | +#Printing the total, average and maximum values |
| 266 | +print("The total = {}".format(total)) |
| 267 | +print("The average = {}".format(average)) |
| 268 | +print("The maximum value = {}".format(maximum)) |
| 269 | + |
| 270 | +del stat_cursor |
| 271 | + |
| 272 | + |
| 273 | +# In[ ]: |
| 274 | + |
| 275 | + |
| 276 | +#Searching for popchg field, if found, do nothing, otherwise, create popchg field, and populate the field |
| 277 | +#defining the find_field function |
| 278 | +def find_field(field_list, field_name): |
| 279 | + for field in field_list: |
| 280 | + if field.name == field_name: |
| 281 | + return True |
| 282 | + return False |
| 283 | + |
| 284 | +# Testing to see if the "popchg" field exists |
| 285 | +if find_field(numerical_fields, "popchg"): |
| 286 | + print ("popchg field already exists, deleting... ") |
| 287 | + arcpy.DeleteField_management(states2010_file, ["popchg"]) |
| 288 | + |
| 289 | +# Adding a float field named "popchg" here |
| 290 | +#arcpy.AddField_management('name of shapefile "'field name'", "'field type'","10","3") |
| 291 | +arcpy.AddField_management(states2010_file, "popchg", "FLOAT","10","3") |
| 292 | +#calling the rows that I want to work with |
| 293 | +rows = arcpy.da.UpdateCursor(states2010_file, ["POP2000","POP2010","popchg"]) |
| 294 | +for row in rows: |
| 295 | + # Getting pop2000 and pop2010 values, calculating the pop change value (row[2] = ...) |
| 296 | + row[2]=((row[1]-row[0])/row[0])*100 |
| 297 | + rows.updateRow(row) |
| 298 | +del row, rows |
| 299 | + |
| 300 | + |
| 301 | +# In[ ]: |
| 302 | + |
| 303 | + |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | +# In[ ]: |
| 308 | + |
| 309 | + |
| 310 | + |
| 311 | + |
| 312 | + |
| 313 | +# In[ ]: |
| 314 | + |
| 315 | + |
| 316 | + |
| 317 | + |
0 commit comments