Hello, I have a table with buildings distributed in blocks. I would like to know how to calculate the distance between the neighboring buildings. can you Help me please?
This calculates the distance to the closest building in the same block:
// load the whole feature class var fs_buildings = FeatureSetByName($datastore, "BuildingFC", ["BlockID", "BuildingID"], false) // only select rows with the current feature's BlockID and not this building var block_id = $feature.BlockID var building_id = $feature.BuildingID var fs_buildings_block = Filter(fs_buildings, "BlockID = @block_id AND BuildingID <> @building_id") function closest_feature(fs) { var min_dist = 99999999 var closest_feature = null for(var f in fs) { var f_dist = Distance(f, $feature) if(f_dist < min_dist) { closest_building = f min_dist = f_dist } } return closest_feature } var closest_building = closest_feature(fs_buildings_block) if(closest_building == null) { return null } return Distance($feature, closest_building)
Thanks Mr. Johannes,
i'm sorry for disturbing you again and for my late reaction to your answer.i tried to use the expression you gave me but it doesn't work. could you please check if there is an error in the expression? please. because I can't find the error on my side
Thanks Mr. @JohannesLindner ,
What I suggested should work, but in hindsight it seems overly complicated. I just copied code from my attribute rules where I need access to the closest feature and its attributes.
If you just need the shortest distance and have a feature class like this:
Then you can get the distance to the closest building in the same block like this:
// get the whole featureset var buildings = $featureset // get the $feature's BuildingID and exclude it from the featureset // also get its BlockID and only include buildings from the same block var building_id = $feature.BuildingID var block_id = $feature.BlockID var filtered_buildings = Filter(buildings, "BuildingID <> @building_id AND BlockID = @block_id") // get the distance between the $feature and each other building in the same block var distances = [] for(var b in filtered_buildings) { Push(distances, Distance($feature, b)) } // return the shortest distance return Sort(distances)[0]
If this doesn't work or you need something else, then you'll have to describe it better. What value do you want to get? Is this for a Popup, Attribute Rule, Field Calculation, something else?
Hello Mr. @JohannesLindner , thank you very much for your answer.I would like to calculate a field in which there will be the distances between the closest buildings in the same block. So I would like to have in my table, a field where there will be these distances grouped by block.
OK, so does this do what you want? You can use this as Attribute Rule or in the Field Calculator:
// get all buildings in block var block_id = $feature.BlockID var buildings = Filter($featureset, "BlockID = @block_id") // get the distances between all buildings var distances = [] for(var b1 in buildings) { var building_id = b1.BuildingID var other_buildings = Filter(buildings, "BuildingID <> @building_id") for(var b2 in other_buildings) { Push(distances, Distance(b1, b2)) } } // return the smallest distance between buildings in the block of the current building return Sort(distances)[0]
(If you have many buildings in a block, this will take some time!)
Hello Mr @JohannesLindner ,
I'm sorry but it still doesn't work
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.