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")