All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geomajas.gwt2.client.service.GeometryOperationServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.4.3
Show newest version
/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2014 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */

package org.geomajas.gwt2.client.service;

import com.google.gwt.core.client.Callback;
import org.geomajas.command.dto.BufferInfo;
import org.geomajas.command.dto.GeometryBufferRequest;
import org.geomajas.command.dto.GeometryBufferResponse;
import org.geomajas.command.dto.GeometryConvexHullRequest;
import org.geomajas.command.dto.GeometryConvexHullResponse;
import org.geomajas.command.dto.GeometryMergeRequest;
import org.geomajas.command.dto.GeometryMergeResponse;
import org.geomajas.command.dto.UnionInfo;
import org.geomajas.geometry.Bbox;
import org.geomajas.geometry.Geometry;
import org.geomajas.geometry.service.GeometryService;
import org.geomajas.gwt.client.command.AbstractCommandCallback;
import org.geomajas.gwt.client.command.GwtCommand;
import org.geomajas.gwt.client.command.GwtCommandDispatcher;

import java.util.ArrayList;
import java.util.List;

/**
 * Service for available operations on {@link Geometry} instances. This class contains implementations for; 
    *
  • Applying a buffer distance on a geometry or several geometries
  • Merging several geometries (or obtaining * a union)
  • Obtaining the convex hull of a geometry or several geometries
  • Obtaining the bounds between * several geometries
* * @author Emiel Ackermann */ public class GeometryOperationServiceImpl implements GeometryOperationService { @Override public void buffer(Geometry geometry, BufferInfo bufferInfo, final Callback callback) { List geometries = new ArrayList(); geometries.add(geometry); buffer(geometries, bufferInfo, new Callback, Throwable>() { public void onFailure(Throwable reason) { callback.onFailure(reason); } public void onSuccess(List result) { callback.onSuccess(result.get(0)); } }); } @Override public void buffer(List geometries, BufferInfo bufferInfo, final Callback, Throwable> callback) { GeometryBufferRequest request = new GeometryBufferRequest(); request.setGeometries(geometries); request.setBufferDistance(bufferInfo.getDistance()); request.setQuadrantSegments(bufferInfo.getQuadrantSegments()); GwtCommand command = new GwtCommand(GeometryBufferRequest.COMMAND); command.setCommandRequest(request); GwtCommandDispatcher.getInstance().execute(command, new AbstractCommandCallback() { public void execute(GeometryBufferResponse response) { callback.onSuccess(response.getGeometries()); for (Throwable throwable : response.getErrors()) { callback.onFailure(throwable); } } }); } @Override public void union(List geometries, UnionInfo unionInfo, final Callback callback) { GeometryMergeRequest request = new GeometryMergeRequest(); request.setGeometries(geometries); request.setPrecision(unionInfo.getPrecision()); request.setUsePrecisionAsBuffer(unionInfo.isUsePrecisionAsBuffer()); GwtCommand command = new GwtCommand(GeometryMergeRequest.COMMAND); command.setCommandRequest(request); GwtCommandDispatcher.getInstance().execute(command, new AbstractCommandCallback() { public void execute(GeometryMergeResponse response) { callback.onSuccess(response.getGeometry()); for (Throwable throwable : response.getErrors()) { callback.onFailure(throwable); } } }); } @Override public void convexHull(Geometry geometry, final Callback callback) { List geometries = new ArrayList(); geometries.add(geometry); convexHull(geometries, new Callback, Throwable>() { public void onFailure(Throwable reason) { callback.onFailure(reason); } public void onSuccess(List result) { callback.onSuccess(result.get(0)); } }); } @Override public void convexHull(List geometries, final Callback, Throwable> callback) { GeometryConvexHullRequest request = new GeometryConvexHullRequest(); request.setGeometries(geometries); GwtCommand command = new GwtCommand(GeometryConvexHullRequest.COMMAND); command.setCommandRequest(request); GwtCommandDispatcher.getInstance().execute(command, new AbstractCommandCallback() { public void execute(GeometryConvexHullResponse response) { callback.onSuccess(response.getGeometries()); for (Throwable throwable : response.getErrors()) { callback.onFailure(throwable); } } }); } @Override public void bounds(List geometries, Callback callback) { try { Bbox result = GeometryService.getBounds(geometries.get(0)); double minX = result.getX(); double minY = result.getY(); double maxX = result.getMaxX(); double maxY = result.getMaxY(); for (int i = 1; i < geometries.size(); i++) { Bbox bounds = GeometryService.getBounds(geometries.get(i)); double boundsX = bounds.getX(); minX = boundsX < minX ? boundsX : minX; double boundsY = bounds.getY(); minY = boundsY < minY ? boundsY : minY; double boundsMaxX = bounds.getMaxX(); maxX = boundsMaxX > maxX ? boundsMaxX : maxX; double boundsMaxY = bounds.getMaxY(); maxY = boundsMaxY > maxY ? boundsMaxY : maxY; } result.setX(minX); result.setY(minY); result.setMaxX(maxX); result.setMaxY(maxY); callback.onSuccess(result); } catch (Exception e) { callback.onFailure(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy