-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoptimize_attribute_table.py
More file actions
75 lines (53 loc) · 2.6 KB
/
optimize_attribute_table.py
File metadata and controls
75 lines (53 loc) · 2.6 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Optimizing geospatial data with appropriate field lengths
'''
Some attributes tables are created without considering optimal field leghth especially for string data type. Instead the field leght is set
with large values like 254. This large field lengths in multiple fileds with many rows cause heavy file size not suitable for attched or upload to
internet.
By calculating max lenght of text in the fields and create the field with this max string lenght value greatly reduce the file size. In some cases 1:10
ratio.
'''
# credit - Kyaw Naing Win, Ye Htet Aung, OneMap.- 2020 June 10
# software - arcpy
import arcpy
import os
source_fc ="your_dir_path\\your_shapefile.shp"
out_folder = r"your_\dir_path\_to_store_new_file" #e.g "C:\temp"
buffer_length = 0 # add a few extra length to max length of the string if you wish, to define length in string field in new file
# create a new file with same name but empty
out_name = os.path.basename(source_fc)
geometry_type = arcpy.Describe(source_fc).shapeType.upper()
arcpy.CreateFeatureclass_management (out_folder, out_name, geometry_type)
arcpy.DeleteField_management (target_fc, "Id") #Id filed is created by default and need to delete
# extract schema from source
fields = arcpy.ListFields(source_fc)
schema = []
for field in fields:
mylen =0
if field.type == "Double" or field.type=="Float":
schema.append([field.name,field.type,field.length, field.precision, field.scale])
elif field.type=='String':
for row in arcpy.da.SearchCursor(source_fc,field.name):
len0 =len(row[0])
if len0>mylen:
mylen=len0
schema.append([field.name,field.type,field.length,mylen])
else:
schema.append([field.name,field.type,field.length])
# create attribute table in target with same schema as source file
target_fc = out_folder+"\\"+out_name
for index,item in enumerate(schema):
if index>1:
NAME = item[0]
TYPE = item[1]
LENGTH = int(item[2])
if TYPE == "Double" or TYPE=="Float":
PRECISION = int(item[3])
SCALE = int(item[4])
arcpy.AddField_management(target_fc,NAME,TYPE,field_precision=PRECISION,field_scale=SCALE,field_length=LENGTH,)
if TYPE =='String':
LENGTH =int(item[3])+buffer_length
arcpy.AddField_management(target_fc,NAME,TYPE,field_length=LENGTH)
else:
arcpy.AddField_management(target_fc,NAME,TYPE,field_length=LENGTH)
# copy features and attributes from source to target
arcpy.Append_management(source_fc,target_fc)