How to use the @turf/union function in @turf/union

To help you get started, we’ve selected a few @turf/union 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 w3reality / three-geo / src / index.js View on Github external
                let mergedElevationPoly = feats.reduce(((accm, feat) => turfUnion(accm, feat)), feats[0]);
                // console.log('@@@', mergedElevationPoly, currentElevation);
github devilesk / dota-interactive-map / src / js / generateGeoJson.js View on Github external
geometry: {
                    type: 'Polygon',
                    coordinates: [[
                        [worldXY.x - 32, worldXY.y - 32],
                        [worldXY.x - 32, worldXY.y + 32],
                        [worldXY.x + 32, worldXY.y + 32],
                        [worldXY.x + 32, worldXY.y - 32],
                        [worldXY.x - 32, worldXY.y - 32],
                    ]],
                },
            };
            if (result == null) {
                result = poly;
            }
            else {
                result = union(result, poly);
            }
            if (count % 200 === 0) {
                console.log('grids', index, '/', grids.length, ', points', count, '/', total);
            }
            count++;
        }
    });
    // transformData(result);
github uber / nebula.gl / modules / edit-modes / src / lib / geojson-edit-mode.js View on Github external
) {
        // eslint-disable-next-line no-console,no-undef
        console.warn(
          'booleanOperation only supported for single Polygon or MultiPolygon selection'
        );
        return null;
      }

      const feature = {
        type: 'Feature',
        geometry
      };

      let updatedGeometry;
      if (modeConfig.booleanOperation === 'union') {
        updatedGeometry = turfUnion(selectedFeature, feature);
      } else if (modeConfig.booleanOperation === 'difference') {
        updatedGeometry = turfDifference(selectedFeature, feature);
      } else if (modeConfig.booleanOperation === 'intersection') {
        updatedGeometry = turfIntersect(selectedFeature, feature);
      } else {
        // eslint-disable-next-line no-console,no-undef
        console.warn(`Invalid booleanOperation ${modeConfig.booleanOperation}`);
        return null;
      }

      if (!updatedGeometry) {
        // eslint-disable-next-line no-console,no-undef
        console.warn('Canceling edit. Boolean operation erased entire polygon.');
        return null;
      }
github geomoose / gm3 / src / gm3 / jsts.js View on Github external
export function bufferAndUnion(features, meters) {
    let geometry = null;

    for(let i = 0, ii = features.length; i < ii; i++) {
        // buffer the geometry.
        const g = turf_buffer(features[i], meters, {units: 'meters'});

        // if the output geometry is still null, then set the
        //  first member to the new geometry
        if(geometry === null) {
            geometry = g;
        // otherwise buffer it.
        } else {
            geometry = turf_union(geometry, g);
        }
    }

    return geometry.geometry;
}
github terrestris / ol-util / src / GeometryUtil / GeometryUtil.js View on Github external
});
    let invalid = false;
    const geoJsonsFeatures = polygons.map((geometry) => {
      const feature = geometry instanceof OlFeature
        ? geometry
        : new OlFeature({ geometry });
      if (!['Polygon', 'MultiPolygon'].includes(feature.getGeometry().getType())) {
        invalid = true;
      }
      return geoJsonFormat.writeFeatureObject(feature);
    });
    if (invalid) {
      // Logger.warn('Can only create union of polygons.');
      return undefined;
    }
    const unioned = union(...geoJsonsFeatures);
    const feature = geoJsonFormat.readFeature(unioned);
    if (polygons[0] instanceof OlFeature) {
      return feature;
    } else {
      return feature.getGeometry();
    }
  }
github terrestris / ol-util / src / GeometryUtil / GeometryUtil.js View on Github external
// reprojecting them to the internal turf.js projection 'EPSG:4326'.
      const turfPolygon = geoJsonFormat.writeGeometryObject(geometry);
      const turfPolygonCoordinates = turfPolygon.coordinates;
      // outer lines of the given polygon
      const outer = lineString(turfPolygonCoordinates[0]);
      // polygonized outer polygon
      const outerPolygon = lineToPolygon(outer);
      // holes in the polygon
      const inners = [];
      turfPolygonCoordinates.slice(1, turfPolygonCoordinates.length).forEach(function (coord) {
        inners.push(lineString(coord));
      });
      // Polygonize the holes in the polygon
      const innerPolygon = polygonize(featureCollection(inners));
      // make a lineString from the spliting line and the outer of the polygon
      let unionGeom = union(outer, turfLine);
      // Polygonize the combined lines.
      const polygonizedUnionGeom = polygonize(unionGeom);
      // Array of the split polygons within the geometry
      const splitedPolygons = [];
      // Iterate over each feature in the combined feature and remove sections that are outside the initial polygon and
      // remove the parts from the cutted polygons that are in polygon holes.
      featureEach(polygonizedUnionGeom, cuttedSection => {
        // checks to see if segment is in polygon
        const segmentInPolygon = intersect(cuttedSection, outerPolygon);
        if (segmentInPolygon && segmentInPolygon.geometry.type === 'Polygon') {
          let polygonWithoutHoles = [];
          if (innerPolygon.features.length > 0) {
            // iterates over all the holes and removes their intersection with the cutted polygon
            innerPolygon.features.forEach(holes => {
              const toCut = polygonWithoutHoles.length > 0 ? polygonWithoutHoles : [segmentInPolygon];
              toCut.forEach((tocutPart, i) => {
github Turfjs / turf / packages / turf-dissolve / index.js View on Github external
matchFeaturePosition = matchFeaturePosition - closestNumber;
                    }
                }
            }

            if (matchFeaturePosition === +i) return;

            var matchFeature = features[matchFeaturePosition];
            if (!matchFeature || !polygon) return;

            if (propertyName !== undefined &&
                matchFeature.properties[propertyName] !== polygon.properties[propertyName]) return;

            if (!overlap(polygon, matchFeature) || !ringsIntersect(polygon, matchFeature)) return;

            features[i] = turfUnion(polygon, matchFeature);

            originalIndexOfItemsRemoved.push(potentialMatchingFeature.properties.origIndexPosition);
            originalIndexOfItemsRemoved.sort(function (a, b) {
                return a - b;
            });

            tree.remove(potentialMatchingFeature);
            features.splice(matchFeaturePosition, 1);
            polygon.properties.origIndexPosition = i;
            tree.remove(polygon, function (a, b) {
                return a.properties.origIndexPosition === b.properties.origIndexPosition;
            });
            featureChanged = true;
        });
github TerraEclipse / react-mapboxgl / modules / button-layer / src / ButtonLayer.js View on Github external
result.push(features.reduce((joined, next) => {
        if (joined.geometry.type === 'Polygon' || joined.geometry.type === 'MultiPolygon') {
          if (next.geometry.type === 'Polygon' || next.geometry.type === 'MultiPolygon') {
            return union(joined, next)
          } else {
            result.push(next)
            return joined
          }
        } else {
          result.push(joined)
          return next
        }
      }))
      return result

@turf/union

turf union module

MIT
Latest version published 5 months ago

Package Health Score

94 / 100
Full package analysis

Popular @turf/union functions