11import { useEffect } from 'react'
2- import { handleColor } from '@/app/itinerary/transit/motisRequest '
2+ import { handleColor } from '@/app/itinerary/transit/colors '
33import { findContrastedTextColor } from '@/components/utils/colors'
44import { safeRemove } from './utils'
55import { filterNextConnections } from '../itinerary/transit/utils'
66import bezierSpline from '@turf/bezier-spline'
77import { lineString } from '@turf/turf'
8+ import mapboxPolyline from '@mapbox/polyline'
9+ import { notTransitType } from '../itinerary/transit/motisRequest'
810
911export default function useDrawTransit ( map , transit , selectedConnection , date ) {
12+ console . log ( 'useDrawTransit' , transit )
1013 const connections =
1114 transit &&
12- transit . connections &&
13- filterNextConnections ( transit . connections , date )
15+ transit . itineraries &&
16+ filterNextConnections ( transit . itineraries , date )
1417
1518 const connection = connections && connections [ selectedConnection || 0 ]
1619
1720 useEffect ( ( ) => {
1821 if ( ! map || ! connection ) return
1922
20- const { transports , stops } = connection
23+ const { legs } = connection
2124
2225 const featureCollection = {
2326 type : 'FeatureCollection' ,
24- features : transports
27+ features : legs
2528 . reduce ( ( memo , next ) => {
26- const route_text_color = handleColor ( next . route_text_color , '#000000' )
29+ const routeTextColor = handleColor ( next . routeTextColor , '#000000' )
2730 console . log ( 'next' , next )
2831
32+ const geometry = mapboxPolyline . toGeoJSON ( next . legGeometry . points , 6 )
33+ window . mapboxPolyline = mapboxPolyline
34+
2935 return [
3036 ...memo ,
3137 {
38+ geometry,
3239 type : 'Feature' ,
3340 properties : {
34- name : next . route_short_name || '' ,
35- move_type : next . move_type ,
36- route_color : next . route_color || '#d3b2ee' ,
37- route_color_darker : next . route_color_darker || '' ,
38- route_text_color,
39- inverse_color : findContrastedTextColor ( route_text_color , true ) ,
40- } ,
41- geometry : {
42- type : 'LineString' ,
43- coordinates : stops
44- . slice ( next . move . range . from , next . move . range . to + 1 )
45- . map ( ( stop ) => [ stop . station . pos . lng , stop . station . pos . lat ] ) ,
41+ name : next . routeShortName || '' ,
42+ mode : next . mode ,
43+ isTransit : ! notTransitType . includes ( next . mode ) ? 'Yes' : 'No' ,
44+ route_color : handleColor ( next . routeColor ) || '#d3b2ee' ,
45+ route_color_darker : next . routeColorDarker || '' ,
46+ route_text_color : routeTextColor ,
47+ inverse_color : findContrastedTextColor ( routeTextColor , true ) ,
48+ stopsCount :
49+ next . intermediateStops && next . intermediateStops . length + 2 ,
4650 } ,
4751 } ,
4852 ]
4953 } , [ ] )
5054 . map ( ( feature ) => {
5155 const coordinates = feature . geometry . coordinates
52- if ( coordinates . length <= 2 ) return feature
56+ if (
57+ coordinates . length <= 2 ||
58+ ! feature . properties . stopsCount ||
59+ coordinates . length > feature . properties . stopsCount * 1.5 // testing here if the polyline is a GTFS real route or not
60+ )
61+ return feature
5362
5463 var curved = bezierSpline ( lineString ( coordinates ) , {
5564 sharpness : 0.6 ,
@@ -87,7 +96,7 @@ export default function useDrawTransit(map, transit, selectedConnection, date) {
8796 source : id ,
8897 type : 'line' ,
8998 id : id + '-lines-contour' ,
90- filter : [ '==' , [ 'get' , 'move_type ' ] , 'Transport ' ] ,
99+ filter : [ '==' , [ 'get' , 'isTransit ' ] , 'Yes ' ] ,
91100 layout : {
92101 'line-join' : 'round' ,
93102 'line-cap' : 'round' ,
@@ -111,7 +120,7 @@ export default function useDrawTransit(map, transit, selectedConnection, date) {
111120 source : id ,
112121 type : 'line' ,
113122 id : id + '-lines' ,
114- filter : [ '==' , [ 'get' , 'move_type ' ] , 'Transport ' ] ,
123+ filter : [ '==' , [ 'get' , 'isTransit ' ] , 'Yes ' ] ,
115124
116125 layout : {
117126 'line-join' : 'round' ,
@@ -151,12 +160,14 @@ export default function useDrawTransit(map, transit, selectedConnection, date) {
151160 'text-halo-width' : 1 ,
152161 } ,
153162 } )
163+ // TODO shared line style for non-transit (walk, bike, car)
164+ // could be better, reusing the same styles as the other itineraries
154165 map . addLayer (
155166 {
156167 source : id ,
157168 type : 'line' ,
158169 id : id + '-lines-walking-background' ,
159- filter : [ '==' , [ 'get' , 'move_type ' ] , 'Walk ' ] ,
170+ filter : [ '==' , [ 'get' , 'isTransit ' ] , 'No ' ] ,
160171 layout : {
161172 'line-join' : 'round' ,
162173 'line-cap' : 'round' ,
@@ -173,7 +184,7 @@ export default function useDrawTransit(map, transit, selectedConnection, date) {
173184 source : id ,
174185 type : 'line' ,
175186 id : id + '-lines-walking' ,
176- filter : [ '==' , [ 'get' , 'move_type ' ] , 'Walk ' ] ,
187+ filter : [ '==' , [ 'get' , 'isTransit ' ] , 'No ' ] ,
177188 layout : {
178189 'line-join' : 'round' ,
179190 'line-cap' : 'round' ,
0 commit comments