Skip to content
Advertisement

Saving centroid of a (multi)polygon as point geometry in a model

I have two tables, one with multipolygon geometries, the other with a column for point geometries. I want the centroid of the selected polygon to be saved as the point geometry for the other table.

JavaScript

To add a new site, an existing administrative unit must be associated with it and the center of it’s polygon should be used as the location/geometry of the site. For now I made this:

JavaScript

calling this form:

JavaScript

however, this leads to this error:

enter image description here

So I am basically not calculating a point but a ‘centroid object’ – so far so good, the django documentation tells us that. Now I am stuck trying to get something out of this centroid thingy that I can shove into that geometry column. As far as I understand I take the right data and handle it over to the right function (otherwise the error should appear earlier in this code i think?), but the result is not useful for inserting it into a geometry column. So how do i get my point? (lol)

Advertisement

Answer

finally I found a solution. Added this to the CreateView:

def form_valid(self, form):

JavaScript

I takes the geometry from the polygon, calculates the centroid of it and inserts it’s geometry as well-known text into the form, then saves the form.

Advertisement