You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

feeds.py 5.6KB

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