Class RendererUtilities


  • public final class RendererUtilities
    extends Object
    Class for holding utility functions that are common tasks for people using the "StreamingRenderer/Renderer".
    Author:
    dblasby, Simone Giannecchini
    • Method Detail

      • worldToScreenTransform

        public static AffineTransform worldToScreenTransform​(ReferencedEnvelope mapExtent,
                                                             Rectangle paintArea)
        Sets up the affine transform

        NOTE It is worth to note that here we do not take into account the half a pixel translation stated by ogc for coverages bounds. One reason is that WMS 1.1.1 does not follow it!!!

        Parameters:
        mapExtent - the map extent
        paintArea - the size of the rendering output area
        Returns:
        a transform that maps from real world coordinates to the screen
      • createMapEnvelope

        public static Envelope createMapEnvelope​(Rectangle paintArea,
                                                 AffineTransform worldToScreen)
                                          throws NoninvertibleTransformException
        Creates the map's bounding box in real world coordinates.
        Parameters:
        worldToScreen - a transform which converts World coordinates to screen pixel coordinates. No assumptions are done on axis order as this is assumed to be pre-calculated. The affine transform may specify an rotation, in case the envelope will encompass the complete (rotated) world polygon.
        paintArea - the size of the rendering output area
        Returns:
        the envelope in world coordinates corresponding to the screen rectangle.
        Throws:
        NoninvertibleTransformException
      • calculatePixelsPerMeterRatio

        public static double calculatePixelsPerMeterRatio​(double scaleDenominator,
                                                          Map hints)
        Calculates the pixels per meter ratio based on a scale denominator.
        Parameters:
        scaleDenominator - The scale denominator value.
        hints - The hints used in calculation. if "dpi" key is present, it uses it's Integer value as the dpi of the current device. if not it uses 90 that is the OGC default value.
        Returns:
        The pixels per meter ratio for the given scale denominator.
      • calculateOGCScale

        public static double calculateOGCScale​(ReferencedEnvelope envelope,
                                               int imageWidth,
                                               Map hints)
        This method performs the computation using the methods suggested by the OGC SLD specification, page 26.

        In GeoTools 12 this method started to take into account units of measure, if this is not desirable in your application you can set the system variable "org.geotoools.render.lite.scale.unitCompensation" to false.

      • toMeters

        public static double toMeters​(double size,
                                      CoordinateReferenceSystem crs)
        Method used by the OGC scale calculation to turn a given length in the specified CRS towards meters.

        GeographicCRS uses OGC_DEGREE_TO_METERS for conversion of lat/lon measures

        Otherwise the horizontal component of the CRS is assumed to have a uniform axis unit of measure providing the Unit used for conversion. To ignore unit disable SCALE_UNIT_COMPENSATION to for the unaltered size.

        Returns:
        size adjusted for GeographicCRS or CRS units
      • calculateOGCScaleAffine

        public static double calculateOGCScaleAffine​(CoordinateReferenceSystem crs,
                                                     AffineTransform worldToScreen,
                                                     Map hints)
        This method performs the computation using the methods suggested by the OGC SLD specification, page 26.
        Parameters:
        crs - the coordinate reference system. Used to check if we are operating in degrees or meters.
        worldToScreen - the transformation mapping world coordinates to screen coordinates. Might specify a rotation in addition to translation and scaling.
      • calculateScale

        public static double calculateScale​(ReferencedEnvelope envelope,
                                            int imageWidth,
                                            int imageHeight,
                                            Map<?,​?> hints)
                                     throws TransformException,
                                            FactoryException
        First searches the hints for the scale denominator hint otherwise calls #calculateScale(org.geotools.util.SoftValueHashMap.Reference, int, int, double). If the hints contains a DPI then that DPI is used otherwise 90 is used (the OGS default).
        Throws:
        TransformException
        FactoryException
      • getDpi

        public static double getDpi​(Map hints)
        Either gets a DPI from the hints, or return the OGC standard, stating that a pixel is 0.28 mm (the result is a non integer DPI...)
        Returns:
        DPI as doubles, to avoid issues with integer trunking in scale computation expression
      • calculateScale

        public static double calculateScale​(ReferencedEnvelope envelope,
                                            int imageWidth,
                                            int imageHeight,
                                            double DPI)
                                     throws TransformException,
                                            FactoryException
        Find the scale denominator of the map. Method: 1. find the diagonal distance (meters) 2. find the diagonal distance (pixels) 3. find the diagonal distance (meters) -- use DPI 4. calculate scale (#1/#2)

        NOTE: return the scale denominator not the actual scale (1/scale = denominator)

        TODO: (SLD spec page 28): Since it is common to integrate the output of multiple servers into a single displayed result in the web-mapping environment, it is important that different map servers have consistent behaviour with respect to processing scales, so that all of the independent servers will select or deselect rules at the same scales. To insure consistent behaviour, scales relative to coordinate spaces must be handled consistently between map servers. For geographic coordinate systems, which use angular units, the angular coverage of a map should be converted to linear units for computation of scale by using the circumference of the Earth at the equator and by assuming perfectly square linear units. For linear coordinate systems, the size of the coordinate space should be used directly without compensating for distortions in it with respect to the shape of the real Earth.

        NOTE: we are actually doing a a much more exact calculation, and accounting for non-square pixels (which are allowed in WMS) ADDITIONAL NOTE from simboss: I added soe minor fixes. See below.

        Parameters:
        DPI - screen dots per inch (OGC standard is 90)

        TODO should I take into account also the destination CRS? Otherwise I am just assuming that the final crs is lon,lat that is it maps lon to x (n raster space) and lat to y (in raster space).

        Throws:
        TransformException
        FactoryException
      • rollLongitude

        protected static double rollLongitude​(double x)
      • rollLatitude

        protected static double rollLatitude​(double x)
      • worldToScreenTransform

        public static AffineTransform worldToScreenTransform​(Envelope mapExtent,
                                                             Rectangle paintArea,
                                                             CoordinateReferenceSystem destinationCrs)
                                                      throws TransformException
        This worldToScreenTransform method makes the assumption that the crs is in Lon,Lat or Lat,Lon. If the provided envelope does not carry along a crs the assumption that the map extent is in the classic Lon,Lat form. In case the provided envelope is of type.

        Note that this method takes into account also the OGC standard with respect to the relation between pixels and sample.

        Parameters:
        mapExtent - The envelope of the map in lon,lat
        paintArea - The area to paint as a rectangle
        Throws:
        TransformException
      • getCentroid

        public static Geometry getCentroid​(Geometry g)
        Finds the centroid of the input geometry if input = point, line, polygon --> return a point that represents the centroid of that geom if input = geometry collection --> return a multipoint that represents the centoid of each sub-geom
      • getStyle2DSize

        public static double getStyle2DSize​(Style2D style)
      • getPolygonCentroid

        public static Point getPolygonCentroid​(Polygon geom)
        Finds a centroid for a polygon catching any exceptions resulting from generalization or other polygon irregularities.
        Parameters:
        geom - The polygon.
        Returns:
        The polygon centroid, or null if it can't be found.
      • sampleForInternalPoint

        public static Point sampleForInternalPoint​(Polygon geom,
                                                   Point centroid,
                                                   PreparedGeometry pg,
                                                   GeometryFactory gf,
                                                   double stepSize,
                                                   int numSamples)
        Uses a sampling technique to obtain a central point that lies inside the specified polygon.

        Sampling occurs horizontally along the middle of the polygon obtained from the y coordinate of the polygon centroid.

        Parameters:
        geom - The polygon.
        centroid - The centroid of the polygon, can be null in which case it will be computed from getPolygonCentroid(Polygon).
        pg - The prepared version of geom, can be null in which case it will be computed on demand.
        gf - The geometry factory, can be null in which case the polygons factory will be used.
        Returns:
        A central point that lies inside of the polygon, or null if one could not be found.