How to get a centroid with java SDK from a Polygon after inquiry an on-demand feature layer?

1771
1
07-30-2016 08:25 AM
JavierUría
New Contributor

Hi guys,

I'm a completly newbie with Esri arcgis SDK for Java. I have some experience with the Javascript API, but not much. In any case, due to a requirement we developed an small piece of code intended to locate incidences in a map. In order to do so, we needed to get the centroid of an specific shape coming from a Futurelayer deployed within an Esri server.

In order to do so, we found a really useful javascript instruction; features[0].geometry.getExtent().getCenter(). Right now,  I'm trying to find something similar in Java. I didn't manage to find anything meaningful for me, assuming my 'incompetence' due to the fact of a lack of skills. For that reason, in a despair I'm trying to ask this question in this forum and many others.

Hoping you don't you consider it as stupid question, but as I mentioned before I was unable to find any solution yet.

Any help on this will be really welcomed!!! I'm so sorry, if this question or a similar one wes already answered. I'll appreciate a link o guideline in order to help sort out this issue.

Many thanks and best regards.

Javier

0 Kudos
1 Reply
JavierUría
New Contributor

Hi Guys,

I've found a Workaround to this. Basically my solution was based on the creation of an Envelope with the max and min x/y Geometry coordenates and then from there get the centroid. The solution, only works with Polygons, but it's useful to illustrate how to extend the code in order to support all the Geometry types.

I've been comparing the outcome with the one of the javascript function 'features[0].geometry.getExtent().getCenter()' and both are equal. So, I can confirm that the solution in suitable for my use case, cause I'm only working with Polygons.

public static Point getCentroid(Polygon poly) {

    Vector<Double> x = new Vector<Double>();

    Vector<Double> y = new Vector<Double>();

    for (int i = 0; i < poly.getPointCount(); i++) {

       x.add(poly.getPoint(i).getX());

       y.add(poly.getPoint(i).getY());

    }

    Envelope envelope = new Envelope(Collections.min(x), Collections.min(y), Collections.max(x), Collections.max(y));

    return envelope.getCenter();

}

This is just a workaround for my use case, but I'm looking for a more official Esri solution. Please any help on this is gonna be welcomed. 

Many thanks for your time and support!!!

Javier Uria

0 Kudos