1 # Support for the GeoRSS format 2 # Copyright 2010-2023 Kurt McKee <contactme@kurtmckee.org> 3 # Copyright 2002-2008 Mark Pilgrim 4 # All rights reserved. 5 # 6 # This file is a part of feedparser. 7 # 8 # Redistribution and use in source and binary forms, with or without 9 # modification, are permitted provided that the following conditions are met: 10 # 11 # * Redistributions of source code must retain the above copyright notice, 12 # this list of conditions and the following disclaimer. 13 # * Redistributions in binary form must reproduce the above copyright notice, 14 # this list of conditions and the following disclaimer in the documentation 15 # and/or other materials provided with the distribution. 16 # 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' 18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 # POSSIBILITY OF SUCH DAMAGE. 28 29 # Required for Python 3.6 compatibility. 30 from __future__ import generator_stop 31 32 from ..util import FeedParserDict 33 34 35 class Namespace(object): 36 supported_namespaces = { 37 'http://www.w3.org/2003/01/geo/wgs84_pos#': 'geo', 38 'http://www.georss.org/georss': 'georss', 39 'http://www.opengis.net/gml': 'gml', 40 } 41 42 def __init__(self): 43 self.ingeometry = 0 44 super(Namespace, self).__init__() 45 46 def _start_georssgeom(self, attrs_d): 47 self.push('geometry', 0) 48 context = self._get_context() 49 context['where'] = FeedParserDict() 50 51 _start_georss_point = _start_georssgeom 52 _start_georss_line = _start_georssgeom 53 _start_georss_polygon = _start_georssgeom 54 _start_georss_box = _start_georssgeom 55 56 def _save_where(self, geometry): 57 context = self._get_context() 58 context['where'].update(geometry) 59 60 def _end_georss_point(self): 61 geometry = _parse_georss_point(self.pop('geometry')) 62 if geometry: 63 self._save_where(geometry) 64 65 def _end_georss_line(self): 66 geometry = _parse_georss_line(self.pop('geometry')) 67 if geometry: 68 self._save_where(geometry) 69 70 def _end_georss_polygon(self): 71 this = self.pop('geometry') 72 geometry = _parse_georss_polygon(this) 73 if geometry: 74 self._save_where(geometry) 75 76 def _end_georss_box(self): 77 geometry = _parse_georss_box(self.pop('geometry')) 78 if geometry: 79 self._save_where(geometry) 80 81 def _start_where(self, attrs_d): 82 self.push('where', 0) 83 context = self._get_context() 84 context['where'] = FeedParserDict() 85 _start_georss_where = _start_where 86 87 def _parse_srs_attrs(self, attrs_d): 88 srs_name = attrs_d.get('srsname') 89 try: 90 srs_dimension = int(attrs_d.get('srsdimension', '2')) 91 except ValueError: 92 srs_dimension = 2 93 context = self._get_context() 94 if 'where' not in context: 95 context['where'] = {} 96 context['where']['srsName'] = srs_name 97 context['where']['srsDimension'] = srs_dimension 98 99 def _start_gml_point(self, attrs_d): 100 self._parse_srs_attrs(attrs_d) 101 self.ingeometry = 1 102 self.push('geometry', 0) 103 104 def _start_gml_linestring(self, attrs_d): 105 self._parse_srs_attrs(attrs_d) 106 self.ingeometry = 'linestring' 107 self.push('geometry', 0) 108 109 def _start_gml_polygon(self, attrs_d): 110 self._parse_srs_attrs(attrs_d) 111 self.push('geometry', 0) 112 113 def _start_gml_exterior(self, attrs_d): 114 self.push('geometry', 0) 115 116 def _start_gml_linearring(self, attrs_d): 117 self.ingeometry = 'polygon' 118 self.push('geometry', 0) 119 120 def _start_gml_pos(self, attrs_d): 121 self.push('pos', 0) 122 123 def _end_gml_pos(self): 124 this = self.pop('pos') 125 context = self._get_context() 126 srs_name = context['where'].get('srsName') 127 srs_dimension = context['where'].get('srsDimension', 2) 128 swap = True 129 if srs_name and "EPSG" in srs_name: 130 epsg = int(srs_name.split(":")[-1]) 131 swap = bool(epsg in _geogCS) 132 geometry = _parse_georss_point(this, swap=swap, dims=srs_dimension) 133 if geometry: 134 self._save_where(geometry) 135 136 def _start_gml_poslist(self, attrs_d): 137 self.push('pos', 0) 138 139 def _end_gml_poslist(self): 140 this = self.pop('pos') 141 context = self._get_context() 142 srs_name = context['where'].get('srsName') 143 srs_dimension = context['where'].get('srsDimension', 2) 144 swap = True 145 if srs_name and "EPSG" in srs_name: 146 epsg = int(srs_name.split(":")[-1]) 147 swap = bool(epsg in _geogCS) 148 geometry = _parse_poslist( 149 this, self.ingeometry, swap=swap, dims=srs_dimension) 150 if geometry: 151 self._save_where(geometry) 152 153 def _end_geom(self): 154 self.ingeometry = 0 155 self.pop('geometry') 156 _end_gml_point = _end_geom 157 _end_gml_linestring = _end_geom 158 _end_gml_linearring = _end_geom 159 _end_gml_exterior = _end_geom 160 _end_gml_polygon = _end_geom 161 162 def _end_where(self): 163 self.pop('where') 164 _end_georss_where = _end_where 165 166 167 # GeoRSS geometry parsers. Each return a dict with 'type' and 'coordinates' 168 # items, or None in the case of a parsing error. 169 170 def _parse_poslist(value, geom_type, swap=True, dims=2): 171 if geom_type == 'linestring': 172 return _parse_georss_line(value, swap, dims) 173 elif geom_type == 'polygon': 174 ring = _parse_georss_line(value, swap, dims) 175 return {'type': 'Polygon', 'coordinates': (ring['coordinates'],)} 176 else: 177 return None 178 179 180 def _gen_georss_coords(value, swap=True, dims=2): 181 # A generator of (lon, lat) pairs from a string of encoded GeoRSS 182 # coordinates. Converts to floats and swaps order. 183 latlons = (float(ll) for ll in value.replace(',', ' ').split()) 184 while True: 185 try: 186 t = [next(latlons), next(latlons)][::swap and -1 or 1] 187 if dims == 3: 188 t.append(next(latlons)) 189 yield tuple(t) 190 except StopIteration: 191 return 192 193 194 def _parse_georss_point(value, swap=True, dims=2): 195 # A point contains a single latitude-longitude pair, separated by 196 # whitespace. We'll also handle comma separators. 197 try: 198 coords = list(_gen_georss_coords(value, swap, dims)) 199 return {'type': 'Point', 'coordinates': coords[0]} 200 except (IndexError, ValueError): 201 return None 202 203 204 def _parse_georss_line(value, swap=True, dims=2): 205 # A line contains a space separated list of latitude-longitude pairs in 206 # WGS84 coordinate reference system, with each pair separated by 207 # whitespace. There must be at least two pairs. 208 try: 209 coords = list(_gen_georss_coords(value, swap, dims)) 210 return {'type': 'LineString', 'coordinates': coords} 211 except (IndexError, ValueError): 212 return None 213 214 215 def _parse_georss_polygon(value, swap=True, dims=2): 216 # A polygon contains a space separated list of latitude-longitude pairs, 217 # with each pair separated by whitespace. There must be at least four 218 # pairs, with the last being identical to the first (so a polygon has a 219 # minimum of three actual points). 220 try: 221 ring = list(_gen_georss_coords(value, swap, dims)) 222 except (IndexError, ValueError): 223 return None 224 if len(ring) < 4: 225 return None 226 return {'type': 'Polygon', 'coordinates': (ring,)} 227 228 229 def _parse_georss_box(value, swap=True, dims=2): 230 # A bounding box is a rectangular region, often used to define the extents 231 # of a map or a rough area of interest. A box contains two space separate 232 # latitude-longitude pairs, with each pair separated by whitespace. The 233 # first pair is the lower corner, the second is the upper corner. 234 try: 235 coords = list(_gen_georss_coords(value, swap, dims)) 236 return {'type': 'Box', 'coordinates': tuple(coords)} 237 except (IndexError, ValueError): 238 return None 239 240 241 # The list of EPSG codes for geographic (latitude/longitude) coordinate 242 # systems to support decoding of GeoRSS GML profiles. 243 _geogCS = [ 244 3819, 3821, 3824, 3889, 3906, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008, 245 4009, 4010, 4011, 4012, 4013, 4014, 4015, 4016, 4018, 4019, 4020, 4021, 4022, 246 4023, 4024, 4025, 4027, 4028, 4029, 4030, 4031, 4032, 4033, 4034, 4035, 4036, 247 4041, 4042, 4043, 4044, 4045, 4046, 4047, 4052, 4053, 4054, 4055, 4075, 4081, 248 4120, 4121, 4122, 4123, 4124, 4125, 4126, 4127, 4128, 4129, 4130, 4131, 4132, 249 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4140, 4141, 4142, 4143, 4144, 4145, 250 4146, 4147, 4148, 4149, 4150, 4151, 4152, 4153, 4154, 4155, 4156, 4157, 4158, 251 4159, 4160, 4161, 4162, 4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 252 4172, 4173, 4174, 4175, 4176, 4178, 4179, 4180, 4181, 4182, 4183, 4184, 4185, 253 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195, 4196, 4197, 4198, 4199, 4200, 254 4201, 4202, 4203, 4204, 4205, 4206, 4207, 4208, 4209, 4210, 4211, 4212, 4213, 255 4214, 4215, 4216, 4218, 4219, 4220, 4221, 4222, 4223, 4224, 4225, 4226, 4227, 256 4228, 4229, 4230, 4231, 4232, 4233, 4234, 4235, 4236, 4237, 4238, 4239, 4240, 257 4241, 4242, 4243, 4244, 4245, 4246, 4247, 4248, 4249, 4250, 4251, 4252, 4253, 258 4254, 4255, 4256, 4257, 4258, 4259, 4260, 4261, 4262, 4263, 4264, 4265, 4266, 259 4267, 4268, 4269, 4270, 4271, 4272, 4273, 4274, 4275, 4276, 4277, 4278, 4279, 260 4280, 4281, 4282, 4283, 4284, 4285, 4286, 4287, 4288, 4289, 4291, 4292, 4293, 261 4294, 4295, 4296, 4297, 4298, 4299, 4300, 4301, 4302, 4303, 4304, 4306, 4307, 262 4308, 4309, 4310, 4311, 4312, 4313, 4314, 4315, 4316, 4317, 4318, 4319, 4322, 263 4324, 4326, 4463, 4470, 4475, 4483, 4490, 4555, 4558, 4600, 4601, 4602, 4603, 264 4604, 4605, 4606, 4607, 4608, 4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 265 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625, 4626, 4627, 4628, 4629, 266 4630, 4631, 4632, 4633, 4634, 4635, 4636, 4637, 4638, 4639, 4640, 4641, 4642, 267 4643, 4644, 4645, 4646, 4657, 4658, 4659, 4660, 4661, 4662, 4663, 4664, 4665, 268 4666, 4667, 4668, 4669, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 269 4679, 4680, 4681, 4682, 4683, 4684, 4685, 4686, 4687, 4688, 4689, 4690, 4691, 270 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700, 4701, 4702, 4703, 4704, 271 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4713, 4714, 4715, 4716, 4717, 272 4718, 4719, 4720, 4721, 4722, 4723, 4724, 4725, 4726, 4727, 4728, 4729, 4730, 273 4731, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739, 4740, 4741, 4742, 4743, 274 4744, 4745, 4746, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4756, 275 4757, 4758, 4759, 4760, 4761, 4762, 4763, 4764, 4765, 4801, 4802, 4803, 4804, 276 4805, 4806, 4807, 4808, 4809, 4810, 4811, 4813, 4814, 4815, 4816, 4817, 4818, 277 4819, 4820, 4821, 4823, 4824, 4901, 4902, 4903, 4904, 4979, 278 ] 279