Skip to content

Commit 00d6091

Browse files
committed
batch elevation call with chunks of 2000
1 parent fb721dc commit 00d6091

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

app/javascript/maplibre/routing/openrouteservice.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,34 @@ export async function getRouteFeature (feature, waypoints, profile) {
5757
// return route points including elevation
5858
// The API restricts to 2000 vertexes per request: https://openrouteservice.org/restrictions/
5959
export async function getRouteElevation (waypoints) {
60+
const BATCH_SIZE = 2000
6061
const Elevation = new Openrouteservice.Elevation({api_key: window.gon.map_keys.openrouteservice})
61-
return Elevation.lineElevation({
62-
format_in: 'geojson',
63-
format_out: 'geojson',
64-
geometry: {
65-
coordinates: functions.removeElevation(waypoints),
66-
type: 'LineString'
62+
const coords = functions.removeElevation(waypoints)
63+
64+
// Split into batches of BATCH_SIZE with 1-point overlap
65+
const batches = []
66+
for (let i = 0; i < coords.length; i += BATCH_SIZE - 1) {
67+
batches.push(coords.slice(i, i + BATCH_SIZE))
68+
}
69+
70+
try {
71+
const allCoordinates = []
72+
for (const [index, batch] of batches.entries()) {
73+
const response = await Elevation.lineElevation({
74+
format_in: 'geojson',
75+
format_out: 'geojson',
76+
geometry: {
77+
coordinates: batch,
78+
type: 'LineString'
79+
}
80+
})
81+
console.log(`Openrouteservice elevation response (batch ${batch.length}/${coords.length}):`, response)
82+
const batchCoords = response.geometry.coordinates
83+
// Drop first point of subsequent batches to avoid duplicates from overlap
84+
allCoordinates.push(...(index === 0 ? batchCoords : batchCoords.slice(1)))
6785
}
68-
}).then(response => {
69-
console.log('Openrouteservice elevation response:', response)
70-
return response.geometry.coordinates
71-
}).catch(async err => {
86+
return allCoordinates
87+
} catch (err) {
7288
// Extract error details from API response
7389
let errorMessage = 'OpenRouteService elevation error'
7490
try {
@@ -84,7 +100,7 @@ export async function getRouteElevation (waypoints) {
84100
console.error("OpenRouteService error:", err)
85101
errorMessage = err.message || errorMessage
86102
}
87-
})
103+
}
88104
}
89105

90106
export async function getRouteUpdate (originalFeature, updatedFeature) {

0 commit comments

Comments
 (0)