Exemple #1
0
def rhumb_distance(start, to, units="km"):
    """
    Calculates the distance along a rhumb line between two points in degrees, radians,
    miles, or kilometers.

    :param start: Start Point or Point Feature from which distance to be calculated.
    :param to: End Point or Point Feature upto which distance to be calculated.
    :param units: Units in which distance to be calculated, values can be 'deg', 'rad',
        'mi', 'km'
    :return: Distance calculated from provided start to end Point.

    Example:

    >>> from turfpy.measurement import rhumb_distance
    >>> from geojson import Point, Feature
    >>> start = Feature(geometry=Point((-75.343, 39.984)))
    >>> end = Feature(geometry=Point((-75.534, 39.123)))
    >>> rhumb_distance(start, end,'mi')
    """
    origin = get_coord(start)
    dest = get_coord(to)

    if dest[0] - origin[0] > 180:
        temp = -360
    elif origin[0] - dest[0] > 180:
        temp = 360
    else:
        temp = 0
    dest[0] += temp

    distance_in_meters = _calculate_rhumb_distance(origin, dest)
    ru_distance = convert_length(distance_in_meters, "m", units)
    return ru_distance
Exemple #2
0
def rhumb_destination(origin, distance, bearing, options) -> Feature:
    """
    Returns the destination Point having travelled the given distance along a Rhumb line
    from the origin Point with the (varant) given bearing.

    :param origin: Starting Point
    :param distance: Distance from the starting point
    :param bearing: Varant bearing angle ranging from -180 to 180 degrees from north
    :param options: A dict of two values 'units' for the units of distance provided and
        'properties' that are to be passed to the Destination Feature Point
        Example :- {'units':'mi', 'properties': {"marker-color": "F00"}}
    :return: Destination Feature Point

    Example:

    >>> from turfpy.measurement import rhumb_destination
    >>> from geojson import Point, Feature
    >>> start = Feature(geometry=Point((-75.343, 39.984)),
    properties={"marker-color": "F00"})
    >>> distance = 50
    >>> bearing = 90
    >>> rhumb_destination(start, distance, bearing, {'units':'mi',
    'properties': {"marker-color": "F00"}})
    """
    was_negative_distance = distance < 0
    distance_in_meters = convert_length(abs(distance), options.get("units", ""), "m")
    if was_negative_distance:
        distance_in_meters = -1 * (abs(distance_in_meters))
    coords = get_coord(origin)
    destination_point = _calculate_rhumb_destination(coords, distance_in_meters, bearing)
    return Feature(
        geometry=Point(destination_point), properties=options.get("properties", ""),
    )
Exemple #3
0
def point_to_line_distance(point: Feature,
                           line: Feature,
                           units="km",
                           method="geodesic"):
    """
    Returns the minimum distance between a Point and any segment of the LineString.

    :param point: Point Feature from which distance to be measured.
    :param line: Point LineString from which distance to be measured.
    :param units: units for distance 'km', 'm', 'mi, 'ft', 'in', 'deg', 'cen', 'rad',
        'naut', 'yd'
    :param method: Method which is used to calculate, values can be 'geodesic' or 'planar'
    :return: Approximate distance between the LineString and Point

    Example:

    >>> from turfpy.measurement import point_to_line_distance
    >>> from geojson import LineString, Point, Feature
    >>> point = Feature(geometry=Point((0, 0)))
    >>> linestring = Feature(geometry=LineString([(1, 1),(-1, 1)]))
    >>> point_to_line_distance(point, linestring)
    """
    if method != "geodesic" and method != "planar":
        raise Exception(
            "method name is incorrect ot should be either geodesic or planar")

    options = {"units": units, "method": method}

    if not point:
        raise Exception("pt is required")

    if isinstance(point, list):
        point = Feature(geometry=Point(point))
    elif point["type"] == "Point":
        point = Feature(point)
    else:
        feature_of(point, "Point", "point")

    if not line:
        raise Exception("line is required")

    if isinstance(point, list):
        line = Feature(geometry=LineString(line))
    elif line["type"] == "LineString":
        line = Feature(geometry=line)
    else:
        feature_of(line, "LineString", "line")

    distance = float("inf")

    p = point["geometry"]["coordinates"]

    def _callback_segment_each(
        current_segment,
        feature_index,
        multi_feature_index,
        geometry_index,
        segment_index,
    ):
        nonlocal options, distance
        a = current_segment["geometry"]["coordinates"][0]
        b = current_segment["geometry"]["coordinates"][1]
        d = distance_to_segment(p, a, b, options)
        if d < distance:
            distance = d

    segment_each(line, _callback_segment_each)

    return convert_length(distance, "deg", options.get("units", ""))