<commit_before>package geoscript.process

import org.junit.Test
import static org.junit.Assert.*
import geoscript.geom.*
import geoscript.layer.*
import org.geotools.process.Processors
import org.opengis.feature.type.Name
import org.geotools.feature.NameImpl
import org.geotools.process.ProcessFactory
import org.geotools.feature.FeatureCollection
import org.geotools.parameter.Parameter
import geoscript.feature.Feature
import org.geotools.process.function.ProcessFunctionFactory

/**
 *  The Process UnitTest
 *  @author <NAME>
 */
class ProcessTestCase {
    
    private Layer getStatesLayer () {
        File file = new File(getClass().getClassLoader().getResource("states.shp").toURI())
        new Shapefile(file)
    }
    
    @Test void conversions() {
        def pt = new Point(0,0)
        assert Process.convert(pt, com.vividsolutions.jts.geom.Geometry) instanceof com.vividsolutions.jts.geom.Geometry
        assertTrue Process.convert(pt.g, geoscript.geom.Geometry) instanceof geoscript.geom.Geometry

        def b = new Bounds(0,0,10,10,"EPSG:4326")
        assertTrue Process.convert(b, org.geotools.geometry.jts.ReferencedEnvelope) instanceof org.geotools.geometry.jts.ReferencedEnvelope
        assertTrue Process.convert(b.env, geoscript.geom.Bounds) instanceof geoscript.geom.Bounds

        def layer = getStatesLayer()
        assertTrue Process.convert(layer, org.geotools.feature.FeatureCollection) instanceof org.geotools.feature.FeatureCollection
        assertTrue Process.convert(layer.fs.features, geoscript.layer.Layer) instanceof geoscript.layer.Layer
        assertTrue Process.convert(layer.fs.features, geoscript.layer.Cursor) instanceof geoscript.layer.Cursor
        
        assertTrue Process.convert(layer.cursor, org.geotools.feature.FeatureCollection) instanceof org.geotools.feature.FeatureCollection
        assertTrue Process.convert(layer.fs.features, geoscript.layer.Cursor) instanceof geoscript.layer.Cursor
    }
    
    @Test void getProcessNames() {
        def names = Process.processNames
        assertTrue names.size() > 0
    }
    
    @Test void boundsGeoToolsProcess() {
        def p = new Process("gs:Bounds")
        assertEquals "gs:Bounds", p.name
        assertEquals "Computes the overlall bounds of the input features", p.title
        assertEquals "Computes the overlall bounds of the input features", p.description
        assertEquals "1.0.0", p.version
        assertEquals "gs:Bounds", p.toString()
        
        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")
        
        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("bounds")
        assertEquals geoscript.geom.Bounds, p.results.get("bounds")

        Layer layer = getStatesLayer()
        Map results = p.execute(["features": layer])
        assertTrue results.containsKey("bounds")
        assertTrue results.bounds instanceof geoscript.geom.Bounds

        results = p.execute(["features": layer.getCursor("STATE_NAME = 'Washington' OR STATE_NAME = 'Oregon'")])
        assertTrue results.containsKey("bounds")
        assertTrue results.bounds instanceof geoscript.geom.Bounds
    }
    
    @Test void countGeoToolsProcess() {
        Process p = new Process("gs:Count")
        assertEquals "gs:Count", p.name
        assertEquals "Counts the number of features in the specified collection", p.title
        assertEquals "Counts the number of features in the specified collection", p.description
        assertEquals "1.0.0", p.version
        assertEquals "gs:Count", p.toString()

        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")

        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("result")
        assertEquals java.lang.Number, p.results.get("result")

        Layer layer = getStatesLayer()
        Map results = p.execute(["features": layer])
        assertTrue results.containsKey("result")
        assertEquals 49, results.result

        results = p.execute(["features": layer.getCursor("STATE_NAME = 'Washington' OR STATE_NAME = 'Oregon'")])
        assertTrue results.containsKey("result")
        assertEquals 2, results.result
    }

    @Test void collectGeometriesGeoToolsProcess() {
        Process p = new Process("gs:CollectGeometries")
        assertEquals "gs:CollectGeometries", p.name
        assertEquals "Collects all the default geometries in the feature collection and returns them as a single geometry collection", p.title
        assertEquals "Collects all the default geometries in the feature collection and returns them as a single geometry collection", p.description
        assertEquals "1.0.0", p.version
        assertEquals "gs:CollectGeometries", p.toString()

        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")

        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("result")
        assertEquals geoscript.geom.Geometry, p.results.get("result")

        Layer layer = getStatesLayer()
        Map results = p.execute(["features": layer])
        assertTrue results.containsKey("result")
        assertTrue results.result instanceof geoscript.geom.Geometry
        assertEquals 95, (results.result as Geometry).numGeometries

        results = p.execute(["features": layer.getCursor("STATE_NAME = 'Washington' OR STATE_NAME = 'Oregon'")])
        assertTrue results.containsKey("result")
        assertTrue results.result instanceof geoscript.geom.Geometry
        assertEquals 4, (results.result as Geometry).numGeometries
    }
    
    @Test void convexHullClosureProcess() {
        Process p = new Process("convexhull",
            "Create a convexhull around the features",
            [features: geoscript.layer.Cursor],
            [result: geoscript.layer.Cursor],
            { inputs ->
                def geoms = new GeometryCollection(inputs.features.collect{f -> f.geom})
                def output = new Layer()
                output.add([geoms.convexHull])
                [result: output]
            }
        )
        assertEquals "geoscript:convexhull", p.name
        assertEquals "convexhull", p.title
        assertEquals "Create a convexhull around the features", p.description
        assertEquals "1.0.0", p.version
        assertEquals "geoscript:convexhull", p.toString()

        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")

        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("result")
        assertEquals geoscript.layer.Cursor, p.results.get("result")

        Layer layer = getStatesLayer()
        Map results = p.execute([features: layer.cursor])<commit_msg>Fix failing unit test for the collectgeometry process.<commit_after>package geoscript.process

import org.junit.Test
import static org.junit.Assert.*
import geoscript.geom.*
import geoscript.layer.*
import org.geotools.process.Processors
import org.opengis.feature.type.Name
import org.geotools.feature.NameImpl
import org.geotools.process.ProcessFactory
import org.geotools.feature.FeatureCollection
import org.geotools.parameter.Parameter
import geoscript.feature.Feature
import org.geotools.process.function.ProcessFunctionFactory

/**
 *  The Process UnitTest
 *  @author <NAME>
 */
class ProcessTestCase {
    
    private Layer getStatesLayer () {
        File file = new File(getClass().getClassLoader().getResource("states.shp").toURI())
        new Shapefile(file)
    }
    
    @Test void conversions() {
        def pt = new Point(0,0)
        assert Process.convert(pt, com.vividsolutions.jts.geom.Geometry) instanceof com.vividsolutions.jts.geom.Geometry
        assertTrue Process.convert(pt.g, geoscript.geom.Geometry) instanceof geoscript.geom.Geometry

        def b = new Bounds(0,0,10,10,"EPSG:4326")
        assertTrue Process.convert(b, org.geotools.geometry.jts.ReferencedEnvelope) instanceof org.geotools.geometry.jts.ReferencedEnvelope
        assertTrue Process.convert(b.env, geoscript.geom.Bounds) instanceof geoscript.geom.Bounds

        def layer = getStatesLayer()
        assertTrue Process.convert(layer, org.geotools.feature.FeatureCollection) instanceof org.geotools.feature.FeatureCollection
        assertTrue Process.convert(layer.fs.features, geoscript.layer.Layer) instanceof geoscript.layer.Layer
        assertTrue Process.convert(layer.fs.features, geoscript.layer.Cursor) instanceof geoscript.layer.Cursor
        
        assertTrue Process.convert(layer.cursor, org.geotools.feature.FeatureCollection) instanceof org.geotools.feature.FeatureCollection
        assertTrue Process.convert(layer.fs.features, geoscript.layer.Cursor) instanceof geoscript.layer.Cursor
    }
    
    @Test void getProcessNames() {
        def names = Process.processNames
        assertTrue names.size() > 0
    }
    
    @Test void boundsGeoToolsProcess() {
        def p = new Process("gs:Bounds")
        assertEquals "gs:Bounds", p.name
        assertEquals "Computes the overlall bounds of the input features", p.title
        assertEquals "Computes the overlall bounds of the input features", p.description
        assertEquals "1.0.0", p.version
        assertEquals "gs:Bounds", p.toString()
        
        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")
        
        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("bounds")
        assertEquals geoscript.geom.Bounds, p.results.get("bounds")

        Layer layer = getStatesLayer()
        Map results = p.execute(["features": layer])
        assertTrue results.containsKey("bounds")
        assertTrue results.bounds instanceof geoscript.geom.Bounds

        results = p.execute(["features": layer.getCursor("STATE_NAME = 'Washington' OR STATE_NAME = 'Oregon'")])
        assertTrue results.containsKey("bounds")
        assertTrue results.bounds instanceof geoscript.geom.Bounds
    }
    
    @Test void countGeoToolsProcess() {
        Process p = new Process("gs:Count")
        assertEquals "gs:Count", p.name
        assertEquals "Counts the number of features in the specified collection", p.title
        assertEquals "Counts the number of features in the specified collection", p.description
        assertEquals "1.0.0", p.version
        assertEquals "gs:Count", p.toString()

        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")

        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("result")
        assertEquals java.lang.Number, p.results.get("result")

        Layer layer = getStatesLayer()
        Map results = p.execute(["features": layer])
        assertTrue results.containsKey("result")
        assertEquals 49, results.result

        results = p.execute(["features": layer.getCursor("STATE_NAME = 'Washington' OR STATE_NAME = 'Oregon'")])
        assertTrue results.containsKey("result")
        assertEquals 2, results.result
    }

    @Test void collectGeometriesGeoToolsProcess() {
        Process p = new Process("gs:CollectGeometries")
        assertEquals "gs:CollectGeometries", p.name
        assertEquals "Collects all the default geometries in the feature collection and returns them as a single geometry collection", p.title
        assertEquals "Collects all the default geometries in the feature collection and returns them as a single geometry collection", p.description
        assertEquals "1.0.0", p.version
        assertEquals "gs:CollectGeometries", p.toString()

        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")

        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("result")
        assertEquals geoscript.geom.Geometry, p.results.get("result")

        Layer layer = getStatesLayer()
        Map results = p.execute(["features": layer])
        assertTrue results.containsKey("result")
        assertTrue results.result instanceof geoscript.geom.Geometry
        assertEquals 39, (results.result as Geometry).numGeometries

        results = p.execute(["features": layer.getCursor("STATE_NAME = 'Washington' OR STATE_NAME = 'Oregon'")])
        assertTrue results.containsKey("result")
        assertTrue results.result instanceof geoscript.geom.Geometry
        assertEquals 3, (results.result as Geometry).numGeometries
    }
    
    @Test void convexHullClosureProcess() {
        Process p = new Process("convexhull",
            "Create a convexhull around the features",
            [features: geoscript.layer.Cursor],
            [result: geoscript.layer.Cursor],
            { inputs ->
                def geoms = new GeometryCollection(inputs.features.collect{f -> f.geom})
                def output = new Layer()
                output.add([geoms.convexHull])
                [result: output]
            }
        )
        assertEquals "geoscript:convexhull", p.name
        assertEquals "convexhull", p.title
        assertEquals "Create a convexhull around the features", p.description
        assertEquals "1.0.0", p.version
        assertEquals "geoscript:convexhull", p.toString()

        assertEquals 1, p.parameters.size()
        assertTrue p.parameters.containsKey("features")
        assertEquals geoscript.layer.Cursor, p.parameters.get("features")

        assertEquals 1, p.results.size()
        assertTrue p.results.containsKey("result")
        assertEquals geoscript.layer.Cursor, p.results.get("result")

        Layer layer = getStatesLayer()
        Map results = p.execute([features: layer.cursor])
{'fraction_non_alphanumeric': 0.10027443529660122, 'fraction_numerical': 0.005488705932024488, 'mean_word_length': 3.1182266009852215, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 11, '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': '824555', 'n_tokens_mistral': 3807, 'n_tokens_neox': 3720, 'n_words': 898}