Select to view content in your preferred language

Facing Problem while Join managment through Python

3416
5
04-01-2013 10:09 PM
MatthewWeed
Emerging Contributor
Hi,

I am automating LAYER dissolve process through pyhthon almost done.But faced some strange thing today.

While running the python script from ARCMAP every thing is running fine(PFA screenshot), but when I am running same script from PYSCRIPTER or COMMAND Prompt. It shows me Error 000840.

While I am abel to loacte the new created Feature table through catalog, any Idea for same.

Following is the Script for same


# PermanentJoin.py
# Purpose: Join two fields from a table to a feature class
# Author: ESRI
# Import system modules
import arcpy
from arcpy import env
import os
import sys
# Set the local parameters
conStr=r"C:\Users\174314\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\SDE@ME5P.sde"
# Set the current workspace
env.workspace = conStr
levelNameIntial="AZ_US_"
alignment_id=111113
joinField = "ZIP"
joinTable = r"C:\PythonScripts\CSV\lilly_iAlign_10_qc.csv"
csvFileName="lilly_iAlign_10_qc.Csv."
fieldList = []
levelStartCounter=1
levelFielStartCounter=1
totLevel=5
mapEnviornment="QC"
alignmentDataJoinField="zip_code"
#outWorkspace = "c:/Output/output.gdb"
outWorkSpace=""
dissolveFieldName=""
alignmentDatotLeveltaJoinField="ZIP_CODE"
baseZipPoly='SDE.C_ZIP_POLY_2008_ESRI'
mxdFilePath=r'C:\GISWORKAREA\Python'
#Set Qualified name to fase for ommiting longer column name
env.qualifiedFieldNames = False
symbFilePath=r'C:\PythonScripts\Symbology'
connectionFilePath = r'C:\Users\174314\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\CTSUSADCGISPOC.ags'

def CreateFeatureDataSet(connectiontr,dataSetname):
    print 'Feature Dataset creation process start'
    sr=""
    try:
        sr = arcpy.Describe(baseZipPoly).spatialReference
        arcpy.CreateFeatureDataset_management(conStr, dataSetname, sr)
    except IOError:
        print 'Fail to Create Dataset', sys.exc_info()[0]
    print 'Feature Dataset creation process start Dataset Name-' + dataSetname

def CreateZipPoly(baseZipPoly,dissolveTableName):
    print("ZIP Poly file Creation Start")
    try:
        arcpy.Copy_management(baseZipPoly,dissolveTableName)
        #print(dissolveTableName)
    except:
        print "Unexpected error While copying ZIP Poly:", sys.exc_info()[0]
    print("ZIP Poly file Creation End")


#Creating FeatureDatset


def JoinTable(dissolveTableName,joinField,joinTable, alignmentDataJoinField):
    print("Joining Process Start" + dissolveTableName + joinTable)
    try:
        arcpy.AddJoin_management(dissolveTableName,joinField,joinTable, alignmentDataJoinField,"KEEP_COMMON")
    except:
        print "Unexpected error While Joining data:", sys.exc_info()[0]
        raise
    print 'Join process end'





dataSet_name = levelNameIntial + mapEnviornment + "_" + str(alignment_id)
outWorkSpace = os.path.join(conStr, dataSet_name)
dissolveTableName=levelNameIntial + mapEnviornment +  "_ZIP_POLY_" + str(alignment_id)
dissolveTableName=os.path.join(outWorkSpace, dissolveTableName)

#Create Data Set
CreateFeatureDataSet(conStr,dataSet_name)


#Create a new ZIP Poly by copying from one existing please ensure baseZipPoly is exist on current schema
CreateZipPoly(baseZipPoly,dissolveTableName)

#Append SDE in front of Dissolve Table for join process
dissolveTableName= "SDE." + levelNameIntial +mapEnviornment +  "_ZIP_POLY_" + str(alignment_id)
#Join Process
JoinTable(dissolveTableName,joinField,joinTable, alignmentDataJoinField)
0 Kudos
5 Replies
by Anonymous User
Not applicable
Original User: Cogni_Rx

I am simplifying problem bit more

This line is working perfectly while I am executing python script from ARCMAP

    arcpy.AddJoin_management(os.path.join(conStr,'SDE.AZ_US_QC_ZIP_POLY_1111146'),'ZIP','C:\\PythonScripts\\CSV\\lilly_iAlign_10

But while I am running following script through COMMAND PROMPT,it's showing me error mentiond earlier.

#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:     
#
# Created:     02/04/2013
# Copyright:   (c) 174314 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import arcpy
from arcpy import env
import os
import sys
conStr=r"C:\Users\174314\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\SDE@ME5P.sde"
joinTable = r"C:\PythonScripts\CSV\lilly_iAlign_10_qc.csv"
# Set the current workspace
env.workspace = conStr

def main():
    arcpy.AddJoin_management(os.path.join(conStr,'SDE.AZ_US_QC_ZIP_POLY_1111146'),'ZIP','C:\\PythonScripts\\CSV\\lilly_iAlign_10_qc.csv', 'ZIP_CODE',"KEEP_COMMON")
    pass

if __name__ == '__main__':
    main()
0 Kudos
KimOllivier
Honored Contributor
I would never attempt to use a CSV file as an input table. There are just too many things to go wrong with the schema. Invalid fields names, incorrect guesses of fields types and null values. There are clearly differences when using the interactive interface in ArcMap, it must do a bit more validation such as checking for indexes.

The first thing is to define a schema for the CSV using the Microsoft schema.ini definition. This helps the software stop having to guess the format of the CSV. Then it will load the rows into a nicely defined table with valid field names, instead of all numbers being floats and all strings being 255 characters.

Next, copy the temporary virtual "CSV-table" into a Real Database table, such as a file geodatabase using CopyRows, something that can have a primary unique indexed key. Your join key can also be indexed and validated. For example if it is supposed to be an integer, if the field is defined as a float the join will not work.

Now your JoinField tool has a chance of working.
0 Kudos
by Anonymous User
Not applicable
Original User: Cogni_Rx

Hi Kim,

Thanks for reply, but I think there is some gap actually this script is running fine while I am executing this from ARCMAP python editor,but when I am executing this from command prompt then the error is comming so question is I am missing some point to run this script from command prompt?
0 Kudos
KimOllivier
Honored Contributor
Yes, if you run from the command prompt you will not be using a Layer and will not have a selection. You will be accessing the featureclass directly because it happens to have the same name.

Try renaming the layer different from the featureclass, run in Python window and then run the same script from the command line. It will fail with an error message "no such featureclass".

To get the same count you would have to save the layer to a file while it has a selection and then reference that layer file (a definition of a featureclass with selection) in the command line.
0 Kudos
by Anonymous User
Not applicable
Original User: miao6798

You need to create a layer first then use the layer as input for AddJoin tool:

    lyr = arcpy.MakeFeatureLayer_management('featureClassName', lyr)
    arcpy.AddJoin_management(lyr, 'column1', joinTable, 'column2','KEEP_COMMON')

http://resources.arcgis.com/en/help/main/10.1/index.html#//001700000064000000
The input must be a feature layer, a table view, or a raster layer that has an attribute table; it cannot be a feature class or table.

http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000006p000000
arcpy.MakeFeatureLayer_management (in_features, out_layer, {where_clause}, {workspace}, {field_info})
0 Kudos