2

I have multiple shapefiles with bars names and location attributes and I want to create a script which asks the user for the area or shapefile name where they want to go and then prints a random location from inside that shapefile.

i am using arcGIS 10.2.2 writing the script on python win I am new to Python.

here is the script i currently have... i was just trying to print all the names of the locations in this script but i am having trouble with the cursor. i know i do not need to do this to complete my final task...

import random

import arcpy

arcpy.env.workspace = r"G:\finalproject"

x = raw_input("Which nightlife area would you like to visit?")

fc = "2nd.shp"

fc2 = "4th.shp"

fc3 = "Downtown.shp"

fc4 = "LongBeach.shp"

if x == Second:

    cursor = arcpy.da.SearchCursor(fc, ["Name"])

    print "Bar Name: ", cursor

elif x == Fourth:

    cursor = arcpy.da.SearchCursor(fc2, ["Name"])

    print "Bar Name: ", row[0]

elif x == Downtown:

    cursor = arcpy.da.SearchCursor(fc3, ["Name"])

    print "Bar Name: ", row[0]


else:

    cursor = arcpy.da.SearchCursor(fc4, ["Name"])

    print "Bar Name: ", row[0]

del row 

del cursor

My GIS skills greatly overpower my Programming skills.

  • 2
    Have a look at Random docs.python.org/2/library/random.html, do you have any code so far? If so can you edit that into your question. I would suggest Select Layer by Location to refine results to an area and then return a random row within that. – Michael Stimson May 13 '15 at 22:53
  • 3
    Are you using ArcGIS for Desktop? If so, which version? Both may be important details to edit into your question. – PolyGeo May 13 '15 at 22:54
  • my shape files were created using arcGIS10.2.2. My main goal is to have a script which asks the user which shapefile to use and based off the one they chose it prints a random location from inside that shape file... if that helps. thank you – erik May 14 '15 at 0:31
  • By location do you mean x,y or suburb or bar (or any combination of)? For location you can get the extent object from the describe statement then RandomX = ext.XMin + (random.random() * ext.Width) likewise for the Y.. is that the sort of thing you're after? – Michael Stimson May 14 '15 at 0:51
  • By location i mean the name of the bar... from the name attribute in my shape files – erik May 14 '15 at 1:14
6

There is a large amount of documentation from ESRI on creating your own tools in Python, so I will assume that you can access your selected layer without issue. There are many methods for selecting data randomly, for example choosing in random number in the range of the number of features (using the random module as suggested by @MichaelMiles-Stimson above), and selecting a feature with that FID. Given you know the size of the datasets from the start this should be the method you use.

One other method that I quite like is Reservoir Sampling which is a method to iterate over data of indeterminate size and select an element with a probability of 1/N. You can implement this in using an arcpy.SearchCursor as follows:

import random
import arcpy

path = "path/to/feature/class"

cursor = arcpy.SearchCursor(path)
keep = None
for n, row in enumerate(cursor, start=1):
    if random.random() < 1.0 / n:
        keep = row
print(row.getValue("Bar name"))

#cleanup afterwards
del row, cursor
  • 1
    I've not seen enumerate.. that sounds handy, does it work for arcpy.da cursors as well? Perhaps when you hit 'keep' break the loop so it no longer searches, returning the first valid entry found. Don't forget to del cursor... +1 BTW. – Michael Stimson May 14 '15 at 0:55
  • 3
    @MichaelMiles-Stimson enumerate works for any iterable object happily. As for breaking, it would work if you knew in advance what index you were after, but the Reservoir search requires a loop over the entire stream (importantly only one loop though, which is why it's used when you don't know how long the stream is going to be, and when you can't index into the stream). Also, updated the code to delete the cursor once you're done - thanks! – om_henners May 14 '15 at 1:32

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.