if layer.name.lower() == "BND_GDT_CNTY":
#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")
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?
if layer.name.upper() == "BND_GDT_CNTY":
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
logpath = r"C:\temp\python\file.txt" if os.path.exists(logpath): os.remove(logpath) f = open(logpath, "w") mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] for layer in arcpy.mapping.ListLayers(mxd, '', df): if layer.name.lower() == "cities": arcpy.MakeFeatureLayer_management(layer, "cities_lyr") arcpy.SelectLayerByLocation_management("cities_lyr", "INTERSECT", "point", "30 FEET", "NEW_SELECTION") rows = arcpy.SearchCursor("cities_lyr") for row in rows: f.write(row.NAME) del row, rows f.close()
arcpy.CopyFeatures_management(ptGeometry, r"C:\temp\python\POINT.shp") arcpy.MakeFeatureLayer_management(r"C:\temp\python\POINT.shp", "point")
arcpy.CopyFeatures_management(ptGeometry, "IN_MEMORY/POINT") arcpy.MakeFeatureLayer_management("IN_MEMORY/POINT", "point")
I was able to get this working if I wrote the PointGeometry to a shapefile rather than an IN_MEMORY feature, or a feature class. Here's an example:import arcpy arcpy.env.overwriteOutput = 1 input = arcpy.GetParameterAsText(0) rows = arcpy.SearchCursor(input) for row in rows: geom = row.shape X = geom.centroid.X Y = geom.centroid.Y point = arcpy.Point(X, Y) ptGeometry = arcpy.PointGeometry(point) arcpy.CopyFeatures_management(ptGeometry, r"C:\temp\python\POINT.shp") arcpy.MakeFeatureLayer_management(r"C:\temp\python\POINT.shp", "point") mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] for layer in arcpy.mapping.ListLayers(mxd, '', df): if layer.name.lower() == "cities": arcpy.MakeFeatureLayer_management(layer, "cities_lyr") arcpy.SelectLayerByLocation_management("cities_lyr", "INTERSECT", "point", "30 FEET", "NEW_SELECTION") rows = arcpy.SearchCursor("cities_lyr") for row in rows: arcpy.AddMessage(row.NAME) print row.NAME del row, rows arcpy.Delete_management(r"C:\temp\python\POINT.shp")You can add more 'if' statements for the other layers you would like to identify, and alter the 'arcpy.AddMessage' to display the info you would like to show. The info will be reported to the GP windows, or to the 'Results' window > Messages. I couldn't find a way to display the info to the Python window at 10.0, but this works using an add-in at 10.1.Also, I believe since I used a shapefile the XY coordinates were slightly different when trying to identify a point feature class. I had to adjust the search distance to around 30 feet in the Select Layer by Location function to identify the correct point (city in the above example).
import arcpy arcpy.env.overwriteOutput = 1 input = arcpy.GetParameterAsText(0) rows = arcpy.SearchCursor(input) for row in rows: geom = row.shape X = geom.centroid.X Y = geom.centroid.Y point = arcpy.Point(X, Y) ptGeometry = arcpy.PointGeometry(point) arcpy.CopyFeatures_management(ptGeometry, r"C:\temp\python\POINT.shp") arcpy.MakeFeatureLayer_management(r"C:\temp\python\POINT.shp", "point") mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] for layer in arcpy.mapping.ListLayers(mxd, '', df): if layer.name.lower() == "cities": arcpy.MakeFeatureLayer_management(layer, "cities_lyr") arcpy.SelectLayerByLocation_management("cities_lyr", "INTERSECT", "point", "30 FEET", "NEW_SELECTION") rows = arcpy.SearchCursor("cities_lyr") for row in rows: arcpy.AddMessage(row.NAME) print row.NAME del row, rows arcpy.Delete_management(r"C:\temp\python\POINT.shp")
point = arcpy.Point(-88.24873, 34.232039) ptGeometry = arcpy.PointGeometry(point) arcpy.CopyFeatures_management(ptGeometry, "c:/temp/python/test.gdb/points")
pointList = [[-88.24873, 34.232039], [-77.99395, 34.232196]] point = arcpy.Point() pointGeometryList = [] for pt in pointList: point.X = pt[0] point.Y = pt[1] pointGeometry = arcpy.PointGeometry(point) pointGeometryList.append(pointGeometry) arcpy.CopyFeatures_management(pointGeometryList, "c:/temp/python/test.gdb/points")
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.