Calculate the distance between polygons of a table

05-28-2022 09:58 AM
Labels (1)
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?

2 Replies
MVP Regular 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!
0 Kudos
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