Question

I'm trying to create a buffer around a Point using this ArcGIS service in android:

http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer

and I'd like to create a buffer programatically using this:

http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer/buffer

It's for a school job so it has to be this service, and the API must be ArcGIS SDK 10.1.1

Unfourtunelly, there's not much documentation about this. All I know is that at certain moment I should obtain a Polygon object, which I'll add to the map.

What I need to know is (having the point) how to call the service, pass the parameters needed and get the polygon.

Thank you

Was it helpful?

Solution 2

If I were you, I'd skip the GeometryServer and use GeometryEngine.buffer(Geometry, SpatialReference, double, Unit). You don't need to call a service this way. This is the right way to do it.

However, if for your school assignment you are required to call a service, then go to the service and click the link that says API Reference to bring up the documentation for GeometryServer. You'll have to use f=json and work with JSON. GeometryEngine has geometryToJson and jsonToGeometry methods to help you, but you can also use a JSON library if you like. If you don't know how to open a URL connection in Android/Java code, use the Google.

OTHER TIPS

I am using MapBox and i had to create a buffered area on top of a polygon on the map. I haven't used ArcGis instead i used library from vividsolutions
Git repo link : https://github.com/RanaRanvijaySingh/MapBoxDemo

Addition in build.gradle file

dependencies {
   ...
   compile 'com.vividsolutions:jts:1.13'
}

In MainActivity i have taken a polygon with following points:

final List<LatLng> latLngPolygon = new ArrayList<>();
    {
        latLngPolygon.add(new LatLng(28.6139, 77.2090));//delhi
        latLngPolygon.add(new LatLng(22.2587, 71.1924));//gujarat
        latLngPolygon.add(new LatLng(18.5204, 73.8567));//pune
        latLngPolygon.add(new LatLng(12.9716, 77.5946));//banglore
        latLngPolygon.add(new LatLng(25.5941, 85.1376));//patna
        //this is needed to completed a covered area, without this it would not work
        latLngPolygon.add(new LatLng(28.6139, 77.2090));//delhi
    }

below is the function to create polygon and buffered polygon on your map

/**
 * Function is called on click of Buffer Example button
 *
 * @param view View
 */
public void onClickBufferExample(View view) {
    //Initialize geometry factory object to get Geometry object.
    geometryFactory = new GeometryFactory();
    //Create geometry object using your own lat lang points
    //TODO : latLngPolygon - Used in this example is to show a bigger picture. Replace it
    //TODO : with your requirement.
    Geometry geometryOriginal = getGeometryForPolygon(latLngPolygon);
    //Draw polygon on map
    createPolygon(geometryOriginal);
    /**
     * Create geometry object with given buffer distance
     * Now buffer distance will vary on your requirement
     * Range could be anything
     * Hit and try
     */
    Geometry geometryBuffered = geometryOriginal.buffer(1);
    //Draw buffer polygon
    createPolygon(geometryBuffered);
}

/**
 * Function to get Geometry object (Class from vividsolutions)
 * from given list of latlng
 *
 * @param bounds List
 * @return Geometry (Class from vividsolutions)
 */
public Geometry getGeometryForPolygon(List<LatLng> bounds) {
    List<Coordinate> coordinates = getCoordinatesList(bounds);
    if (!coordinates.isEmpty()) {
        return geometryFactory.createPolygon(getLinearRing(coordinates), null);
    }
    return null;
}

/**
 * Function to create a list of coordinates from a list of lat lng
 *
 * @param listLatLng list<LatLng>
 * @return List<Coordinate> (Class from vividsolutions)
 */
private List<Coordinate> getCoordinatesList(List<LatLng> listLatLng) {
    List<Coordinate> coordinates = new ArrayList<>();
    for (int i = 0; i < listLatLng.size(); i++) {
        coordinates.add(new Coordinate(
                listLatLng.get(i).getLatitude(), listLatLng.get(i).getLongitude()));
    }
    return coordinates;
}

/**
 * Function to create a polygon on the map
 *
 * @param geometry Geometry Class from vividsolutions
 */
private void createPolygon(Geometry geometry) {
    LatLng[] points = getPoints(geometry.getCoordinates());
    mapboxMap.addPolyline(new PolylineOptions()
            .add(points)
            .width(4)
            .color(Color.parseColor("#FF0000")));
}

/**
 * Function to convert array of Coordinates (Class from vividsolutions)
 * to Android LatLng array
 *
 * @param coordinates Coordinates (Class from vividsolutions)
 * @return LatLng[]
 */
@NonNull
private LatLng[] getPoints(Coordinate[] coordinates) {
    List<LatLng> listPoints = new ArrayList<>();
    for (Coordinate coordinate : coordinates) {
        listPoints.add(new LatLng(coordinate.x, coordinate.y));
    }
    return listPoints.toArray(new LatLng[listPoints.size()]);
}

/**
 * Function to create LinearRing (Class from vividsolutions) from a list of
 * Coordinate (Class from vividsolutions)
 *
 * @param coordinates List
 * @return LinearRing
 */
@NonNull
private LinearRing getLinearRing(List<Coordinate> coordinates) {
    return new LinearRing(getPoints(coordinates), geometryFactory);
}

/**
 * Function to get points of CoordinateArraySequence (Class from vividsolutions)
 *
 * @param coordinates List (Class from vividsolutions)
 * @return CoordinateArraySequence (Class from vividsolutions)
 */
@NonNull
private CoordinateArraySequence getPoints(List<Coordinate> coordinates) {
    return new CoordinateArraySequence(getCoordinates(coordinates));
}

/**
 * Function to get coordinates array from a list of coordinates
 *
 * @param coordinates List<Coordinate> (Class from vividsolutions)
 * @return Coordinate [] (Class from vividsolutions)
 */
@NonNull
private Coordinate[] getCoordinates(List<Coordinate> coordinates) {
    return coordinates.toArray(new Coordinate[coordinates.size()]);
}

DONE.
go ahead a refactor the way you want but this is it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top