Programmingjoe
Programmingjoe

Reputation: 2249

How to extract a list of points from Django PolygonField?

In one of my models that contains a PolygonField I have a to_dict method that takes all the values and turns them into a readable dictionary.

I do a similar thing for a model that has a PointField and it looks like this:

'point': {
   'type': 'Point',
   'coordinates': [self.point.x, self.point.y]
 },

For the PolygonField I have to loop through the points to put them in the dictionary. I tried this but as expected, django complained:

'polygon': {
   'type': 'Polygon',
   'coordinates': [
     for point in self.path:
       [point.x, point.y]
   ]
 },

How do you add all the points from a PolygonField into a dictionary?

Upvotes: 1

Views: 1027

Answers (1)

Programmingjoe
Programmingjoe

Reputation: 2249

Figured it out!

'polygon': {
   'type': 'Polygon',
   'coordinates': [[point.x, point.y] for point in self.polygon]
},

Upvotes: 2

Related Questions