11package fi.hsl.jore4.hastus.service.exporting
22
3+ import fi.hsl.jore4.hastus.Constants.MAX_LENGTH_HASTUS_ROUTE_DESCRIPTION
4+ import fi.hsl.jore4.hastus.Constants.MAX_LENGTH_HASTUS_ROUTE_VARIANT_DESCRIPTION
5+ import fi.hsl.jore4.hastus.Constants.MAX_LENGTH_HASTUS_STOP_NAME
6+ import fi.hsl.jore4.hastus.Constants.MAX_LENGTH_HASTUS_STOP_STREET_NAME
37import fi.hsl.jore4.hastus.data.format.JoreRouteDirection
48import fi.hsl.jore4.hastus.data.format.NumberWithAccuracy
59import fi.hsl.jore4.hastus.data.hastus.IHastusData
@@ -18,49 +22,66 @@ import fi.hsl.jore4.hastus.data.jore.JoreTimingPlace
1822
1923object ConversionsToHastus {
2024
21- fun convertJoreLinesToHastus (lines : List <JoreLine >): List <IHastusData > {
22- return lines.flatMap { joreLine ->
23- val hastusRoute = Route (
24- identifier = joreLine.label,
25- description = joreLine.name,
26- serviceType = 0 ,
27- direction = 0 ,
28- serviceMode = joreLine.vehicleMode
29- )
25+ fun convertJoreLinesToHastus (lines : List <JoreLine >): List <IHastusData > = lines.flatMap { joreLine ->
26+ listOf (
27+ convertJoreLineToHastus(joreLine)
28+ ).plus(
29+ convertJoreRoutesToHastusRouteVariants(joreLine.routes, joreLine.label)
30+ )
31+ }
3032
31- listOf (hastusRoute) + convertJoreRoutesToHastusRouteVariants(joreLine.routes, joreLine.label)
32- }
33+ internal fun convertJoreLineToHastus (joreLine : JoreLine ): Route {
34+ // The cut is done due to length limit in Hastus.
35+ val hastusRouteDescription: String = joreLine.name.take(MAX_LENGTH_HASTUS_ROUTE_DESCRIPTION )
36+
37+ return Route (
38+ identifier = joreLine.label,
39+ description = hastusRouteDescription,
40+ serviceType = 0 ,
41+ direction = 0 ,
42+ serviceMode = joreLine.vehicleMode
43+ )
3344 }
3445
3546 private fun convertJoreRoutesToHastusRouteVariants (
3647 joreRoutes : List <JoreRoute >,
3748 joreLineLabel : String
3849 ): List <IHastusData > {
3950 return joreRoutes.flatMap { joreRoute ->
40- val hastusRouteVariantId: String =
41- getHastusRouteVariantId(joreLineLabel, joreRoute.label, joreRoute.variant, joreRoute.direction)
42- val hastusRouteIdAndVariantId: String = joreLineLabel + hastusRouteVariantId
43- val hastusRouteVariantDirection: Int = getRouteDirectionAsNumberOrThrow(joreRoute.direction) - 1
44-
45- val hastusRouteVariant = RouteVariant (
46- identifier = hastusRouteVariantId,
47- description = joreRoute.name,
48- direction = hastusRouteVariantDirection,
49- reversible = joreRoute.reversible,
50- routeIdAndVariantId = hastusRouteIdAndVariantId,
51- routeId = joreLineLabel
52- )
51+ val hastusRouteVariant = convertJoreRouteToHastusRouteVariant(joreRoute, joreLineLabel)
5352
5453 val hastusRouteVariantPoints: List <RouteVariantPoint > =
5554 convertJoreStopPointsInJourneyPatternToHastusRouteVariantPoints(
5655 joreRoute.stopPointsInJourneyPattern,
57- hastusRouteIdAndVariantId
56+ hastusRouteVariant.routeIdAndVariantId
5857 )
5958
6059 listOf (hastusRouteVariant) + hastusRouteVariantPoints
6160 }
6261 }
6362
63+ internal fun convertJoreRouteToHastusRouteVariant (
64+ joreRoute : JoreRoute ,
65+ joreLineLabel : String
66+ ): RouteVariant {
67+ val hastusRouteVariantId: String =
68+ getHastusRouteVariantId(joreLineLabel, joreRoute.label, joreRoute.variant, joreRoute.direction)
69+ val hastusRouteIdAndVariantId: String = joreLineLabel + hastusRouteVariantId
70+ val hastusRouteVariantDirection: Int = getRouteDirectionAsNumberOrThrow(joreRoute.direction) - 1
71+
72+ // The cut is done due to length limit in Hastus.
73+ val hastusRouteVariantDescription: String = joreRoute.name.take(MAX_LENGTH_HASTUS_ROUTE_VARIANT_DESCRIPTION )
74+
75+ return RouteVariant (
76+ identifier = hastusRouteVariantId,
77+ description = hastusRouteVariantDescription,
78+ direction = hastusRouteVariantDirection,
79+ reversible = joreRoute.reversible,
80+ routeIdAndVariantId = hastusRouteIdAndVariantId,
81+ routeId = joreLineLabel
82+ )
83+ }
84+
6485 internal fun getHastusRouteVariantId (
6586 joreLineLabel : String ,
6687 joreRouteLabel : String ,
@@ -121,23 +142,30 @@ object ConversionsToHastus {
121142 }
122143 }
123144
124- fun convertJoreStopPointsToHastus (
125- joreStopPoints : List <JoreScheduledStop >
126- ): List <Stop > {
127- return joreStopPoints.map {
128- Stop (
129- identifier = it.label,
130- platform = it.platform,
131- descriptionFinnish = it.nameFinnish,
132- descriptionSwedish = it.nameSwedish,
133- streetFinnish = it.streetNameFinnish,
134- streetSwedish = it.streetNameSwedish,
135- place = it.timingPlaceShortName,
136- gpsX = NumberWithAccuracy (it.location.x, 2 , 6 ),
137- gpsY = NumberWithAccuracy (it.location.y, 2 , 6 ),
138- shortIdentifier = it.label
139- )
140- }
145+ fun convertJoreStopPointsToHastus (joreStopPoints : List <JoreScheduledStop >): List <Stop > =
146+ joreStopPoints.map(this ::convertJoreStopPointToHastus)
147+
148+ internal fun convertJoreStopPointToHastus (joreStopPoint : JoreScheduledStop ): Stop {
149+ // The cuts below are done due to length limits in Hastus.
150+
151+ val stopNameFi = joreStopPoint.nameFinnish.take(MAX_LENGTH_HASTUS_STOP_NAME )
152+ val stopNameSv = joreStopPoint.nameSwedish.take(MAX_LENGTH_HASTUS_STOP_NAME )
153+
154+ val stopStreetNameFi = joreStopPoint.streetNameFinnish.take(MAX_LENGTH_HASTUS_STOP_STREET_NAME )
155+ val stopStreetNameSv = joreStopPoint.streetNameSwedish.take(MAX_LENGTH_HASTUS_STOP_STREET_NAME )
156+
157+ return Stop (
158+ identifier = joreStopPoint.label,
159+ platform = joreStopPoint.platform,
160+ descriptionFinnish = stopNameFi,
161+ descriptionSwedish = stopNameSv,
162+ streetFinnish = stopStreetNameFi,
163+ streetSwedish = stopStreetNameSv,
164+ place = joreStopPoint.timingPlaceShortName,
165+ gpsX = NumberWithAccuracy (joreStopPoint.location.x, 2 , 6 ),
166+ gpsY = NumberWithAccuracy (joreStopPoint.location.y, 2 , 6 ),
167+ shortIdentifier = joreStopPoint.label
168+ )
141169 }
142170
143171 fun convertJoreTimingPlacesToHastus (
0 commit comments