{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-guides/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":[]},"type":"markdown"},"seo":{"title":"Custom Model","llmstxt":{"title":"GraphHopper Directions API","description":"REST API for route optimization (vehicle routing, traveling salesman), route planning, turn-by-turn navigation, distance matrices, isochrones, geocoding, map matching, and location clustering. Built on OpenStreetMap and TomTom data. Base URL: https://graphhopper.com/api/1\n"}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"custom-model","__idx":0},"children":["Custom Model"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["A custom model allows you to modify the default routing behavior of a vehicle profile by specifying a set of rules in JSON language."," ","There are three JSON properties to change a profile: ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]},", ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["distance_influence"]},". For a quick overview, there is an ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.graphhopper.com/blog/2020/05/31/examples-for-customizable-routing/"},"children":["example-driven blog post"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["But first we will give an introductory example for each of these JSON properties. Let's start with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [{\n    \"if\": \"road_class == MOTORWAY\",\n    \"limit_to\": \"90\"\n  }]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["As you might have already guessed this limits the speed on motorways to 90km/h."," ","Changing the speed will of course change the travel time, but at the same time this makes other road classes more likely as well, so you can use this model to avoid motorways."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can immediately try this out in the Browser ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://graphhopper.com/maps/?point=50.856527%2C12.876127&point=51.02952%2C13.295603&profile=car&custom_model=%7B%22speed%22%3A%5B%7B%22if%22%3A%22road_class+=%3D+MOTORWAY%22%2C%22limit_to%22%3A%2290%22%7D%5D%7D"},"children":["on GraphHopper Maps"]},"."," ","GraphHopper Maps offers an interactive text editor to comfortably enter custom models."," ","You can open it by pressing the gear icon in the top left corner. It will check the syntax of your custom model and mark errors in red. You can press"," ","Ctrl+Space or Alt+Enter to retrieve auto-complete suggestions. Pressing Ctrl+Enter will send a routing request for the"," ","custom model you entered. To disable the custom model you click the gear icon again."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["In the second example we show how to avoid certain road classes without changing the travel time:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"priority\": [{\n    \"if\": \"road_class == LIVING_STREET || road_class == RESIDENTIAL || road_class == UNCLASSIFIED\",\n    \"multiply_by\": \"0.1\"\n  }]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This example avoids certain smaller streets. ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://graphhopper.com/maps/?point=51.125708%2C13.067915&point=51.125964%2C13.082271&profile=car&custom_model=%7B%22priority%22%3A%5B%7B%22if%22%3A%22road_class+=%3D+LIVING_STREET+%7C%7C+road_class+%3D%3D+RESIDENTIAL+%7C%7C+road_class+%3D%3D+UNCLASSIFIED%22%2C%22multiply_by%22%3A%220.1%22%7D%5D%7D"},"children":["View it in GraphHopper Maps"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The third example shows how to prefer shortest paths:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"distance_influence\": 200\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://graphhopper.com/maps/?point=51.04188%2C13.057766&point=51.057527%2C13.068237&profile=car&custom_model=%7B%22distance_influence%22%3A200%7D"},"children":["View this example in GraphHopper Maps"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["There is a fourth JSON property ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["areas"]}," that allows you to define areas that can then be used in the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if"]}," or ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else_if"]}," conditions for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]},"."," ","Try examples in ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://graphhopper.com/maps/"},"children":["GraphHopper Maps"]},", for instance those from the"," ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://www.graphhopper.com/blog/2020/05/31/examples-for-customizable-routing/"},"children":["example-driven blog post"]},"."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"customizing-speed","__idx":1},"children":["Customizing speed"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["When using custom models you do not need to define rules that specify a speed for every road segment, but rather GraphHopper"," ","assumes a default speed. All you need to do is adjust this default speed to your use-case as you will always use the custom"," ","model in conjunction with a routing profile which is used to determine the default speed."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The custom model is a JSON object and the first property we will learn about here is the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]}," property. The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]}," ","property's value is a list of conditional statements that modify the default speed. Every such statement consists of a"," ","condition and an operation. The different statements are applied to the default speed from top to bottom, i.e."," ","statements that come later in the list are applied to the resulting value of previous operations. Each statement is only"," ","executed if the corresponding condition applies for the current road segment. This will become more clear in the following"," ","examples."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Currently the custom model language supports two operators:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiply_by"]}," multiplies the speed value with a given number"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["limit_to"]}," limits the speed value to a given number"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"conditional-multiplication","__idx":2},"children":["Conditional multiplication"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Let's start with a simple example using ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiply_by"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.5\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This custom model reduces the speed of every road segment for which the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class"]}," attribute is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["MOTORWAY"]}," to"," ","fifty percent of the default speed (the default speed is multiplied by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5"]},"). Again, the default speed is the speed"," ","that GraphHopper would normally use for the profile's vehicle. Note the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if"]}," clause which means that the operation"," ","(",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiply_by"]},") is only applied ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["if"]}," the condition ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == MOTORWAY"]}," is fulfilled for the road segment under"," ","consideration. The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["=="]}," indicates equality, i.e. the condition reads ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["the road_class equals MOTORWAY"]},". If you're a bit"," ","familiar with programming note that the condition (the value of the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if"]}," key) is just a boolean condition in Java"," ","language (other programming languages like C or JavaScript are very similar in this regard). A more complex condition"," ","could look like this: ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == PRIMARY || road_class == TERTIARY"]}," which uses the ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["or"]}," ","(",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["||"]},") operator and literally means \"road_class equals PRIMARY or road_class equals TERTIARY\"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["There can be multiple such 'if statements' in the speed section, and they are evaluated from top to bottom:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.5\"\n    },\n    {\n      \"if\": \"road_class == PRIMARY || road_environment == TUNNEL\",\n      \"multiply_by\": \"0.7\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["In this example the default speed of road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == MOTORWAY"]}," will be multiplied by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5"]},", the default speed"," ","of road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == PRIMARY"]}," will be multiplied by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.7"]}," and for road segments with both ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == MOTORWAY"]}," and"," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_environment == TUNNEL"]}," the default speed will be multiplied first by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5"]}," and then by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.7"]},". So overall the"," ","default speed will be multiplied by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.35"]},". For road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == PRIMARY"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_environment == TUNNEL"]}," we"," ","only multiply by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.7"]},", even though both parts of the second condition apply. It only matters whether the road segment matches"," ","the condition or not."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_environment"]}," are road attributes of 'enum' type, i.e. their value can only be one of a fixed set of"," ","values, like ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["MOTORWAY"]}," for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Other road attributes like ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class_link"]}," are of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["boolean"]}," type. They can be used as conditions directly, for example:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class_link\",\n      \"multiply_by\": \"0.6\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["which means that for road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class_link==true"]}," the speed factor will be ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.6"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["For attributes with numeric values, like ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["max_width"]}," you should not use the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["=="]}," (equality) or ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["!="]}," ("," ","inequality) operators, but the numerical comparison operators ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["bigger"]}," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":[">"]},", ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["bigger or equals"]}," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":[">="]},", ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["smaller"]}," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<"]},", or"," ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["smaller or equals"]}," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<="]},", e.g.:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"max_width < 2.5\",\n      \"multiply_by\": \"0.8\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["which means that for all road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["max_width"]}," smaller than ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["2.5m"]}," the speed is multiplied by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.8"]},"."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"conditional-capping","__idx":3},"children":["Conditional capping"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Besides the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiply_by"]}," operator there is also the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["limit_to"]}," operator. As the name suggests ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["limit_to"]}," limits the"," ","current value to the given value. Take this example:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.8\"\n    },\n    {\n      \"if\": \"surface == GRAVEL\",\n      \"limit_to\": \"60\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This implies that on all road segments with the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["GRAVEL"]}," value for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["surface"]}," the speed will be at most ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["60km/h"]},","," ","regardless of the default speed and the previous rules. So for a road segment with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == MOTORWAY"]},","," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["surface == GRAVEL"]}," and default speed ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["100"]}," the first statement reduces the speed from ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["100"]}," to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["80"]}," and the second"," ","statement further reduces the speed from ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["80"]}," to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["60"]},". If the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class"]}," was ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["PRIMARY"]}," and the default speed was ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["50"]}," ","the first rule would not apply and the second rule would do nothing, because limiting ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["50"]}," to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["60"]}," still yields ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["50"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["A common use-case for the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["limit_to"]}," operation is the following pattern:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"true\",\n      \"limit_to\": \"90\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["which means that the speed is limited to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["90km/h"]}," for all road segments regardless of its properties. The condition"," ","\"",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["true"]},"\" is always fulfilled."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"conditionals-with-multiple-branches","__idx":4},"children":["Conditionals with multiple branches"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else"]}," statement allows you to define that some operations should be applied if an road segment does ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["not"]}," match a"," ","condition. So this example:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.5\"\n    },\n    {\n      \"else\": \"\",\n      \"limit_to\": \"50\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["means that for all road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == MOTORWAY"]}," we multiply the default speed by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5"]}," and for all others we"," ","limit the default speed to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["50"]}," (but never both)."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["In case you want to distinguish more than two cases (road segments that match or match not a condition) you can use ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else_if"]}," ","statements which are only evaluated in case the previous ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if"]}," or ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else_if"]}," statement did ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["not"]}," match:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.5\"\n    },\n    {\n      \"else_if\": \"road_environment == TUNNEL\",\n      \"limit_to\": \"70\"\n    },\n    {\n      \"else\": \"\",\n      \"multiply_by\": \"0.9\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["So if the first condition matches (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == MOTORWAY"]},") the default speed is multiplied by ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5"]},", but the other two"," ","statements are ignored. Only if the first statement does not match (e.g. ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class == PRIMARY"]},") the second statement"," ","is even considered and only if it matches (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_environment == TUNNEL"]},") the default speed is limited to 70. The last"," ","operation (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiply_by: \"0.9\""]},") is only applied if both previous conditions did not match."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else_if"]}," statements always require a preceding ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if"]}," or ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else_if"]}," statement. However, there can be multiple"," ","'blocks' of subsequent ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if/else_if/else"]}," statements in the list of rules for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"code","attributes":{},"children":["else_if"]}," is useful for example in case you have multiple ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["multiply_by"]}," operations, but you do not want that the speed"," ","gets reduced by all of them. For the following model"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.5\"\n    },\n    {\n      \"else_if\": \"road_environment == TUNNEL\",\n      \"multiply_by\": \"0.8\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["only the first factor (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5"]},") will be applied even for road segments that fulfill both conditions."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"limit-rules-to-certain-areas","__idx":5},"children":["Limit rules to certain areas"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can not only modify the speed of road segments based on properties, like we saw in the previous examples, but you"," ","can also modify the speed of road segments based on their location. To do this you need to first create and add some"," ","areas to the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["areas"]}," section of the custom model. You can then use the name of these areas in the conditions of your"," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["if/else/else_if"]}," statements."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["In the following example we multiply the speed of all road segments in an area called ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["custom1"]}," with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.7"]}," and also limit it"," ","to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["50km/h"]},". Note that each area's name needs to be prefixed with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["in_"]},":"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"speed\": [\n    {\n      \"if\": \"in_custom1\",\n      \"multiply_by\": \"0.7\"\n    },\n    {\n      \"if\": \"in_custom1\",\n      \"limit_to\": \"50\"\n    }\n  ],\n  \"areas\": {\n    \"type\": \"FeatureCollection\",\n    \"features\": [{\n      \"type\": \"Feature\",\n      \"id\": \"custom1\",\n      \"geometry\": {\n        \"type\": \"Polygon\",\n        \"coordinates\": [\n          [\n            [\n              1.525,\n              42.511\n            ],\n            [\n              1.510,\n              42.503\n            ],\n            [\n              1.531,\n              42.495\n            ],\n            [\n              1.542,\n              42.505\n            ],\n            [\n              1.525,\n              42.511\n            ]\n          ]\n        ]\n      }\n    }]\n  }\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Areas are given in GeoJson format (FeatureCollection). Currently a member of this collection must be a ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Feature"]}," with a"," ","geometry type ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Polygon"]},". Note that the coordinates array of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["Polygon"]}," is an array of arrays that"," ","each must describe a closed ring, i.e. the first point must be equal to the last, identical to the GeoJSON specs."," ","Each point is given as an array [longitude, latitude], so the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["coordinates"]}," array has three dimensions total."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Using the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["areas"]}," feature you can also block entire areas i.e. by multiplying the speed with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0"]},", but for this you"," ","should rather use the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]}," section that we will explain next."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"customizing-priority","__idx":6},"children":["Customizing priority"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]}," factor"," ","allows similar modifications as ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]},", but instead of modifying the road segment weights ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["and"]}," travel times it will only"," ","affect the weights. By default, the priority is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["1"]}," for every road segment, so it does not affect the weight. However,"," ","changing the priority of a road can yield a relative weight difference in comparison to other roads."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Customizing the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]}," works very much like ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"#customizing-speed"},"children":["customizing the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]}]},"."," ","The only real difference is that there is no ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["limit_to"]}," operator for"," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]},". As a quick reminder here is an example for priority:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"priority\": [\n    {\n      \"if\": \"road_class == MOTORWAY\",\n      \"multiply_by\": \"0.5\"\n    },\n    {\n      \"else_if\": \"road_class == SECONDARY\",\n      \"multiply_by\": \"0.9\"\n    },\n    {\n      \"if\": \"road_environment == TUNNEL\",\n      \"multiply_by\": \"0.1\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["means that road segments with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class==MOTORWAY"]}," and ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_environment==TUNNEL"]}," get priority ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.5*0.1=0.05"]}," and"," ","those with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["road_class==SECONDARY"]}," and no TUNNEL, get priority ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0.9"]}," and so on."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Edges with lower priority values will be less likely part of the optimal route calculated by GraphHopper, higher values"," ","mean that these road segments shall be preferred. If you do not want to state which road segments shall be avoided, but"," ","rather which ones shall be preferred, you need to ",{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["decrease"]}," the priority of others:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"priority\": [\n    {\n      \"if\": \"road_class != CYCLEWAY\",\n      \"multiply_by\": \"0.8\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["means decreasing the priority for all road_classes ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["except"]}," cycleways."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Just like we saw for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["speed"]}," you can also adjust the priority for road segments in a certain area. It works exactly the"," ","same way:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"priority\": [\n    {\n      \"if\": \"in_custom1\",\n      \"multiply_by\": \"0.7\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To block an entire area set the priority value to ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["0"]},". You can even set the priority only for certain roads in an area"," ","like this:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"priority\": [\n    {\n      \"if\": \"road_class == MOTORWAY && in_custom1\",\n      \"multiply_by\": \"0.1\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Some other useful attributes to restrict access to certain roads depending on your vehicle dimensions are the"," ","following:"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"json","header":{"controls":{"copy":{}}},"source":"{\n  \"priority\": [\n    {\n      \"if\": \"max_width < 2.5\",\n      \"multiply_by\": \"0\"\n    },\n    {\n      \"if\": \"max_length < 10\",\n      \"multiply_by\": \"0\"\n    },\n    {\n      \"if\": \"max_weight < 3.5\",\n      \"multiply_by\": \"0\"\n    }\n  ]\n}\n","lang":"json"},"children":[]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["which means that the priority for all road segments that allow a maximum vehicle width of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["2.5m"]},", a maximum vehicle"," ","length of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["10m"]}," or a maximum vehicle weight of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["3.5tons"]},", or less, is zero, i.e. these ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["narrow"]}," road segments are"," ","blocked."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"customizing-distance_influence","__idx":7},"children":["Customizing distance_influence"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["distance_influence"]}," property allows you to control the trade-off between a fast route (minimum time) and a short route"," ","(minimum distance). The larger ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["distance_influence"]}," is the more GraphHopper will prioritize routes with a small"," ","total distance. More precisely, the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["distance_influence"]}," is the time you need to save on a detour (a longer distance"," ","route option) such that you prefer taking the detour compared to a shorter route. Please note that this value is a number, not a string."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["A value of ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["100"]}," means that one extra kilometer of detour must save you ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["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 ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["600s"]}," and is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["10km"]}," long,"," ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["distance_influence=100"]}," means that you are willing to take an alternative route that is ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["11km"]}," long only if"," ","it takes no longer than ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["500s"]}," (saves ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["100s"]},"). Things get a bit more complicated when ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]}," is not ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["1"]},", but the"," ","effect stays the same: The larger ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["distance_influence"]}," is, the more GraphHopper will focus on finding short routes."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"road-attributes","__idx":8},"children":["Road attributes"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["GraphHopper stores different attributes for every road segment. Some frequently used are the following (some of their possible values are given in brackets):"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["road_class: (OTHER, MOTORWAY, TRUNK, PRIMARY, SECONDARY, TRACK, STEPS, CYCLEWAY, FOOTWAY, ...)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["road_environment: (ROAD, FERRY, BRIDGE, TUNNEL, ...)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["road_access: (DESTINATION, DELIVERY, PRIVATE, NO, ...)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["surface: (PAVED, DIRT, SAND, GRAVEL, ...)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["smoothness: (EXCELLENT, GOOD, INTERMEDIATE, ...)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["toll: (MISSING, NO, HGV, ALL)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["bike_network, foot_network: (MISSING, INTERNATIONAL, NATIONAL, REGIONAL, LOCAL, OTHER)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["country: (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["MISSING"]}," or the country as a ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["ISO3166-1:alpha3"]}," code e.g. ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["DEU"]},")"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["hazmat: (YES, NO), hazmat_tunnel: (A, B, .., E), hazmat_water: (YES, PERMISSIVE, NO)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["hgv: (MISSING, YES, DESIGNATED, ...)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["track_type: (MISSING, GRADE1, GRADE2, ..., GRADE5)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["urban_density: (RURAL, RESIDENTIAL, CITY)"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To learn about all available encoded values you can query the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["/info"]}," endpoint"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Besides this kind of categories, which can take multiple different string values, there are also some that represent a"," ","boolean value (they are either true or false for a given road segment), like:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["road_class_link"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["roundabout"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["There are also some that take on a numeric value, like:"]},{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["average_slope: a number for ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["100 * elevation change / edge_distance"]}," for a road segment; it changes the sign in reverse direction; see also max_slope"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["curvature: ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["beeline distance / edge_distance"]}," (0..1) e.g. a curvy road is smaller than 1"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["hike_rating, horse_rating, mtb_rating: a number from 0 to 6 for the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["sac_scale"]}," in OSM, e.g. 0 means ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["missing"]},", 1 means ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["hiking"]},", 2 means ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["mountain_hiking"]}," and so on"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["lanes: number of lanes"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["max_slope: an unsigned decimal for the maximum slope (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["100 * elevation change / distance_i"]},") of an edge with ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["sum(distance_i)=edge_distance"]},". Important for longer road segments where ups (or downs) can be much bigger than the average_slope."]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["max_speed: the speed limit from a sign (km/h)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["max_height (meter), max_width (meter), max_length (meter)"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["max_weight (ton)"]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"limitations","__idx":9},"children":["Limitations"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["You can directly use custom models with the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/openapi/routing/postroute"},"children":["POST Route Endpoint"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use custom models with the Route Optimization API, the Matrix API or the Isochrone API, use the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/openapi/custom-profiles"},"children":["Profiles API"]}," ","to create a new named profile with your custom model. You can then use that profile like you"," ","would use a pre-defined profile."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This feature will strongly benefit from feedback, so do not hesitate to share your experience, your favorite custom"," ","model or some of the problems you ran into when you tried building your own custom model."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"troubleshooting","__idx":10},"children":["Troubleshooting"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"recommendations","__idx":11},"children":["Recommendations"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["For debugging you can use the custom model editor in ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://graphhopper.com/maps/"},"children":["GraphHopper Maps"]}," (click the 'gear' button in the top left)."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["When debugging problems with custom models you should first try if your request goes through without an error using an empty custom model."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"route-calculation-is-slower","__idx":12},"children":["Route calculation is slower"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The route calculation with custom_models will be slower as a different algorithm has to be used. The more the result deviates from the optimum the slower the response can get."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If you create ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/openapi/custom-profiles"},"children":["custom profile"]}," with a custom_model you'll get faster response times nearly as fast as the default vehicle profile."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If a custom profile is not something you can use then for certain use cases you can make the route calculation faster when you tune the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["custom_model"]}," and e.g. exclude certain ways via ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["{ \"if\": \"road_class == TRACK || road_class == RESIDENTIAL\", \"multiply_by\": \"0\" }"]}," in the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["priority"]},"."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["If this is still not sufficient and you need even faster response times, you can use the parameter ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["astarbi.epsilon"]}," (",{"$$mdtype":"Tag","name":"code","attributes":{},"children":[">=1"]},"). This enables a heuristic that speeds up routing"," ","but may occasionally cause unwanted detours. The closer this value is to 1, the nearer the route is to optimal."," ","In practice, a value 1.3-1.4 provides good speed improvements (10-20%) while causing little to no deviation from the optimal route in most cases."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"all-routes-for-my-custom-model-fail","__idx":13},"children":["All routes for my custom model fail"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This could mean that either your custom model made some of the roads near the start and destination inaccessible,"," ","then usually we return a PointNotFoundException with the point_index with the ",{"$$mdtype":"Tag","name":"em","attributes":{},"children":["location snap"]}," problem."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Or, the custom model made a route between your start and destination impossible, then we return a ConnectionNotFoundException. This happens e.g. when you exclude tunnels,"," ","ferries or motorways but all routes between start and destination have these road attributes satisfied, i.e. we cannot find a route."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":[{"$$mdtype":"Tag","name":"strong","attributes":{},"children":["Solution for both cases"]},": relax your custom model and e.g. instead of excluding certain road attributes via ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["\"multiply_by\": \"0\""]}," you should try to use ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["\"0.01\""]},"."]}]},"headings":[{"value":"Custom Model","id":"custom-model","depth":1},{"value":"Customizing speed","id":"customizing-speed","depth":2},{"value":"Conditional multiplication","id":"conditional-multiplication","depth":3},{"value":"Conditional capping","id":"conditional-capping","depth":3},{"value":"Conditionals with multiple branches","id":"conditionals-with-multiple-branches","depth":3},{"value":"Limit rules to certain areas","id":"limit-rules-to-certain-areas","depth":2},{"value":"Customizing priority","id":"customizing-priority","depth":2},{"value":"Customizing distance_influence","id":"customizing-distance_influence","depth":2},{"value":"Road attributes","id":"road-attributes","depth":2},{"value":"Limitations","id":"limitations","depth":2},{"value":"Troubleshooting","id":"troubleshooting","depth":2},{"value":"Recommendations","id":"recommendations","depth":3},{"value":"Route calculation is slower","id":"route-calculation-is-slower","depth":3},{"value":"All routes for my custom model fail","id":"all-routes-for-my-custom-model-fail","depth":3}],"frontmatter":{"seo":{"title":"Custom Model"}},"lastModified":"2026-11-30T03:52:58.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/guides/custom-model","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}