[GIS] Creating buffer using ArcGIS API for JavaScript

arcgis-javascript-apibufferextents

How can I create a buffer that is based on the shape of the geometry that has been drawn?

My map application creates a default square buffer extent and I want to create a buffer round a circle based on the shape not the min and max x y coordinates:

if (geometry.type === "polygon" || geometry.type === "multipoint" || geometry.type === "polyline") {
    var searchExtent = geometry.getExtent();
    var biggerExtent = new esri.geometry.Extent(searchExtent.xmin - 500, searchExtent.ymin - 500, searchExtent.xmax + 500, searchExtent.ymax + 500, new esri.SpatialReference({
                wkid : 27700
            }));

Can I give the esri.geometry.Extent a different searchExtent?

Best Answer

You may want to use a Geometry Service on your server to accomplish this. See the example here: https://developers.arcgis.com/javascript/jssamples/util_buffergraphic.html That will allow you to create a buffer that's the same shape as the geometry you're creating a buffer for.

However, it is possible to create a circle based on a polygon because a polygon can return a centroid:

// get an extent that's a factor larger than the original
var newExtent= geometry.getExtent().expand(1.5);
// create a polygon from this new extent
var newGeometry= Polygon.fromExtent(newExtent);

// get the center of the polygon
var center= newGeometry.getCentroid();
// getWidth returns the distance between the xmin, xmax. Use half of that as the radius
var radius= newGeometry.getExtent().getWidth()/2;
// this circle will enclose the original geometry
var circleGeometry = new Circle(center,{"radius": radius});

Use the circle you get from this as your buffer.

The documentation for Polygon, Circle and Extent will be helpful but fair warning: the Circle class DOES NOT have the fromExtent function, despite the documentation saying it was added in 3.11. It will cause an error stating that the function does not exist. Use the Polygon class to use that function.

Related Question