All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.apis.support.TileJson Maven / Gradle / Ivy

The newest version!
package org.opentripplanner.apis.support;

import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.UriInfo;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.opentripplanner.framework.io.HttpUtils;
import org.opentripplanner.model.FeedInfo;
import org.opentripplanner.service.worldenvelope.model.WorldEnvelope;

/**
 * Container for TileJSON response
 */
public class TileJson implements Serializable {

  // Some fields(all @SuppressWarnings("unused")) below are required to support the TileJSON format.

  @SuppressWarnings("unused")
  public final String tilejson = "2.2.0";

  @SuppressWarnings("unused")
  public final String scheme = "xyz";

  @SuppressWarnings("unused")
  public final int minzoom = 9;

  @SuppressWarnings("unused")
  public final int maxzoom = 20;

  public final String name = "OpenTripPlanner";
  public final String attribution;
  public final String[] tiles;
  public final double[] bounds;
  public final double[] center;

  public TileJson(String tileUrl, WorldEnvelope envelope, String attribution) {
    this.attribution = attribution;
    tiles = new String[] { tileUrl };

    bounds = new double[] {
      envelope.lowerLeft().longitude(),
      envelope.lowerLeft().latitude(),
      envelope.upperRight().longitude(),
      envelope.upperRight().latitude(),
    };

    var c = envelope.center();
    center = new double[] { c.longitude(), c.latitude(), 9 };
  }

  public TileJson(String tileUrl, WorldEnvelope envelope, Collection feedInfos) {
    this(tileUrl, envelope, attributionFromFeedInfo(feedInfos));
  }

  /**
   * Creates a vector source layer URL from a hard-coded path plus information from the incoming
   * HTTP request.
   */
  public static String urlWithDefaultPath(
    UriInfo uri,
    HttpHeaders headers,
    List layers,
    String ignoreRouterId,
    String path
  ) {
    return "%s/otp/routers/%s/%s/%s/{z}/{x}/{y}.pbf".formatted(
        HttpUtils.getBaseAddress(uri, headers),
        ignoreRouterId,
        path,
        String.join(",", layers)
      );
  }

  /**
   * Creates a vector source layer URL from a configured base path plus information from the incoming
   * HTTP request.
   */
  public static String urlFromOverriddenBasePath(
    UriInfo uri,
    HttpHeaders headers,
    String overridePath,
    List layers
  ) {
    var strippedPath = StringUtils.stripStart(overridePath, "/");
    strippedPath = StringUtils.stripEnd(strippedPath, "/");
    return "%s/%s/%s/{z}/{x}/{y}.pbf".formatted(
        HttpUtils.getBaseAddress(uri, headers),
        strippedPath,
        String.join(",", layers)
      );
  }

  private static String attributionFromFeedInfo(Collection feedInfos) {
    return feedInfos
      .stream()
      .map(feedInfo ->
        "%s".formatted(feedInfo.getPublisherUrl(), feedInfo.getPublisherName())
      )
      .distinct()
      .collect(Collectors.joining(", "));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy