# How to calculate centroid of a polygon defined by a list of longitude/latitude points? [duplicate]

I've imported some polygons (ways) from OpenStreetMap and now want to calculate the centroids of them.

For this purpose I wrote following code:

``````    final List<Point2D.Double> points = getPoints();
final Point2D.Double actualCentroid = calculateCentroidOurWay(points);

LOGGER.debug("Centroid, x: " + actualCentroid.x);
LOGGER.debug("Centroid, y: " + actualCentroid.y);

private Point.Double calculateCentroidOurWay(final List<Point2D.Double> aPoints) {
final Building objectUnderTest = new Building();

objectUnderTest.setPolygon(new Polygon(aPoints));

return objectUnderTest.calculateCentroid();
}

@Override
public Point2D.Double calculateCentroid() {
double x = 0.;
double y = 0.;
double area = 0.;

for (int i = 0; i < polygon.getPoints().size() - 1; i++) {
final Point2D.Double point = polygon.getPoints().get(i);
final Point2D.Double pointN = polygon.getPoints().get(i + 1);

final double temp = point.getX() * pointN.getY() - pointN.getX() * point
.getY();
x += (point.getX() + pointN.getX()) * temp;

y += (point.getY() + pointN.getY()) * temp;

area += temp;
}

area /= 2.;

x *= 1 / 6. * area;
y *= 1 / 6. * area;

return new Point2D.Double(x, y);
}

private List<Point.Double> getPoints() {
final List<Point.Double> points = new LinkedList<>();

return points;
}
``````

The result of the calculation is

``````Centroid, x: 1.8787362164666136E-11
Centroid, y: 6.162273026564668E-12
``````

which is obviously wrong (it's not a latitude/longitude pair).

When I use Java Topology Suite, it returns same results.

``````private Coordinate calculateJtsCentroid(final List<Point2D.Double> aPoints) {
final CentroidPoint centroidPoint = new CentroidPoint();

for (final Point.Double curPoint : aPoints) {
}

return centroidPoint.getCentroid();
}
``````

How should I change my code in order to get the centroid of the polygon as a point identified by latitude and longitude?

Since you have the latitudes and longitudes of each point you can use the Finite Set of Points method indicated on Wikipedia. I think something like this might work.

``````public Point2D.Double calculateCentroid() {
double x = 0.;
double y = 0.;
int pointCount = polygon.getPoints().size();
for (int i = 0;i < pointCount - 1;i++){
final Point2D.Double point = polygon.getPoints().get(i);
x += point.getX();
y += point.getY();
}

x = x/pointCount;
y = y/pointCount;

return new Point2D.Double(x, y);
}
``````

Basically you just need to average the x and y separately, then create a new point with those averages as the coordinates.

• Thank you for your contribution! This would be the centroid of the polygon's vertices, not of the polygon itself, assuming the latitudes and longitudes do not vary a lot (for then you would have to account for the spherical geometry). The code in the question makes it clear the polygon's centroid (center of mass) is desired instead. See gis.stackexchange.com/questions/22739/… for an explanation of the distinction. Commented Nov 13, 2013 at 21:24
• Oops. You're right. Commented Nov 13, 2013 at 21:28