Create a user named summary table of each feature class using the Legend field to get the
Sum or Count of the total for each. (Use mysum as the default and add _line, _poly and
_point at the end of each and store in the Geodatabase).
and
Using a Cursor, loop through each record of the three summary table results and
print\AddMessage the summary result fields to screen one at a time.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
###### Create summary tables---------------------------------------------------------------------------------------------------------------------------------
arcpy.AddMessage("CREATE SUMMARY TABLES")
Merged_points_Clip = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_points_Clip"
mysum_point = arcpy.GetParameter(3)
StatisticsField = [["DESCRIPT", "COUNT"]]
CaseField = "DESCRIPT"
arcpy.Statistics_analysis(Merged_points_Clip, mysum_point, StatisticsField,)
arcpy.AddMessage("Finished Summarizing points")
Merged_Polylines_Clip = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polylines_Clip"
mysum_line = arcpy.GetParameter(4)
StatisticsField = [["DESCRIPT", "COUNT"]]
CaseField = "DESCRIPT"
arcpy.Statistics_analysis(Merged_Polylines_Clip, mysum_line, StatisticsField,)
arcpy.AddMessage("Finished Summarizing Polylines")
Merged_Polygons_Clip = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polygons_Clip"
mysum_poly = arcpy.GetParameter(5)
StatisticsField = [["DESCRIPT", "COUNT"]]
CaseField = "DESCRIPT"
arcpy.Statistics_analysis(Merged_Polygons_Clip, mysum_poly, StatisticsField,)
arcpy.AddMessage("Finished Summarizing Polylines")
###### Using Cursors---------------------------------------------------------------------------------------------------------------------------------
arcpy.AddMessage("USING CURSORS")
mysum_point = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\mysum_point"
point_cursor = arcpy.da.SearchCursor(mysum_point, ["*"])
for row in point_cursor:
arcpy.AddMessage("OBJECTIDE: {0}, FREQUENCY: {1}, COUNT_DESCRIPT: {2}".format(row[0], row[1], row[2]))
arcpy.AddMessage("Finished points cursor")
del point_cursor
mysum_line = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\mysum_line"
polyline_cursor = arcpy.da.SearchCursor(mysum_line, ["*"])
for row in polyline_cursor:
arcpy.AddMessage("OBJECTIDE: {0}, FREQUENCY: {1}, COUNT_DESCRIPT: {2}".format(row[0], row[1], row[2]))
arcpy.AddMessage("Finished lines cursor")
del polyline_cursor
mysum_poly = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\mysum_poly"
polygon_cursor = arcpy.da.SearchCursor(mysum_poly, ["*"])
for row in polygon_cursor:
arcpy.AddMessage("OBJECTIDE: {0}, FREQUENCY: {1}, COUNT_DESCRIPT: {2}".format(row[0], row[1], row[2]))
arcpy.AddMessage("Finished poly cursor")
del line_cursor
######---------------------------------------------------------------------------------------------------------------------------------
except:
import traceback
e = sys.exc_info()[1]
arcpy.AddMessage(e.args[0])
arcpy.AddMessage("ERROR!")
print (e.args[0])
print ("ERROR!")
# Script Finished
arcpy.AddMessage("Script finished")
# Cleans out the memory for the script
del arcpy
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Permanently Join NSTDB_FCODES.csv in the Assignment 1 data folder for each of the clipped
dataset
and
Print the total number of Points, Polyline and Polygon features to screen for each feature
class
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
###### Joins ---------------------------------------------------------------------------------------------------------------------------------
arcpy.AddMessage("JOINS")
# The feature_codes table is already a dbf. Go into the Python\Data folder and find the feature codes table and you'll see its a .dbf
# No need to convert from .csv to a .dbf
# Permanently Join NSTDB_FCODES.csv in the Assignment 1 data folder for each of the clipped datasets
points = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_points_Clip"
pointsField = "FEAT_CODE"
JoinTable = r"D:\Python\Data\feature_codes.dbf"
feature_codes_Field = "FEAT_CODE"
arcpy.JoinField_management(points, pointsField, JoinTable, feature_codes_Field)
arcpy.AddMessage("Joined points")
Polylines = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polylines_Clip"
PolylinesField = "FEAT_CODE"
JoinTable = r"D:\Python\Data\feature_codes.dbf"
feature_codes_Field = "FEAT_CODE"
arcpy.JoinField_management(Polylines, PolylinesField, JoinTable, feature_codes_Field)
arcpy.AddMessage("Joined Polylines")
Polygons = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polygons_Clip"
PolygonsField = "FEAT_CODE"
JoinTable = r"D:\Python\Data\feature_codes.dbf"
feature_codes_Field = "FEAT_CODE"
arcpy.JoinField_management(Polygons, PolygonsField, JoinTable, feature_codes_Field)
arcpy.AddMessage("Joined Polygons")
###### Printing\AddMessage results---------------------------------------------------------------------------------------------------------------------------------
arcpy.AddMessage("PRINTING\ADDMESSAGE RESULTS")
Merged_points_Clip = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_points_Clip"
pointsCount = arcpy.GetCount_management(Merged_points_Clip)
arcpy.AddMessage("This is how many Points there is: " + str(pointsCount))
Merged_Polylines_Clip = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polylines_Clip"
PolylineCount = arcpy.GetCount_management(Merged_Polylines_Clip)
arcpy.AddMessage("This is how many Polylines there is: " + str(PolylineCount))
Merged_Polygons_Clip = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polygons_Clip"
PolygonCount = arcpy.GetCount_management(Merged_Polygons_Clip)
arcpy.AddMessage("This is how many Polygons there is: " + str(PolygonCount))
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Make a copy the AOI shapefile and place in the Basemap feature dataset.
and
Clip the Points, Polylines and Polygon feature classes to the AOI and store in the Basemap
feature dataset add _Clip to the end of each name.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Make a copy the AOI shapefile and place in the Basemap feature dataset
AOI = r"D:\Python\Data\Amherst_Clip2022.shp"
out_AOI = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\AOI"
arcpy.CopyFeatures_management(AOI, out_AOI)
arcpy.AddMessage("AOI copied")
# Clip the Points, Polylines and Polygon feature classes to the AOI and store in the Basemap feature dataset add _Clip to the end of each name
pointFC = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_points"
clip_feature = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\AOI"
clipped_points = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_points_Clip"
arcpy.Clip_analysis(pointFC, clip_feature, clipped_points)
arcpy.AddMessage("Clipped points")
PolylineFC = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polylines"
clip_feature = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\AOI"
clipped_Polylines = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polylines_Clip"
arcpy.Clip_analysis(PolylineFC, clip_feature, clipped_Polylines)
arcpy.AddMessage("Clipped Polylines")
PolygonFC = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polygons"
clip_feature = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\AOI"
clipped_Polygons = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polygons_Clip"
arcpy.Clip_analysis(PolygonFC, clip_feature, clipped_Polygons)
arcpy.AddMessage("Clipped Polygons")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Merge the Points, Polylines and Polygon feature classes and store outputs in Basemap
feature dataset.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Merge the Points, Polylines and Polygon feature classes and store outputs in Basemap feature dataset
feature_class_to_merge_location = r"D:\Python\Data"
points_to_merge = arcpy.ListFeatureClasses("", "Point")
Merged_points = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_points"
arcpy.Merge_management(points_to_merge, Merged_points)
arcpy.AddMessage("Merged points")
Polylines_to_merge = arcpy.ListFeatureClasses("", "Polyline")
Merged_Polylines = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polylines"
arcpy.Merge_management(Polylines_to_merge, Merged_Polylines)
arcpy.AddMessage("Merged Polylines")
Polygons_to_merge = arcpy.ListFeatureClasses("", "Polygon")
Merged_Polygons = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Basemap\Merged_Polygons"
arcpy.Merge_management(Polygons_to_merge, Merged_Polygons)
arcpy.AddMessage("Merged Polygons")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Create a new feature dataset called Basemap, again same spatial reference as above.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Create a new feature dataset called Basemap, again same spatial reference as above
Basemap_location = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb"
BasemapDataset_Location = Basemap_location
BasemapDataset = "Basemap"
arcpy.CreateFeatureDataset_management(BasemapDataset_Location, BasemapDataset)
arcpy.AddMessage("BasemapDataset made")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Describe each feature class by type (point, polyline and polygon) and print to screen one at a
time.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
###### Using Describe ---------------------------------------------------------------------------------------------------------------------------------
arcpy.AddMessage("USING DESCRIBE")
# Describe each feature class by type (point, polyline and polygon) and arcpy.AddMessage to screen one at a time
feature_class_location = r"D:\Python\Data"
fcList = arcpy.ListFeatureClasses(feature_dataset = "*")
for fc in fcList:
desc = arcpy.Describe(fc)
arcpy.AddMessage("Here's the Feature claas and it type " + str(desc.shapeType))
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Create a list of each shapefile by type (point, polyline and polygon) and print to screen one at a
time.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
###### Using Lists ---------------------------------------------------------------------------------------------------------------------------------
arcpy.AddMessage("USING LISTS")
# Create a list of each shapefile by type (point, polyline and polygon) and print to screen one at a time
shapefile_location = r"D:\Python\Data"
arcpy.AddMessage ("Here's the lists of shapefiles")
point_output_location = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\Point"
Pointlist = arcpy.ListFeatureClasses("", "Point")
arcpy.AddMessage(Pointlist)
for fc in Pointlist:
if 'WA' in fc:
arcpy.AddMessage(fc)
fc1 = fc.split("_") # .split to removes the numeric prefix because its not allowed in a GDB
outfc = fc1[1] + "_" + fc1[0] + "_" + fc1[2] + "_" + fc1[3].rstrip(".shp")
arcpy.CopyFeatures_management(fc, point_output_location + os.sep + outfc) # copy the points
arcpy.AddMessage(point_output_location + os.sep + outfc + " has been copied into the point dataset")
else:
arcpy.AddMessage(fc)
fc2 = fc.split("_") # .split to removes the numeric prefix because its not allowed in a GDB
outfc = fc2[1] + "_" + fc2[0] + "_" + fc2[2].rstrip(".shp")
arcpy.CopyFeatures_management(fc, point_output_location + os.sep + outfc) # copy the points
arcpy.AddMessage(point_output_location + os.sep + outfc + " has been copied into the point dataset")
polyline_output_location = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\polyline"
polylinelist = arcpy.ListFeatureClasses("", "polyline")
arcpy.AddMessage(polylinelist)
for fc in polylinelist:
if 'WA' in fc:
arcpy.AddMessage(fc)
fc1 = fc.split("_") # .split to removes the numeric prefix because its not allowed in a GDB
outfc = fc1[1] + "_" + fc1[0] + "_" + fc1[2] + "_" + fc1[3].rstrip(".shp")
arcpy.CopyFeatures_management(fc, polyline_output_location + os.sep + outfc) # copy the polylines
arcpy.AddMessage(polyline_output_location + os.sep + outfc + " has been copied into the polyline dataset")
else:
arcpy.AddMessage(fc)
fc2 = fc.split("_") # .split to removes the numeric prefix because its not allowed in a GDB
outfc = fc2[1] + "_" + fc2[0] + "_" + fc2[2].rstrip(".shp")
arcpy.CopyFeatures_management(fc, polyline_output_location + os.sep + outfc) # copy the polylines
arcpy.AddMessage(polyline_output_location + os.sep + outfc + " has been copied into the polyline dataset")
polygon_output_location = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb\polygon"
polygonlist = arcpy.ListFeatureClasses("", "Polygon")
removeAOI = polygonlist.remove("Amherst_Clip2022.shp")
arcpy.AddMessage("REMOVED Amherst_Clip2022.shp")
arcpy.AddMessage(polygonlist)
for fc in polygonlist:
if 'WA' in fc:
arcpy.AddMessage(fc)
fc1 = fc.split("_") # .split to removes the numeric prefix because its not allowed in a GDB
outfc = fc1[1] + "_" + fc1[0] + "_" + fc1[2] + "_" + fc1[3].rstrip(".shp")
arcpy.CopyFeatures_management(fc, polygon_output_location + os.sep + outfc) # copy the polygons
arcpy.AddMessage (polygon_output_location + os.sep + outfc + " has been copied into the polygon dataset")
else:
arcpy.AddMessage(fc)
fc2 = fc.split("_") # .split to removes the numeric prefix because its not allowed in a GDB
outfc = fc2[1] + "_" + fc2[0] + "_" + fc2[2].rstrip(".shp")
arcpy.CopyFeatures_management(fc, polygon_output_location + os.sep + outfc) # copy the polygons
arcpy.AddMessage (polygon_output_location + os.sep + outfc + " has been copied into the polygon dataset")
arcpy.AddMessage("LISTS made, REMOVED Amherstt_Clip2022.shp and LOOPED through each list copied each to respective dataset")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Create 3 feature datasets: Point, Polyline and Polygon. Use the Spatial Reference object from
Describe to set the coordinate system of each.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Create 3 feature datasets: Point, Polyline and Polygon. Use the Spatial Reference object from Describe to set the coordinate system of each
New_out_workspace = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb
output_location = r"D:\Python\Data\Results\Assignment2_AniMorrow.gdb"
PointDataset_Location = output_location
PointDataset = "Point"
Point_spatial_reference = Spatial_Reference
arcpy.CreateFeatureDataset_management(PointDataset_Location, PointDataset, Point_spatial_reference)
arcpy.AddMessage("PointDataset made")
PolygonDataset_Location = output_location
PolygonDataset = "Polygon"
Polygon_spatial_reference = Spatial_Reference
arcpy.CreateFeatureDataset_management(PolygonDataset_Location, PolygonDataset, Polygon_spatial_reference)
arcpy.AddMessage("PolygonDataset made")
PolylineDataset_Location = output_location
PolylineDataset = "Polyline"
Polyline_spatial_reference = Spatial_Reference
arcpy.CreateFeatureDataset_management(PolylineDataset_Location, PolylineDataset, Polyline_spatial_reference)
arcpy.AddMessage("PolylineDataset made")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Create a Geodatabase in the new output folder.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Create a user named Geodatabase in the new output folder
GeodatabaseLocation = out_workspace
GeodatabaseName = "Assignment2_AniMorrow.gdb"
arcpy.CreateFileGDB_management(GeodatabaseLocation, GeodatabaseName)
arcpy.AddMessage("Created a Geodatabase")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Use Exists to determine if the user supplied output folder exists (“Results”). If it exists, delete it
and recreate it.
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = r"D:\Python\Data"
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = r"D:\Python\Data\Results"
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Created by: Ani Morrow
# Determine if output folder "Results" exists. If it exists, delete it and recreate it
arcpy.AddMessage(" If Statement: Determine if output folder 'Results' exists. delete it and recreate it")
if arcpy.Exists("Results") == True:
arcpy.Delete_management("Results")
arcpy.AddMessage("Results folder deleted!")
arcpy.CreateFolder_management(workspace, "Results")
arcpy.AddMessage("Results folder recreated!")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Choose one of the shapefiles 1045700064100_BL_POINT.shp and use Describe to obtain the Spatial Reference.
#-----------------------------------------------------------------------------------------------------------------------
### Technique 1: Running the tool with a AddMessage statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
import os,sys,arcpy
from arcpy import env as e
arcpy.AddMessage("Imported arcpy, os, sys, and env as e")
# Try-Except statement
try:
###### Data prep and management ---------------------------------------------------------------------------------------------------
arcpy.AddMessage("DATA PREP AND MANAGEMENT")
# Allow user to specify a workspace and a new output folder. (Use "D:\Python\Data" as the default and folder named "Results")
e.workspace = arcpy.GetParameter(0)
workspace = e.workspace
arcpy.AddMessage("Specified a workspace")
out_workspace = arcpy.GetParameter(1)
e.overwriteOutput = True
arcpy.AddMessage("Specified an out workspace")
# Use Describe to obtain the Spatial Reference
Spatial_Reference = arcpy.Describe("1045700064100_BL_POINT.shp").spatialReference
arcpy.AddMessage("Described to obtain the Spatial Reference and here is the spatial refrence: " + str(Spatial_Reference))
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Add and update a field
Add a field to a feature class and populate that field with values from two other fields.
Use the AddField geoprocessing tool to add a Text field called Name_Loc to the D:\Python\Data\GIST2005_Data.gdb\PortHawkesbury\GSA_Poly feature class.
Populate the new field with values from the GSA_NAME and CO_CODE fields, using the tables
below as a guideline.
### Technique 1: Running the tool with a print statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
print ("Script Starting.......")
# Importing site package
print ("Importing arcpy site package.......")
import arcpy, sys, os
#-------------------------------------------------------------------------------------------------------------------------
# Imports environment settings
print ("Imports environment settings.......")
from arcpy import env
#-------------------------------------------------------------------------------------------------------------------------
# Setting the workspace
print ("Setting the workspace.......\n")# \n adds new line to distinguish sections
env.workspace = r"D:\Python\Data"
ws = env.workspace
print ("The current workspace is: " + ws)
print ("\n")# \n adds new line to distinguish sections
#----------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------------
# Try - Except statement
try:
# Created by Ani Morrow
# Making variables for AddField tool
print ("Making variables for AddField tool")
GSA_Poly = r"GIST2005_Data.gdb\GSA_Poly"
FieldToBeAdded = "Name_Loc"
FieldType = "TEXT"
# AddField to add Name_Loc
print ("AddField starts.")
arcpy.AddField_management(GSA_Poly, FieldToBeAdded, FieldType)
print ("AddField finishes.\n")# \n adds new line to distinguish sections
# Making Variables for UpdateCursor
print ("Making Variables for UpdateCursor")
GSA_Poly = r"GIST2005_Data.gdb\GSA_Poly"
# UpdateCurser to update Name_Loc with values other fields GSA_NAME and CO_CODE
print ("UpdateCurser starts.")
with arcpy.da.UpdateCursor(GSA_Poly, ["GSA_NAME", "CO_CODE", "Name_Loc"]) as cursor:
for row in cursor:
row[2] = row[0] + ", " + row[1]
cursor.updateRow(row)
print ("This is now Name_Loc is now this: " + row[2])
print ("UpdateCurser ended.\n")# \n adds new line to distinguish sections
# Created by Ani Morrow
del cursor
del row
#-------------------------------------------------------------------------------------------------------------------------------
# Script Ending
print ("Script Ended.......")
except:
import traceback
e = sys.exc_info()[1]
print (e.args[0])
print ("ERROR!")
# Clean up after script
del arcpy
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Print the name and area for each GSA in the
D:\Python\Data\GIST2005_Data.gdb\PortHawkesbury\GSA_Poly feature class to the Shell.
Format the output as follows:
Pirate Harbour has an area of 1843743.355469 square metres.
Lennox Passage has an area of 39349105.148739 square metres.
Macdale has an area of 6318247.32921 square metres. etc...
Be sure to use del to delete the cursor object and remove locks.
### Technique 1: Running the tool with a print statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
print ("Script Starting.......")
# Importing site package
print ("Importing arcpy site package.......")
import arcpy, sys, os
#-------------------------------------------------------------------------------------------------------------------------
# Imports environment settings
print ("Imports environment settings.......")
from arcpy import env
#-------------------------------------------------------------------------------------------------------------------------
# Setting the workspace
print ("Setting the workspace.......\n")# \n adds new line to distinguish sections
env.workspace = r"D:\Python\Data"
ws = env.workspace
print ("The current workspace is: " + ws)
print ("\n")# \n adds new line to distinguish sections
#----------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------------
# Try - Except statement
try:
# Putting GSA_Poly inside a variable
print ("Making variable with GSA_Poly")
GSA_Poly = r"GIST2005_Data.gdb\PortHawkesbury\GSA_Poly"
# Created by Ani Morrow
# Making variable with Searchcursor inside
print ("Making variable with SearchCursor inside starts.\n")
GSA_Poly_Cursor = arcpy.da.SearchCursor(GSA_Poly, ["GSA_NAME", "Shape_Area"])
print ("Making variable with SearchCursor inside finished.\n")# \n adds new line to distinguish sections
# For loop to print name and area of each GSA
print ("For loop to print name and area of each GSA starts.\n")
for row in GSA_Poly_Cursor:
print ("{0} has an area of {1} square metres ".format (row[0], row[1]))
print ("\n")
print ("For loop to print name and area of eash GSA finished.")
# Created by Ani Morrow
#-------------------------------------------------------------------------------------------------------------------------------
# Script Ending
print ("Script Ended.......")
except:
import traceback
e = sys.exc_info()[1]
print (e.args[0])
print ("ERROR!")
# Clean up after script
del arcpy
del GSA_Poly_Cursor
del row
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Using the supplied 1:10,000 NSTDB sheet and write a script that will
add a NSTDB feature code table. Enter a new fictitious Feature Code
and Description.
### Technique 1: Running the tool with a print statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
print ("Script Starting.......")
# Importing site package
print ("Importing arcpy site package.......")
import arcpy, sys, os
#-------------------------------------------------------------------------------------------------------------------------
# Imports environment settings
print ("Imports environment settings.......")
from arcpy import env
#-------------------------------------------------------------------------------------------------------------------------
# Setting the workspace
print ("Setting the workspace.......\n")# \n adds new line to distinguish sections
env.workspace = r"D:\Python\Data"
ws = env.workspace
print ("The current workspace is: " + ws)
print ("\n")# \n adds new line to distinguish sections
#----------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------------
# Try - Except statement
try:
# Making variables for InsertCursor
print ("Making variables")
TableToAddRow = "feature_codes.dbf"
# Created by Ani Morrow
# Putting the InsertCursor inside a variable
print ("Making variable with cursor inside starts.")
NewFeatureCode = arcpy.da.InsertCursor(TableToAddRow, ["FEAT_CODE", "DESCRIPT"])
print ("Making variable with cursor inside finished.\n")# \n adds new line to distinguish sections
# Adding the NSTDB feature code row
print ("Making variable with insert row inside starts.")
NewFeatureCode.insertRow(["HO", "House"])
print ("Making variable with insert row inside finished.")
# Created by Ani Morrow
del NewFeatureCode
#-------------------------------------------------------------------------------------------------------------------------------
# Script Ending
print ("Script Ended.......")
except:
import traceback
e = sys.exc_info()[1]
print (e.args[0])
print ("ERROR!")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Using the supplied NSTDB 1:10,000 sheet. Create a script that uses
one shapefile to generate a summary table using FEAT_CODE. Use
cursors to loop through each row of the summary table and print
the summary table results to screen.
### Technique 1: Running the tool with a print statement before stating it is started and after stating it finished
### Technique 2: Adding a print statement stating which tool is running
### Technique 3: Try-Except
#-------------------------------------------------------------------------------------------------------------------------
print ("Script Starting.......")
# Importing site package
print ("Importing arcpy site package.......")
import arcpy, sys, os
#-------------------------------------------------------------------------------------------------------------------------
# Imports environment settings
print ("Imports environment settings.......")
from arcpy import env
#-------------------------------------------------------------------------------------------------------------------------
# Setting the workspace
print ("Setting the workspace.......\n")# \n adds new line to distinguish sections
env.workspace = r"D:\Python\Data"
ws = env.workspace
print ("The current workspace is: " + ws)
print ("\n")# \n adds new line to distinguish sections
#----------------------------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------------------------
# Try - Except statement
try:
# Making variables for InsertCursor
print ("Making variables")
TableToAddRow = "feature_codes.dbf"
# Created by Ani Morrow
# Putting the InsertCursor inside a variable
print ("Making variable with cursor inside starts.")
NewFeatureCode = arcpy.da.InsertCursor(TableToAddRow, ["FEAT_CODE", "DESCRIPT"])
print ("Making variable with cursor inside finished.\n")# \n adds new line to distinguish sections
# Adding the NSTDB feature code row
print ("Making variable with insert row inside starts.")
NewFeatureCode.insertRow(["HO", "House"])
print ("Making variable with insert row inside finished.")
# Created by Ani Morrow
del NewFeatureCode
#-------------------------------------------------------------------------------------------------------------------------------
# Script Ending
print ("Script Ended.......")
except:
import traceback
e = sys.exc_info()[1]
print (e.args[0])
print ("ERROR!")
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)