
com.graphhopper.routing.util.FootFlagEncoder Maven / Gradle / Ivy
Show all versions of graphhopper Show documentation
/*
* Licensed to GraphHopper and Peter Karich under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.routing.util;
import com.graphhopper.reader.OSMNode;
import com.graphhopper.reader.OSMWay;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Peter Karich
* @author Nop
*/
public class FootFlagEncoder extends AbstractFlagEncoder
{
private int safeWayBit = 0;
protected HashSet intended = new HashSet();
protected HashSet sidewalks = new HashSet();
/**
* Should be only instantied via EncodingManager
*/
protected FootFlagEncoder()
{
restrictions = new String[]
{
"foot", "access"
};
restrictedValues.add("private");
restrictedValues.add("no");
restrictedValues.add("restricted");
intended.add("yes");
intended.add("designated");
intended.add("official");
intended.add("permissive");
sidewalks.add("yes");
sidewalks.add("both");
sidewalks.add("left");
sidewalks.add("right");
ferries.add("shuttle_train");
ferries.add("ferry");
potentialBarriers.add("gate");
//potentialBarriers.add( "lift_gate" ); you can always pass them on foot
potentialBarriers.add("swing_gate");
acceptedRailways.add("station");
acceptedRailways.add("platform");
acceptedRailways.add("crossing");
acceptedRailways.add("tram_stop");
acceptedRailways.add("stop");
acceptedRailways.add("halt");
acceptedRailways.add("subway_entrance");
acceptedRailways.add("miniature");
}
@Override
public int defineBits( int index, int shift )
{
// first two bits are reserved for route handling in superclass
shift = super.defineBits(index, shift);
// larger value required - ferries are faster than pedestrians
speedEncoder = new EncodedValue("Speed", shift, 4, 1, SPEED.get("mean"), SPEED.get("max"));
shift += 4;
safeWayBit = 1 << shift++;
return shift;
}
public Integer getSpeed( String string )
{
Integer speed = SPEED.get(string);
if (speed == null)
{
throw new IllegalStateException("foot, no speed found for:" + string);
}
return speed;
}
@Override
public String toString()
{
return "FOOT";
}
/**
* Some ways are okay but not separate for pedestrians.
*
* @param way
*/
@Override
public int isAllowed( OSMWay way )
{
String highwayValue = way.getTag("highway");
if (highwayValue == null)
{
if (way.hasTag("route", ferries))
{
if (!way.hasTag("foot", "no"))
return acceptBit | ferryBit;
}
return 0;
}
if (way.hasTag("sidewalk", sidewalks))
return acceptBit;
// no need to evaluate ferries or fords - already included here
if (way.hasTag("foot", intended))
return acceptBit;
if (!allowedHighwayTags.contains(highwayValue))
return 0;
if (way.hasTag("motorroad", "yes"))
return 0;
// do not get our feet wet, "yes" is already included above
if (way.hasTag("highway", "ford") || way.hasTag("ford"))
return 0;
if (way.hasTag("bicycle", "official"))
return 0;
// check access restrictions
if (way.hasTag(restrictions, restrictedValues))
return 0;
// do not accept railways (sometimes incorrectly mapped!)
if (way.hasTag("railway") && !way.hasTag("railway", acceptedRailways))
return 0;
return acceptBit;
}
@Override
public int handleWayTags( int allowed, OSMWay way )
{
if ((allowed & acceptBit) == 0)
{
return 0;
}
int encoded;
if ((allowed & ferryBit) == 0)
{
encoded = speedEncoder.setDefaultValue(0);
encoded |= directionBitMask;
// mark safe ways or ways with cycle lanes
if (safeHighwayTags.contains(way.getTag("highway"))
|| way.hasTag("sidewalk", sidewalks))
{
encoded |= safeWayBit;
}
} else
{
// TODO read duration and calculate speed 00:30 for ferry
encoded = speedEncoder.setValue(0, 10);
encoded |= directionBitMask;
}
return encoded;
}
@Override
public int analyzeNodeTags( OSMNode node )
{
// movable barriers block if they are not marked as passable
if (node.hasTag("barrier", potentialBarriers)
&& !node.hasTag(restrictions, intended)
&& !node.hasTag("locked", "no"))
{
return directionBitMask;
}
if ((node.hasTag("highway", "ford") || node.hasTag("ford"))
&& !node.hasTag(restrictions, intended))
{
return directionBitMask;
}
return 0;
}
private final Set safeHighwayTags = new HashSet()
{
{
add("footway");
add("path");
add("steps");
add("pedestrian");
add("living_street");
add("track");
add("residential");
add("service");
}
};
private final Set allowedHighwayTags = new HashSet()
{
{
addAll(safeHighwayTags);
add("trunk");
add("trunk_link");
add("primary");
add("primary_link");
add("secondary");
add("secondary_link");
add("tertiary");
add("tertiary_link");
add("unclassified");
add("road");
// disallowed in some countries
//add("bridleway");
}
};
private static final Map SPEED = new HashMap()
{
{
put("min", 2);
put("slow", 4);
put("mean", 5);
put("fast", 6);
put("max", 7);
}
};
}