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
Tags:
geodjango kml google-earth polygon orientation right-hand-rule
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 code for "Google v3 geocoding for Geodjango admin site" by samhag 2 years, 6 months ago
  2. KMLMiddleware by takinbo 4 years, 3 months ago
  3. Django Incremental Counter Tag by gwrtheyrn 3 years, 5 months ago
  4. geodjango Metric Buffer by dhke 5 months, 3 weeks ago
  5. GeoDjango maps in admin TabularInlines by alanB 4 years, 7 months ago

Comments

Please login first before commenting.