traffic/core/flight.py
def distance(  # noqa: F811
        self,
        other: Union[None, "Flight", "Airspace", Polygon, PointMixin] = None,
        column_name: str = "distance",
    ) -> Union[None, float, "Flight", pd.DataFrame]:

        """Computes the distance from a Flight to another entity.

        The behaviour is different according to the type of the second
        element:

        - if the other element is None (i.e. flight.distance()), the method
          returns a distance in nautical miles between the first and last
          recorded positions in the DataFrame.

        - if the other element is a Flight, the method returns a pandas
          DataFrame with corresponding data from both flights, aligned
          with their timestamps, and two new columns with `lateral` and
          `vertical` distances (resp. in nm and ft) separating them.

        - otherwise, the same Flight is returned enriched with a new
          column (by default, named "distance") with the distance of each
          point of the trajectory to the geometrical element.

        .. warning::

            - An Airspace is (currently) considered as its flattened
              representation
            - Computing a distance to a polygon is quite slow at the moment.
              Consider a strict resampling (e.g. one point per minute, "1T")
              before calling the method.

        """

        if other is None:
            first = self.at_ratio(0)
            last = self.at_ratio(1)
            if first is None or last is None:
                return 0
            return (
                geo.distance(
                    first.latitude,
                    first.longitude,
                    last.latitude,
                    last.longitude,
                )
                / 1852  # in nautical miles
            )

        if isinstance(other, PointMixin):
            size = len(self)
            return self.assign(
                **{
                    column_name: geo.distance(
                        self.data.latitude.values,
                        self.data.longitude.values,
                        other.latitude * np.ones(size),
                        other.longitude * np.ones(size),
                    )
                    / 1852  # in nautical miles
                }
            )

        from .airspace import Airspace  # noqa: F811

        if isinstance(other, Airspace):
            other = other.flatten()

        if isinstance(other, Polygon):
            bounds = other.bounds

            projection = pyproj.Proj(
                proj="aea",  # equivalent projection
                lat_1=bounds[1],
                lat_2=bounds[3],
                lat_0=(bounds[1] + bounds[3]) / 2,
                lon_0=(bounds[0] + bounds[2]) / 2,
            )

            transformer = pyproj.Transformer.from_proj(
                pyproj.Proj("epsg:4326"), projection, always_xy=True
            )
            projected_shape = transform(transformer.transform, other)

            self_xy = self.compute_xy(projection)

            return self.assign(
                **{
                    column_name: list(
                        projected_shape.exterior.distance(p)
                        * (-1 if projected_shape.contains(p) else 1)
                        for p in MultiPoint(
                            list(zip(self_xy.data.x, self_xy.data.y))
                        )
                    )
                }
            )

        start = max(self.start, other.start)
        stop = min(self.stop, other.stop)
        f1, f2 = (self.between(start, stop), other.between(start, stop))
        if f1 is None or f2 is None:
            return None

        cols = ["timestamp", "latitude", "longitude", "altitude"]
        cols += ["icao24", "callsign"]
        if "flight_id" in f1.data.columns:
            cols.append("flight_id")
        table = f1.data[cols].merge(f2.data[cols], on="timestamp")

        return table.assign(
            lateral=geo.distance(
                table.latitude_x.values,
                table.longitude_x.values,
                table.latitude_y.values,
                table.longitude_y.values,
            )
            / 1852,  # in nautical miles
            vertical=(table.altitude_x - table.altitude_y).abs(),
        )
Computes the distance from a Flight to another entity.

        The behaviour is different according to the type of the second
        element:

        - if the other element is None (i.e. flight.distance()), the method
          returns a distance in nautical miles between the first and last
          recorded positions in the DataFrame.

        - if the other element is a Flight, the method returns a pandas
          DataFrame with corresponding data from both flights, aligned
          with their timestamps, and two new columns with `lateral` and
          `vertical` distances (resp. in nm and ft) separating them.

        - otherwise, the same Flight is returned enriched with a new
          column (by default, named "distance") with the distance of each
          point of the trajectory to the geometrical element.

        .. warning::

            - An Airspace is (currently) considered as its flattened
              representation
            - Computing a distance to a polygon is quite slow at the moment.
              Consider a strict resampling (e.g. one point per minute, "1T")
              before calling the method.

        
Computes the distance from a Flight to another entity.
The behaviour is different according to the type of the second
element.

if the other element is None ), the method
returns a distance in nautical miles between the first and last
recorded positions in the DataFrame.

if the other element is a Flight, the method returns a pandas
DataFrame with corresponding data from both flights, aligned
with their timestamps, and two new columns with `lateral` and
`vertical` distances (resp. in nm and ft) separating them.

otherwise, the same Flight is returned enriched with a new
column (by default, named "distance") with the distance of each
point of the trajectory to the geometrical element.



An Airspace is (currently) considered as its flattened
representation
Computing a distance to a polygon is quite slow at the moment.
Consider a strict resampling
before calling the method.
Computes the distance from a Flight to another entity . The behaviour is different according to the type of the second element . if the other element is None ) the method returns a distance in nautical miles between the first and last recorded positions in the DataFrame . if the other element is a Flight the method returns a pandas DataFrame with corresponding data from both flights aligned with their timestamps and two new columns with ` lateral ` and ` vertical ` distances ( resp . in nm and ft ) separating them . otherwise the same Flight is returned enriched with a new column ( by default named " distance " ) with the distance of each point of the trajectory to the geometrical element . An Airspace is ( currently ) considered as its flattened representation Computing a distance to a polygon is quite slow at the moment . Consider a strict resampling before calling the method .
def distance(  
        self,
        other: Union[None, "Flight", "Airspace", Polygon, PointMixin] = None,
        column_name: str = "distance",
    ) -> Union[None, float, "Flight", pd.DataFrame]:
        if other is None:
            first = self.at_ratio(0)
            last = self.at_ratio(1)
            if first is None or last is None:
                return 0
            return (
                geo.distance(
                    first.latitude,
                    first.longitude,
                    last.latitude,
                    last.longitude,
                )
                / 1852  
            )
        if isinstance(other, PointMixin):
            size = len(self)
            return self.assign(
                **{
                    column_name: geo.distance(
                        self.data.latitude.values,
                        self.data.longitude.values,
                        other.latitude * np.ones(size),
                        other.longitude * np.ones(size),
                    )
                    / 1852  
                }
            )
        from .airspace import Airspace  
        if isinstance(other, Airspace):
            other = other.flatten()
        if isinstance(other, Polygon):
            bounds = other.bounds
            projection = pyproj.Proj(
                proj="aea",  
                lat_1=bounds[1],
                lat_2=bounds[3],
                lat_0=(bounds[1] + bounds[3]) / 2,
                lon_0=(bounds[0] + bounds[2]) / 2,
            )
            transformer = pyproj.Transformer.from_proj(
                pyproj.Proj("epsg:4326"), projection, always_xy=True
            )
            projected_shape = transform(transformer.transform, other)
            self_xy = self.compute_xy(projection)
            return self.assign(
                **{
                    column_name: list(
                        projected_shape.exterior.distance(p)
                        * (-1 if projected_shape.contains(p) else 1)
                        for p in MultiPoint(
                            list(zip(self_xy.data.x, self_xy.data.y))
                        )
                    )
                }
            )
        start = max(self.start, other.start)
        stop = min(self.stop, other.stop)
        f1, f2 = (self.between(start, stop), other.between(start, stop))
        if f1 is None or f2 is None:
            return None
        cols = ["timestamp", "latitude", "longitude", "altitude"]
        cols += ["icao24", "callsign"]
        if "flight_id" in f1.data.columns:
            cols.append("flight_id")
        table = f1.data[cols].merge(f2.data[cols], on="timestamp")
        return table.assign(
            lateral=geo.distance(
                table.latitude_x.values,
                table.longitude_x.values,
                table.latitude_y.values,
                table.longitude_y.values,
            )
            / 1852,  
            vertical=(table.altitude_x - table.altitude_y).abs(),
        )
def distance ( self , other : Union [ None , "Flight" , "Airspace" , Polygon , PointMixin ] = None , column_name : str = "distance" , ) -> Union [ None , float , "Flight" , pd . DataFrame ] : if other is None : first = self . at_ratio ( 0 ) last = self . at_ratio ( 1 ) if first is None or last is None : return 0 return ( geo . distance ( first . latitude , first . longitude , last . latitude , last . longitude , ) / 1852 ) if isinstance ( other , PointMixin ) : size = len ( self ) return self . assign ( ** { column_name : geo . distance ( self . data . latitude . values , self . data . longitude . values , other . latitude * np . ones ( size ) , other . longitude * np . ones ( size ) , ) / 1852 } ) from . airspace import Airspace if isinstance ( other , Airspace ) : other = other . flatten ( ) if isinstance ( other , Polygon ) : bounds = other . bounds projection = pyproj . Proj ( proj = "aea" , lat_1 = bounds [ 1 ] , lat_2 = bounds [ 3 ] , lat_0 = ( bounds [ 1 ] + bounds [ 3 ] ) / 2 , lon_0 = ( bounds [ 0 ] + bounds [ 2 ] ) / 2 , ) transformer = pyproj . Transformer . from_proj ( pyproj . Proj ( "epsg:4326" ) , projection , always_xy = True ) projected_shape = transform ( transformer . transform , other ) self_xy = self . compute_xy ( projection ) return self . assign ( ** { column_name : list ( projected_shape . exterior . distance ( p ) * ( - 1 if projected_shape . contains ( p ) else 1 ) for p in MultiPoint ( list ( zip ( self_xy . data . x , self_xy . data . y ) ) ) ) } ) start = max ( self . start , other . start ) stop = min ( self . stop , other . stop ) f1 , f2 = ( self . between ( start , stop ) , other . between ( start , stop ) ) if f1 is None or f2 is None : return None cols = [ "timestamp" , "latitude" , "longitude" , "altitude" ] cols += [ "icao24" , "callsign" ] if "flight_id" in f1 . data . columns : cols . append ( "flight_id" ) table = f1 . data [ cols ] . merge ( f2 . data [ cols ] , on = "timestamp" ) return table . assign ( lateral = geo . distance ( table . latitude_x . values , table . longitude_x . values , table . latitude_y . values , table . longitude_y . values , ) / 1852 , vertical = ( table . altitude_x - table . altitude_y ) . abs ( ) , )
Computes the distance from a Flight to another entity.
Computes the distance from a Flight to another entity .
# noqa: F811 """Computes the distance from a Flight to another entity.

        The behaviour is different according to the type of the second
        element:

        - if the other element is None (i.e. flight.distance()), the method
          returns a distance in nautical miles between the first and last
          recorded positions in the DataFrame.

        - if the other element is a Flight, the method returns a pandas
          DataFrame with corresponding data from both flights, aligned
          with their timestamps, and two new columns with `lateral` and
          `vertical` distances (resp. in nm and ft) separating them.

        - otherwise, the same Flight is returned enriched with a new
          column (by default, named "distance") with the distance of each
          point of the trajectory to the geometrical element.

        .. warning::

            - An Airspace is (currently) considered as its flattened
              representation
            - Computing a distance to a polygon is quite slow at the moment.
              Consider a strict resampling (e.g. one point per minute, "1T")
              before calling the method.

        """ # in nautical miles # in nautical miles # noqa: F811 # equivalent projection # in nautical miles
self
other
Union[None, "Flight", "Airspace", Polygon, PointMixin]
column_name
str