Python - GetCount help

3840
3
Jump to solution
04-16-2015 03:41 AM
MarkWisniewski
New Contributor III

Hi all,

I am having trouble getting the arcp.GetCount_management to print the GetCount arcpy.AddMessage in the geoprocessing dialog window;

# 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))
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

HI Ernst,

I don't believe you can use 'format' for the arcpy.AddMessage function.  Try the following instead:

arcpy.AddMessage(str(projCount) + " datasets have been reprojected to: " + spatialRef + " and saved in " + OutFolder)

View solution in original post

3 Replies
JohannesBierer
Occasional Contributor III

For me this does the job:

Count_Lines = arcpy.GetCount_management(Meka) 

print Count_Lines ?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

HI Ernst,

I don't believe you can use 'format' for the arcpy.AddMessage function.  Try the following instead:

arcpy.AddMessage(str(projCount) + " datasets have been reprojected to: " + spatialRef + " and saved in " + OutFolder)

XanderBakker
Esri Esteemed Contributor

Although you marked you question as solved already, let me add some additional information. You can use a format statement inside the arcpy.AddMessage statement. I do it all the time and there is really no reason why you shouldn't.

I do see some strange indenting in you code, which may be the reason for a possible error. Was there an error occurring? Look at the slightly changed code below (I commented the project and copy statements)

import arcpy, os
template = arcpy.GetParameterAsText(0)
arcpy.env.workspace = arcpy.GetParameterAsText(1)
OutFolder = arcpy.GetParameterAsText(2)

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

# create a list of featureclasses in the current workspace
fcList = arcpy.ListFeatureClasses()

#Loop through shapfiles in folder and reproject
for fc in fcList:
    fcspatialRef = arcpy.Describe(fc).spatialReference.name
    if fcspatialRef != spatialRef:
        arcpy.AddMessage("Projecting: {0}".format(fc))
        # arcpy.Project_management(fc, os.path.join(outFolder , fc), template)
        process = "reprojected to"
    else:
        arcpy.AddMessage("Copying: {0}".format(fc))
        process = "copied using"
        # arcpy.CopyFeatures_management(fc, os.path.join(outFolder,fc))

    projCount = int(arcpy.GetCount_management(fc).getOutput(0))
    # Get the count from GetCount's Result object
    arcpy.AddMessage("{0} features (not datasets) have been {3}: {1} and saved in {2}".format(projCount, spatialRef, OutFolder, process))

Depending on the inputs selected, this produced the following output in the geoprocessing window.

In case the sr is different:

Executing: revisarmensajes D:\Xander\GeoNet\DistFireHouse\datatest\firehistory.shp D:\Xander\GeoNet\DistFireHouse\shp D:\Xander\GeoNet\DistFireHouse\test
Start Time: Thu Apr 16 06:50:31 2015
Running script revisarmensajes...
Projecting: firehistory_NAD_fips3002_feet.shp
636 features (not datasets) have been reprojected to: NAD_1983_UTM_Zone_13N and saved in D:\Xander\GeoNet\DistFireHouse\test
Projecting: housesaledata.shp
7204 features (not datasets) have been reprojected to: NAD_1983_UTM_Zone_13N and saved in D:\Xander\GeoNet\DistFireHouse\test
Completed script revisarmensajes...
Succeeded at Thu Apr 16 06:50:31 2015 (Elapsed Time: 0,08 seconds)

In case the sr is the same:

Executing: revisarmensajes D:\Xander\GeoNet\DistFireHouse\shp\firehistory_NAD_fips3002_feet.shp D:\Xander\GeoNet\DistFireHouse\shp D:\Xander\GeoNet\DistFireHouse\test
Start Time: Thu Apr 16 06:51:17 2015
Running script revisarmensajes...
Copying: firehistory_NAD_fips3002_feet.shp
636 features (not datasets) have been copied to: NAD_1983_StatePlane_New_Mexico_Central_FIPS_3002_Feet and saved in D:\Xander\GeoNet\DistFireHouse\test
Copying: housesaledata.shp
7204 features (not datasets) have been copied to: NAD_1983_StatePlane_New_Mexico_Central_FIPS_3002_Feet and saved in D:\Xander\GeoNet\DistFireHouse\test
Completed script revisarmensajes...
Succeeded at Thu Apr 16 06:51:17 2015 (Elapsed Time: 0,08 seconds)