How to use @turf/turf - 10 common examples

To help you get started, we’ve selected a few @turf/turf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github grasslandnetwork / node_lite / gui / app.js View on Github external
// top_right = { "lat": positionAndBearing[0].lat + tr_lat_displacement, "lng": positionAndBearing[0].lng + tr_lng_displacement };
		// bottom_right = { "lat": positionAndBearing[0].lat + br_lat_displacement, "lng": positionAndBearing[0].lng + br_lng_displacement };
		// bottom_left = { "lat": positionAndBearing[0].lat + bl_lat_displacement, "lng": positionAndBearing[0].lng + bl_lng_displacement };
		// top_left = { "lat": positionAndBearing[0].lat + tl_lat_displacement, "lng": positionAndBearing[0].lng + tl_lng_displacement };


		top_right = [ positionAndBearing[0].lng + (this.defaultFootprint[category].lng_dst/2), positionAndBearing[0].lat + (this.defaultFootprint[category].lat_dst/2) ];

		bottom_right = [ positionAndBearing[0].lng + (this.defaultFootprint[category].lng_dst/2), positionAndBearing[0].lat - (this.defaultFootprint[category].lat_dst/2) ];

		bottom_left = [ positionAndBearing[0].lng - (this.defaultFootprint[category].lng_dst/2), positionAndBearing[0].lat - (this.defaultFootprint[category].lat_dst/2) ];

		top_left = [ positionAndBearing[0].lng - (this.defaultFootprint[category].lng_dst/2), positionAndBearing[0].lat + (this.defaultFootprint[category].lat_dst/2) ];
		
		
		var poly = turfPolygon(
			[
				[
					top_right,
					bottom_right,
					bottom_left,
					top_left,
					top_right
				]
			]
		);

		// console.log(positionAndBearing[1]);

		var rotatedPoly = turfTransformRotate(poly, positionAndBearing[1]);

		if (DEMO_MODE) {
github DoFabien / OsmGo / src / services / osmApi.service.ts View on Github external
private wayToPoint(FeatureCollection) {
        let features = FeatureCollection.features;
        for (let i = 0; i < features.length; i++) {

            let feature = features[i];
            // console.log(feature);
            if (feature.geometry) {
                if (feature.geometry.type !== 'Point') {

                    // on stocke la géométrie d'origine dans .way_geometry
                    feature.properties.way_geometry = JSON.parse(JSON.stringify(feature.geometry));
                    let geom;
                    switch (feature.geometry.type) {
                        case 'Polygon':
                            feature.properties['mesure'] = area(feature.geometry)
                            geom = polygon(feature.geometry.coordinates);
                            break;
                        case 'MultiPolygon':
                            feature.properties['mesure'] = area(feature.geometry)
                            geom = multiPolygon(feature.geometry.coordinates);
                            break;
                        case 'LineString':
                            feature.properties['mesure'] = length(feature.geometry)
                            geom = lineString(feature.geometry.coordinates);
                            break;
                        case 'MultiLineString':
                            feature.properties['mesure'] = length(feature.geometry)
                            geom = multiLineString(feature.geometry.coordinates);
                            break;
                    }
github w3reality / three-laser-pointer / examples / demo-path / src / index.js View on Github external
//========
                // - turf.merge is deprecated, so...
                // - https://github.com/turf-junkyard/turf-merge
                //     This module is now deprecated in favor of using
                //     the turf-union module repeatedly on an array.
                // - https://gis.stackexchange.com/questions/243460/turf-js-union-with-array-of-features
                // console.log('feat collection:', turf.featureCollection(elevationPolys));
                let mergedElevationPoly = turf.union.apply(
                    this, turf.featureCollection(elevationPolys).features);

                // trim to desired search area
                mergedElevationPoly = turf.intersect(
                    polygon, mergedElevationPoly);

                if (mergedElevationPoly) {
                    let contourArea = turf.area(mergedElevationPoly.geometry);
                    // FIXME: ???????? ???????? ???????? ????????
                    // L.mapbox.featureLayer().setGeoJSON(mergedElevationPoly).addTo(map);

                    contours.push({
                        'geometry': mergedElevationPoly,
                        'ele': currentElevation,
                        'area': contourArea,
                    });
                }
            } catch (error) { // on merge fail, insert the previous contour again and skip
                console.log('merge failed at elevation '+currentElevation);
                console.log(error.message);
            }
        }

        // remove contour undercuts
github w3reality / three-laser-pointer / examples / demo-path / src / index.js View on Github external
//========
                // this was being used...
                // let mergedElevationPoly = turf.merge(
                //     turf.featureCollection(elevationPolys));
                //========
                // - turf.merge is deprecated, so...
                // - https://github.com/turf-junkyard/turf-merge
                //     This module is now deprecated in favor of using
                //     the turf-union module repeatedly on an array.
                // - https://gis.stackexchange.com/questions/243460/turf-js-union-with-array-of-features
                // console.log('feat collection:', turf.featureCollection(elevationPolys));
                let mergedElevationPoly = turf.union.apply(
                    this, turf.featureCollection(elevationPolys).features);

                // trim to desired search area
                mergedElevationPoly = turf.intersect(
                    polygon, mergedElevationPoly);

                if (mergedElevationPoly) {
                    let contourArea = turf.area(mergedElevationPoly.geometry);
                    // FIXME: ???????? ???????? ???????? ????????
                    // L.mapbox.featureLayer().setGeoJSON(mergedElevationPoly).addTo(map);

                    contours.push({
                        'geometry': mergedElevationPoly,
                        'ele': currentElevation,
                        'area': contourArea,
                    });
                }
            } catch (error) { // on merge fail, insert the previous contour again and skip
                console.log('merge failed at elevation '+currentElevation);
                console.log(error.message);
github kurisubrooks / sherlock / modules / api / fire / main.js View on Github external
results.push(final);
        };

        // Each filter
        for (let filterFeature of filter.features) {
            try {
                let geometry = filterFeature.geometry;
                let filter = geometry.type === "Point" ? turf.circle(geometry, geometry.properties.radius) : geometry;

                // Each incident
                for (let feature of incidents.features) {
                    let geometry = feature.geometry;

                    // filter results to overlapping regions
                    let result = turf.intersect(filter, geometry.type === "Point" ? turf.circle(geometry, radius) : geometry.geometries[1].geometries[0]);

                    // if match
                    if (result !== undefined) format(feature);
                }
            } catch(error) {
                res.status(500).send({ ok: false, code: 500, error: "Internal Server Error" });
                console.error(error);
                break;
            }
        }

        // Sort results by HIGH→LOW warning levels
        // then sort by Distance from Home
        results.sort((a, b) => {
            let home = ["-33.746", "150.7123"];
github farmOS / farmOS-client / src / client / store / geoModule.js View on Github external
});
        } else {
          geoJSON.push(parsePoly(geometry.slice(10, -2)));
        }
        return geoJSON;
      }

      const geometry = params.area.geofield[0].geom;
      const geomJSON = geoJSONify(geometry);

      // Now I'll check whether the point is inside the polygon (isInside)
      // OR if the point is within [radius] km of the polygon border (isNear)
      const circle = turf.circle(params.point, params.radius, { units: 'kilometers' });
      const polygon = turf.polygon(geomJSON);
      const isInside = turf.inside(params.point, polygon);
      const isNear = turf.intersect(circle, polygon) !== null;
      if (isInside || isNear) {
        commit('addLocalArea', params.area);
      }
    },
  },
github kurisubrooks / sherlock2 / api / georfs / fires.js View on Github external
}

    for (const feature of filters.filter.features) {
      try {
        const geometry = feature.geometry;
        const filter = geometry.type === 'Point'
          ? turf.circle(geometry, geometry.properties.radius)
          : geometry;

        // Each incident
        for (const feature of incidents.features) {
          const geometry = feature.geometry;

          // Filter Results for Overlapping Regions
          try {
            const result = turf.intersect(filter, geometry.type === 'Point'
              ? turf.circle(geometry, radius)
              : geometry.geometries[1].geometries[0]);

            // Match
            if (result !== undefined) {
              const formatted = this.format(feature);
              if (formatted) results.push(formatted); // eslint-disable-line max-depth
            }
          } catch(error) {
            console.log(error.message);
            ++missing;
            continue;
          }
        }
      } catch(error) {
        res.status(500).send({ ok: false, error: 'Internal Server Error' });
github Liquid-Zhangliquan / ycyfans-map / src / components / cymap / SzBuilding / index.jsx View on Github external
const segments = mapArray(item.segments, segment => {
            length += (distance(last, point(segment), { units: 'kilometers' }) * 10);
            const coord = [
              segment[0],
              segment[1],
              length,
            ];
            last = point(segment);
            return coord;
          });
          // maxLength = Math.max(maxLength, length);
github syousif94 / frugalmaps / functions / getEvents.js View on Github external
.then(results => {
        if (!results.hits.hits.length) {
          return results.hits.hits;
        }

        const points = results.hits.hits.map(doc =>
          turf.point(doc._source.coordinates)
        );

        if (lat && lng && !bounds) {
          points.push(turf.point([lng, lat]));
        }

        // create a bounding box for the results
        const envelope = turf.bbox(
          turf.buffer(turf.envelope(turf.featureCollection(points)), 0.5, {
            units: "miles"
          })
        );

        bounds = {
          southwest: {
            lat: envelope[1],
            lng: envelope[0]
          },
          northeast: {
            lat: envelope[3],
github open-apparel-registry / open-apparel-registry / src / app / src / components / Map.jsx View on Github external
findNearestPoint = (targetFeature, allFeatures) => {
        if (
            !targetFeature ||
            !targetFeature.geometry ||
            !targetFeature.geometry.coordinates
        ) {
            return;
        }
        const targetPoint = turf.point(targetFeature.geometry.coordinates);
        const uniqueFeatues = allFeatures.filter(a =>
            a.geometry.coordinates[0] !==
                    targetFeature.geometry.coordinates[0] &&
                a.geometry.coordinates[1] !==
                    targetFeature.geometry.coordinates[1]);

        const allPoints = uniqueFeatues.map(f =>
            turf.point(f.geometry.coordinates));
        const points = turf.featureCollection(allPoints);
        const nearest = turf.nearestPoint(targetPoint, points);
        if (!nearest) {
            return;
        }
        const nearestFeature = uniqueFeatues[nearest.properties.featureIndex];
        return [targetFeature, nearestFeature]; // eslint-disable-line consistent-return
    };