Ejemplo n.º 1
0
def explode(geojson):
    points = []
    if geojson["type"] == "FeatureCollection":

        def _callback_feature_each(feature, feature_index):
            def _callback_coord_each(
                coord, coord_index, feature_index, multi_feature_index, geometry_index,
            ):
                nonlocal points
                point = Point(coord)
                points.append(Feature(geometry=point, properties=feature["properties"]))

            coord_each(feature, _callback_coord_each)

        feature_each(geojson, _callback_feature_each)
    else:

        def _callback_coord_each(
            coord, coord_index, feature_index, multi_feature_index, geometry_index,
        ):
            nonlocal points, geojson
            point = Point(coord)
            points.append(Feature(geometry=point, properties=geojson["properties"]))

        coord_each(geojson, _callback_coord_each)
    return FeatureCollection(points)
Ejemplo n.º 2
0
def transform_scale(
    features,
    factor: float,
    origin: Union[str, list] = "centroid",
    mutate: bool = False,
):
    """
    Scale a GeoJSON from a given
    point by a factor of scaling
    (ex: factor=2 would make the GeoJSON 200% larger).
    If a FeatureCollection is provided, the origin
    point will be calculated based on each individual Feature.

    :param features: GeoJSON to be scaled
    :param factor: of scaling, positive or negative values greater than 0
    :param origin: Point from which the scaling will occur
        (string options: sw/se/nw/ne/center/centroid)
    :param mutate: allows GeoJSON input to be mutated
        (significant performance increase if true)
    :return: Scaled Geojson

    Example :-

    >>> from turfpy.transformation import transform_scale
    >>> from geojson import Polygon, Feature
    >>> f = Feature(geometry=Polygon([[[0,29],[3.5,29],[2.5,32],[0,29]]]))
    >>> transform_scale(f, 3, origin=[0, 29])
    """
    if not features:
        raise Exception("geojson is required")

    if not factor:
        raise Exception("invalid factor")

    if not mutate:
        features = copy.deepcopy(features)

    if features["type"] == "FeatureCollection":

        def _callback_feature_each(feature, feature_index):
            nonlocal factor, origin, features
            features["features"][feature_index] = scale(
                feature, factor, origin)

        feature_each(features, _callback_feature_each)
        return features

    return scale(features, factor, origin)
Ejemplo n.º 3
0
def nearest_point(target_point: Feature, points: FeatureCollection) -> Feature:
    """
    Takes a reference Point Feature and FeatureCollection of point features and returns
    the point from the FeatureCollection closest to the reference Point Feature.

    :param target_point: Feature Point of reference.
    :param points: FeatureCollection of points.
    :return: a Point Feature from the FeatureCollection which is closest to the reference
         Point.

    Example:

    >>> from turfpy.measurement import nearest_point
    >>> from geojson import Point, Feature, FeatureCollection
    >>> f1 = Feature(geometry=Point((28.96991729736328,41.01190001748873)))
    >>> f2 = Feature(geometry=Point((28.948459, 41.024204)))
    >>> f3 = Feature(geometry=Point((28.938674, 41.013324)))
    >>> fc = FeatureCollection([f1, f2 ,f3])
    >>> t = Feature(geometry=Point((28.973865, 41.011122)))
    >>> nearest_point(t ,fc)
    """
    if not target_point:
        raise Exception("target_point is required")

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

    min_dist = float("inf")
    best_feature_index = 0

    def _callback_feature_each(pt, feature_index):
        nonlocal min_dist, best_feature_index
        distance_to_point = distance(target_point, pt)
        if float(distance_to_point) < min_dist:
            best_feature_index = feature_index
            min_dist = distance_to_point
        return True

    feature_each(points, _callback_feature_each)

    nearest = points["features"][best_feature_index]
    nearest["properties"]["featureIndex"] = best_feature_index
    nearest["properties"]["distanceToPoint"] = min_dist
    return nearest
Ejemplo n.º 4
0
def points_within_polygon(
        points: Union[Feature, FeatureCollection],
        polygons: Union[Feature, FeatureCollection]) -> FeatureCollection:
    """Find Point(s) that fall within (Multi)Polygon(s).

    This function takes two inputs GeoJSON Feature :class:`geojson.Point` or
    :class:`geojson.FeatureCollection` of Points and GeoJSON Feature
    :class:`geojson.Polygon` or Feature :class:`geojson.MultiPolygon` or
    FeatureCollection of :class:`geojson.Polygon` or Feature
    :class:`geojson.MultiPolygon`. and returns all points with in in those
    Polygon(s) or (Multi)Polygon(s).

    :param points: A single GeoJSON ``Point`` feature or FeatureCollection of Points.
    :param polygons: A Single GeoJSON Polygon/MultiPolygon or FeatureCollection of
        Polygons/MultiPolygons.
    :return: A :class:`geojson.FeatureCollection` of Points.
    """
    results = []

    def __callback_feature_each(feature, feature_index):
        contained = False

        def __callback_geom_each(current_geometry, feature_index,
                                 feature_properties, feature_bbox, feature_id):
            if boolean_point_in_polygon(feature, current_geometry):
                nonlocal contained
                contained = True

            if contained:
                nonlocal results
                results.append(feature)

        geom_each(polygons, __callback_geom_each)
        return True

    feature_each(points, __callback_feature_each)
    return FeatureCollection(results)