Ecotrust / madrona

A software framework for effective place-based decision making
Other
52 stars 9 forks source link

Enforce clockwise orientation on KML LinearRings #186

Closed perrygeo closed 12 years ago

perrygeo commented 12 years ago

Though it is not in the specification for 2.2, GE expects clockwise-oriented polygons. Or at least it uses the orientation of coordinates to determine the direction of the "face". In other words, if the LinearRing coordinates are in counter-clockwise direction, the face of the polygon will be facing the center of the earth rather than the sky. This has implications for styling, extruding, clicking, etc.

http://bbs.keyhole.com/ubb/ubbthreads.php?ubb=showflat&Number=166922&site_id=1#import

But postgis doesn't appear to enforce any orientation by default: http://postgis.refractions.net/pipermail/postgis-users/2008-November/021793.html

So its up to the dev to handle this; geodjangos geom.kml should be smart enough to enforce clockwise output using: http://postgis.refractions.net/docs/ST_ForceRHR.html

Google Code Info: Issue #: 190 Author: perrygeo...@gmail.com Created On: 2009-12-10T15:28:16.000Z Closed On: 2009-12-11T21:12:29.000Z

perrygeo commented 12 years ago

IRC log from #geodjango

perry_: anyone know if geodjango exposes methods on a geometry to a) check orientation of polygon rings or b) reverse coordinate order or c) directly enforce the right-hand rule (ala Postgis' ST_ForceRHR)

[09:54am] jbronn: perry_: as to reversing coordinate order, you can get a tuple of a geometry via the .coords attribute. example: coords = poly.coords; ring = list(coords[0]); ring.reverse(); rev_poly = Polygon(ring)

[10:01am] jbronn: perry_: no explicit support for ST_ForceRHR, but you could do qs = WorldBorders.objects.extra(select={'rhr' : 'ST_ForceRHR("world_worldborders"."mpoly")'}); rhr_mpoly = GEOSGeometry(qs[0].rhr

Google Code Info: Author: perrygeo...@gmail.com Created On: 2009-12-10T17:31:43.000Z

perrygeo commented 12 years ago

Slight misunderstanding - GE expects rings to go in COUNTER-CLOCKWISE vertex order. This is apparent from the keyhole bbs post I just misinterpreted. Therefore, we need to enforce the LEFTHAND rule....

SELECT ST_Reverse(ST_ForceRHR(geom)) ....

Google Code Info: Author: perrygeo...@gmail.com Created On: 2009-12-11T19:20:09.000Z

perrygeo commented 12 years ago

added a custom QuerySet manager to the MPA model which provides an add_kml() method to fire off the postgis query and add a .kml property to the returned mpa objects. Note that this is different than the mpa.GEOSGeometry.kml used previously.

This is postgis specific and is not exactly a pretty solution but it works. By enforcing the LHR (reverse RHR) and letting postgis spit out the geometries rather than relying on geodjango's GEOSGeometry.kml, we get much more reliable output in terms of rendering and behavior in GE.

Google Code Info: Author: perrygeo...@gmail.com Created On: 2009-12-11T21:12:29.000Z