Either custom model or weighting not working as expected

I am having an issue using the route web API. What I want is to get the shortest path between two points, so I use weighting = shortest. Another requirement is that my customers need to be able to close roads, meaning that they submit a coordinate, and I ensure that that coordinate is avoided. The way I have implemented this is by drawing a square around that point, and then mapping it to a custom model, where I set the priority to zero.

However, it seems that one of these two are not working (shortest weighting or closing a road). When I post the following json:

{
    "points": [
        [
            9.974817036680832,
            56.15063242652721
        ],
        [
            9.606132685260434,
            56.165955689633904
        ]
    ],
    "profile": "truck",
    "ch.disable": true,
    "debug": false,
    "calc_points": false,
    "instructions": false,
    "custom_model": {
        "areas": {
            "type": "FeatureCollection",
            "features": [
                {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [
                                    9.754235338325909,
                                    56.15088662234999
                                ],
                                [
                                    9.747784661674093,
                                    56.15088662234999
                                ],
                                [
                                    9.747784661674093,
                                    56.147293377650016
                                ],
                                [
                                    9.754235338325909,
                                    56.147293377650016
                                ],
                                [
                                    9.754235338325909,
                                    56.15088662234999
                                ]
                            ]
                        ]
                    },
                    "id": "spaerring0"
                },
                {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "Polygon",
                        "coordinates": [
                            [
                                [
                                    9.619902611394895,
                                    55.93195337765002
                                ],
                                [
                                    9.626317388605106,
                                    55.93195337765002
                                ],
                                [
                                    9.626317388605106,
                                    55.93554662234999
                                ],
                                [
                                    9.619902611394895,
                                    55.93554662234999
                                ],
                                [
                                    9.619902611394895,
                                    55.93195337765002
                                ]
                            ]
                        ]
                    },
                    "id": "spaerring1"
                }
            ]
        },
        "priority": [
            {
                "if": "in_spaerring0",
                "multiply_by": "0"
            },
            {
                "if": "in_spaerring1",
                "multiply_by": "0"
            }
        ]
    },
    "weighting": "shortest"
}

I get this result back:

{
    "hints": {
        "visited_nodes.sum": 1080,
        "visited_nodes.average": 1080.0
    },
    "info": {
        "copyrights": [
            "GraphHopper",
            "OpenStreetMap contributors"
        ],
        "took": 3,
        "road_data_timestamp": "2024-09-21T17:00:00Z"
    },
    "paths": [
        {
            "distance": 24998.667,
            "weight": 1727.025036,
            "time": 1249956,
            "transfers": 0,
            "points_encoded": true,
            "points_encoded_multiplier": 100000.0,
            "snapped_waypoints": "w|uuIqe{{@q~A|~fA"
        }
    ]
}

But without the custom model I get this:

{
    "hints": {
        "visited_nodes.sum": 1080,
        "visited_nodes.average": 1080.0
    },
    "info": {
        "copyrights": [
            "GraphHopper",
            "OpenStreetMap contributors"
        ],
        "took": 4,
        "road_data_timestamp": "2024-09-21T17:00:00Z"
    },
    "paths": [
        {
            "distance": 24998.667,
            "weight": 1727.025036,
            "time": 1249956,
            "transfers": 0,
            "points_encoded": true,
            "points_encoded_multiplier": 100000.0,
            "snapped_waypoints": "w|uuIqe{{@q~A|~fA"
        }
    ]
}

Which has a higher distance. How can this be true when I use shortest as the weighting? Am I supposed to do something else to get the shortest weighting?

Below you will find my custom model and my points drawn in geojson.io.

The shortest weighting is deprecated and cannot be used in combination with custom models. Instead you can use:

  "distance_influence": 200

for your custom model to prefer shorter routes.

btw: where did you find this documented?

1 Like

Well don’t I feel silly then. I have spent way longer than I should have trying to look into this, but have somehow missed this.

Is the 200 just some magic number, or what is the significance of this?

I can’t remember exactly where, but it most have been either outdated documentation or old community posts.

Thank you very much for your help!

The value has a specific meaning highlighted in our documentation:

A value of 100 means that one extra kilometer of detour must save you 100s of travelling time or else you are not willing to take the detour. Or to put it another way, if a reference route takes 600s and is 10km long, distance_influence=100 means that you are willing to take an alternative route that is 11km long only if it takes no longer than 500s (saves 100s). Things get a bit more complicated when priority is not 1, but the effect stays the same: The larger distance_influence is, the more GraphHopper will focus on finding short routes.

I have spent way longer than I should have trying to look into this, but have somehow missed this.

Maybe you point me to the outdated documentation then we can get rid of it :slight_smile:

I appreciate your dedication to removing outdated documentation.

It must have been older community or stackoverflow posts. I am unable to track my steps back to any official documentation with any metion of this. It was a few months ago when the issue first became apparent, and I can’t be sure how I convinced myself that this was the solution.

I am sorry that I can’t be more helpful than that. I would love to help others to avoid the same confusion.

Once again, thank you very much for the help. Its feels great to get such helpful feedback and in such a timely manner!

1 Like

Ah, ok. No worries. Hopefully search engines will rank your question here now higher than outdated posts :slight_smile:

A bit off-topic, but you can also use https://explorer.graphhopper.com/ to comfortably visualize the routing response.