How to avoid area in java

I am trying to make a GHRequest with a custom model containing an area to avoid, but I am not fully sure of how obtaining the desired result.
I guess it is something related to the graph preparation or speed/hybrid mode or the profile weighting.

My code is the following.

GHRequest req = new GHRequest(39.3309657322564, 16.239947664138324, 39.33230086519411, 16.23923098056929)
				.setProfile("car").putHint(Parameters.CH.DISABLE, true).setLocale(Locale.ITALY);
CustomModel model = new CustomModel();
Coordinate[] shell = new Coordinate[5];

shell[0] = new Coordinate(16.2399070658626, 39.331077386134275);
shell[1] = new Coordinate(16.23982586931115, 39.33093084038301);
shell[2] = new Coordinate(16.239379288278197, 39.3311018103963);
shell[3] = new Coordinate(16.23943793023202, 39.33125533413365);
shell[4] = new Coordinate(16.2399070658626, 39.331077386134275);
GeometryFactory geometryFactory = new GeometryFactory();
Polygon polygon = geometryFactory.createPolygon(shell);
JsonFeature area = new JsonFeature("avoid", "Polygon", null, polygon, new HashMap<String, Object>());
model.getAreas().put("avoid", area);
model.getPriority().put("area_avoid", 0.0);
req.putHint(CustomModel.KEY, model);

GHResponse response = hopper.route(req);

Am I missing something?

What error do you get? Is your car profile a CustomProfile?

Hi, I don’t get any error but my result is a route passing inside the forbidden area.
My car profile is a normal Profile, like this.

hopper.setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest").setTurnCosts(true));
hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));

Here you need to specify new CustomProfile. I thought we throw a proper error if this does not match. Will investigate.

hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));

This part is not relevant when you request a custom route per request and disable CH (CH.DISABLE)

Hi, thanks for your response.
I did like this

CustomProfile customCarProfile = new CustomProfile("car");
customCarProfile.setCustomModel(new CustomModel()).setVehicle("car");

Profile footProfile = new Profile("foot").setVehicle("foot").setWeighting("fastest");
Profile bikeProfile = new Profile("bike").setVehicle("bike").setWeighting("fastest");  	
hopper.setProfiles(customCarProfile.setTurnCosts(true), footProfile, bikeProfile);

apparently it doesn’t work, as I don’t get any response (“cannot fetch best reresponse if list is empty”).

I also noticed that if I comment out this line


it finds a route but it snaps my starting point on the boundary of the forbidden area that is closest to the arrival point.

is it somehow related to the fact that the starting point is on a road edge which is also in the forbidden area?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.