3

Can anyone help by suggesting how to specify the units when using the SelectLayerbyLocation_management tool. I am selecting by "WITHIN_A_DISTANCE" and the ArcGIS resources do not suggest how to do this. I am getting some funny results at the moment.

4

The answer is actually buried in the syntax description of the tool itself. Here is a screenshot of the quick help for how to set up the SelectLayerbyLocation_management tool. Select Layer By Location Python Help

I highlighted the section where it discusses the search distance. If you look, the following syntax is shown: search_distance {Linear unit}:. What this means is that you can enter the search distance as a number. What is optional is the unit of measure that you enter after the number. The trick is finding a list of the appropriate units of measure.

A quick search of the help documentation brings up not a list of the units, but a response to an error of an Invalid Linear Unit type.
From the error solution:

These are unit of measure keywords: CENTIMETERS | DECIMAL DEGREES | DECIMETERS | FEET | INCHES | KILOMETERS | METERS | MILES | MILLIMETERS | NAUTICAL MILES | POINTS | UNKNOWN | YARDS.

This unit of measure is optional. If you do not specify the linear unit, then it is not completely clear what unit is used, but I think it is the unit of measure as defined by the spatial reference of your Selecting Feature Layer.

There is one place that comes up when you search for Linear unit, and that is: About distance units and editing. I would caution you about using these though, because they may be more detailed than what is allowed in the selection tool.

Another place to look, if you ever forget the options, is to open up the Select By Location Window in ArcGIS. The pulldown next to the box for Apply a search distance, contains all of the applicable linear units. Simple use the all caps version of these in the python tool. Select By Location Window with Units dropdown

|improve this answer|||||
0

Just extending get-spatial's answer to show more explicitly the required code:

distance = 400 # your distance here (could be a string as well)
linearUnit = distance + " Meters" # use any of the above provided measurement keywords
arcpy.SelectLayerByLocation_management(targetLayer, "WITHIN_A_DISTANCE", inputLayer, linearUnit)

This worked for me. Hope this helps.

|improve this answer|||||
  • You probably already know this, but your distance should be a string if you're intending to add another string to it (as you've done with 'Meters' above). That, or your middle line should be linearUnit = str(distance) + " Meters" – Mike Davlantes Jul 7 '15 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.