Commits

Anonymous committed b16b83a

Add flag to choose lat-lon or lon-lat input format.

Comments (0)

Files changed (2)

tests/test_feedgenerator.py

     result = feed.writeString("utf-8")
     control = """<?xml version="1.0" encoding="utf-8"?>\n<rss version="2.0"><channel><title>Poynter E-Media Tidbits</title><link>http://www.poynter.org/column.asp?id=31</link><description>A group weblog by the &lt;em&gt;sharpest&lt;/em&gt; minds in online media &amp; journalism.</description><language>en</language><lastBuildDate>Fri, 18 Dec 2009 23:45:12 -0000</lastBuildDate><item><title>Hello</title><link>http://www.holovaty.com/test/</link><description>Testing.</description><pubDate>Fri, 18 Dec 2009 23:45:12 -0000</pubDate></item></channel></rss>"""
     eq_(result, control)
+
 def test_geo_point_feed():
     pubdate = datetime.datetime(2009, 12, 18, 23, 45, 12)
     feed = fg.GeoAtom1Feed(
     control = """<?xml version="1.0" encoding="utf-8"?>
 <feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xml:lang="en"><title>Poynter E-Media Tidbits</title><link href="http://www.poynter.org/column.asp?id=31" rel="alternate"></link><id>http://www.poynter.org/column.asp?id=31</id><updated>2009-12-18T23:45:12Z</updated><entry><title>Hello</title><link href="http://www.holovaty.com/test/" rel="alternate"></link><updated>2009-12-18T23:45:12Z</updated><published>2009-12-18T23:45:12Z</published><id>tag:www.holovaty.com,2009-12-18:/test/</id><summary type="html">Testing.</summary><georss:point>-120.500000 50.500000</georss:point></entry></feed>"""
     eq_(result, control)
+
+def test_geo_point_feed_longitude_first():
+    pubdate = datetime.datetime(2009, 12, 18, 23, 45, 12)
+    feed = fg.GeoAtom1Feed(
+        title=u"Poynter E-Media Tidbits",
+        link=u"http://www.poynter.org/column.asp?id=31",
+        description=u"A group weblog by the sharpest minds in online media/journalism/publishing.",
+        language=u"en",
+    )
+    feed.is_input_latitude_first = False
+    feed.add_item(
+        title="Hello", 
+        link=u"http://www.holovaty.com/test/",
+        description="Testing.",  
+        pubdate=pubdate,
+        geometry=(50.5, -120.5))
+    result = feed.writeString("utf-8")
+    f = open("/tmp/feed", "w")
+    f.write(result)
+    f.close()
+    control = """<?xml version="1.0" encoding="utf-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xml:lang="en"><title>Poynter E-Media Tidbits</title><link href="http://www.poynter.org/column.asp?id=31" rel="alternate"></link><id>http://www.poynter.org/column.asp?id=31</id><updated>2009-12-18T23:45:12Z</updated><entry><title>Hello</title><link href="http://www.holovaty.com/test/" rel="alternate"></link><updated>2009-12-18T23:45:12Z</updated><published>2009-12-18T23:45:12Z</published><id>tag:www.holovaty.com,2009-12-18:/test/</id><summary type="html">Testing.</summary><georss:point>-120.500000 50.500000</georss:point></entry></feed>"""
+    eq_(result, control)

webhelpers/feedgenerator.py

 # - Note: 'generator' and 'source' properties were lost from a previous
 #   revision of webhelpers.feedgenerator. The implementation had a bug and
 #   can't be used as is.
-# - Don't swap latitude and longitude in ``GeoFeedMixin.georss_coords()``.
-#   Django's feedgenerator does to be compatible with GeoDjango which has
-#   longitude first, but that doesn't apply to WebHelpers.
+# - Extend latitude-longitude behavior so that data can be input in either
+#   lat-lon or lon-lat format. Output is always written in lat-lon per the
+#   GeoRSS spec. Django's feedgenerator expects lon-lat input for GeoDjango
+#   compatibility.  WebHelpers defaults to lat-lon but can be switched to
+#   lon-lat by setting the ``GeoFeedMixin.is_input_latitude_first`` flag to
+#   false. (The flag can be set in a subclass or instance anytime before the
+#   output is written.) The swapping is done in
+#   ``GeoFeedMixin.georss_coords()``.
 
 
 """
     to produce simple GeoRSS or W3C Geo elements.
     """
 
+    # Set to True if the input data is in lat-lon order, or False if lon-lat.
+    # The output is always written in lat-lon order.
+    is_input_latitude_first = True
+
     def georss_coords(self, coords):
         """
         In GeoRSS coordinate pairs are ordered by lat/lon and separated by
         a single white space.  Given a tuple of coordinates, this will return
         a unicode GeoRSS representation.
         """
-        return u' '.join([u'%f %f' % coord for coord in coords])
+        if self.is_input_latitude_first:
+            return u' '.join([u'%f %f' % x for x in coords])
+        else:
+            return u' '.join([u'%f %f' % (x[1], x[0]) for x in coords])
 
     def add_georss_point(self, handler, coords, w3c_geo=False):
         """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.