Calculate the distance between polygons of a table

673
8
05-28-2022 09:58 AM
Labels (1)
User1RECS
New Contributor II

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?

8 Replies
JohannesLindner
MVP Frequent Contributor

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)

Have a great day!
Johannes
0 Kudos
User1RECS
New Contributor II

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

0 Kudos
User1RECS
New Contributor II

Thanks Mr. @JohannesLindner ,

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

0 Kudos
JohannesLindner
MVP Frequent Contributor

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:

JohannesLindner_0-1660307176524.png

 

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?


Have a great day!
Johannes
0 Kudos
User1RECS
New Contributor II

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.

0 Kudos
JohannesLindner
MVP Frequent Contributor

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!)


Have a great day!
Johannes
0 Kudos
User1RECS
New Contributor II

Hello Mr @JohannesLindner ,

I'm sorry but it still doesn't work

User1RECS_0-1660571132702.png

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Hmmm... No idea what the error message means. This is probably something specific to AGOL (I tested in Pro) or to your data.

Is your data publicly available so I could take a look?


Have a great day!
Johannes
0 Kudos