Select to view content in your preferred language

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

2015
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
ArkadiuszMatoszka
Frequent Contributor
You can create geometry from coordinates:
http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000006t000000

Than use arcpy.SelectLayerByLocation_management to select feature layers:

http://resources.arcgis.com/en/help/main/10.1/index.html#/Select_Layer_By_Location/00170000007200000...

And search cursor to extract values.

Maybe someone will have better idea, but that should do.

Hope it will help.

Cheers.
0 Kudos
ChristopherThompson
Frequent Contributor
Arcadius' suggestions are probably the right direction, but there are other things you need to handle to.  For instance, what do you need as some sort of 'output' from the tool? For instance, do you need that output into a table or a text file or associated with a point feature class (where the points are your input coordinates), or does this need to be something that feeds another process as in a web application? How do you do you plan to supply the coordinates at which you need the information - user input into a form? a mouse click in a map? from a point shapefile that is created in advance? Those kind of interactions (input and output) can greatly influence all the particulars between.  If you can help with those details that will help direct the feedback get.
0 Kudos
ChristiNelson1
Deactivated User
For instance, do you need that output into a table or a text file or associated with a point feature class (where the points are your input coordinates), or does this need to be something that feeds another process as in a web application? How do you do you plan to supply the coordinates at which you need the information - user input into a form? a mouse click in a map? from a point shapefile that is created in advance? Those kind of interactions (input and output) can greatly influence all the particulars between. If you can help with those details that will help direct the feedback get.

So, the user will click on the map to get the XY coordinates.  Those coordinates will then be used as the input.  Output will be a  window that looks similar to the widow that the identify tool displays in ArcMap (but it will only show a specific record from each feature class).

Hope that helps,
Christi
0 Kudos
ChristiNelson1
Deactivated User
Any ideas about how to get those xy coordinates from the 'click' into the rest of the process?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Christi,

Are you using ArcGIS 10 or 10.1?

The difficult part would be creating the output dialog box that mimics the identify tool's dialog box.  You could easily write the information to the python window, but I'm not sure if that would work for you.
0 Kudos
ChristiNelson1
Deactivated User
Hi,

Im in 10.0.  I think for now, just writing it to the python window and then being able to use the coordinates as input to be able to select the intersecting layers in the .mxd would be the best bet.  I just don't know how to do that. 

I think another programmer could then call this information/tool and bring it into javascript or other code for a pop-up. 

Thoughts?
0 Kudos
ChristiNelson1
Deactivated User
I found a great script below to get me started on getting XY from a click, but I need help with modifying.

This script is set up as a Feature Set type of tool input.  So it allows user to interactively click on screen to get a temporary point location.  It copies the coordinates to temp table, where the output is copied to a clipboard.

Im wondering, instead of copying XY coords to the clipboard,  how I can get them to be given as an output that can be used an input variable for my next tool....  Can someone help modify?  Code is below.  The tool sample (script and toolbox) can be downloaded here:
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=0BF2E736-1422-2418-3416-5AA91...

import arcpy, os, tempfile
import win32clipboard as w
##import win32con

input = arcpy.GetParameterAsText(0)

temp_table = tempfile.mktemp(suffix='.txt')

output = open(temp_table, "w")

cur = arcpy.SearchCursor(input)

for row in cur:
    geom = row.Shape
    X = geom.centroid.X
    Y = geom.centroid.Y
    output.write(str(X) + " " + str(Y) + " ")
   
##output.close()
   
output = open(temp_table, "r")
XY = output.read()
##FROM HERE IS WHERE I NEED ADVICE ON HOW TO GET XY AS A VARIABLE FOR AN INPUT PARAMETER TO MY NEXT TOOL##
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(w.CF_TEXT, XY)
w.CloseClipboard()
0 Kudos
JakeSkinner
Esri Esteemed Contributor
That's actually a script that I wrote a while ago that I'm trying to modify.  I'm trying to alter the XY coordinates and create a point geometry from them.  Ex:

point = arcpy.Point(-88.24873, 34.232039)
ptGeometry = arcpy.PointGeometry(point)

arcpy.CopyFeatures_management(ptGeometry, "c:/temp/python/test.gdb/points")


However, the output is empty.  If I add two sets of points, it works:

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


I'm trying to figure out why I cannot create a PointGeometry with a single point.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
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).
0 Kudos