Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

The script below does a series of things, and towards the end it is supposed to write values pulled from a list to a new field, if the feature class name is found anywhere in a list element. It does write values but there are strange characters that appear, or it doesn't write the entire list element value. I can't tell what the problem is?

import os, shutil,arcpy,arcgisscripting
gp = arcgisscripting.create()
path = r'C:\Temp\FME_share'
copy_dir = r'C:\test'
gdb_dir = copy_dir+'\\'+'CAD_Export_to_GDB'
BB_gdb_path = gdb_dir+'\\'+'BoundBoxes.gdb'
file_ext_lst = ['.2dm','.2de','.3dm','.3de','.dgn']
ext_to_chng = ['.2dm','.2de','.3dm','.3de']
file_lst = []
TxtVal_lst = []
repr_lst = []
found = dict()
arcpy.env.overwriteOutput = 'TRUE'


for dirpath, dirnames, filenames in os.walk(path):
  for filename in filenames:
      fullPath = os.path.join(dirpath, filename)
      name,ext = os.path.splitext(filename)
      if ext not in file_ext_lst:
          continue
      if name not in found:
          found[name] = fullPath

print 'creating unique list of file names...'
for name,path in found.iteritems():
  file_lst.append(path)

print 'copying files to local drive...'
for f in file_lst:
  file_nm = f.split('\\')[-1]
  shutil.copyfile(f, copy_dir+'\\'+file_nm)

print 'changing file extensions of copied files to .dgn...'
for dirpath, dirnames, copiedfiles in os.walk(copy_dir):
  for copyfile in copiedfiles:
      c_name,c_ext = os.path.splitext(copyfile)
      filename_new = copy_dir+'\\'+c_name+'.dgn'
      if c_ext in ext_to_chng:
          os.rename(copy_dir+'\\'+copyfile, filename_new)

print 'exporting dgn files to geodatabase and exploding complex features...'
for dirpath, dirnames, renamed_files in os.walk(copy_dir):
  for rn_file in renamed_files:
      rn_name, rn_ext = os.path.splitext(rn_file)
      in_dgn = dirpath+'\\'+rn_file
      out_gdb = gdb_dir+'\\'+rn_name+'.gdb'
      if rn_ext == '.dgn':
          gp.ImportCAD_conversion(in_dgn,out_gdb,'','Explode_Complex')

print 'renaming geodatabase output to include original dgn file names...'
for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_dir):
  for filename in filenames:
      gdb_name = dirpath.split('\\')[3]
      orig_dgn_nm = gdb_name.split('.')[0]
      if not dirpath.endswith('.gdb'):
          arcpy.Rename_management(dirpath+'\\'+filename,dirpath+'\\'+filename+'_'+orig_dgn_nm)

print 'joining dgn text attributes to point feature classes, then adding text values to a list for searching out coordinate system information...'
for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_dir):
  for filename in filenames:
      fullPath = dirpath+'\\'+filename
      if 'Point' in filename:
          Point_FC = dirpath+'\\'+filename
        arcpy.JoinField_management(Point_FC,'EntID',dirpath.replace('CADStaging','')+'TxtProp','EntID','TxtValue')
        values = [row[0] for row in arcpy.da.SearchCursor(Point_FC,'TxtValue')]
        unique_vals = set(values)
        unique_vals_str = ' '.join(str(uv) for uv in unique_vals)

        if ('CCS 83' or 'CCS83') and 'ZONE 3' and 'METERS' in unique_vals_str:
            print 'Trying to assign coordinate system to',dirpath,'....'
            arcpy.DefineProjection_management(dirpath,26943)
        elif ('CCS 83' or 'CCS83') and 'ZONE 3' and 'FEET' in unique_vals_str:
            print 'Trying to assign coordinate system to',dirpath,'....'
            arcpy.DefineProjection_management(dirpath,2227)
        elif ('CCS27' or'CCS 27') and 'ZONE 3' in unique_vals_str:
            print 'Trying to assign coordinate system to',dirpath,'....'
            arcpy.DefineProjection_management(dirpath,26743)
        elif ('CCS 83' or 'CCS83') and 'ZONE 2' and 'METERS' in unique_vals_str:
            print 'Trying to assign coordinate system to',dirpath,'....'
            arcpy.DefineProjection_management(dirpath,26942)
        elif ('CCS 83' or 'CCS83') and 'ZONE 2' and 'FEET' in unique_vals_str:
            print 'Trying to assign coordinate system to',dirpath,'....'
            arcpy.DefineProjection_management(dirpath,2226)
        elif ('CCS27' or 'CCS 27') and 'ZONE 2' in unique_vals_str:
            print 'Trying to assign coordinate system to',dirpath,'....'
            arcpy.DefineProjection_management(dirpath,26742)
        else:
            print 'Couldn\'t find coordinate system'

print 'creating minimum bounding box for line feature classes...'
for dirpath, dirnames, filenames in arcpy.da.Walk(gdb_dir):
  for filename in filenames:
      if 'Line' in filename:
          BB_in_fc = dirpath+'\\'+filename
          BB_out_fc = BB_gdb_path+'\\'+'BoundBox'+filename.replace('Line','')
          arcpy.MinimumBoundingGeometry_management(BB_in_fc,BB_out_fc,'RECTANGLE_BY_AREA','ALL')

print 'adding and calculating SHEET_NAME and SHEET_PATH fields to bounding box feature classes, then reprojecting to WGS84...'
for dirpath, dirnames, filenames in arcpy.da.Walk(BB_gdb_path):
  for filename in filenames:
      sheet = filename.replace('BoundBox_','')
      BB_fullpath = dirpath+'\\'+filename
      arcpy.AddField_management(BB_fullpath,'SHEET_NAME','STRING')
    arcpy.CalculateField_management(BB_fullpath,'SHEET_NAME',"'"+sheet+"'","PYTHON_9.3","#")
    arcpy.AddField_management(BB_fullpath,'SHEET_PATH','STRING',200)
    for f in file_lst:
        if sheet in f:
            f_str = str(f)
            print f_str
            arcpy.CalculateField_management(BB_fullpath,'SHEET_PATH','"'+f_str+'"',"PYTHON_9.3","#")
    BB_desc = arcpy.Describe(BB_fullpath)
    BB_CS = BB_desc.spatialReference
    BB_repr_file = BB_gdb_path+'\\'+filename+'_repr'
    BB_repr_CS = arcpy.CreateSpatialReference_management('WGS 1984')
    if '1927' in BB_CS.name:
        arcpy.Project_management(BB_fullpath,BB_repr_file,BB_repr_CS,'NAD_1927_To_WGS_1984_79_CONUS')
    else:
        arcpy.Project_management(BB_fullpath,BB_repr_file,BB_repr_CS,'NAD_1983_To_WGS_1984_1')
    repr_lst.append(BB_repr_file)
merge_fc = BB_gdb_path+'\\'+'BoundBoxes_All'
print 'finally, merging all bounding boxes into one feature class!'
arcpy.Merge_management(repr_lst,merge_fc)
share|improve this question

closed as unclear what you're asking by PolyGeo, Luke, Chad Cooper, Mapperz Jun 15 at 18:35

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Is it possible that it's a char/unicode problem? Try printing the value that you're calculating to the console and see if it's gibberish prior to the calculation. I take it the problem line is at arcpy.CalculateField_management(BB_fullpath,'SHEET_PATH','"'+f_str+'"'," –  Michael Miles-Stimson Jun 15 at 1:37
    
yes that is the line. I did print the value for f_str (and f) and it looks as I would suspect - its a filepath –  kflaw Jun 15 at 1:38
    
I am not sure how do I determine that? I'm using pyscripter. Parts of the value look correct –  kflaw Jun 15 at 1:52
1  
Is your computer language not english? Can you give an example of what does end up in the field.. –  Michael Miles-Stimson Jun 15 at 1:55
2  
Try the VB parser BB_fullpath,'SHEET_PATH','"'+f_str+'"',"VB","#" it could be getting confused as the "\" is an escape character in python. If that doesn't work you might need to use an update cursor instead. –  Michael Miles-Stimson Jun 15 at 2:09
show 6 more comments