How to use the @turf/turf.centroid function in @turf/turf

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 sharedstreets / mobility-metrics / src / summarize.js View on Github external
trip.route.features[0].geometry.coordinates[1],
        trip.route.features[0].geometry.coordinates[0],
        Z
      );
      var b = h3.geoToH3(
        trip.route.features[trip.route.features.length - 1].geometry
          .coordinates[1],
        trip.route.features[trip.route.features.length - 1].geometry
          .coordinates[0],
        Z
      );

      // store pair geometry
      var pair = turf.lineString(
        [
          turf.centroid(turf.polygon([h3.h3ToGeoBoundary(a, true)])).geometry
            .coordinates,
          turf.centroid(turf.polygon([h3.h3ToGeoBoundary(b, true)])).geometry
            .coordinates
        ],
        { pair: a + ">" + b }
      );
      stats.geometry.pairs[pair.properties.pair] = pair;

      var timeBins = getTimeBins(reportDay, trip.start_time);
      // populate time bins
      if (!stats.flows.pairs.day[timeBins.day]) {
        stats.flows.pairs.day[timeBins.day] = {};
      }
      if (!stats.flows.pairs.hour[timeBins.hour]) {
        stats.flows.pairs.hour[timeBins.hour] = {};
      }
github iandees / building-diff / map.js View on Github external
continue;
    }
    osmCover.insert(feature);
  }

  var features = [];

  for (let bFeature of data.buildings.bingbuildings.features) {
    var bingOsmIntersect = osmCover.search(bFeature);

    if (bingOsmIntersect.features.length == 0) {
      // No intersection in the rbush means we should just write out the building
      features.push(bFeature);
      continue;
    } else if (bingOsmIntersect.features.length > 0) {
      var bCentroid = turf.centroid(bFeature);

      var matchFound = false;
      for (let oFeature of bingOsmIntersect.features) {
        var oCentroid = turf.centroid(oFeature);
        var dist = turf.distance(bCentroid, oCentroid);
        if (dist < 0.05) {
          matchFound = true;
          break;
        }
      }

      if (!matchFound) {
        features.push(bFeature);
      }
    }
  }
github cswbrian / district-councils-dashboard / web / src / utils / features.js View on Github external
export const getCentroidFromYearAndCode = (year, code) => {
  const featuresArray = [dc2003, dc2007, dc2011, dc2015, dc2019]
  const selectedIndex = [2003, 2007, 2011, 2015, 2019].findIndex(
    y => y === year
  )

  if (selectedIndex > -1) {
    const selectedFeature = featuresArray[selectedIndex]
    const polygon = selectedFeature.features.find(
      feature => feature.properties.CACODE === code
    )
    const centroid = turf.centroid(polygon)
    // finetune for concave polygon for 2019 districts
    return (
      adjustConcavePolygonCentroid(year, code) || centroid.geometry.coordinates
    )
  }
  return
}
github mapbox / malaria-mapping / scripts / centroid.js View on Github external
centroidStream._transform = function (feature, encoding, done) {
  // Shave away all attributes except day for centroid features
  const centroidFeature = turf.centroid(feature);
  centroidFeature.properties = feature.properties;
  this.push(centroidFeature);
  done();
};
github osmlab / osmlint / validators / doubledPOI / map.js View on Github external
function getDistance(o1, o2) {
  var p, pp;
  if (o1.geometry.type === 'Polygon') {
    pp = turf.centroid(o1);
    p = o2;
  } else {
    pp = turf.centroid(o2);
    p = o1;
  }
  return turf.distance(pp, p, {
    units: 'kilometers'
  });
}
github osmlab / osmlint / validators / doubledSchools / map.js View on Github external
function getDistance(o1, o2) {
  var p, pp;
  if (o1.geometry.type === 'Polygon') {
    pp = turf.centroid(o1);
    p = o2;
  } else {
    pp = turf.centroid(o2);
    p = o1;
  }
  return turf.distance(pp, p, {
    units: 'kilometers'
  });
}
github osmlab / osmlint / validators / doubledSchools / map.js View on Github external
function getDistance(o1, o2) {
  var p, pp;
  if (o1.geometry.type === 'Polygon') {
    pp = turf.centroid(o1);
    p = o2;
  } else {
    pp = turf.centroid(o2);
    p = o1;
  }
  return turf.distance(pp, p, {
    units: 'kilometers'
  });
}
github mapbox / malaria-mapping / scripts / filter-area.js View on Github external
filterStream._transform = function (feature, encoding, done) {
  let result = null;
  if (feature.geometry.type === 'Point') {
    result = query(feature.geometry.coordinates);
  } else {
    const centroidFeature = turf.centroid(feature);
    result = query(centroidFeature.geometry.coordinates);
  }

  if (result) {
    this.push(feature);
  }

  done();
};
github osmlab / osmlint / validators / doubledPOI / map.js View on Github external
function getDistance(o1, o2) {
  var p, pp;
  if (o1.geometry.type === 'Polygon') {
    pp = turf.centroid(o1);
    p = o2;
  } else {
    pp = turf.centroid(o2);
    p = o1;
  }
  return turf.distance(pp, p, {
    units: 'kilometers'
  });
}