| 1 |
from django.contrib.syndication.views import Feed as BaseFeed |
| 2 |
from django.utils.feedgenerator import Atom1Feed, Rss201rev2Feed |
| 3 |
|
| 4 |
|
| 5 |
class GeoFeedMixin: |
| 6 |
""" |
| 7 |
This mixin provides the necessary routines for SyndicationFeed subclasses |
| 8 |
to produce simple GeoRSS or W3C Geo elements. |
| 9 |
""" |
| 10 |
|
| 11 |
def georss_coords(self, coords): |
| 12 |
""" |
| 13 |
In GeoRSS coordinate pairs are ordered by lat/lon and separated by |
| 14 |
a single white space. Given a tuple of coordinates, return a string |
| 15 |
GeoRSS representation. |
| 16 |
""" |
| 17 |
return " ".join("%f %f" % (coord[1], coord[0]) for coord in coords) |
| 18 |
|
| 19 |
def add_georss_point(self, handler, coords, w3c_geo=False): |
| 20 |
""" |
| 21 |
Adds a GeoRSS point with the given coords using the given handler. |
| 22 |
Handles the differences between simple GeoRSS and the more popular |
| 23 |
W3C Geo specification. |
| 24 |
""" |
| 25 |
if w3c_geo: |
| 26 |
lon, lat = coords[:2] |
| 27 |
handler.addQuickElement("geo:lat", "%f" % lat) |
| 28 |
handler.addQuickElement("geo:lon", "%f" % lon) |
| 29 |
else: |
| 30 |
handler.addQuickElement("georss:point", self.georss_coords((coords,))) |
| 31 |
|
| 32 |
def add_georss_element(self, handler, item, w3c_geo=False): |
| 33 |
"""Add a GeoRSS XML element using the given item and handler.""" |
| 34 |
# Getting the Geometry object. |
| 35 |
geom = item.get("geometry") |
| 36 |
if geom is not None: |
| 37 |
if isinstance(geom, (list, tuple)): |
| 38 |
# Special case if a tuple/list was passed in. The tuple may be |
| 39 |
# a point or a box |
| 40 |
box_coords = None |
| 41 |
if isinstance(geom[0], (list, tuple)): |
| 42 |
# Box: ( (X0, Y0), (X1, Y1) ) |
| 43 |
if len(geom) == 2: |
| 44 |
box_coords = geom |
| 45 |
else: |
| 46 |
raise ValueError("Only should be two sets of coordinates.") |
| 47 |
else: |
| 48 |
if len(geom) == 2: |
| 49 |
# Point: (X, Y) |
| 50 |
self.add_georss_point(handler, geom, w3c_geo=w3c_geo) |
| 51 |
elif len(geom) == 4: |
| 52 |
# Box: (X0, Y0, X1, Y1) |
| 53 |
box_coords = (geom[:2], geom[2:]) |
| 54 |
else: |
| 55 |
raise ValueError("Only should be 2 or 4 numeric elements.") |
| 56 |
# If a GeoRSS box was given via tuple. |
| 57 |
if box_coords is not None: |
| 58 |
if w3c_geo: |
| 59 |
raise ValueError( |
| 60 |
"Cannot use simple GeoRSS box in W3C Geo feeds." |
| 61 |
) |
| 62 |
handler.addQuickElement( |
| 63 |
"georss:box", self.georss_coords(box_coords) |
| 64 |
) |
| 65 |
else: |
| 66 |
# Getting the lowercase geometry type. |
| 67 |
gtype = str(geom.geom_type).lower() |
| 68 |
if gtype == "point": |
| 69 |
self.add_georss_point(handler, geom.coords, w3c_geo=w3c_geo) |
| 70 |
else: |
| 71 |
if w3c_geo: |
| 72 |
raise ValueError("W3C Geo only supports Point geometries.") |
| 73 |
# For formatting consistent w/the GeoRSS simple standard: |
| 74 |
# http://georss.org/1.0#simple |
| 75 |
if gtype in ("linestring", "linearring"): |
| 76 |
handler.addQuickElement( |
| 77 |
"georss:line", self.georss_coords(geom.coords) |
| 78 |
) |
| 79 |
elif gtype in ("polygon",): |
| 80 |
# Only support the exterior ring. |
| 81 |
handler.addQuickElement( |
| 82 |
"georss:polygon", self.georss_coords(geom[0].coords) |
| 83 |
) |
| 84 |
else: |
| 85 |
raise ValueError( |
| 86 |
'Geometry type "%s" not supported.' % geom.geom_type |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
# ### SyndicationFeed subclasses ### |
| 91 |
class GeoRSSFeed(Rss201rev2Feed, GeoFeedMixin): |
| 92 |
def rss_attributes(self): |
| 93 |
attrs = super().rss_attributes() |
| 94 |
attrs["xmlns:georss"] = "http://www.georss.org/georss" |
| 95 |
return attrs |
| 96 |
|
| 97 |
def add_item_elements(self, handler, item): |
| 98 |
super().add_item_elements(handler, item) |
| 99 |
self.add_georss_element(handler, item) |
| 100 |
|
| 101 |
def add_root_elements(self, handler): |
| 102 |
super().add_root_elements(handler) |
| 103 |
self.add_georss_element(handler, self.feed) |
| 104 |
|
| 105 |
|
| 106 |
class GeoAtom1Feed(Atom1Feed, GeoFeedMixin): |
| 107 |
def root_attributes(self): |
| 108 |
attrs = super().root_attributes() |
| 109 |
attrs["xmlns:georss"] = "http://www.georss.org/georss" |
| 110 |
return attrs |
| 111 |
|
| 112 |
def add_item_elements(self, handler, item): |
| 113 |
super().add_item_elements(handler, item) |
| 114 |
self.add_georss_element(handler, item) |
| 115 |
|
| 116 |
def add_root_elements(self, handler): |
| 117 |
super().add_root_elements(handler) |
| 118 |
self.add_georss_element(handler, self.feed) |
| 119 |
|
| 120 |
|
| 121 |
class W3CGeoFeed(Rss201rev2Feed, GeoFeedMixin): |
| 122 |
def rss_attributes(self): |
| 123 |
attrs = super().rss_attributes() |
| 124 |
attrs["xmlns:geo"] = "http://www.w3.org/2003/01/geo/wgs84_pos#" |
| 125 |
return attrs |
| 126 |
|
| 127 |
def add_item_elements(self, handler, item): |
| 128 |
super().add_item_elements(handler, item) |
| 129 |
self.add_georss_element(handler, item, w3c_geo=True) |
| 130 |
|
| 131 |
def add_root_elements(self, handler): |
| 132 |
super().add_root_elements(handler) |
| 133 |
self.add_georss_element(handler, self.feed, w3c_geo=True) |
| 134 |
|
| 135 |
|
| 136 |
# ### Feed subclass ### |
| 137 |
class Feed(BaseFeed): |
| 138 |
""" |
| 139 |
This is a subclass of the `Feed` from `django.contrib.syndication`. |
| 140 |
This allows users to define a `geometry(obj)` and/or `item_geometry(item)` |
| 141 |
methods on their own subclasses so that geo-referenced information may |
| 142 |
placed in the feed. |
| 143 |
""" |
| 144 |
|
| 145 |
feed_type = GeoRSSFeed |
| 146 |
|
| 147 |
def feed_extra_kwargs(self, obj): |
| 148 |
return {"geometry": self._get_dynamic_attr("geometry", obj)} |
| 149 |
|
| 150 |
def item_extra_kwargs(self, item): |
| 151 |
return {"geometry": self._get_dynamic_attr("item_geometry", item)} |