Login

Forcing Right-Hand Rule (Counter Clockwise) Polygons in GeoDjango for Google Earth / KML

Author:
gabejackson
Posted:
January 27, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

According to the KML Specification, Polygons must be oriented according to the Right-Hand Rule (Counter Clockwise orientation) for them to display correctly in Google Earth. Since not all Polygons are defined according to the Right-Hand Rule, you can use this code to orient them correctly when using GeoDjango.

Thanks goes to jbronn of #geodjango on irc.freenode.net for this!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
""" models.py """
from django.contrib.gis.db import models

class Airspaces(models.Model):
    geom = models.PolygonField()

""" your script """
# Import your models as usual
from airspaces.models import *

# Import the GeomField from geodjango
from django.contrib.gis.db.models.sql import GeomField

# Add the custom select field for the geometry field (this needs PostGIS to work)
qs = Airspaces.objects.extra(select={'rhr' : 'ST_ForceRHR(geom)'})

# Cast the field to the GeomField type
qs.query.extra_select_fields['rhr'] = GeomField()

# Iterate over objects as usual
for airspace in qs:
    # Output our counter clockwise polygon
    print airspace.rhr.coords

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.