Exemplo n.º 1
0
  public void testJTS(JtsSpatialContext ctx, String name) throws Exception {
    ShapeReader reader = ctx.getFormats().getReader(name);
    ShapeWriter writer = ctx.getFormats().getWriter(name);
    Shape shape = null;

    //
    //    wkt = readFirstLineFromRsrc("/russia.wkt.txt");
    //    shape = ctx.readShape(wkt);
    //  //  testReadAndWriteTheSame(shape,format);

    // Examples from Wikipedia
    shape = ctx.readShape("LINESTRING (30 10, 10 30, 40 40)");
    //  testReadAndWriteTheSame(shape,format);

    shape = ctx.readShape("POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10))");
    testReadAndWriteTheSame(shape, reader, writer);

    shape =
        ctx.readShape("POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))");
    testReadAndWriteTheSame(shape, reader, writer);

    shape = ctx.readShape("MULTIPOINT ((10 40), (40 30), (20 20), (30 10))");
    testReadAndWriteTheSame(shape, reader, writer);

    shape = ctx.readShape("MULTIPOINT (10 40, 40 30, 20 20, 30 10)");
    testReadAndWriteTheSame(shape, reader, writer);

    shape = ctx.readShape("MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))");
    testReadAndWriteTheSame(shape, reader, writer);

    shape =
        ctx.readShape(
            "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))");
    testReadAndWriteTheSame(shape, reader, writer);
  }
  @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());
  }