Select to view content in your preferred language

Batch Define script not working

921
5
09-26-2012 01:18 PM
AshleyMott
Occasional Contributor
I use an old ESRI developed batch define (projection) script in a model and it was working until recently. The script has not been altered, so I am unsure of what sparked the change in performance.

Here is the error I am getting:
Executing: DefineDIAGRID M:\EDS_Library\Scratch_Workspace\Ashley\Default.gdb\newRWsigns_dwg_Polyline_og;M:\EDS_Library\Scratch_Workspace\Ashley\Default.gdb\newRWsigns_dwg_Polygon_og
Start Time: Wed Sep 26 14:57:53 2012
Executing (Batch Define Coordinate System): BatchDefine M:\EDS_Library\Scratch_Workspace\Ashley\Default.gdb\newRWsigns_dwg_Polyline_og;M:\EDS_Library\Scratch_Workspace\Ashley\Default.gdb\newRWsigns_dwg_Polygon_og PROJCS['DIA_GRID',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',-142028.98],PARAMETER['False_Northing',-679801.16],PARAMETER['Central_Meridian',-105.5],PARAMETER['Standard_Parallel_1',38.45],PARAMETER['Standard_Parallel_2',39.75],PARAMETER['Scale_Factor',1.00024481],PARAMETER['Latitude_Of_Origin',37.83333333333334],UNIT['Foot_US',0.3048006096012192]] #
Start Time: Wed Sep 26 14:57:53 2012
Running script BatchDefine...
<type 'exceptions.SyntaxError'>: EOF while scanning triple-quoted string literal (BatchDefineSR.py, line 94)
Failed to execute (Batch Define Coordinate System).
Failed at Wed Sep 26 14:57:53 2012 (Elapsed Time: 0.00 seconds)
Failed to execute (DefineDIAGRID).
Failed at Wed Sep 26 14:57:53 2012 (Elapsed Time: 0.00 seconds)


and here is the script (developed by ESRI):
""" #####################################################
 NAME: BatchDefineSR
 Source Name: BatchDefineSR.py
 Version: ArcGIS 9.0
 Author: Environmental Systems Research Institute Inc.
 Usage: BatchDefineSR <input_data>, {output_coordinate_system}, {template_geodataset}
 Required Arguments: A set of input geodatasets or layers
                     
 Optional Arguments: A Coordinate System. This is not required if a template feature class is used.
                     A template geodataset. This is not required if a Coordinate System is used.
 Description: Define spatial reference on multiple input geodatasets.  
             A coordinate system can be specified by defining it manually or using the coordinate system of an
             existing geodataset. 
 Date Created: June 4, 2003
 Updated: February, 15, 2005
 Updated: September 20, 2007
                 - use arcgisscripting.create(9.3)
                 - add progress bar to indicate percent of work done
                 - centralize error messages with error number
##################################################### """

###Import required modules
import ConversionUtils, time

#Define message constants so they may be translated easily
msgCoordinateSystem = "Must Enter a Spatial Reference or Template Geodataset."
msgInvalidParameters = "Invalid number of parameters. Spatial Reference or Template must be provided."
msgPrjAlreadyDefine = "The dataset already has a projection defined."
unknown_projection = "Unknown"

try:

    #Set the input datasets
    inputs  = ConversionUtils.gp.GetParameterAsText(0)
    inDatasets = ConversionUtils.SplitMultiInputs(inputs)
    
    #Set the spatial reference
    out_CS = ConversionUtils.gp.GetParameterAsText(1)

    #Set the template dataset
    temp_data = ConversionUtils.gp.GetParameterAsText(2)

    #Set output boolean parameter "Completed" to false by default.    
    ConversionUtils.gp.SetParameterAsText(3, 0)

    #Set output boolean parameter "Error" to false by default.        
    ConversionUtils.gp.SetParameterAsText(4, 0)

    #Set the spatial reference. Check for template dataset.
    if (out_CS == "" or out_CS == "#") and (temp_data == "" or temp_data == "#"):
        raise ConversionUtils.GPError(msgCoordinateSystem) 
    elif (out_CS != "") and (out_CS != "#"):
        sr = out_CS
    elif (temp_data != "") and (temp_data != "#"):
        dsc = ConversionUtils.gp.Describe(temp_data)
        sr = dsc.SpatialReference

    ConversionUtils.gp.SetProgressor("step", "Defining projection for multiple datasets", 0, len(inDatasets))
    
    #Loop through each dataset and define projection on it.
    for dataset in inDatasets:
        try:
            ConversionUtils.gp.SetProgressorLabel("Defining projection for " + dataset) 
            #Describe input dataset to check if a projection is already defined.
            dsc_Dataset = ConversionUtils.gp.Describe(dataset)
            cs_Dataset = dsc_Dataset.SpatialReference
            #Check if a projection is already define for the input dataset.
            if cs_Dataset.Name != unknown_projection:
                ConversionUtils.gp.AddWarning(msgPrjAlreadyDefine)
            #Define the Projection
            ConversionUtils.gp.DefineProjection_management(dataset, sr)
            ConversionUtils.gp.AddMessage("Defined projection for %s successfully" % (dataset))
        except:
            #If an error set output boolean parameter "Error" to True.
            err = ConversionUtils.gp.GetMessages(2)
            ConversionUtils.gp.SetParameterAsText(3, `0`)
            ConversionUtils.gp.SetParameterAsText(4, `1`)
            ConversionUtils.gp.AddWarning(err)
            #ConversionUtils.gp.AddError(err)
            
        ConversionUtils.gp.SetProgressorPosition()
    
    time.sleep(0.5)

    #If output boolean parameter remains false after define projection, set Completed to True.    
    if ConversionUtils.gp.GetParameterAsText(4) == "false":
        ConversionUtils.gp.SetParameterAsText(3, `1`)
            
except Exception, ErrorDesc:
    #If an error set output boolean parameter "Error" to True.
    ConversionUtils.gp.SetParameterAsText(3, `0`)
    ConversionUtils.gp.SetParameterAsText(4, `1`)
    ConversionUtils.gp.AddError(str(ErrorDesc))


Thank you
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Honored Contributor
Those don't look like proper unicode quotation marks.
`1`
should be
'1'
etc

Not sure how that issue would just appear over night though. Or why they are there in the first place. May just be a hold over from the older version of python. I don't get a syntax error with it but it just doesn't look right with those characters.
0 Kudos
NeilAyres
MVP Alum
I also couldn't get this old samples script to go in 10.1 Yes, did see those funny ` and wondered what they were doing.
So ended up hacking my own script for arcpy from the body of this.

py script attached.

If any of you py experts want to improve this, please do as long as you post the new improved version back here of course.
I need all the help I can get.

Cheers,
Neil
0 Kudos
AshleyMott
Occasional Contributor
Wow, thanks guys! Neil, I tried to use your script and got the following error:
Executing: BatchDefine M:\EDS_Library\Scratch_Workspace\Ashley\Default.gdb\RWsigns PROJCS['DIA_GRID',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',-142028.98],PARAMETER['False_Northing',-679801.16],PARAMETER['Central_Meridian',-105.5],PARAMETER['Standard_Parallel_1',38.45],PARAMETER['Standard_Parallel_2',39.75],PARAMETER['Scale_Factor',1.00024481],PARAMETER['Latitude_Of_Origin',37.83333333333334],UNIT['Foot_US',0.3048006096012192]]
Start Time: Mon Oct 08 14:45:47 2012
Running script BatchDefine...
Object: Error in getting parameter as text
Completed script BatchDefine...
Failed to execute (BatchDefine).
Failed at Mon Oct 08 14:45:48 2012 (Elapsed Time: 1.00 seconds)


I found one thread discussing this kind of error, but it didn't give me any insight to my problem: http://forums.arcgis.com/threads/14229-python-arcmap-9.3.1-quot-error-in-getting-parameter-as-text-q...

I think that it might have to do with me using your script with a ArcToolbox "script tool." I use two parameters in the script tool: one geodataset parameter and one coordinate system parameter.

Thank you for any additional help. I truly appreciate it.
0 Kudos
AshleyMott
Occasional Contributor
I filed an ESRI incident for this, so I will post back with answers. I also realized the script called for 3 parameters and my script tool only had 2.
0 Kudos
AshleyMott
Occasional Contributor
So, I didn't get to the cause of the problem with the original script through my incident report to ESRI. Honestly, I didn't really care "why" it wasn't working, I just wanted to get back to work.

I got the following script to work with a ArcToolbox "script tool." The tool's parameters are: "Input Feature Class or Dataset" - data type is dataset and is set Multivalue = yes and "Coordinate System" - data type is projection file.

Here is my new baby sized script:
import arcpy

features    = arcpy.GetParameterAsText(0)
prj         = arcpy.GetParameterAsText(1)

featurelist = features.split(";")

arcpy.AddMessage(features)
arcpy.AddMessage(featurelist)

for feature in featurelist:
    arcpy.AddMessage("Processing: ")
    arcpy.DefineProjection_management(feature.strip("'"), prj)
    arcpy.AddMessage(feature + " defined")

0 Kudos