use of org.onebusaway.gtfs.model.ShapePoint in project onebusaway-gtfs-modules by OneBusAway.
the class ShapeDirectionTransformStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
List<Trip> tripsToTransform = new ArrayList<Trip>();
Collection<Trip> allTrips = dao.getAllTrips();
for (Trip t : allTrips) {
if (t.getShapeId().getId().equals(shapeId) && !t.getDirectionId().equals(shapeDirection)) {
tripsToTransform.add(t);
}
}
if (!tripsToTransform.isEmpty()) {
String agencyId = context.getDefaultAgencyId();
AgencyAndId inputShapeId = new AgencyAndId(agencyId, shapeId);
AgencyAndId newShapeId = new AgencyAndId(agencyId, shapeId + "R");
List<ShapePoint> shapePoints = new ArrayList<ShapePoint>(dao.getShapePointsForShapeId(inputShapeId));
Collections.reverse(shapePoints);
int newIndex = 1;
for (ShapePoint sp : shapePoints) {
ShapePoint nsp = new ShapePoint();
nsp.setShapeId(newShapeId);
nsp.setSequence(newIndex++);
nsp.setLat(sp.getLat());
nsp.setLon(sp.getLon());
dao.saveEntity(nsp);
}
for (Trip t : tripsToTransform) {
t.setShapeId(newShapeId);
}
}
}
use of org.onebusaway.gtfs.model.ShapePoint in project onebusaway-gtfs-modules by OneBusAway.
the class ShapeTransformStrategy method decode.
private List<ShapePoint> decode(String pointString) {
double lat = 0;
double lon = 0;
int strIndex = 0;
List<ShapePoint> points = new ArrayList<ShapePoint>();
while (strIndex < pointString.length()) {
int[] rLat = decodeSignedNumberWithIndex(pointString, strIndex);
lat = lat + rLat[0] * 1e-5;
strIndex = rLat[1];
int[] rLon = decodeSignedNumberWithIndex(pointString, strIndex);
lon = lon + rLon[0] * 1e-5;
strIndex = rLon[1];
ShapePoint point = new ShapePoint();
point.setLat(lat);
point.setLon(lon);
points.add(point);
}
return points;
}
use of org.onebusaway.gtfs.model.ShapePoint in project onebusaway-gtfs-modules by OneBusAway.
the class ShapeTransformStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
String agencyId = context.getDefaultAgencyId();
AgencyAndId ashapeId = new AgencyAndId(agencyId, shapeId);
List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(ashapeId);
if (shapePoints.isEmpty()) {
// this cannot be a logger as it is BeanWrapped
System.err.println("no points found for shape: " + ashapeId);
return;
}
// Duplicate the list into something we can modify
shapePoints = new ArrayList<ShapePoint>(shapePoints);
List<ShapePoint> segment = decode(shape);
ShapePoint from = segment.get(0);
ShapePoint to = segment.get(segment.size() - 1);
int fromIndex = 0;
int toIndex = shapePoints.size() - 1;
if (matchStart)
fromIndex = closest(shapePoints, from, 0);
if (matchEnd)
toIndex = closest(shapePoints, to, fromIndex);
if (toIndex < fromIndex) {
// this cannot be a logger as it is BeanWrapped
System.err.println("segment match is out of order: fromIndex=" + fromIndex + " toIndex=" + toIndex);
return;
}
List<ShapePoint> subList = shapePoints.subList(fromIndex, toIndex + 1);
for (ShapePoint point : subList) dao.removeEntity(point);
subList.clear();
subList.addAll(segment);
int index = 0;
for (ShapePoint point : shapePoints) {
point.setDistTraveled(ShapePoint.MISSING_VALUE);
point.setSequence(index++);
point.setShapeId(ashapeId);
}
for (ShapePoint point : segment) dao.saveEntity(point);
UpdateLibrary.clearDaoCache(dao);
}
use of org.onebusaway.gtfs.model.ShapePoint in project onebusaway-gtfs-modules by OneBusAway.
the class ShapeTransformStrategy method closest.
private int closest(List<ShapePoint> shapePoints, ShapePoint point, int index) {
int minIndex = -1;
double minValue = Double.POSITIVE_INFINITY;
for (int i = index; i < shapePoints.size(); i++) {
ShapePoint p = shapePoints.get(i);
double dy = p.getLat() - point.getLat();
double dx = p.getLon() - point.getLon();
double d = Math.sqrt(dy * dy + dx * dx);
if (d < minValue) {
minIndex = i;
minValue = d;
}
}
return minIndex;
}
use of org.onebusaway.gtfs.model.ShapePoint in project onebusaway-gtfs-modules by OneBusAway.
the class SubsectionTripTransformStrategy method getClosestShapePointToStop.
private int getClosestShapePointToStop(List<ShapePoint> points, Stop stop) {
int minIndex = -1;
double minDistance = Double.POSITIVE_INFINITY;
for (int i = 0; i < points.size(); ++i) {
ShapePoint point = points.get(i);
double dx = point.getLon() - stop.getLon();
double dy = point.getLat() - stop.getLat();
double d = Math.sqrt(dx * dx + dy * dy);
if (d < minDistance) {
minIndex = i;
minDistance = d;
}
}
return minIndex;
}
Aggregations