# Calculate centroid of a point cluster

Having this function that inserts locations in a database:

``````def insertLocation(cur, label, point):
print label
cur.execute("""
SELECT label, centroid, point_cluster
FROM locations
WHERE label=%s
""", (label, ))
if cur.rowcount > 0:
# Updates current location set of points and centroid
_, centroid, point_cluster = cur.fetchone()

################ problem here! ####################
point_cluster = pointsFromDb(point_cluster)

centroid = computeCentroid(point_cluster)

#point_cluster.points.append(ppygis.Point(point.latitude, point.longitude, point.elevation, srid=4326))

cur.execute("""
UPDATE locations
SET centroid=%s, point_cluster=%s
WHERE label=%s
""", (centroid.write_ewkb(), point_cluster.write_ewkb(), label))
``````

As you can see what I'm trying to achieve with the `computeCentroid` function is that for a set of points that represent the same location I can have the approximate location by calculating a mean of that cluster. So, I tried to create that `computeCentroid` function:

``````def computeCentroid(points):
xs = map(lambda p: p[0], points)
ys = map(lambda p: p[1], points)
centroid = [np.mean(xs), np.mean(ys)]
return centroid
``````

How can I process my point_cluster in order to use this centroid function? That point cluster is a collection of linestrings but I can't seem to access them in order to calculate the centroid.

I'm trying this function in order to transform the cluster to be used by the centroid function:

``````def pointsFromDb(gis_points):
print gis_points
result = []
for i, point in enumerate(gis_points):
result.append(ppygis.Point(point.x, point.y, p.elevation, srid=4326))
return result
``````

But it says:

``````TypeError: must be convertible to a buffer, not LineString
``````

Any ideas?