<commit_before>package geoscript.viewer

import java.awt.Color
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.Composite
import java.awt.Dimension
import java.awt.BasicStroke
import java.awt.RenderingHints
import java.awt.AlphaComposite
import java.awt.BorderLayout
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.WindowConstants
import java.awt.geom.AffineTransform
import com.vividsolutions.jts.geom.Geometry as JtsGeometry
import com.vividsolutions.jts.geom.Polygon as JtsPolygon
import com.vividsolutions.jts.geom.Envelope
import com.vividsolutions.jts.geom.MultiPolygon as JtsMultiPolygon
import org.geotools.geometry.jts.LiteShape
import geoscript.geom.*

/**
 * A Viewer
 */
class Viewer {

    /**
     * Draw a Geometry (or Geometries) onto a Canvas
     */
    void draw(def geom, List size=[500,500]) {
        
        double buf = 50.0
        
        if (!(geom instanceof List)) {
            geom = [geom]
        }

        def g = geom.collect{
            it.g
        }.toArray() as JtsGeometry[]

        JtsGeometry gc = Geometry.factory.createGeometryCollection(g)
        Envelope e = gc.envelopeInternal

        double scale =  (e.width > 0) ? size[0] / e.width : Double.MAX_VALUE
        scale = (e.height > 0) ? Math.min(scale, size[1] / e.height) : new Double(1).doubleValue()

        double tx = -e.minX
        double ty = -e.minY

        AffineTransform at = new AffineTransform()
        // Scale to size of canvas (inverting the y axis)
        at.scale(scale, -scale)
        // translate to the origin
        //at.translate(tx, ty)
        // translate to account for invert
        //at.translate(0, (-size[1]/ scale))
        // buffer
        //at.translate(buf/scale, -buf/scale)
        at.translate(
            (buf/scale) - e.minX + (((size[0] / scale) - e.width) / 2),
            (-buf/scale) - e.maxY - (((size[1] / scale) - e.height) / 2))
        

        //double scaleX = size[0] / e.width
        //double scaleY = size[1] / e.height
        //double tx = -e.minX * scaleX
        //double ty = (e.minY * scaleY) + size[1]
        //AffineTransform at = new AffineTransform(scaleX, 0.0, 0.0, -scaleY, tx, ty)

        Panel panel = new Panel(geom, at)
        Dimension dim = new Dimension((int) (size[0] + 2 * buf), (int) (size[1] + 2 * buf))
        panel.preferredSize = dim
        panel.minimumSize = dim
        JFrame frame = new JFrame("Geoescript Viewer")
        try {
            frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        }
        catch(SecurityException ex) {
            frame.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        }
        frame.layout = new BorderLayout()
        frame.add(panel, BorderLayout.CENTER)
        frame.pack()
        frame.visible = true
    }

}

class Panel extends JPanel {
    
    List<Geometry> geoms

    AffineTransform atx

    Panel(List<Geometry> geoms, AffineTransform atx) {
        super()
        background = Color.WHITE
        opaque = true
        this.geoms = geoms
        this.atx = atx
    }

    void paintComponent(Graphics gr) {<commit_msg>Groovy docs and review in addition to a static method<commit_after>package geoscript.viewer

import java.awt.Color
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.Composite
import java.awt.Dimension
import java.awt.BasicStroke
import java.awt.RenderingHints
import java.awt.AlphaComposite
import java.awt.BorderLayout
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.WindowConstants
import java.awt.geom.AffineTransform
import com.vividsolutions.jts.geom.Geometry as JtsGeometry
import com.vividsolutions.jts.geom.Polygon as JtsPolygon
import com.vividsolutions.jts.geom.Envelope
import com.vividsolutions.jts.geom.MultiPolygon as JtsMultiPolygon
import org.geotools.geometry.jts.LiteShape
import geoscript.geom.*

/**
 * A Viewer can be used to visualize Geometry.
 * <code><pre>
 * import geoscript.geom.Point
 * import geoscript.viewer.Viewer
 * Point p = new Point(10,10)
 * Viewer.open(p.buffer(100))
 * </pre></code>
 */
class Viewer {

    /**
     * Draw a Geometry (or Geometries) onto a Canvas
     * @param geom The Geomtry to List of Geometries to draw
     * @param A List containing the size of the viewer (defaults to 500 by 500)
     */
    static void open(def geom, List size=[500,500]) {
        Viewer v = new Viewer()
        v.draw(geom, size)
    }

    /**
     * Draw a Geometry (or Geometries) onto a Canvas
     * @param geom The Geomtry to List of Geometries to draw
     * @param A List containing the size of the viewer (defaults to 500 by 500)
     */
    void draw(def geom, List size=[500,500]) {
        
        double buf = 50.0
        
        if (!(geom instanceof List)) {
            geom = [geom]
        }

        def g = geom.collect{
            it.g
        }.toArray() as JtsGeometry[]

        JtsGeometry gc = Geometry.factory.createGeometryCollection(g)
        Envelope e = gc.envelopeInternal

        double scale =  (e.width > 0) ? size[0] / e.width : Double.MAX_VALUE
        scale = (e.height > 0) ? Math.min(scale, size[1] / e.height) : new Double(1).doubleValue()

        double tx = -e.minX
        double ty = -e.minY

        AffineTransform at = new AffineTransform()
        // Scale to size of canvas (inverting the y axis)
        at.scale(scale, -scale)
        // translate to the origin
        //at.translate(tx, ty)
        // translate to account for invert
        //at.translate(0, (-size[1]/ scale))
        // buffer
        //at.translate(buf/scale, -buf/scale)
        at.translate(
            (buf/scale) - e.minX + (((size[0] / scale) - e.width) / 2),
            (-buf/scale) - e.maxY - (((size[1] / scale) - e.height) / 2))
        

        //double scaleX = size[0] / e.width
        //double scaleY = size[1] / e.height
        //double tx = -e.minX * scaleX
        //double ty = (e.minY * scaleY) + size[1]
        //AffineTransform at = new AffineTransform(scaleX, 0.0, 0.0, -scaleY, tx, ty)

        Panel panel = new Panel(geom, at)
        Dimension dim = new Dimension((int) (size[0] + 2 * buf), (int) (size[1] + 2 * buf))
        panel.preferredSize = dim
        panel.minimumSize = dim
        JFrame frame = new JFrame("Geoescript Viewer")
        try {
            frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
        }
        catch(SecurityException ex) {
            frame.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
        }
        frame.layout = new BorderLayout()
        frame.add(panel, BorderLayout.CENTER)
        frame.pack()
        frame.visible = true
    }

}

/**
 * The JPanel used to draw Geometry
 */
private class Panel extends JPanel {

    /**
     * The List of Geometries to draw
     */
    List<Geometry> geoms

    /**
     * The AffineTransform that converts between mapping and screen coordinates
     */
    AffineTransform atx

    /**
     * Create a new Panel with the List of Geometries to draw and the
     * AffineTransform that converts between mapping and screen coordinates
     * @param geoms The List of Geometries to draw
     * @param atx The AffineTransform that converts between mapping and screen coordinates
     */
    Panel(List<Geometry> geoms, AffineTransform atx) {
        super()
        background = Color.WHITE
        opaque = true
        this.geoms = geoms
        this.atx = atx
    }

    /**
     * Override the paintComponent method to draw the Geometries
     * @param gr The Graphics context
     */
    void paintComponent(Graphics gr) {
{'fraction_non_alphanumeric': 0.10728372337628188, 'fraction_numerical': 0.011701288456481725, 'mean_word_length': 2.2578158458244113, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1682926', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2355, 'n_words': 651}