2017-09-12 21:50:05 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
feedgen.ext.geo_entry
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Extends the FeedGenerator to produce Simple GeoRSS feeds.
|
|
|
|
|
|
|
|
:copyright: 2017, Bob Breznak <bob.breznak@gmail.com>
|
|
|
|
|
|
|
|
:license: FreeBSD and LGPL, see license.* for more details.
|
|
|
|
'''
|
2019-07-08 04:31:34 +02:00
|
|
|
import numbers
|
2019-07-08 05:38:39 +02:00
|
|
|
import warnings
|
2017-09-12 21:50:05 +02:00
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
from lxml import etree
|
|
|
|
from feedgen.ext.base import BaseEntryExtension
|
|
|
|
|
2018-03-04 22:55:37 +01:00
|
|
|
|
2019-07-08 05:38:39 +02:00
|
|
|
class GeoRSSPolygonInteriorWarning(Warning):
|
|
|
|
"""
|
|
|
|
Simple placeholder for warning about ignored polygon interiors.
|
|
|
|
|
|
|
|
Stores the original geom on a ``geom`` attribute (if required warnings are
|
|
|
|
raised as errors).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, geom, *args, **kwargs):
|
|
|
|
self.geom = geom
|
|
|
|
super(GeoRSSPolygonInteriorWarning, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{:d} interiors of polygon ignored'.format(
|
2019-07-29 02:24:02 +02:00
|
|
|
len(self.geom.__geo_interface__['coordinates']) - 1
|
|
|
|
) # ignore exterior in count
|
|
|
|
|
2019-07-08 05:38:39 +02:00
|
|
|
|
|
|
|
class GeoRSSGeometryError(ValueError):
|
|
|
|
"""
|
|
|
|
Subclass of ValueError for a GeoRSS geometry error
|
|
|
|
|
|
|
|
Only some geometries are supported in Simple GeoRSS, so if not raise an
|
|
|
|
error. Offending geometry is stored on the ``geom`` attribute.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, geom, *args, **kwargs):
|
|
|
|
self.geom = geom
|
|
|
|
super(GeoRSSGeometryError, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-07-29 02:24:02 +02:00
|
|
|
msg = "Geometry of type '{}' not in Point, Linestring or Polygon"
|
|
|
|
return msg.format(
|
2019-07-08 05:38:39 +02:00
|
|
|
self.geom.__geo_interface__['type']
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
class GeoEntryExtension(BaseEntryExtension):
|
2017-09-12 21:50:05 +02:00
|
|
|
'''FeedEntry extension for Simple GeoRSS.
|
|
|
|
'''
|
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
def __init__(self):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''Simple GeoRSS tag'''
|
|
|
|
# geometries
|
2017-09-12 16:50:26 +02:00
|
|
|
self.__point = None
|
2019-07-08 04:31:34 +02:00
|
|
|
self.__line = None
|
|
|
|
self.__polygon = None
|
|
|
|
self.__box = None
|
|
|
|
|
|
|
|
# additional properties
|
|
|
|
self.__featuretypetag = None
|
|
|
|
self.__relationshiptag = None
|
|
|
|
self.__featurename = None
|
|
|
|
|
|
|
|
# elevation
|
|
|
|
self.__elev = None
|
|
|
|
self.__floor = None
|
|
|
|
|
|
|
|
# radius
|
|
|
|
self.__radius = None
|
2017-09-12 16:50:26 +02:00
|
|
|
|
2017-09-12 21:50:05 +02:00
|
|
|
def extend_file(self, entry):
|
|
|
|
'''Add additional fields to an RSS item.
|
|
|
|
|
|
|
|
:param feed: The RSS item XML element to use.
|
|
|
|
'''
|
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
GEO_NS = 'http://www.georss.org/georss'
|
|
|
|
|
|
|
|
if self.__point:
|
|
|
|
point = etree.SubElement(entry, '{%s}point' % GEO_NS)
|
|
|
|
point.text = self.__point
|
|
|
|
|
2019-07-08 04:31:34 +02:00
|
|
|
if self.__line:
|
|
|
|
line = etree.SubElement(entry, '{%s}line' % GEO_NS)
|
|
|
|
line.text = self.__line
|
|
|
|
|
|
|
|
if self.__polygon:
|
|
|
|
polygon = etree.SubElement(entry, '{%s}polygon' % GEO_NS)
|
|
|
|
polygon.text = self.__polygon
|
|
|
|
|
|
|
|
if self.__box:
|
|
|
|
box = etree.SubElement(entry, '{%s}box' % GEO_NS)
|
|
|
|
box.text = self.__box
|
|
|
|
|
|
|
|
if self.__featuretypetag:
|
2019-07-29 02:24:02 +02:00
|
|
|
featuretypetag = etree.SubElement(
|
|
|
|
entry,
|
|
|
|
'{%s}featuretypetag' % GEO_NS
|
|
|
|
)
|
2019-07-08 04:31:34 +02:00
|
|
|
featuretypetag.text = self.__featuretypetag
|
|
|
|
|
|
|
|
if self.__relationshiptag:
|
2019-07-29 02:24:02 +02:00
|
|
|
relationshiptag = etree.SubElement(
|
|
|
|
entry,
|
|
|
|
'{%s}relationshiptag' % GEO_NS
|
|
|
|
)
|
2019-07-08 04:31:34 +02:00
|
|
|
relationshiptag.text = self.__relationshiptag
|
|
|
|
|
|
|
|
if self.__featurename:
|
|
|
|
featurename = etree.SubElement(entry, '{%s}featurename' % GEO_NS)
|
|
|
|
featurename.text = self.__featurename
|
|
|
|
|
|
|
|
if self.__elev:
|
|
|
|
elevation = etree.SubElement(entry, '{%s}elev' % GEO_NS)
|
2019-07-08 06:58:21 +02:00
|
|
|
elevation.text = str(self.__elev)
|
2019-07-08 04:31:34 +02:00
|
|
|
|
|
|
|
if self.__floor:
|
|
|
|
floor = etree.SubElement(entry, '{%s}floor' % GEO_NS)
|
2019-07-08 06:58:21 +02:00
|
|
|
floor.text = str(self.__floor)
|
2019-07-08 04:31:34 +02:00
|
|
|
|
|
|
|
if self.__radius:
|
|
|
|
radius = etree.SubElement(entry, '{%s}radius' % GEO_NS)
|
2019-07-08 06:58:21 +02:00
|
|
|
radius.text = str(self.__radius)
|
2019-07-08 04:31:34 +02:00
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
return entry
|
|
|
|
|
2017-09-12 21:50:05 +02:00
|
|
|
def extend_rss(self, entry):
|
|
|
|
return self.extend_file(entry)
|
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
def extend_atom(self, entry):
|
2017-09-12 21:50:05 +02:00
|
|
|
return self.extend_file(entry)
|
2017-09-12 16:50:26 +02:00
|
|
|
|
|
|
|
def point(self, point=None):
|
2017-09-12 21:50:05 +02:00
|
|
|
'''Get or set the georss:point of the entry.
|
|
|
|
|
|
|
|
:param point: The GeoRSS formatted point (i.e. "42.36 -71.05")
|
2018-03-04 22:55:37 +01:00
|
|
|
:returns: The current georss:point of the entry.
|
2017-09-12 21:50:05 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
if point is not None:
|
|
|
|
self.__point = point
|
|
|
|
|
2017-09-12 16:50:26 +02:00
|
|
|
return self.__point
|
2019-07-08 04:31:34 +02:00
|
|
|
|
|
|
|
def line(self, line=None):
|
|
|
|
'''Get or set the georss:line of the entry
|
|
|
|
|
2019-07-29 02:24:02 +02:00
|
|
|
:param point: The GeoRSS formatted line (i.e. "45.256 -110.45 46.46
|
|
|
|
-109.48 43.84 -109.86")
|
2019-07-08 04:31:34 +02:00
|
|
|
:return: The current georss:line of the entry
|
|
|
|
'''
|
|
|
|
if line is not None:
|
|
|
|
self.__line = line
|
|
|
|
|
|
|
|
return self.__line
|
|
|
|
|
|
|
|
def polygon(self, polygon=None):
|
|
|
|
'''Get or set the georss:polygon of the entry
|
|
|
|
|
2019-07-29 02:24:02 +02:00
|
|
|
:param polygon: The GeoRSS formatted polygon (i.e. "45.256 -110.45
|
|
|
|
46.46 -109.48 43.84 -109.86 45.256 -110.45")
|
2019-07-08 04:31:34 +02:00
|
|
|
:return: The current georss:polygon of the entry
|
|
|
|
'''
|
|
|
|
if polygon is not None:
|
|
|
|
self.__polygon = polygon
|
|
|
|
|
|
|
|
return self.__polygon
|
|
|
|
|
|
|
|
def box(self, box=None):
|
|
|
|
'''
|
|
|
|
Get or set the georss:box of the entry
|
|
|
|
|
2019-07-29 02:24:02 +02:00
|
|
|
:param box: The GeoRSS formatted box (i.e. "42.943 -71.032 43.039
|
|
|
|
-69.856")
|
2019-07-08 04:31:34 +02:00
|
|
|
:return: The current georss:box of the entry
|
|
|
|
'''
|
|
|
|
if box is not None:
|
|
|
|
self.__box = box
|
|
|
|
|
|
|
|
return self.__box
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
def featuretypetag(self, featuretypetag=None):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''
|
|
|
|
Get or set the georss:featuretypetag of the entry
|
|
|
|
|
|
|
|
:param featuretypetag: The GeoRSS feaaturertyptag (e.g. "city")
|
|
|
|
:return: The current georss:featurertypetag
|
|
|
|
'''
|
|
|
|
if featuretypetag is not None:
|
|
|
|
self.__featuretypetag = featuretypetag
|
|
|
|
|
|
|
|
return self.__featuretypetag
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
def relationshiptag(self, relationshiptag=None):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''
|
|
|
|
Get or set the georss:relationshiptag of the entry
|
|
|
|
|
2019-07-29 02:24:02 +02:00
|
|
|
:param relationshiptag: The GeoRSS relationshiptag (e.g.
|
|
|
|
"is-centred-at")
|
2019-07-08 04:31:34 +02:00
|
|
|
:return: the current georss:relationshiptag
|
|
|
|
'''
|
|
|
|
if relationshiptag is not None:
|
|
|
|
self.__relationshiptag = relationshiptag
|
|
|
|
|
|
|
|
return self.__relationshiptag
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
def featurename(self, featurename=None):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''
|
|
|
|
Get or set the georss:featurename of the entry
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
:param featuretypetag: The GeoRSS featurename (e.g. "Footscray")
|
2019-07-08 04:31:34 +02:00
|
|
|
:return: the current georss:featurename
|
|
|
|
'''
|
|
|
|
if featurename is not None:
|
|
|
|
self.__featurename = featurename
|
|
|
|
|
|
|
|
return self.__featurename
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
def elev(self, elev=None):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''
|
|
|
|
Get or set the georss:elev of the entry
|
|
|
|
|
|
|
|
:param elev: The GeoRSS elevation (e.g. 100.3)
|
|
|
|
:type elev: numbers.Number
|
|
|
|
:return: the current georss:elev
|
|
|
|
'''
|
|
|
|
if elev is not None:
|
|
|
|
if not isinstance(elev, numbers.Number):
|
|
|
|
raise ValueError("elev tag must be numeric: {}".format(elev))
|
|
|
|
|
|
|
|
self.__elev = elev
|
|
|
|
|
|
|
|
return self.__elev
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
def floor(self, floor=None):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''
|
|
|
|
Get or set the georss:floor of the entry
|
|
|
|
|
|
|
|
:param floor: The GeoRSS floor (e.g. 4)
|
|
|
|
:type floor: int
|
|
|
|
:return: the current georss:floor
|
|
|
|
'''
|
|
|
|
if floor is not None:
|
|
|
|
if not isinstance(floor, int):
|
|
|
|
raise ValueError("floor tag must be int: {}".format(floor))
|
|
|
|
|
|
|
|
self.__floor = floor
|
|
|
|
|
|
|
|
return self.__floor
|
|
|
|
|
2019-07-08 06:58:21 +02:00
|
|
|
def radius(self, radius=None):
|
2019-07-08 04:31:34 +02:00
|
|
|
'''
|
|
|
|
Get or set the georss:radius of the entry
|
|
|
|
|
|
|
|
:param radius: The GeoRSS radius (e.g. 100.3)
|
|
|
|
:type radius: numbers.Number
|
|
|
|
:return: the current georss:radius
|
|
|
|
'''
|
|
|
|
if radius is not None:
|
|
|
|
if not isinstance(radius, numbers.Number):
|
2019-07-29 02:24:02 +02:00
|
|
|
raise ValueError(
|
|
|
|
"radius tag must be numeric: {}".format(radius)
|
|
|
|
)
|
2019-07-08 04:31:34 +02:00
|
|
|
|
|
|
|
self.__radius = radius
|
|
|
|
|
|
|
|
return self.__radius
|
2019-07-08 05:38:39 +02:00
|
|
|
|
|
|
|
def geom_from_geo_interface(self, geom):
|
|
|
|
'''
|
|
|
|
Generate a georss geometry from some Python object with a
|
|
|
|
``__geo_interface__`` property (see the `geo_interface specification by
|
|
|
|
Sean Gillies`_geointerface )
|
|
|
|
|
2019-07-29 02:24:02 +02:00
|
|
|
Note only a subset of GeoJSON (see `geojson.org`_geojson ) can be
|
|
|
|
easily converted to GeoRSS:
|
2019-07-08 05:38:39 +02:00
|
|
|
|
|
|
|
- Point
|
|
|
|
- LineString
|
2019-07-29 02:24:02 +02:00
|
|
|
- Polygon (if there are holes / donuts in the polygons a warning will
|
|
|
|
be generaated
|
2019-07-08 05:38:39 +02:00
|
|
|
|
|
|
|
Other GeoJson types will raise a ``ValueError``.
|
|
|
|
|
|
|
|
.. note:: The geometry is assumed to be x, y as longitude, latitude in
|
|
|
|
the WGS84 projection.
|
|
|
|
|
|
|
|
.. _geointerface: https://gist.github.com/sgillies/2217756
|
|
|
|
.. _geojson: https://geojson.org/
|
|
|
|
|
|
|
|
:param geom: Geometry object with a __geo_interface__ property
|
|
|
|
:return: the formatted GeoRSS geometry
|
|
|
|
'''
|
|
|
|
geojson = geom.__geo_interface__
|
|
|
|
|
|
|
|
if geojson['type'] not in ('Point', 'LineString', 'Polygon'):
|
|
|
|
raise GeoRSSGeometryError(geom)
|
|
|
|
|
|
|
|
if geojson['type'] == 'Point':
|
|
|
|
|
2019-07-08 07:16:20 +02:00
|
|
|
coords = '{:f} {:f}'.format(
|
2019-07-08 05:38:39 +02:00
|
|
|
geojson['coordinates'][1], # latitude is y
|
|
|
|
geojson['coordinates'][0]
|
|
|
|
)
|
|
|
|
return self.point(coords)
|
|
|
|
|
|
|
|
elif geojson['type'] == 'LineString':
|
|
|
|
|
|
|
|
coords = ' '.join(
|
2019-07-08 07:16:20 +02:00
|
|
|
'{:f} {:f}'.format(vertex[1], vertex[0])
|
2019-07-08 05:38:39 +02:00
|
|
|
for vertex in
|
|
|
|
geojson['coordinates']
|
|
|
|
)
|
|
|
|
return self.line(coords)
|
|
|
|
|
|
|
|
elif geojson['type'] == 'Polygon':
|
|
|
|
|
|
|
|
if len(geojson['coordinates']) > 1:
|
|
|
|
warnings.warn(GeoRSSPolygonInteriorWarning(geom))
|
|
|
|
|
|
|
|
coords = ' '.join(
|
2019-07-08 07:16:20 +02:00
|
|
|
'{:f} {:f}'.format(vertex[1], vertex[0])
|
2019-07-08 05:38:39 +02:00
|
|
|
for vertex in
|
|
|
|
geojson['coordinates'][0]
|
|
|
|
)
|
|
|
|
return self.polygon(coords)
|