add XY not work for calculate XY coordinate

492
3
Jump to solution
04-29-2012 12:31 AM
ElaineKuo
Occasional Contributor
Dear all,

I have a python code to calculate longitude and latitude for points in shapefiles.
(ArcGIS 9.3 and python 2.5)

However, the function of AddXY did not work.
Please kindly help and thank you in advance.


Code
##Script Name: XY coordinates
##Description: Get centroid X and Y of shapefiles
##Created By: Elaine Kuo
##Date: 29/04/2012


#Import standard library modules
import win32com.client, sys, os

#Create the Geoprocessor object
GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
GP.Workspace= "G:/temp_stage_3_centroid_of_B_NB/Sylviidae_86"

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "G:/temp_stage_3_centroid_of_B_NB/Sylviidae_86"


try:
    #Get a list of the featureclasses in the input folder
    fcs = GP.ListFeatureClasses()

    # Loop through the above list
    fcs.reset
    fc = fcs.next()


    while fc:
           
            #Validate the new feature class name for the output workspace.
            outFeatureClass =GP.ValidateTableName(fc,outWorkspace)

           # Get centroids of the feature classes
            # a single feature class
            GP.Toolbox = "Data Management"
           
           
            # add two new fields

            GP.AddField_management(fc, "Logitude", "DOUBLE", 4, 4)
            GP.AddField_management(fc, "Latitude", "DOUBLE", 4, 4)

   
            # Add X Y coordinate to the feature classes
            GP.Toolbox = "Data Management"
            GP.AddXY(fc)
         
   

except:
    GP.AddMessage(GP.GetMessages(2))
    print GP.GetMessages(2)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
markdenil
Occasional Contributor III
This should work better:
###------------------------------ #Import standard library modules  <- use arcgisscripting instead of win32com.client import arcgisscripting, sys, os  #Create the Geoprocessor object GP = arcgisscripting.create(9.3)  # use arcgisscripting.create() if your Arc is < 9.3  #Set the workspace. GP.Workspace= r"C:\test" GP.Toolbox = "Data Management" # <- should not have to be loaded  try: #Get a list of the featureclasses in the input folder     fcs = GP.ListFeatureClasses()     for fc in fcs:  # <- fcs is a list, not a cursor, so you do not use .next()         # Add X Y coordinate to the feature classes         GP.AddXY_management(fc)  except:     GP.AddMessage(GP.GetMessages(2))     print GP.GetMessages(2) 


You do realize that your source file is in Behrmann's, so the coordinates are NOT latitude and longitude, eh?
Behrmann's uses meters, not degrees.
Project it to a geographic coordinate space first, so you get the units you want.

View solution in original post

0 Kudos
3 Replies
markdenil
Occasional Contributor III
First off: use the CODE tags around your python code, so it displays properly.... (use the # formatting key)

This code clearly seems lifted from something larger, and it is not very cleanly extracted....

for instance, you have a comment about "# Get centroids of the feature classes"
and you validate a new feature class name:
outFeatureClass =GP.ValidateTableName(fc,outWorkspace)

but you never actually make a point feature class out of what I am gussing may be the centroids of a polygon feature class....

AddXY only works on points, so if you are feeding it something else it won't work.
Is every feature class in the workspace a point fc?

Again, you add Logitude and Latitude fields to the original feature class (not to the one you never make, the one with the validated name...)
and then you pass the original feature class to AddXY....
But AddXY adds its own items: POINT_X and POINT_Y...

.... and you add the Data management toolbox twice.... INSIDE a loop, so it is added twice for every processes feature class.....

It is hard to debug a script that doesn't really seem to be all there, or at least logically trimmed.
0 Kudos
ElaineKuo
Occasional Contributor
Thanks for comments.

I corrected the code and please kindly advise any improvement to make the batch process smoothly.
(ArcGIS 9.3)
Also, a shapefile is attached for trial.
Thanks a lot.

Code

##Script Name: XY coordinates
##Description: Get longitude and latitude of point shapefile
##Created By: Elaine Kuo
##Date: 29/04/2012


#Import standard library modules
import win32com.client, sys, os

#Create the Geoprocessor object
GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

#Set the workspace.
GP.Workspace= "G:/temp_stage_3_centroid_of_B_NB/Sylviidae_86"

#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "G:/temp_stage_3_centroid_of_B_NB/Sylviidae_86"


try:
    #Get a list of the featureclasses in the input folder
    fcs = GP.ListFeatureClasses()

    # Loop through the above list
    fcs.reset
    fc = fcs.next()

    while fc:

    #Validate the new feature class name for the output workspace.
        outFeatureClass =GP.ValidateTableName(fc,outWorkspace)

    # add two new fields
    GP.AddField_management(fc, "POINT_X", "DOUBLE", 4, 4)
    GP.AddField_management(fc, "POINT_Y", "DOUBLE", 4, 4)


    # Add X Y coordinate to the feature classes
    GP.Toolbox = "Data Management"
    GP.AddXY(fc)

except:
    GP.AddMessage(GP.GetMessages(2))
print GP.GetMessages(2)
0 Kudos
markdenil
Occasional Contributor III
This should work better:
###------------------------------ #Import standard library modules  <- use arcgisscripting instead of win32com.client import arcgisscripting, sys, os  #Create the Geoprocessor object GP = arcgisscripting.create(9.3)  # use arcgisscripting.create() if your Arc is < 9.3  #Set the workspace. GP.Workspace= r"C:\test" GP.Toolbox = "Data Management" # <- should not have to be loaded  try: #Get a list of the featureclasses in the input folder     fcs = GP.ListFeatureClasses()     for fc in fcs:  # <- fcs is a list, not a cursor, so you do not use .next()         # Add X Y coordinate to the feature classes         GP.AddXY_management(fc)  except:     GP.AddMessage(GP.GetMessages(2))     print GP.GetMessages(2) 


You do realize that your source file is in Behrmann's, so the coordinates are NOT latitude and longitude, eh?
Behrmann's uses meters, not degrees.
Project it to a geographic coordinate space first, so you get the units you want.
0 Kudos