Skip to content

Commit 4e3bed9

Browse files
Merge pull request #4289 from GeotrekCE/fix_apidae_parser
Minor fixes for Apidae parser
2 parents a442a37 + 35715dc commit 4e3bed9

File tree

16 files changed

+375
-31
lines changed

16 files changed

+375
-31
lines changed

docs/changelog.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@ CHANGELOG
55
2.109.1+dev (XXXX-XX-XX)
66
----------------------------
77

8+
**Improvements**
9+
10+
- ApidaeTrekParser now imports all features from GPX
11+
812
**Minor fixes**
913

1014
- Fix swapped plural and singular translations for Annotation Categories (#4032)
1115
- Filter out deleted services in API responses (#4284)
1216

17+
**Bug fixes**
18+
19+
- ApidaeTrekParser now fallbacks on trace filename extension if no extension property
20+
- ApidaeTrekParser now ignores when no linestring in GPX
21+
- ApidaeTrekParser duration import is fixed for multiple-days treks
22+
- Apidae tourism parser now handles missing contact properties
23+
- ApidaeTrekParser now handles missing source website
24+
1325
2.109.1 (2024-08-22)
1426
----------------------------
1527

geotrek/tourism/parsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _filter_comm(self, val, code, multiple=True):
107107
"""
108108
if not val:
109109
return None
110-
vals = [subval['coordonnees']['fr'] for subval in val if subval['type']['id'] == code]
110+
vals = [subval['coordonnees']['fr'] for subval in val if subval.get('type', {}).get('id') == code]
111111
if multiple:
112112
return ' / '.join(vals)
113113
if vals:

geotrek/trekking/locale/de/LC_MESSAGES/django.po

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: \n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2024-05-28 12:55+0000\n"
10+
"POT-Creation-Date: 2024-09-03 15:01+0000\n"
1111
"PO-Revision-Date: 2015-10-21 11:16+0200\n"
1212
"Last-Translator: \n"
1313
"Language-Team: \n"
@@ -491,7 +491,13 @@ msgid "APIDAE Trek #%(eid_val)s at line %(line)s"
491491
msgstr ""
492492

493493
msgid ""
494-
"The geometry cannot be converted to a single continuous LineString feature"
494+
"Feature geometry cannot be converted to a single continuous LineString "
495+
"feature"
496+
msgstr ""
497+
498+
msgid ""
499+
"Geometries from various features cannot be converted to a single continuous "
500+
"LineString feature"
495501
msgstr ""
496502

497503
msgid "Attached files"

geotrek/trekking/locale/en/LC_MESSAGES/django.po

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2024-05-28 12:55+0000\n"
11+
"POT-Creation-Date: 2024-09-03 15:01+0000\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -476,7 +476,13 @@ msgid "APIDAE Trek #%(eid_val)s at line %(line)s"
476476
msgstr ""
477477

478478
msgid ""
479-
"The geometry cannot be converted to a single continuous LineString feature"
479+
"Feature geometry cannot be converted to a single continuous LineString "
480+
"feature"
481+
msgstr ""
482+
483+
msgid ""
484+
"Geometries from various features cannot be converted to a single continuous "
485+
"LineString feature"
480486
msgstr ""
481487

482488
msgid "Attached files"

geotrek/trekking/locale/es/LC_MESSAGES/django.po

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: \n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2024-05-28 12:55+0000\n"
10+
"POT-Creation-Date: 2024-09-03 15:01+0000\n"
1111
"PO-Revision-Date: 2015-10-21 11:16+0200\n"
1212
"Last-Translator: [email protected]\n"
1313
"Language-Team: \n"
@@ -476,7 +476,13 @@ msgid "APIDAE Trek #%(eid_val)s at line %(line)s"
476476
msgstr ""
477477

478478
msgid ""
479-
"The geometry cannot be converted to a single continuous LineString feature"
479+
"Feature geometry cannot be converted to a single continuous LineString "
480+
"feature"
481+
msgstr ""
482+
483+
msgid ""
484+
"Geometries from various features cannot be converted to a single continuous "
485+
"LineString feature"
480486
msgstr ""
481487

482488
msgid "Attached files"

geotrek/trekking/locale/fr/LC_MESSAGES/django.po

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: \n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2024-05-28 12:55+0000\n"
10+
"POT-Creation-Date: 2024-09-03 15:01+0000\n"
1111
"PO-Revision-Date: 2020-09-22 15:57+0000\n"
1212
"Last-Translator: Emmanuelle Helly <[email protected]>\n"
1313
"Language-Team: French <https://weblate.makina-corpus.net/projects/geotrek-"
@@ -506,10 +506,16 @@ msgid "APIDAE Trek #%(eid_val)s at line %(line)s"
506506
msgstr "Itinéraire APIDAE %(eid_val)s à la ligne %(line)s"
507507

508508
msgid ""
509-
"The geometry cannot be converted to a single continuous LineString feature"
509+
"Feature geometry cannot be converted to a single continuous LineString "
510+
"feature"
510511
msgstr ""
511-
"La géométrie ne peut pas être convertie en une unique feature continue de "
512-
"type LineString"
512+
"La géométrie de la feature ne peut pas être convertie en une unique LineString continue"
513+
514+
msgid ""
515+
"Geometries from various features cannot be converted to a single continuous "
516+
"LineString feature"
517+
msgstr ""
518+
"Les géométries de plusieurs features ne peuvent pas être converties en une unique LineString continue"
513519

514520
msgid "Attached files"
515521
msgstr ""

geotrek/trekking/locale/it/LC_MESSAGES/django.po

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: \n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2024-05-28 12:55+0000\n"
10+
"POT-Creation-Date: 2024-09-03 15:01+0000\n"
1111
"PO-Revision-Date: 2015-10-21 11:16+0200\n"
1212
"Last-Translator: \n"
1313
"Language-Team: \n"
@@ -488,7 +488,13 @@ msgid "APIDAE Trek #%(eid_val)s at line %(line)s"
488488
msgstr ""
489489

490490
msgid ""
491-
"The geometry cannot be converted to a single continuous LineString feature"
491+
"Feature geometry cannot be converted to a single continuous LineString "
492+
"feature"
493+
msgstr ""
494+
495+
msgid ""
496+
"Geometries from various features cannot be converted to a single continuous "
497+
"LineString feature"
492498
msgstr ""
493499

494500
msgid "Attached files"

geotrek/trekking/locale/nl/LC_MESSAGES/django.po

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ msgid ""
77
msgstr ""
88
"Project-Id-Version: \n"
99
"Report-Msgid-Bugs-To: \n"
10-
"POT-Creation-Date: 2024-05-28 12:55+0000\n"
10+
"POT-Creation-Date: 2024-09-03 15:01+0000\n"
1111
"PO-Revision-Date: 2015-10-21 11:16+0200\n"
1212
"Last-Translator: [email protected]\n"
1313
"Language-Team: \n"
@@ -476,7 +476,13 @@ msgid "APIDAE Trek #%(eid_val)s at line %(line)s"
476476
msgstr ""
477477

478478
msgid ""
479-
"The geometry cannot be converted to a single continuous LineString feature"
479+
"Feature geometry cannot be converted to a single continuous LineString "
480+
"feature"
481+
msgstr ""
482+
483+
msgid ""
484+
"Geometries from various features cannot be converted to a single continuous "
485+
"LineString feature"
480486
msgstr ""
481487

482488
msgid "Attached files"

geotrek/trekking/parsers.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import PurePath
2+
13
import io
24
import json
35
import re
@@ -647,7 +649,7 @@ def filter_geom(self, src, val):
647649
plan = self._find_first_plan_with_supported_file_extension(val, supported_extensions)
648650
geom_file = self._fetch_geometry_file(plan)
649651

650-
ext = plan['traductionFichiers'][0]['extension']
652+
ext = self._get_plan_extension(plan)
651653
if ext == 'gpx':
652654
return ApidaeTrekParser._get_geom_from_gpx(geom_file)
653655
elif ext == 'kml':
@@ -694,7 +696,7 @@ def filter_source(self, src, val):
694696
val=[manager['nom']]
695697
)
696698
source = sources[0]
697-
source.website = manager['siteWeb']
699+
source.website = manager.get('siteWeb')
698700
source.save()
699701
return sources
700702

@@ -902,14 +904,26 @@ def _find_first_plan_with_supported_file_extension(items, supported_extensions):
902904
plans = [item for item in items if item['type'] == 'PLAN']
903905
if not plans:
904906
raise RowImportError('The trek from APIDAE has no attachment with the type "PLAN"')
905-
supported_plans = [plan for plan in plans if plan['traductionFichiers'][0]['extension'] in supported_extensions]
907+
supported_plans = [plan for plan in plans if
908+
ApidaeTrekParser._get_plan_extension(plan) in supported_extensions]
906909
if not supported_plans:
907910
raise RowImportError(
908911
"The trek from APIDAE has no attached \"PLAN\" in a supported format. "
909912
f"Supported formats are : {', '.join(supported_extensions)}"
910913
)
911914
return supported_plans[0]
912915

916+
@staticmethod
917+
def _get_plan_extension(plan):
918+
info_fichier = plan['traductionFichiers'][0]
919+
extension_prop = info_fichier.get('extension')
920+
if extension_prop:
921+
return extension_prop
922+
url_suffix = PurePath(info_fichier['url']).suffix
923+
if url_suffix:
924+
return url_suffix.split('.')[1]
925+
return None
926+
913927
@staticmethod
914928
def _get_geom_from_gpx(data):
915929
"""Given GPX data as bytes it returns a geom."""
@@ -925,6 +939,8 @@ def _get_geom_from_gpx(data):
925939
geos = ApidaeTrekParser._maybe_get_linestring_from_layer(layer)
926940
if geos:
927941
break
942+
else:
943+
raise RowImportError("No LineString feature found in GPX layers tracks or routes")
928944
geos.transform(settings.SRID)
929945
return geos
930946

@@ -998,13 +1014,24 @@ def _convert_to_geos(geom):
9981014
def _maybe_get_linestring_from_layer(layer):
9991015
if layer.num_feat == 0:
10001016
return None
1001-
first_feature = layer[0]
1002-
geos = ApidaeTrekParser._convert_to_geos(first_feature.geom)
1003-
if geos.geom_type == 'MultiLineString':
1004-
geos = geos.merged
1017+
geoms = []
1018+
for feat in layer:
1019+
if feat.geom.num_coords == 0:
1020+
continue
1021+
geos = ApidaeTrekParser._convert_to_geos(feat.geom)
10051022
if geos.geom_type == 'MultiLineString':
1006-
raise RowImportError(_("The geometry cannot be converted to a single continuous LineString feature"))
1007-
return geos
1023+
geos = geos.merged # If possible we merge the MultiLineString into a LineString
1024+
if geos.geom_type == 'MultiLineString':
1025+
raise RowImportError(_("Feature geometry cannot be converted to a single continuous LineString feature"))
1026+
geoms.append(geos)
1027+
1028+
full_geom = MultiLineString(geoms)
1029+
full_geom.srid = geoms[0].srid
1030+
full_geom = full_geom.merged # If possible we merge the MultiLineString into a LineString
1031+
if full_geom.geom_type == 'MultiLineString':
1032+
raise RowImportError(_("Geometries from various features cannot be converted to a single continuous LineString feature"))
1033+
1034+
return full_geom
10081035

10091036
@staticmethod
10101037
def _find_matching_practice_in_mapping(activities_ids, mapping):
@@ -1095,12 +1122,19 @@ def est_fermé_temporairement(ouverture):
10951122

10961123
@staticmethod
10971124
def _make_duration(duration_in_minutes=None, duration_in_days=None):
1098-
"""Returns the duration in hours. The method expects one argument or the other, not both. If both arguments have
1099-
non-zero values the method only considers `duration_in_minutes` and discards `duration_in_days`."""
1100-
if duration_in_minutes:
1101-
return float((Decimal(duration_in_minutes) / Decimal(60)).quantize(Decimal('.01')))
1102-
elif duration_in_days:
1125+
"""Returns the duration in hours. There are 2 use cases:
1126+
1127+
1. the parsed trek is a one-day trip: only the duration in minutes is provided from Apiade.
1128+
2. the parsed trek is a multiple-day trip: the duration_in_days is provided. The duration_in_minutes may be provided
1129+
as a crude indication of how long each step is. That second value does not fit in Geotrek model.
1130+
1131+
So the duration_in_days is used if provided (and duration_in_minutes discarded), it means we are in the use case 2.
1132+
Otherwise the duration_in_minutes is used, it means use case 1.
1133+
"""
1134+
if duration_in_days:
11031135
return float(duration_in_days * 24)
1136+
elif duration_in_minutes:
1137+
return float((Decimal(duration_in_minutes) / Decimal(60)).quantize(Decimal('.01')))
11041138
else:
11051139
return None
11061140

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0"?>
2+
<gpx version="1.1" creator="GDAL 3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://www.topografix.com/GPX/1/1"
4+
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
5+
6+
<trk>
7+
<trkseg>
8+
<trkpt lat="45.969096946431875" lon="6.588397281348481">
9+
</trkpt>
10+
<trkpt lat="45.969102847169822" lon="6.588340057533565">
11+
</trkpt>
12+
<trkpt lat="45.969102837080015" lon="6.58828059319185">
13+
</trkpt>
14+
</trkseg>
15+
<trkseg>
16+
<trkpt lat="45.969102837080015" lon="6.58828059319185">
17+
</trkpt>
18+
<trkpt lat="45.969062055839991" lon="6.588259867989978">
19+
</trkpt>
20+
<trkpt lat="45.96896405596695" lon="6.588139868263409">
21+
</trkpt>
22+
</trkseg>
23+
</trk>
24+
<trk>
25+
<trkseg>
26+
<trkpt lat="45.96896405596695" lon="6.588139868263409">
27+
</trkpt>
28+
<trkpt lat="45.968927262512906" lon="6.588064192803646">
29+
</trkpt>
30+
</trkseg>
31+
<trkseg>
32+
<trkpt lat="45.968927262512906" lon="6.588064192803646">
33+
</trkpt>
34+
<trkpt lat="45.968868791896192" lon="6.588002807774547">
35+
</trkpt>
36+
<trkpt lat="45.968804552509155" lon="6.587925774641183">
37+
</trkpt>
38+
<trkpt lat="45.968775668366185" lon="6.588058934036015">
39+
</trkpt>
40+
</trkseg>
41+
<trkseg>
42+
<trkpt lat="45.968775668366185" lon="6.588058934036015">
43+
</trkpt>
44+
<trkpt lat="45.96873332012207" lon="6.588127882928957">
45+
</trkpt>
46+
<trkpt lat="45.968695134981964" lon="6.58819005406965">
47+
</trkpt>
48+
<trkpt lat="45.968715423155487" lon="6.588509348314536">
49+
</trkpt>
50+
</trkseg>
51+
</trk>
52+
</gpx>

0 commit comments

Comments
 (0)