1

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()
        centroid = ppygis.Geometry.read_ewkb(centroid)
        point_cluster = ppygis.Geometry.read_ewkb(point_cluster)

################ 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):
    gis_points = ppygis.Geometry.read_ewkb(gis_points).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?

2

I am not sure I really understand what you are doing. How can a point cluster be a collection of linestrings? , but ..

Remember you are using a spatial database, why not use the functionality in PostGIS?

from a bunch of points you can collect them and use ST_Centroid on the collection.

Or if it really is a collection of linestrings that you want to get the centroid from, use ST_Centroid on that collection.

And, don't take the result out of the database. Just update with it at once, and use it from there.

http://postgis.org/docs/ST_Centroid.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.