1

I am having trouble with GetCount to work in my script. It is not printing the GetCount arcpy.AddMessage in the geoprocessing window dialog;

# Get the spatial reference
 spatialRef = arcpy.Describe(template).spatialReference.name

#Loop through shapfiles in folder and reproject
 for fc in fcList:
     fcspatialRef = arcpy.Describe(fc).spatialReference.name
     if fcspatialRef != spatialRef:
         arcpy.AddMessage(fc)
         arcpy.Project_management(fc, outFolder + "\\" + fc, template)
         projCount = int(arcpy.GetCount_management(fc).getOutput(0)) 
     else:
         arcpy.CopyFeatures_management(fc, outFolder + "\\" + fc)

# Get the count from GetCount's Result object
     arcpy.AddMessage("{0} datasets have been reprojected to: {1} and saved in {2}".format(projCount, spatialRef, OutFolder))
  • 2
    GetCount counts the number of features in one feature class, not the number of datasets in a workspace. Are you trying to print the latter? – GISGe Apr 16 '15 at 11:03
  • 2
    In addition, projCount will not be printed if the 'else' condition is met. – GISGe Apr 16 '15 at 11:05
  • Yes GISGe, I am trying to do the latter. Oh, I've seen that result, lol and couldn't work out why that number was appearing.. I want to know many datasets (ie4) have been reprojected. Do you have a suggestion on what I should be using? – Mark W Apr 16 '15 at 11:07
3

If you want to print the number of datasets that have been projected (not those that have just been copied), you should then create a 'count' variable and increment it each time you project a dataset. Be careful with the indentation of your code, the message must be added outside the loop (once, after all datasets have been reviewed).

count = 0
#Loop through shapfiles in folder and reproject
for fc in fcList:
  fcspatialRef = arcpy.Describe(fc).spatialReference.name
    if fcspatialRef != spatialRef:
      arcpy.AddMessage(fc)
      arcpy.Project_management(fc, outFolder + "\\" + fc, template)
      count += 1
    else:
      arcpy.CopyFeatures_management(fc, outFolder + "\\" + fc)

# print the count variable
arcpy.AddMessage("{0} datasets have been reprojected to: {1} and saved in {2}".format(count, spatialRef, OutFolder))
  • 2
    You can create a separate count variable (e.g. count2) before the loop, and increment it the same way but under the else block. – GISGe Apr 16 '15 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.