Improve spatial code generation #631