/* computes the distance, in meters, along a geometry */ protected static double distanceAlongGeometry( Geometry geometry, LinearLocation startIndex, LinearLocation endIndex) { if (endIndex == null) { endIndex = LinearLocation.getEndLocation(geometry); } double total = 0; LinearIterator it = new LinearIterator(geometry, startIndex); LinearLocation index = startIndex; Coordinate previousCoordinate = startIndex.getCoordinate(geometry); it.next(); index = it.getLocation(); while (index.compareTo(endIndex) < 0) { Coordinate thisCoordinate = index.getCoordinate(geometry); double distance = SphericalDistanceLibrary.fastDistance(previousCoordinate, thisCoordinate); total += distance; previousCoordinate = thisCoordinate; if (!it.hasNext()) break; it.next(); index = it.getLocation(); } // now, last bit of last segment Coordinate finalCoordinate = endIndex.getCoordinate(geometry); total += SphericalDistanceLibrary.distance(previousCoordinate, finalCoordinate); return total; }
/** * FIXME OBA parentStation field is a string, not an AgencyAndId, so it has no agency/feed scope * But the DC regional graph has no parent stations pre-defined, so no use dealing with them for * now. However Trimet stops have "landmark" or Transit Center parent stations, so we don't use * the parent stop field. * * <p>Ideally in the future stop clusters will replicate and/or share implementation with GTFS * parent stations. * * <p>We can't use a similarity comparison, we need exact matches. This is because many street * names differ by only one letter or number, e.g. 34th and 35th or Avenue A and Avenue B. * Therefore normalizing the names before the comparison is essential. The agency must provide * either parent station information or a well thought out stop naming scheme to cluster stops -- * no guessing is reasonable without that information. */ public void clusterStops() { int psIdx = 0; // unique index for next parent stop LOG.info("Clustering stops by geographic proximity and name..."); // Each stop without a cluster will greedily claim other stops without clusters. for (Stop s0 : stopForId.values()) { if (stopClusterForStop.containsKey(s0)) continue; // skip stops that have already been claimed by a cluster String s0normalizedName = StopNameNormalizer.normalize(s0.getName()); StopCluster cluster = new StopCluster(String.format("C%03d", psIdx++), s0normalizedName); // LOG.info("stop {}", s0normalizedName); // No need to explicitly add s0 to the cluster. It will be found in the spatial index query // below. Envelope env = new Envelope(new Coordinate(s0.getLon(), s0.getLat())); env.expandBy( SphericalDistanceLibrary.metersToLonDegrees(CLUSTER_RADIUS, s0.getLat()), SphericalDistanceLibrary.metersToDegrees(CLUSTER_RADIUS)); for (TransitStop ts1 : stopSpatialIndex.query(env)) { Stop s1 = ts1.getStop(); double geoDistance = SphericalDistanceLibrary.fastDistance( s0.getLat(), s0.getLon(), s1.getLat(), s1.getLon()); if (geoDistance < CLUSTER_RADIUS) { String s1normalizedName = StopNameNormalizer.normalize(s1.getName()); // LOG.info(" --> {}", s1normalizedName); // LOG.info(" geodist {} stringdist {}", geoDistance, stringDistance); if (s1normalizedName.equals(s0normalizedName)) { // Create a bidirectional relationship between the stop and its cluster cluster.children.add(s1); stopClusterForStop.put(s1, cluster); } } } cluster.computeCenter(); stopClusterForId.put(cluster.id, cluster); } // LOG.info("Done clustering stops."); // for (StopCluster cluster : stopClusterForId.values()) { // LOG.info("{} at {} {}", cluster.name, cluster.lat, cluster.lon); // for (Stop stop : cluster.children) { // LOG.info(" {}", stop.getName()); // } // } }
protected static double distance(Coordinate from, Coordinate to) { return SphericalDistanceLibrary.fastDistance(from, to); }