Created
August 24, 2015 16:17
-
-
Save csmoore/b643a5d3595ebb2fbbd0 to your computer and use it in GitHub Desktop.
Reads a csv with the ArcGIS Feature Class field specifications and adds those fields to the selected Feature Class (with "AddField" GP Tool)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------- | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
#------------------------------------------------------------------------------- | |
# Name: AddFeatureClassFieldsFromCsv.py | |
# Description: Reads a csv with the Feature Class field specification and | |
# adds those fields to the selected feature class | |
#------------------------------------------------------------------------------- | |
# Requires: ArcGIS, arcpy, Python 2 or 3 | |
#------------------------------------------------------------------------------- | |
import csv | |
import os | |
import sys | |
import arcpy | |
# Sample File/Spec: (to run test, save as as "FeatureClassFieldSpec.csv" and set folder path below) | |
# | |
# field_name,field_type,field_length,field_alias,nullability,field_domain | |
# test_text,TEXT,123,Test Text,NULLABLE, | |
# test_long,LONG,,Test Long,NULLABLE, | |
# test_double,DOUBLE,,Test Double,NULLABLE, | |
def addFeatureClassFieldsFromCsv() : | |
arcpy.AddMessage('ArcPy Working') | |
testFilePath = 'C:/{Put Sample CSV and GDB in this folder}/Data' | |
inputFile = os.path.normpath(os.path.join(testFilePath, r"FeatureClassFieldSpec.csv")) | |
# This Gdb should exist | |
outputGDB = os.path.normpath(os.path.join(testFilePath, r'TestGdb.gdb')) | |
outputFC = os.path.normpath(os.path.join(outputGDB, r'TestPoints')) | |
# Check Input | |
try : | |
desc = arcpy.Describe(outputFC) | |
if desc == None : | |
print('Could not open Feature Class: ' + str(outputFC)) | |
print('Exiting...') | |
return | |
except Exception as openEx : | |
print('Could not open Feature Class: ' + str(outputFC)) | |
print('Exiting...') | |
return | |
try : | |
if sys.version < '3': # Python 2 or 3 check for csv difference | |
csv_fp=open(inputFile, 'rb') | |
else: | |
csv_fp=open(inputFile, 'r') | |
except Exception as openEx : | |
print('Could not open file for reading: ' + str(inputFile)) | |
print('Exiting...') | |
return | |
reader = csv.reader(csv_fp) | |
next(reader, None) # skip header row | |
# Read though csv and get values/field specifications | |
for row in reader: | |
print(row) | |
if len(row) < 5 : | |
print('Skipping bad row') | |
# Assumed csv column order: field_name,field_type,field_length,field_alias,nullability,field_domain | |
field_name = row[0] | |
field_type = row[1] | |
field_length = row[2] | |
field_alias = row[3] | |
nullability = 'NULLABLE' | |
if len(row) > 4 : | |
nullability = row[4] | |
field_domain = '' | |
if len(row) > 5 : | |
field_domain = row[5] | |
# Now add a field with that spec to the feature class | |
# AddField_management doc: http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000047000000 | |
arcpy.AddField_management(outputFC, field_name, field_type, '', '', field_length, \ | |
field_alias, nullability, field_domain) | |
if __name__ == '__main__': | |
addFeatureClassFieldsFromCsv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment