Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more options like capstyles for buffering #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 55 additions & 12 deletions src/gis/buffer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import initGeosJs from "geos-wasm";
import { geojsonToGeosGeom } from "../helpers/geojsonToGeosGeom";
import { geosGeomToGeojson } from "../helpers/geosGeomToGeojson";
import { km2deg } from "../utils/km2deg.js";
import { featurecollection } from "../utils/featurecollection.js";
import initGeosJs from 'geos-wasm';
import { geojsonToGeosGeom } from '../helpers/geojsonToGeosGeom';
import { geosGeomToGeojson } from '../helpers/geosGeomToGeojson';
import { km2deg } from '../utils/km2deg.js';
import { featurecollection } from '../utils/featurecollection.js';

/**
* Build a buffer with GEOS-WASM around a FeatureCollection or a set of Features or Geometries.
Expand Down Expand Up @@ -31,26 +31,59 @@ export async function buffer(x, options = {}) {
let wgs84 = options.wgs84 === false ? false : true;
let distance = 0;
switch (typeof options.dist) {
case "number":
case 'number':
distance = wgs84 ? km2deg(options.dist) : options.dist;
break;
case "string":
case 'string':
distance = options.dist;
break;
}

let endCapStyle = options.endCapStyle;
let joinStyle = options.joinStyle;
let mitreLimit = options.mitreLimit;
let singleSided = options.singleSided;

const isBufferWithParams =
endCapStyle || joinStyle || mitreLimit || singleSided;

let bufferParamsPtr;
if (isBufferWithParams) {
bufferParamsPtr = geos.GEOSBufferParams_create();
if (endCapStyle) {
geos.GEOSBufferParams_setEndCapStyle(bufferParamsPtr, endCapStyle);
}
if (joinStyle) {
geos.GEOSBufferParams_setJoinStyle(bufferParamsPtr, joinStyle);
}
if (mitreLimit) {
geos.GEOSBufferParams_setMitreLimit(bufferParamsPtr, mitreLimit);
}
if (quadsegs) {
geos.GEOSBufferParams_setQuadrantSegments(bufferParamsPtr, quadsegs);
}
if (singleSided) {
geos.GEOSBufferParams_setSingleSided(bufferParamsPtr, singleSided);
}
}

// keep properties
let prop = { ...x };
delete prop.features;

// One buffer
let newGeom;
if (options.merge) {
const geosGeom = geojsonToGeosGeom(x, geos);
const newGeom = geos.GEOSBuffer(geosGeom, distance, quadsegs);
if (isBufferWithParams) {
newGeom = geos.GEOSBufferWithParams(geosGeom, bufferParamsPtr, distance);
} else {
newGeom = geos.GEOSBuffer(geosGeom, distance, quadsegs);
}
return Object.assign(prop, {
features: [
{
type: "Feature",
type: 'Feature',
properties: {},
geometry: geosGeomToGeojson(newGeom, geos),
},
Expand All @@ -63,16 +96,26 @@ export async function buffer(x, options = {}) {
let buff = [];
x.features.forEach((d) => {
let featdist =
typeof distance == "number"
typeof distance == 'number'
? distance
: wgs84
? km2deg(d.properties[distance])
: d.properties[distance];
const geosGeom = geojsonToGeosGeom(d, geos);
const newGeom = geos.GEOSBuffer(geosGeom, featdist, quadsegs);

let newGeom;
if (isBufferWithParams) {
newGeom = geos.GEOSBufferWithParams(
geosGeom,
bufferParamsPtr,
distance
);
} else {
newGeom = geos.GEOSBuffer(geosGeom, distance, quadsegs);
}

buff.push({
type: "Feature",
type: 'Feature',
properties: d.properties,
geometry: geosGeomToGeojson(newGeom, geos),
});
Expand Down