示例#1
0
  /**
   * Fits screen to specified geometry bounds.
   *
   * @param aArea A geometry in geo coordinates space.
   * @throws Exception
   */
  public void fit(Geometry aArea) throws Exception {

    Geometry bounds = aArea.getBoundary();
    Envelope envBounds = bounds.getEnvelopeInternal();
    Point2D.Double leftUpCorner = new Point2D.Double(envBounds.getMinX(), envBounds.getMinY());
    Point2D.Double rightBottomCorner = new Point2D.Double(envBounds.getMaxX(), envBounds.getMaxY());
    Point2D.Double cartlu = geo2Cartesian(leftUpCorner);
    Point2D.Double cartrb = geo2Cartesian(rightBottomCorner);
    double destWidth = Math.abs(cartrb.getX() - cartlu.getX());
    double destHeight = Math.abs(cartrb.getY() - cartlu.getY());
    Coordinate centre =
        new Coordinate((cartrb.getX() + cartlu.getX()) / 2, (cartrb.getY() + cartlu.getY()) / 2);

    Dimension size = getSize();
    Point2D.Double screenLT = awtScreen2Cartesian(new Point(0, 0));
    Point2D.Double screenBR = awtScreen2Cartesian(new Point(size.width, size.height));

    double srcWidth = screenBR.x - screenLT.x;
    double srcHeight = screenBR.y - screenLT.y;
    double sx = srcWidth / destWidth;
    double sy = srcHeight / destHeight;
    double coef = Math.min(sx, sy);
    coef = snapScale(coef);
    scaleView(coef, coef, false);

    Point2D.Double projectedScreenCenter = screen2Cartesian(new Point(0, 0));
    translateView(projectedScreenCenter.x - centre.x, projectedScreenCenter.y - centre.y, true);
    repaint();
  }
示例#2
0
  private Polygon getPolygon(ShapeValue s, int w, int h) {

    Geometry g = s.getGeometry();
    Polygon ret = new Polygon();
    HashSet<String> points = new HashSet<String>();
    int sx = 0, sy = 0;
    for (Coordinate c : g.getBoundary().getCoordinates()) {

      int x = (int) ((double) w * (c.x + 180.0) / 360.0);
      int y = h - (int) ((double) h * (c.y + 90.0) / 180.0);

      if (!points.contains(x + "|" + y)) {

        if (ret.npoints == 0) {
          sx = x;
          sy = y;
        }

        ret.addPoint(x, y);
        points.add(x + "|" + y);
      }
    }

    /*
     * close polygon
     */
    if (ret.npoints > 0) {
      ret.addPoint(sx, sy);
    }

    return ret;
  }
  /**
   * @param poly the polygon to split
   * @param line the line to use for the split
   * @return a sorted list of geometries as a result of splitting poly with line
   */
  protected List<Polygon> splitPolygon(Geometry poly, Geometry line) {
    List<Polygon> output = new ArrayList();

    Geometry nodedLinework = poly.getBoundary().union(line);
    Geometry polys = polygonize(nodedLinework);

    // only keep polygons which are inside the input
    for (int i = 0; i < polys.getNumGeometries(); i++) {
      Polygon candpoly = (Polygon) polys.getGeometryN(i);
      if (poly.contains(candpoly.getInteriorPoint())) {
        output.add(candpoly);
      }
    }
    geometrySorter(output);
    return output;
  }
  protected final Geometry calcBoundary(final Set<Geometry> geoms) {
    Geometry geom = null;

    Geometry bounds = null;
    float minLat = Float.POSITIVE_INFINITY;
    float maxLat = Float.NEGATIVE_INFINITY;
    float minLon = Float.POSITIVE_INFINITY;
    float maxLon = Float.NEGATIVE_INFINITY;
    boolean emptySet = false;

    for (final Geometry g : geoms) {
      if (g != null) {
        // A Geometry with no dimensions has to be handled
        if (g.getBoundary().isEmpty()) {
          final Coordinate[] cs = g.getCoordinates();
          for (final Coordinate c : cs) {
            minLat = Math.min(minLat, (float) c.x);
            maxLat = Math.max(maxLat, (float) c.x);
            minLon = Math.min(minLon, (float) c.y);
            maxLon = Math.max(maxLon, (float) c.y);
            emptySet = true;
          }
        } else {
          if (bounds != null) {
            bounds = g.union(bounds);
          } else {
            bounds = g;
          }
        }
      }
    }

    if (emptySet) {
      if (minLat == Float.POSITIVE_INFINITY
          || maxLat == Float.NEGATIVE_INFINITY
          || minLon == Float.POSITIVE_INFINITY
          || maxLon == Float.NEGATIVE_INFINITY) {
        log.error("Warning: empty bounds to calculate were not valid, ignoring");
      } else {
        try {
          final Geometry empty =
              new WKTReader()
                  .read(
                      "POLYGON (("
                          + minLat
                          + " "
                          + minLon
                          + ", "
                          + minLat
                          + " "
                          + maxLon
                          + ", "
                          + maxLat
                          + " "
                          + maxLon
                          + ", "
                          + maxLat
                          + " "
                          + minLon
                          + ", "
                          + minLat
                          + " "
                          + minLon
                          + "))");
          log.info("empty=" + empty);
          if (bounds != null) {
            bounds = empty.union(bounds);
          } else {
            bounds = empty;
          }
        } catch (final Throwable e) {
          log.error(e.toString());
        }
      }
    }

    if (bounds != null) {
      geom = bounds.getBoundary();
    } else {
      geom = null;
    }

    return geom;
  }