forkparser 2026.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,682 @@
1
+ # Support for the GeoRSS format
2
+ # Copyright 2010-2025 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
+ from ..util import FeedParserDict
30
+
31
+
32
+ class Namespace:
33
+ supported_namespaces = {
34
+ "http://www.w3.org/2003/01/geo/wgs84_pos#": "geo",
35
+ "http://www.georss.org/georss": "georss",
36
+ "http://www.opengis.net/gml": "gml",
37
+ }
38
+
39
+ def __init__(self):
40
+ self.ingeometry = 0
41
+ super().__init__()
42
+
43
+ def _start_georssgeom(self, attrs_d):
44
+ self.push("geometry", 0)
45
+ context = self._get_context()
46
+ context["where"] = FeedParserDict()
47
+
48
+ _start_georss_point = _start_georssgeom
49
+ _start_georss_line = _start_georssgeom
50
+ _start_georss_polygon = _start_georssgeom
51
+ _start_georss_box = _start_georssgeom
52
+
53
+ def _save_where(self, geometry):
54
+ context = self._get_context()
55
+ context["where"].update(geometry)
56
+
57
+ def _end_georss_point(self):
58
+ geometry = _parse_georss_point(self.pop("geometry"))
59
+ if geometry:
60
+ self._save_where(geometry)
61
+
62
+ def _end_georss_line(self):
63
+ geometry = _parse_georss_line(self.pop("geometry"))
64
+ if geometry:
65
+ self._save_where(geometry)
66
+
67
+ def _end_georss_polygon(self):
68
+ this = self.pop("geometry")
69
+ geometry = _parse_georss_polygon(this)
70
+ if geometry:
71
+ self._save_where(geometry)
72
+
73
+ def _end_georss_box(self):
74
+ geometry = _parse_georss_box(self.pop("geometry"))
75
+ if geometry:
76
+ self._save_where(geometry)
77
+
78
+ def _start_where(self, attrs_d):
79
+ self.push("where", 0)
80
+ context = self._get_context()
81
+ context["where"] = FeedParserDict()
82
+
83
+ _start_georss_where = _start_where
84
+
85
+ def _parse_srs_attrs(self, attrs_d):
86
+ srs_name = attrs_d.get("srsname")
87
+ try:
88
+ srs_dimension = int(attrs_d.get("srsdimension", "2"))
89
+ except ValueError:
90
+ srs_dimension = 2
91
+ context = self._get_context()
92
+ if "where" not in context:
93
+ context["where"] = {}
94
+ context["where"]["srsName"] = srs_name
95
+ context["where"]["srsDimension"] = srs_dimension
96
+
97
+ def _start_gml_point(self, attrs_d):
98
+ self._parse_srs_attrs(attrs_d)
99
+ self.ingeometry = 1
100
+ self.push("geometry", 0)
101
+
102
+ def _start_gml_linestring(self, attrs_d):
103
+ self._parse_srs_attrs(attrs_d)
104
+ self.ingeometry = "linestring"
105
+ self.push("geometry", 0)
106
+
107
+ def _start_gml_polygon(self, attrs_d):
108
+ self._parse_srs_attrs(attrs_d)
109
+ self.push("geometry", 0)
110
+
111
+ def _start_gml_exterior(self, attrs_d):
112
+ self.push("geometry", 0)
113
+
114
+ def _start_gml_linearring(self, attrs_d):
115
+ self.ingeometry = "polygon"
116
+ self.push("geometry", 0)
117
+
118
+ def _start_gml_pos(self, attrs_d):
119
+ self.push("pos", 0)
120
+
121
+ def _end_gml_pos(self):
122
+ this = self.pop("pos")
123
+ context = self._get_context()
124
+ srs_name = context["where"].get("srsName")
125
+ srs_dimension = context["where"].get("srsDimension", 2)
126
+ swap = True
127
+ if srs_name and "EPSG" in srs_name:
128
+ epsg = int(srs_name.split(":")[-1])
129
+ swap = bool(epsg in _geogCS)
130
+ geometry = _parse_georss_point(this, swap=swap, dims=srs_dimension)
131
+ if geometry:
132
+ self._save_where(geometry)
133
+
134
+ def _start_gml_poslist(self, attrs_d):
135
+ self.push("pos", 0)
136
+
137
+ def _end_gml_poslist(self):
138
+ this = self.pop("pos")
139
+ context = self._get_context()
140
+ srs_name = context["where"].get("srsName")
141
+ srs_dimension = context["where"].get("srsDimension", 2)
142
+ swap = True
143
+ if srs_name and "EPSG" in srs_name:
144
+ epsg = int(srs_name.split(":")[-1])
145
+ swap = bool(epsg in _geogCS)
146
+ geometry = _parse_poslist(this, self.ingeometry, swap=swap, dims=srs_dimension)
147
+ if geometry:
148
+ self._save_where(geometry)
149
+
150
+ def _end_geom(self):
151
+ self.ingeometry = 0
152
+ self.pop("geometry")
153
+
154
+ _end_gml_point = _end_geom
155
+ _end_gml_linestring = _end_geom
156
+ _end_gml_linearring = _end_geom
157
+ _end_gml_exterior = _end_geom
158
+ _end_gml_polygon = _end_geom
159
+
160
+ def _end_where(self):
161
+ self.pop("where")
162
+
163
+ _end_georss_where = _end_where
164
+
165
+
166
+ # GeoRSS geometry parsers. Each return a dict with 'type' and 'coordinates'
167
+ # items, or None in the case of a parsing error.
168
+
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
+ if geom_type == "polygon":
174
+ ring = _parse_georss_line(value, swap, dims)
175
+ return {"type": "Polygon", "coordinates": (ring["coordinates"],)}
176
+ return None
177
+
178
+
179
+ def _gen_georss_coords(value, swap=True, dims=2):
180
+ # A generator of (lon, lat) pairs from a string of encoded GeoRSS
181
+ # coordinates. Converts to floats and swaps order.
182
+ latlons = (float(ll) for ll in value.replace(",", " ").split())
183
+ while True:
184
+ try:
185
+ t = [next(latlons), next(latlons)][:: swap and -1 or 1]
186
+ if dims == 3:
187
+ t.append(next(latlons))
188
+ yield tuple(t)
189
+ except StopIteration:
190
+ return
191
+
192
+
193
+ def _parse_georss_point(value, swap=True, dims=2):
194
+ # A point contains a single latitude-longitude pair, separated by
195
+ # whitespace. We'll also handle comma separators.
196
+ try:
197
+ coords = list(_gen_georss_coords(value, swap, dims))
198
+ return {"type": "Point", "coordinates": coords[0]}
199
+ except (IndexError, ValueError):
200
+ return None
201
+
202
+
203
+ def _parse_georss_line(value, swap=True, dims=2):
204
+ # A line contains a space separated list of latitude-longitude pairs in
205
+ # WGS84 coordinate reference system, with each pair separated by
206
+ # whitespace. There must be at least two pairs.
207
+ try:
208
+ coords = list(_gen_georss_coords(value, swap, dims))
209
+ return {"type": "LineString", "coordinates": coords}
210
+ except (IndexError, ValueError):
211
+ return None
212
+
213
+
214
+ def _parse_georss_polygon(value, swap=True, dims=2):
215
+ # A polygon contains a space separated list of latitude-longitude pairs,
216
+ # with each pair separated by whitespace. There must be at least four
217
+ # pairs, with the last being identical to the first (so a polygon has a
218
+ # minimum of three actual points).
219
+ try:
220
+ ring = list(_gen_georss_coords(value, swap, dims))
221
+ except (IndexError, ValueError):
222
+ return None
223
+ if len(ring) < 4:
224
+ return None
225
+ return {"type": "Polygon", "coordinates": (ring,)}
226
+
227
+
228
+ def _parse_georss_box(value, swap=True, dims=2):
229
+ # A bounding box is a rectangular region, often used to define the extents
230
+ # of a map or a rough area of interest. A box contains two space separate
231
+ # latitude-longitude pairs, with each pair separated by whitespace. The
232
+ # first pair is the lower corner, the second is the upper corner.
233
+ try:
234
+ coords = list(_gen_georss_coords(value, swap, dims))
235
+ return {"type": "Box", "coordinates": tuple(coords)}
236
+ except (IndexError, ValueError):
237
+ return None
238
+
239
+
240
+ # The list of EPSG codes for geographic (latitude/longitude) coordinate
241
+ # systems to support decoding of GeoRSS GML profiles.
242
+ _geogCS = [
243
+ 3819,
244
+ 3821,
245
+ 3824,
246
+ 3889,
247
+ 3906,
248
+ 4001,
249
+ 4002,
250
+ 4003,
251
+ 4004,
252
+ 4005,
253
+ 4006,
254
+ 4007,
255
+ 4008,
256
+ 4009,
257
+ 4010,
258
+ 4011,
259
+ 4012,
260
+ 4013,
261
+ 4014,
262
+ 4015,
263
+ 4016,
264
+ 4018,
265
+ 4019,
266
+ 4020,
267
+ 4021,
268
+ 4022,
269
+ 4023,
270
+ 4024,
271
+ 4025,
272
+ 4027,
273
+ 4028,
274
+ 4029,
275
+ 4030,
276
+ 4031,
277
+ 4032,
278
+ 4033,
279
+ 4034,
280
+ 4035,
281
+ 4036,
282
+ 4041,
283
+ 4042,
284
+ 4043,
285
+ 4044,
286
+ 4045,
287
+ 4046,
288
+ 4047,
289
+ 4052,
290
+ 4053,
291
+ 4054,
292
+ 4055,
293
+ 4075,
294
+ 4081,
295
+ 4120,
296
+ 4121,
297
+ 4122,
298
+ 4123,
299
+ 4124,
300
+ 4125,
301
+ 4126,
302
+ 4127,
303
+ 4128,
304
+ 4129,
305
+ 4130,
306
+ 4131,
307
+ 4132,
308
+ 4133,
309
+ 4134,
310
+ 4135,
311
+ 4136,
312
+ 4137,
313
+ 4138,
314
+ 4139,
315
+ 4140,
316
+ 4141,
317
+ 4142,
318
+ 4143,
319
+ 4144,
320
+ 4145,
321
+ 4146,
322
+ 4147,
323
+ 4148,
324
+ 4149,
325
+ 4150,
326
+ 4151,
327
+ 4152,
328
+ 4153,
329
+ 4154,
330
+ 4155,
331
+ 4156,
332
+ 4157,
333
+ 4158,
334
+ 4159,
335
+ 4160,
336
+ 4161,
337
+ 4162,
338
+ 4163,
339
+ 4164,
340
+ 4165,
341
+ 4166,
342
+ 4167,
343
+ 4168,
344
+ 4169,
345
+ 4170,
346
+ 4171,
347
+ 4172,
348
+ 4173,
349
+ 4174,
350
+ 4175,
351
+ 4176,
352
+ 4178,
353
+ 4179,
354
+ 4180,
355
+ 4181,
356
+ 4182,
357
+ 4183,
358
+ 4184,
359
+ 4185,
360
+ 4188,
361
+ 4189,
362
+ 4190,
363
+ 4191,
364
+ 4192,
365
+ 4193,
366
+ 4194,
367
+ 4195,
368
+ 4196,
369
+ 4197,
370
+ 4198,
371
+ 4199,
372
+ 4200,
373
+ 4201,
374
+ 4202,
375
+ 4203,
376
+ 4204,
377
+ 4205,
378
+ 4206,
379
+ 4207,
380
+ 4208,
381
+ 4209,
382
+ 4210,
383
+ 4211,
384
+ 4212,
385
+ 4213,
386
+ 4214,
387
+ 4215,
388
+ 4216,
389
+ 4218,
390
+ 4219,
391
+ 4220,
392
+ 4221,
393
+ 4222,
394
+ 4223,
395
+ 4224,
396
+ 4225,
397
+ 4226,
398
+ 4227,
399
+ 4228,
400
+ 4229,
401
+ 4230,
402
+ 4231,
403
+ 4232,
404
+ 4233,
405
+ 4234,
406
+ 4235,
407
+ 4236,
408
+ 4237,
409
+ 4238,
410
+ 4239,
411
+ 4240,
412
+ 4241,
413
+ 4242,
414
+ 4243,
415
+ 4244,
416
+ 4245,
417
+ 4246,
418
+ 4247,
419
+ 4248,
420
+ 4249,
421
+ 4250,
422
+ 4251,
423
+ 4252,
424
+ 4253,
425
+ 4254,
426
+ 4255,
427
+ 4256,
428
+ 4257,
429
+ 4258,
430
+ 4259,
431
+ 4260,
432
+ 4261,
433
+ 4262,
434
+ 4263,
435
+ 4264,
436
+ 4265,
437
+ 4266,
438
+ 4267,
439
+ 4268,
440
+ 4269,
441
+ 4270,
442
+ 4271,
443
+ 4272,
444
+ 4273,
445
+ 4274,
446
+ 4275,
447
+ 4276,
448
+ 4277,
449
+ 4278,
450
+ 4279,
451
+ 4280,
452
+ 4281,
453
+ 4282,
454
+ 4283,
455
+ 4284,
456
+ 4285,
457
+ 4286,
458
+ 4287,
459
+ 4288,
460
+ 4289,
461
+ 4291,
462
+ 4292,
463
+ 4293,
464
+ 4294,
465
+ 4295,
466
+ 4296,
467
+ 4297,
468
+ 4298,
469
+ 4299,
470
+ 4300,
471
+ 4301,
472
+ 4302,
473
+ 4303,
474
+ 4304,
475
+ 4306,
476
+ 4307,
477
+ 4308,
478
+ 4309,
479
+ 4310,
480
+ 4311,
481
+ 4312,
482
+ 4313,
483
+ 4314,
484
+ 4315,
485
+ 4316,
486
+ 4317,
487
+ 4318,
488
+ 4319,
489
+ 4322,
490
+ 4324,
491
+ 4326,
492
+ 4463,
493
+ 4470,
494
+ 4475,
495
+ 4483,
496
+ 4490,
497
+ 4555,
498
+ 4558,
499
+ 4600,
500
+ 4601,
501
+ 4602,
502
+ 4603,
503
+ 4604,
504
+ 4605,
505
+ 4606,
506
+ 4607,
507
+ 4608,
508
+ 4609,
509
+ 4610,
510
+ 4611,
511
+ 4612,
512
+ 4613,
513
+ 4614,
514
+ 4615,
515
+ 4616,
516
+ 4617,
517
+ 4618,
518
+ 4619,
519
+ 4620,
520
+ 4621,
521
+ 4622,
522
+ 4623,
523
+ 4624,
524
+ 4625,
525
+ 4626,
526
+ 4627,
527
+ 4628,
528
+ 4629,
529
+ 4630,
530
+ 4631,
531
+ 4632,
532
+ 4633,
533
+ 4634,
534
+ 4635,
535
+ 4636,
536
+ 4637,
537
+ 4638,
538
+ 4639,
539
+ 4640,
540
+ 4641,
541
+ 4642,
542
+ 4643,
543
+ 4644,
544
+ 4645,
545
+ 4646,
546
+ 4657,
547
+ 4658,
548
+ 4659,
549
+ 4660,
550
+ 4661,
551
+ 4662,
552
+ 4663,
553
+ 4664,
554
+ 4665,
555
+ 4666,
556
+ 4667,
557
+ 4668,
558
+ 4669,
559
+ 4670,
560
+ 4671,
561
+ 4672,
562
+ 4673,
563
+ 4674,
564
+ 4675,
565
+ 4676,
566
+ 4677,
567
+ 4678,
568
+ 4679,
569
+ 4680,
570
+ 4681,
571
+ 4682,
572
+ 4683,
573
+ 4684,
574
+ 4685,
575
+ 4686,
576
+ 4687,
577
+ 4688,
578
+ 4689,
579
+ 4690,
580
+ 4691,
581
+ 4692,
582
+ 4693,
583
+ 4694,
584
+ 4695,
585
+ 4696,
586
+ 4697,
587
+ 4698,
588
+ 4699,
589
+ 4700,
590
+ 4701,
591
+ 4702,
592
+ 4703,
593
+ 4704,
594
+ 4705,
595
+ 4706,
596
+ 4707,
597
+ 4708,
598
+ 4709,
599
+ 4710,
600
+ 4711,
601
+ 4712,
602
+ 4713,
603
+ 4714,
604
+ 4715,
605
+ 4716,
606
+ 4717,
607
+ 4718,
608
+ 4719,
609
+ 4720,
610
+ 4721,
611
+ 4722,
612
+ 4723,
613
+ 4724,
614
+ 4725,
615
+ 4726,
616
+ 4727,
617
+ 4728,
618
+ 4729,
619
+ 4730,
620
+ 4731,
621
+ 4732,
622
+ 4733,
623
+ 4734,
624
+ 4735,
625
+ 4736,
626
+ 4737,
627
+ 4738,
628
+ 4739,
629
+ 4740,
630
+ 4741,
631
+ 4742,
632
+ 4743,
633
+ 4744,
634
+ 4745,
635
+ 4746,
636
+ 4747,
637
+ 4748,
638
+ 4749,
639
+ 4750,
640
+ 4751,
641
+ 4752,
642
+ 4753,
643
+ 4754,
644
+ 4755,
645
+ 4756,
646
+ 4757,
647
+ 4758,
648
+ 4759,
649
+ 4760,
650
+ 4761,
651
+ 4762,
652
+ 4763,
653
+ 4764,
654
+ 4765,
655
+ 4801,
656
+ 4802,
657
+ 4803,
658
+ 4804,
659
+ 4805,
660
+ 4806,
661
+ 4807,
662
+ 4808,
663
+ 4809,
664
+ 4810,
665
+ 4811,
666
+ 4813,
667
+ 4814,
668
+ 4815,
669
+ 4816,
670
+ 4817,
671
+ 4818,
672
+ 4819,
673
+ 4820,
674
+ 4821,
675
+ 4823,
676
+ 4824,
677
+ 4901,
678
+ 4902,
679
+ 4903,
680
+ 4904,
681
+ 4979,
682
+ ]