I fallow below steps to create graph as like as shape reader:
1- fetch the data from database and store them in ResultSet
2- read line by line and process every lineString or multilineString record in database:
HashSet tmpSet = new HashSet<>();
for(Postgre p : dataStore){
Geometry geometry;
try {
geometry = wktReader.read(p.getGeom());
geometry.setSRID(4326);
for (Coordinate[] points : getCoords(geometry)) {
tmpSet.clear();
for (int i = 0; i < points.length; i++) {
Coordinate c = points[i];
// don't add the same coord twice for the same edge - happens with bad geometry, i.e.
// duplicate coords or a road which forms a circle (e.g. roundabout)
if (tmpSet.contains(c))
continue;
tmpSet.add(c);
// skip if its already a node
int state = coordState.get(c);
if (state >= FIRST_NODE_ID) {
continue;
}
if (i == 0 || i == points.length - 1 || state == COORD_STATE_PILLAR) {
// turn into a node if its the first or last
// point, or already appeared in another edge
int nodeId = nextNodeId++;
coordState.put(c, nodeId);
saveTowerPosition(nodeId, c);
} else if (state == COORD_STATE_UNKNOWN) {
// mark it as a pillar (which may get upgraded
// to an edge later)
coordState.put(c, COORD_STATE_PILLAR);
}
}
}
}catch (Exception e)
{
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.println("SOMETHING PROBLEMS!!!!");
e.printStackTrace();
}
if (nextNodeId == FIRST_NODE_ID)
throw new IllegalArgumentException("No data found for roads file ");
LOGGER.info("Number of junction points : " + (nextNodeId - FIRST_NODE_ID));
}
3- then ProcessRoad like shape reader .
4- calling addEdge method in process road :
public static interface EdgeAddedListener {
void edgeAdded(ReaderWay way, EdgeIteratorState edge);
}
private void addEdge(int fromTower, int toTower, Postgre road, double distance,
GHPoint estmCentre, PointList pillarNodes) {
EdgeIteratorState edge = graph.edge(fromTower, toTower);
// read the OSM id, should never be null
long id = road.getOsm_id();
// Make a temporary ReaderWay object with the properties we need so we
// can use the enocding manager
// We (hopefully don't need the node structure on here as we're only
// calling the flag
// encoders, which don't use this...
ReaderWay way = new ReaderWay(id);
way.setTag("estimated_distance", distance);
way.setTag("estimated_center", estmCentre);
// read the highway type
Object type = road.getHighway();
Object junction = road.getJunction();
Boolean tunnel = road.getTunnel();
Boolean bridge = road.getBridge();
if (type != null) {
way.setTag("highway", type.toString());
}
if (junction != null) {
way.setTag("junction", junction.toString());
}
if (tunnel != null && tunnel == false) {
way.setTag("tunnel", "no");
} else if (tunnel != null && tunnel == true) {
way.setTag("tunnel", "yes");
}
if (bridge != null && bridge == false) {
way.setTag("bridge", "no");
} else if (bridge != null && bridge == true) {
way.setTag("bridge", "yes");
}
// read maxspeed filtering for 0 which for Geofabrik shapefiles appears
// to correspond to no tag
Object maxSpeed = road.getMaxspeed();
if (maxSpeed != null && !maxSpeed.toString().trim().equals("0")) {
way.setTag("maxspeed", maxSpeed.toString());
}
for (String tag : DIRECT_COPY_TAGS) {
Object val= null;
if(tag == "name")
val= road.getName();
if(tag == "highway")
val= road.getHighway();
if(tag == "junction")
val= road.getJunction();
if(tag == "tunnel")
val= road.getTunnel();
if(tag == "bridge")
val= road.getBridge();
if(tag == "maxspeed")
val= road.getMaxspeed();
if(tag == "oneway")
val= road.getOneway();
if (val != null) {
way.setTag(tag, val.toString());
}
}
// read oneway
Object oneway = road.getOneway();
if (oneway != null) {
// Geofabrik is using an odd convention for oneway field in
// shapefile.
// We map back to the standard convention so that tag can be dealt
// with correctly by the flag encoder.
String val = oneway.toString().trim().toLowerCase();
if (val.equals("b")) {
// both ways
val = "no";
} else if (val.equals("t")) {
// one way against the direction of digitisation
val = "-1";
} else if (val.equals("f")) {
// one way Forward in the direction of digitisation
val = "yes";
} else {
throw new RuntimeException("Unrecognised value of oneway field \"" + val
+ "\" found in road with OSM id " + id);
}
way.setTag("oneway", val);
}
// Process the flags using the encoders
long includeWay = encodingManager.acceptWay(way);
if (includeWay == 0) {
return;
}
// TODO we're not using the relation flags
long relationFlags = 0;
long wayFlags = encodingManager.handleWayTags(way, includeWay, relationFlags);
if (wayFlags == 0)
return;
edge.setDistance(distance);
edge.setFlags(wayFlags);
edge.setWayGeometry(pillarNodes);
edge.setName(road.getName());
System.out.println("add edge");
if (edgeAddedListeners.size() > 0) {
// check size first so we only allocate the iterator if we have
// // listeners
for (EdgeAddedListener l : edgeAddedListeners) {
l.edgeAdded(way, edge);
}
}
}
after create the graph by these steps, sending request by defining source and destination.
i hope my explanation is enough
thanks for your attention.