working version for residue spatial distance extraction