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?

1 Reply
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