Java Code Examples for org.geotools.geometry.jts.ReferencedEnvelope#getSpan()

The following examples show how to use org.geotools.geometry.jts.ReferencedEnvelope#getSpan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RasterUtils.java    From geowave with Apache License 2.0 5 votes vote down vote up
public static Geometry getFootprint(
    final ReferencedEnvelope projectedReferenceEnvelope,
    final GridCoverage gridCoverage) {
  try {
    final Envelope sampleEnvelope = gridCoverage.getEnvelope();
    final double avgSpan =
        (projectedReferenceEnvelope.getSpan(0) + projectedReferenceEnvelope.getSpan(1)) / 2;
    final MathTransform gridCrsToWorldCrs =
        CRS.findMathTransform(
            gridCoverage.getCoordinateReferenceSystem(),
            projectedReferenceEnvelope.getCoordinateReferenceSystem(),
            true);
    final Coordinate[] polyCoords =
        getWorldCoordinates(
            sampleEnvelope.getMinimum(0),
            sampleEnvelope.getMinimum(1),
            sampleEnvelope.getMaximum(0),
            sampleEnvelope.getMaximum(1),
            gridCrsToWorldCrs.isIdentity() ? 2
                : (int) Math.min(
                    Math.max((avgSpan * MIN_SEGMENTS) / SIMPLIFICATION_MAX_DEGREES, MIN_SEGMENTS),
                    MAX_SEGMENTS),
            gridCrsToWorldCrs);
    final Polygon poly = new GeometryFactory().createPolygon(polyCoords);
    if (polyCoords.length > MAX_VERTICES_BEFORE_SIMPLIFICATION) {
      final Geometry retVal = DouglasPeuckerSimplifier.simplify(poly, SIMPLIFICATION_MAX_DEGREES);
      if (retVal.isEmpty()) {
        return poly;
      }
      return retVal;
    } else {
      return poly;
    }
  } catch (MismatchedDimensionException | TransformException | FactoryException e1) {
    LOGGER.warn("Unable to calculate grid coverage footprint", e1);
  }
  return null;
}
 
Example 2
Source File: MapUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
public static BufferedImage render( InternalMap map, Integer maxWidth, Integer maxHeight )
{
    MapContent mapContent = new MapContent();

    // Convert map objects to features, and add them to the map
    
    for ( InternalMapLayer mapLayer : map.getLayers() )
    {
        for ( InternalMapObject mapObject : mapLayer.getMapObjects() )
        {
            mapContent.addLayer( createFeatureLayerFromMapObject( mapObject ) );
        }
    }

    // Create a renderer for this map
    
    GTRenderer renderer = new StreamingRenderer();
    renderer.setMapContent( mapContent );

    // Calculate image height
    
    ReferencedEnvelope mapBounds = mapContent.getMaxBounds();
    double widthToHeightFactor = mapBounds.getSpan( 0 ) / mapBounds.getSpan( 1 );
    int[] widthHeight = getWidthHeight( maxWidth, maxHeight, LegendSet.LEGEND_TOTAL_WIDTH, TITLE_HEIGHT, widthToHeightFactor );
    
    //LegendSet.LEGEND_TOTAL_WIDTH;
    
    Rectangle imageBounds = new Rectangle( 0, 0, widthHeight[0], widthHeight[1] );

    // Create an image and get the graphics context from it
    
    BufferedImage image = new BufferedImage( imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_ARGB );
    Graphics2D graphics = (Graphics2D) image.getGraphics();

    graphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
    
    renderer.paint( graphics, imageBounds, mapBounds );

    mapContent.dispose();
    
    return image;
}