code: def _nearest_query(
      left_df: GeoDataFrame,
      right_df: GeoDataFrame,
      max_distance: float,
      how: str,
      return_distance: bool,
      exclusive: bool,
  ):
      # use the opposite of the join direction for the index
      use_left_as_sindex = how == "right"
      if use_left_as_sindex:
          sindex = left_df.sindex
          query = right_df.geometry
      else:
          sindex = right_df.sindex
          query = left_df.geometry
      if sindex:
          res = sindex.nearest(
              query,
              return_all=True,
              max_distance=max_distance,
              return_distance=return_distance,
              exclusive=exclusive,
          )
          if return_distance:
              (input_idx, tree_idx), distances = res
          else:
              (input_idx, tree_idx) = res
              distances = None
          if use_left_as_sindex:
              l_idx, r_idx = tree_idx, input_idx
              sort_order = np.argsort(l_idx, kind="stable")
              l_idx, r_idx = l_idx[sort_order], r_idx[sort_order]
              if distances is not None:
                  distances = distances[sort_order]
          else:
              l_idx, r_idx = input_idx, tree_idx
          join_df = pd.DataFrame(
              {
                  "_key_left": l_idx,
                  "_key_right": r_idx,
                  "distances": distances,
              }
          )
      else:
          # when sindex is empty / has no valid geometries
          join_df = pd.DataFrame(
              columns=["_key_left", "_key_right", "distances"],
              dtype=float,
          )
      return join_df
  
file_path: geopandas/geopandas/tools/sjoin.py
imports: from typing import Optional
  import warnings
  import numpy
  import pandas
  from geopandas import GeoDataFrame
  from geopandas.array import _check_crs, _crs_mismatch_warn
label_desc: Use image node_id 5 for calling a global function with example usage: _nearest_query(left_df, right_df, max_distance, how, return_distance, exclusive) and returns: join_df