Create Buffer - Java Example

This Java example shows how to use the SOAP interface to the Geometry Service to create a buffer around a point.

To generate the stub classes for the Geometry Service, see Generating Stub Code

import com.mapinfo.midev.service.geometries.v1.MultiPolygon;
import com.mapinfo.midev.service.geometries.v1.Point;
import com.mapinfo.midev.service.geometries.v1.Pos;
import com.mapinfo.midev.service.geometry.v1.BufferRequest;
import com.mapinfo.midev.service.geometry.v1.BufferResponse;
import com.mapinfo.midev.service.geometry.ws.v1.GeometryService;
import com.mapinfo.midev.service.geometry.ws.v1.GeometryServiceInterface;
import com.mapinfo.midev.service.geometry.ws.v1.ServiceException;
import com.mapinfo.midev.service.units.v1.Distance;
import com.mapinfo.midev.service.units.v1.DistanceUnit;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class GeometryServiceDriver {

	// the number of line segments that will compose the buffer
	private static final Integer RESOLUTION = 64;

	// username with the authority to call the Geometry service
	private static final String USERNAME = "admin";

	// password of the caller
	private static final String PASSWORD = "admin";

	public static void main(String[] args) throws ServiceException {

		// get the endpoint class
		GeometryServiceInterface geometryServiceInterface = new GeometryService().getGeometryServiceInterface();

		// if authentication is enabled in Spectrum then the following is required
		Map<String, Object> requestContext = ((BindingProvider)geometryServiceInterface).getRequestContext();
		requestContext.put(BindingProvider.USERNAME_PROPERTY, USERNAME);
		requestContext.put(BindingProvider.PASSWORD_PROPERTY, PASSWORD);

		BufferRequest bufferRequest = new BufferRequest();

		// point to buffer
		{
			Point point = new Point();
			point.setSrsName("epsg:4326");
			Pos pos = new Pos();
			pos.setX(-75.66);
			pos.setY(47.88);
			point.setPos(pos);

			bufferRequest.setGeometry(point);
		}

		// buffer distance
		{
			Distance bufferDistance = new Distance();
			bufferDistance.setUom(DistanceUnit.MILE);
			bufferDistance.setValue(12.0);
			bufferRequest.setDistance(bufferDistance);
		}

		bufferRequest.setResolution(RESOLUTION);

		BufferResponse bufferResponse = geometryServiceInterface.buffer(bufferRequest);

		MultiPolygon bufferGeometry = (MultiPolygon) bufferResponse.getGeometry();

		// do what you want with the bufferGeometry ...

	}

}