0

Context: I'm working on AGOL notebooks

I have a feature layer (points). So, I'd like to randomly select several points based on the value in one of the columns ('level_' column).

For instance, I'd like to randomly select 16 points of level 1 and 10 of level 2, then I will use the result to plan a route using plan_routes(). The thing is that I don't want the two results separate because I need them to be in the same feature so I can use them as input for planning the routes.

In the code below I queried the feature layer to get only the points of level 1 & 2.

# query parcels - level 1 & 2
m_levels = test.query(where="level_= '1' OR level_='2'")
m_levels

1 Answer 1

1

The below will select a user defined number of level 1 and level 2 records at random from the dataset, just update the num_level1 and num_level2 variables. I have commented the code for ease of use. Make sure to add your item id and the layer index. You could turn the random selection for each into a function.

from arcgis import GIS
import random

agol = GIS("home")

item = agol.content.get("***item_id***")

## update the index here to that of the layer you require
lyr = item.layers[0]

num_level1 = 16
num_level2 = 10

################################################################
## lets get random level 1 features ############################

## get all feature with level 1, return only IDs
level1 = lyr.query(where="level_ = 1", return_ids_only=True)

## extract ids from dictionary to list
l1_objids = level1["objectIds"]

## get the highest index in the list
level1_upper = len(l1_objids) - 1

## we will populate this with the amount of objectids
## stated in num_level1
random_level1 = []

## while the list doesnt meet thenumber of random entries
while len(random_level1) < num_level1:
    ## get a random index
    random_index = random.randint(0, level1_upper)
    ## get the onjectid at that index
    objectid = l1_objids[random_index]
    ## and if its not already in the list then add it
    if objectid not in random_level1:
        random_level1.append(objectid)

##print(random_level1)

################################################################
## lets get random level 2 features ############################
level2 = lyr.query(where="level_ = 2", return_ids_only=True)

l2_objids = level1["objectIds"]

level2_upper = len(l2_objids) - 1

random_level2 = []

while len(random_level2) < num_level2:
    random_index = random.randint(0, level2_upper)
    objectid = l2_objids[random_index]
    if objectid not in random_level2:
        random_level2.append(objectid)

##print(random_level2)

################################################################
## combine the lists
all_objids = random_level1 + random_level2
## convert items to string
all_objids = [str(objid) for objid in all_objids]

##print(all_objids)

## create a query string to return only objectids in all_objids
query_exp = "OBJECTID IN ('{0}')".format("','".join(all_objids))

## query out this records only
records = lyr.query(where=query_exp)

print(len(records))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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