@Override
 public void fillBytesRef() {
   if (omitLeafByte) cell.getTokenBytesNoLeaf(bytes);
   else cell.getTokenBytesWithLeaf(bytes);
 }
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
    String name = req.getParameter("name");
    Shape shape = null;
    String country = req.getParameter("country");
    if (country != null && country.length() == 3) {
      InputStream in = WicketApplication.getStreamFromDataResource("countries-poly.txt");
      try {
        SampleDataReader reader = new SampleDataReader(in);
        while (reader.hasNext()) {
          SampleData data = reader.next();
          if (country.equalsIgnoreCase(data.id)) {
            if (StringUtils.isEmpty(name)) name = data.name;
            shape = ctx.readShape(data.shape);
            break;
          }
        }
      } finally {
        IOUtils.closeQuietly(in);
      }

      if (shape == null) {
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "unable to find: " + country);
        return;
      }
    }
    int depth = getIntParam(req, "depth", 16);

    String gridtype = req.getParameter("gridType");

    SpatialPrefixTree grid;
    if ("geohash".equals(gridtype)) {
      grid = new GeohashPrefixTree(ctx, depth);
    } else if ("quad".equals(gridtype)) {
      grid = new QuadPrefixTree(ctx, depth);
    } else {
      res.sendError(HttpServletResponse.SC_BAD_REQUEST, "unknown grid type: " + gridtype);
      return;
    }

    // If they don't set a country, then use the input
    if (shape == null) {
      String geo = req.getParameter("geo");
      if (geo == null) {
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "missing parameter: 'geo'");
        return;
      }
      try {
        shape = ctx.readShape(geo);
      } catch (Exception ex) {
        ex.printStackTrace();
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "error parsing geo: " + ex);
      }
    }
    SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, shape);
    double distErrPct = getDoubleParam(req, "distErrPct", SpatialArgs.DEFAULT_DISTERRPCT);
    double distErr = args.resolveDistErr(grid.getSpatialContext(), distErrPct);
    int detailLevel = grid.getLevelForDistance(distErr);
    List<Cell> nodes = grid.getCells(shape, detailLevel, false, true);

    int biggestLevel = 100;
    for (Cell node : nodes) {
      biggestLevel = Math.min(biggestLevel, node.getLevel());
    }
    String msg =
        "Using detail level "
            + detailLevel
            + " (biggest is "
            + biggestLevel
            + ") yielding "
            + nodes.size()
            + " tokens.";
    log(msg);

    List<String> info = SpatialPrefixTree.cellsToTokenStrings(nodes);
    String format = req.getParameter("format");
    if ("kml".equals(format)) {
      if (name == null || name.length() < 2) {
        name = "KML - " + new Date(System.currentTimeMillis());
      }
      Kml kml = KMLHelper.toKML(name, grid, info);

      res.setHeader("Content-Disposition", "attachment; filename=\"" + name + "\";");
      res.setContentType("application/vnd.google-earth.kml+xml");
      kml.marshal(res.getOutputStream());
      return;
    }

    res.setContentType("text/plain");
    PrintStream out = new PrintStream(res.getOutputStream());
    out.println(msg);
    out.println(info.toString());
  }