Example #1
0
def difference(feature_1: Feature, feature_2: Feature) -> Feature:
    """
    Find the difference between given two features.
    :param feature_1: A GeoJSON feature
    :param feature_2: A GeoJSON feature
    :return: A GeoJSON feature

    Example:

    >>> from geojson import Polygon, Feature
    >>> from turfpy.transformation import difference
    >>> f1 = Feature(geometry=Polygon([[
    >>>     [128, -26],
    >>>     [141, -26],
    >>>     [141, -21],
    >>>     [128, -21],
    >>>     [128, -26]]]), properties={"combine": "yes", "fill": "#00f"})
    >>> f2 = Feature(geometry=Polygon([[
    >>>     [126, -28],
    >>>     [140, -28],
    >>>     [140, -20],
    >>>     [126, -20],
    >>>     [126, -28]]]), properties={"combine": "yes"})
    >>> difference(f1, f2)
    """
    properties_list = []

    if "properties" in feature_1.keys():
        properties_list.append(feature_1["properties"])

    if "properties" in feature_2.keys():
        properties_list.append(feature_2["properties"])

    shape_1 = shape(get_geom(feature_1))

    shape_2 = shape(get_geom(feature_2))

    difference_result = shape_1.difference(shape_2)

    difference_result = mapping(difference_result)

    if len(difference_result["coordinates"]) == 0:
        return None

    properties = merge_dict(properties_list)

    difference_feature = Feature(geometry=difference_result,
                                 properties=properties)

    return difference_feature