Select to view content in your preferred language

Python "attribute information tool for xy location"  HELP!  IM A BEGINNER!

2012
24
09-13-2012 01:53 PM
ChristiNelson1
Deactivated User
A co-worker of mine has a couple of web applications that need to call a Geoprocessing Tool.

The tool creation has been assigned to me - but I am a  novice Python programmer. The tool is supposed to act like the 'i' (identify) button on ArcGIS desktop, but instead of returning info about one feature class, it needs to return only one specific attriburte from many feature classes. 

Specifically, it should return city, county, basemap and other feature layer information for a given coordinate. She has requested such a function be created by using Python in ArcMap. The input values are: cooidinate_east, cooidinate_north. The output values are: city_name, county_nm, bmap_code.


Is this possible?  Suggestions?
Christi
Tags (2)
0 Kudos
24 Replies
MathewCoyle
Honored Contributor
Hi there,

I have a question about this part of the code:   if layer.name.lower() == "cities":

why do you put ".name.lower()"  what does .lower mean?  The lowest layer in the dataframe toc?  Is it necessary to write this?  I will have more than one layer in mine..

Thanks for clarifying.
Christi



It is so it can pick whatever layer is called "cities" regardless of capitalization. So it would return "Cities", "CITIES", "cities" etc.
0 Kudos
ChristiNelson1
Deactivated User
Hi all,

I've had to change the script to just accept coordinates that are written into a .py script.  The programmer doesn't want a tool - just a script that has been tested successfully for a set of coordinates to retrieve attribute values for that point.  So I've changed the script to the following.  The commented out code is potential code for writing the output values to a .txt file, but for now I jsut want the values printed in the PythonWin Interactive Window, so that I can see what I'm getting.  My code seems to be stuck at "for layer in arcpy.mapping.ListLayers(mxd, '', df):
    print(layer)".

It prints the layers into the Interactive Window and then gives me a message ".py returned exit code 0".  Nothing else processes after this step and I don't get any error messages to help me. Attached is a .zip with a .gdb, .py and .mxd.  

Suggestions?


#import modules
import arcpy, os, tempfile
arcpy.env.overwriteOutput = 1

#Create workspace
arcpy.env.workspace = r"H:\Projects\LocationDetector\Scratch.gdb"





#Create a Point Object from coordinates
point = arcpy.Point(6088757.617, 2077662.44)

#Print point properties to make sure entered correctly
print("Point properties:")
print(" ID: {0}".format(point.ID))
print(" X:  {0}".format(point.X))
print(" Y:  {0}".format(point.Y))


#Create Point Geometry object using Spatial Reference Factory Code
ptGeometry = arcpy.PointGeometry(point, 2227)

#Create a copy ofthe point geometry object by using ptGeometry as input to the CopyFeatures tool
arcpy.CopyFeatures_management(ptGeometry, r"H:\Projects\LocationDetector\Scratch.gdb\POINT")

#Use the POINT feature and create a feature layer called "point_lyr"                          
arcpy.MakeFeatureLayer_management(r"H:\Projects\LocationDetector\Scratch.gdb\POINT", "point_lyr")



###Create table to write coordinates into.  Make sure file doesn't already exist.  If it does, remove file.
##logpath = r"H:\Projects\LocationDetector\file.txt"
##if os.path.exists(logpath):
##    os.remove(logpath)
##
###create variable to open .txt file with write access.
##f = open(logpath, "w")

##go to .mxd and first dataframe. 
mxd = arcpy.mapping.MapDocument(r"H:\Projects\LocationDetector\LDtest3.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

#list layers in dataframe, go to counties layer and make it a feature layer.
#select dataframe feature layer by location using point feature layer ("point_lyr" created above from coordinates)
for layer in arcpy.mapping.ListLayers(mxd, '', df):
    print(layer)
    if layer.name.lower() == "BND_GDT_CNTY":
        arcpy.MakeFeatureLayer_management(layer, "counties_lyr")
        arcpy.SelectLayerByLocation_management("counties_lyr", "INTERSECT", "point_lyr", "30 FEET", "NEW_SELECTION")
        rows = arcpy.da.SearchCursor("counties_lyr", "", "", "NAME")

        currentState = ""
#Iterate thru the rows in the cursor
        for row in rows:
            if currentState != row.NAME:
                currentState = row.NAME
#Print out the county name
            print "County: %s" % \
                  (row.NAME)
           
##            print(row.NAME)
##            f.write(row.NAME)
           
        del row, rows


##f.close()
arcpy.Delete_management(r"H:\Projects\LocationDetector\Scratch.gdb\POINT")
0 Kudos
MathewCoyle
Honored Contributor
Hi all,

I've had to change the script to just accept coordinates that are written into a .py script.  The programmer doesn't want a tool - just a script that has been tested successfully for a set of coordinates to retrieve attribute values for that point.  So I've changed the script to the following.  The commented out code is potential code for writing the output values to a .txt file, but for now I jsut want the values printed in the PythonWin Interactive Window, so that I can see what I'm getting.  My code seems to be stuck at "for layer in arcpy.mapping.ListLayers(mxd, '', df):
    print(layer)".

It prints the layers into the Interactive Window and then gives me a message ".py returned exit code 0".  Nothing else processes after this step and I don't get any error messages to help me. Attached is a .zip with a .gdb, .py and .mxd.  

Suggestions?


I think you forgot to attach your files. Also to post code in the forums use [ CODE] [/ CODE] tags.

At a cursory inspection this line will never return anything.

if layer.name.lower() == "BND_GDT_CNTY":

You'll want this I believe.
if layer.name.upper() == "BND_GDT_CNTY":
0 Kudos
ChristiNelson1
Deactivated User
I am unable to upload attachments, for some reason its telling me they are too large (3200 kb).  Oh well.  It was just an mxd with a feature class in it, plus the .gdb that contained it.  Also included was the python script (indented below):

#import modules
import arcpy, os, tempfile
arcpy.env.overwriteOutput = 1

#Create workspace
arcpy.env.workspace = r"H:\Projects\LocationDetector\Scratch.gdb"





#Create a Point Object from coordinates
point = arcpy.Point(6088757.617, 2077662.44)

#Print point properties to make sure entered correctly
print("Point properties:")
print(" ID: {0}".format(point.ID))
print(" X:  {0}".format(point.X))
print(" Y:  {0}".format(point.Y))


#Create Point Geometry object using Spatial Reference Factory Code
ptGeometry = arcpy.PointGeometry(point, 2227)

#Create a copy ofthe point geometry object by using ptGeometry as input to the CopyFeatures tool
arcpy.CopyFeatures_management(ptGeometry, r"H:\Projects\LocationDetector\Scratch.gdb\POINT")

#Use the POINT feature and create a feature layer called "point_lyr"                           
arcpy.MakeFeatureLayer_management(r"H:\Projects\LocationDetector\Scratch.gdb\POINT", "point_lyr")



###Create table to write coordinates into.  Make sure file doesn't already exist.  If it does, remove file. 
##logpath = r"H:\Projects\LocationDetector\file.txt"
##if os.path.exists(logpath):
##    os.remove(logpath)
##
###create variable to open .txt file with write access.
##f = open(logpath, "w")

##go to .mxd and first dataframe.  
mxd = arcpy.mapping.MapDocument(r"H:\Projects\LocationDetector\LDtest3.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

#list layers in dataframe, go to counties layer and make it a feature layer.
#select dataframe feature layer by location using point feature layer ("point_lyr" created above from coordinates)
for layer in arcpy.mapping.ListLayers(mxd, '', df):
    print(layer)
    if layer.name.lower() == "BND_GDT_CNTY":
        arcpy.MakeFeatureLayer_management(layer, "counties_lyr")
        arcpy.SelectLayerByLocation_management("counties_lyr", "INTERSECT", "point_lyr", "30 FEET", "NEW_SELECTION")
        rows = arcpy.da.SearchCursor("counties_lyr", "", "", "NAME")

        currentState = ""
#Iterate thru the rows in the cursor
        for row in rows:
            if currentState != row.NAME:
                currentState = row.NAME
#Print out the county name
            print "County: %s" % \
                  (row.NAME)
            
##            print(row.NAME)
##            f.write(row.NAME)
            
        del row, rows


##f.close()
arcpy.Delete_management(r"H:\Projects\LocationDetector\Scratch.gdb\POINT")
0 Kudos
MathewCoyle
Honored Contributor
As I said before this line seems to be the culprit.

if layer.name.lower() == "BND_GDT_CNTY":


Nothing after that will process. You will want to replace .lower with .upper as I posted above.
0 Kudos