How to use the @turf/turf.booleanPointInPolygon 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 cswbrian / district-councils-dashboard / web / src / utils / features.js View on Github external
export const getSingleFeatureFromPoint = ({ lng, lat }) => {
  const pt = turf.point([lng, lat])
  // TODO: consider using different year in future
  // but need to take care of app size
  const result = {
    code: null,
    year: 2019,
    name_zh: null,
    name_en: null,
  }
  for (let i = 0; i < dc2019.features.length; i++) {
    const feature = dc2019.features[i]
    const poly = turf.polygon(feature.geometry.coordinates)
    if (turf.booleanPointInPolygon(pt, poly)) {
      result.code = feature.properties.CACODE
      result.name_zh = feature.properties.CNAME
      result.name_en = feature.properties.ENAME
      break
    }
  }
  return result
}
github osmlab / osmlint / validators / unconnectedHighways / map.js View on Github external
var valueHighway = highways[valueBbox.id].highway;
    //obtaining first and last coordinates
    var firstCoord = valueHighway.geometry.coordinates[0];
    var firstPoint = turf.point(firstCoord);
    var endCoord =
      valueHighway.geometry.coordinates[
        valueHighway.geometry.coordinates.length - 1
      ];
    var endPoint = turf.point(endCoord);

    if (
      preserveType[valueHighway.properties.highway] &&
      !_.isEqual(firstCoord, endCoord)
    ) {
      var overlapsFirstPoint = [];
      if (!turf.booleanPointInPolygon(firstPoint, bufferLayer)) {
        overlapsFirstPoint = highwaysTree.search(
          objBbox(
            turf.bboxPolygon(
              turf.bbox(turf.buffer(firstPoint, distance, unit)),
              'id'
            )
          )
        );
      }
      var overlapsEndPoint = [];
      if (!turf.booleanPointInPolygon(endPoint, bufferLayer)) {
        overlapsEndPoint = highwaysTree.search(
          objBbox(
            turf.bboxPolygon(
              turf.bbox(turf.buffer(endPoint, distance, unit)),
              'id'
github osmlab / osmlint / validators / unconnectedHighways / map.js View on Github external
}
            }
          }
        }
      }
      if (!avoidPoints[endCoord.join('-')]) {
        for (var l = 0; l < overlapsEndPoint.length; l++) {
          var overlapPointEnd = overlapsEndPoint[l];
          var toHighwayEnd = highways[overlapPointEnd.id].highway;
          if (
            valueBbox.id !== overlapPointEnd.id &&
            (arrayCorrd.indexOf(endCoord[0]) === -1 ||
              arrayCorrd.indexOf(endCoord[1]) === -1)
          ) {
            if (
              turf.booleanPointInPolygon(
                endPoint,
                highways[overlapPointEnd.id].buffer
              )
            ) {
              props.toWay = toHighwayEnd.properties['@id'];
              endPoint.properties = props;
              //Check out whether the streets are connected at some point
              var coordinatesE = valueHighway.geometry.coordinates;
              var valueCoorE = _.flatten([
                coordinatesE[coordinatesE.length - 1],
                coordinatesE[coordinatesE.length - 2]
              ]);
              var overlapCoorE = _.flatten(toHighwayEnd.geometry.coordinates);
              if (_.intersection(valueCoorE, overlapCoorE).length < 2) {
                valueHighway.properties._osmlint = osmlint;
                toHighwayEnd.properties._osmlint = osmlint;
github osmlab / osmlint / validators / unconnectedHighways / map.js View on Github external
preserveType[valueHighway.properties.highway] &&
      !_.isEqual(firstCoord, endCoord)
    ) {
      var overlapsFirstPoint = [];
      if (!turf.booleanPointInPolygon(firstPoint, bufferLayer)) {
        overlapsFirstPoint = highwaysTree.search(
          objBbox(
            turf.bboxPolygon(
              turf.bbox(turf.buffer(firstPoint, distance, unit)),
              'id'
            )
          )
        );
      }
      var overlapsEndPoint = [];
      if (!turf.booleanPointInPolygon(endPoint, bufferLayer)) {
        overlapsEndPoint = highwaysTree.search(
          objBbox(
            turf.bboxPolygon(
              turf.bbox(turf.buffer(endPoint, distance, unit)),
              'id'
            )
          )
        );
      }
      var overlapBboxes = overlapsFirstPoint.concat(overlapsEndPoint);
      var arrayCorrd = [];
      for (var j = 0; j < overlapBboxes.length; j++) {
        var overlapBbox = overlapBboxes[j];
        if (valueBbox.id !== overlapBbox.id) {
          arrayCorrd = arrayCorrd.concat(
            _.flatten(highways[overlapBbox.id].highway.geometry.coordinates)
github osmlab / osmlint / validators / impossibleOneWays / map.js View on Github external
var itemL = objBbox(turf.point(coordsWayL[j]), propsL);
        bboxes.push(itemL);
      }
      highways[idWayL] = val;
    } else if (
      val.geometry.type === 'MultiLineString' &&
      val.properties.highway
    ) {
      //MultiLineString evaluation
      var arrayWays = flatten(val);
      for (var f = 0; f < arrayWays.length; f++) {
        if (arrayWays[f].geometry.type === 'LineString') {
          var coordsWayM = arrayWays[f].geometry.coordinates;
          var isClippedM = false;
          if (
            turf.booleanPointInPolygon(
              turf.point(coordsWayM[0]),
              bufferLayer
            ) ||
            turf.booleanPointInPolygon(
              turf.point(coordsWayM[coordsWayM.length - 1]),
              bufferLayer
            )
          ) {
            isClippedM = true;
          }
          var idWayM = id + 'M' + f;
          for (var t = 0; t < coordsWayM.length; t++) {
            var positionM;
            if (t === 0) {
              positionM = 'first';
            } else if (t === coordsWayM.length - 1) {
github terrestris / react-geo / src / Util / GeometryUtil / GeometryUtil.js View on Github external
segmentEach(unionGeom, (currentSegment, featureIndex, multiFeatureIndex) => {
      const segmentCenter = centroid(currentSegment);
      const isSegmentInPolygon = booleanPointInPolygon(segmentCenter, bufferedTurfPolygon);

      if (isSegmentInPolygon) {
        if (!filteredSegments[multiFeatureIndex]) {
          filteredSegments[multiFeatureIndex] = [];
        }

        if (filteredSegments[multiFeatureIndex].length === 0) {
          filteredSegments[multiFeatureIndex].push(
            getCoords(currentSegment)[0],
            getCoords(currentSegment)[1]
          );
        } else {
          filteredSegments[multiFeatureIndex].push(
            getCoords(currentSegment)[1]
          );
        }
github osmlab / osmlint / validators / impossibleOneWays / map.js View on Github external
} else if (
      val.geometry.type === 'MultiLineString' &&
      val.properties.highway
    ) {
      //MultiLineString evaluation
      var arrayWays = flatten(val);
      for (var f = 0; f < arrayWays.length; f++) {
        if (arrayWays[f].geometry.type === 'LineString') {
          var coordsWayM = arrayWays[f].geometry.coordinates;
          var isClippedM = false;
          if (
            turf.booleanPointInPolygon(
              turf.point(coordsWayM[0]),
              bufferLayer
            ) ||
            turf.booleanPointInPolygon(
              turf.point(coordsWayM[coordsWayM.length - 1]),
              bufferLayer
            )
          ) {
            isClippedM = true;
          }
          var idWayM = id + 'M' + f;
          for (var t = 0; t < coordsWayM.length; t++) {
            var positionM;
            if (t === 0) {
              positionM = 'first';
            } else if (t === coordsWayM.length - 1) {
              positionM = 'end';
            } else {
              positionM = 'middle';
            }
github osmlab / osmlint / validators / islandsHighways / map.js View on Github external
var highwaysTree = rbush(bboxes.length);
  highwaysTree.load(bboxes);
  var output = {};

  for (var i = 0; i < bboxes.length; i++) {
    var valueBbox = bboxes[i];
    var valueHighway = highways[valueBbox.id.id];
    valueHighway.properties._osmlint = osmlint;
    var firstCoord = valueHighway.geometry.coordinates[0];
    var endCoord =
      valueHighway.geometry.coordinates[
        valueHighway.geometry.coordinates.length - 1
      ];
    if (
      !turf.booleanPointInPolygon(turf.point(firstCoord), bufferLayer) &&
      !turf.booleanPointInPolygon(turf.point(endCoord), bufferLayer)
    ) {
      var overlapBboxes = highwaysTree.search(valueBbox);

      if (overlapBboxes.length === 1) {
        output[valueBbox.id.id] = valueHighway;
      } else {
        var nearHighways = turf.featureCollection([]);
        for (var j = 0; j < overlapBboxes.length; j++) {
          var overlapBbox = overlapBboxes[j];
          if (valueBbox.id.id !== overlapBbox.id.id) {
            nearHighways.features.push(highways[overlapBbox.id.id]);
          }
        }
        var valueCoordinates = geojsonCoords(valueHighway);
        var nearCoordinates = geojsonCoords(nearHighways);
        //filter all highways without any connection
github osmlab / osmlint / validators / unconnectedHighways / map.js View on Github external
valueHighway.properties.highway
        )
      };

      if (!avoidPoints[firstCoord.join('-')]) {
        for (var k = 0; k < overlapsFirstPoint.length; k++) {
          var overlapPointFirst = overlapsFirstPoint[k];
          var toHighwayFirst = highways[overlapPointFirst.id].highway;

          if (
            valueBbox.id !== overlapPointFirst.id &&
            (arrayCorrd.indexOf(firstCoord[0]) === -1 ||
              arrayCorrd.indexOf(firstCoord[1]) === -1)
          ) {
            if (
              turf.booleanPointInPolygon(
                firstPoint,
                highways[overlapPointFirst.id].buffer
              )
            ) {
              props.toWay = toHighwayFirst.properties['@id'];
              firstPoint.properties = props;
              //Check out whether the streets are connected at some point
              var coordinatesF = valueHighway.geometry.coordinates;
              var valueCoorF = _.flatten([coordinatesF[1], coordinatesF[2]]);
              var overlapCoorF = _.flatten(toHighwayFirst.geometry.coordinates);
              if (_.intersection(valueCoorF, overlapCoorF).length < 2) {
                valueHighway.properties._osmlint = osmlint;
                toHighwayFirst.properties._osmlint = osmlint;
                //both roads must have the same layer and road to connect should not be in construction
                if (
                  valueHighway.properties.layer ===