-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvDatabaseFields.py
More file actions
59 lines (58 loc) · 2.61 KB
/
EnvDatabaseFields.py
File metadata and controls
59 lines (58 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
print "loading arcpy"
import arcpy
import os
inFolder = r"C:\temp\shapes"
intersectFolder = os.path.join(inFolder, "int")
gridFile = r"C:\Users\Jonah\Documents\ArcGIS\Default.gdb\grid"
arcpy.env.workspace = r"C:\Users\Jonah\Documents\ArcGIS\Default.gdb"
layerName = r"C:\Users\Jonah\Documents\ArcGIS\Default.gdb\gridLayer"
arcpy.MakeFeatureLayer_management(gridFile, layerName)
if not os.path.exists(intersectFolder):
os.makedirs(intersectFolder)
for f in os.listdir(inFolder):
if f.endswith(".shp"):
print "processing", f
inFile = os.path.join(inFolder, f)
intFile = os.path.join(intersectFolder, f)
if not os.path.exists(intFile):
try:
print "intersecting", f
arcpy.Intersect_analysis([inFile, gridFile], intFile, "ALL")
except BaseException as e:
print e
if "area" not in [field.baseName for field in arcpy.ListFields(intFile)]:
try:
print "adding area field to intersected shapefile"
arcpy.AddField_management(intFile, "area", "DOUBLE")
except BaseException as e:
print e
try:
arcpy.CalculateField_management(intFile, "area", "!shape.area!" , "PYTHON_9.3")
except BaseException as e:
print e
oldfieldName = f.split(os.extsep)[0]
newfieldName = os.path.basename(gridFile).split(os.extsep)[0] + "." + f.split(os.extsep)[0]
fieldList = [f.baseName for f in arcpy.ListFields(gridFile)]
if oldfieldName not in fieldList:
try:
print "adding shapefile name field to grid"
arcpy.AddField_management(layerName, oldfieldName, "DOUBLE")
except BaseException as e:
print e
try:
print "joining shapefile to grid"
arcpy.AddJoin_management(layerName, "ID", intFile, "ID")
except BaseException as e:
print e
try:
print "calculating proportional area"
gridFilename = os.path.basename(gridFile).split(os.extsep)[0]
arcpy.CalculateField_management(layerName, newfieldName, "[" + str(oldfieldName) + ".area]/[" + gridFilename + ".Shape_Area]", "VB")
except BaseException as e:
print e
try:
print "removing join"
arcpy.RemoveJoin_management(layerName, oldfieldName)
except BaseException as e:
print e
print ""