I need to figure out how to determine which feature in a dataset has the most features within a defined radius.I'm thinking I need to loop through the entire dataset, perform a Select Layer by Locaiton function, load the result ID and number of features into a list, and continue iterating only updating the list if another feature comes along with a higher number of features.I'm open to ideas on how to best accomplish this.
>>> inFeatureLayer = "Retailers" ... ScoreField = "PTS" ... SearchRadius = ".5 Miles" ... # Determine what the highest set of values is in the FeatureLayer ... maxValue = arcpy.SearchCursor(inFeatureLayer, "", "", "", ScoreField + " D").next().getValue(ScoreField) ... # Select the records which have the max value in the dataset ... arcpy.SelectLayerByAttribute_management(inFeatureLayer, "NEW_SELECTION", '"' + ScoreField + '" = ' + str(maxValue)) ... # Get a list of the StoreIDs of the currently selected records ... StoreList = list(r[0] for r in arcpy.da.SearchCursor(inFeatureLayer, "STOREID")) ... # Establish a variable to hold the StoreID of the record with the most features intersecting its radius ... MaxRecord = "" ... HighestSelected = 0 ... # Iterate through the StoreList ... for Store in StoreList: ... print "Evaluating Store " + str(Store) + "..." ... # Create a new selection, selecting the current store in the iteration ... arcpy.SelectLayerByAttribute_management(inFeatureLayer, "NEW_SELECTION", '"STOREID" = ' + "'" + str(Store) + "'") ... # Select the Stores within search radius of the current Store ... arcpy.SelectLayerByLocation_management(inFeatureLayer, "WITHIN_A_DISTANCE", inFeatureLayer, SearchRadius, "NEW_SELECTION") ... # Create a Describe Object of the Stores Layer ... desc = arcpy.Describe(inFeatureLayer) ... # Get the Number of Selected Records ... SelectCount = len(desc.FIDSet) ... # If the SelectCount is greater than the current HighestSelected Count ... if SelectCount > HighestSelected: ... print "Returned " + str(SelectCount) + " stores within " + SearchRadius + " of Store " + str(Store) + " which is greater than the current Selection Count of " + str(HighestSelected) + " for Store " + str(MaxRecord) ... HighestSelected = SelectCount ... MaxRecord = Store ... # Otherwise ... else: ... print "Returned " + str(SelectCount) + " stores within " + SearchRadius + " of Store " + str(Store) + " which is less than the current Selection Count of " + str(HighestSelected) + " for Store " + str(MaxRecord) ... # Do Nothing and Continue ... pass ... print "StoreID: " + str(MaxRecord) + " was the record with the most features within " + SearchRadius + " with a total count of " + str(HighestSelected) + "."
storesFC = r"C:\temp\stores.shp" trackingDict = {} maxCount = 0 maxCountStoreId = 0 arcpy.MakeFeatureLayer_managment(storesFC, "stores_fl") searchRows = arcpy.da.SearchCursor(storesFC, ["OID@", "SHAPE@"]) for searchRow in searchRows: storeOidValue, storeGeomObj = searchRow[0:2] arcpy.SelectLayerByLocation_management(storeGeomObj, "WITHIN_A_DISTANCE", "stores_fl", "10 MILES", "NEW_SELECTION") selectedStoresOidList = [r[0] for r in arcpy.da.SearchCursor("stores_fl", ["OID@"])] trackingDict[storeOidValue] = selectedStoresOidList if len(trackingDict[storeOidValue]) > maxCount: maxCount = len(trackingDict[storeOidValue]) maxCountStoreId = storeOidValue del searchRow, searchRows print "Store OID with the largest number of other stores close by is: " + str(maxCountStoreId) + " (" + str(maxCount) + " other stores close by)"
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.