<commit_before>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2017, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.mbstyle.transform;

import org.geotools.TestData;
import org.geotools.data.property.PropertyDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.filter.function.CategorizeFunction;
import org.geotools.filter.function.FilterFunction_isometric;
import org.geotools.filter.function.FilterFunction_offset;
import org.geotools.mbstyle.MBStyle;
import org.geotools.mbstyle.MapboxTestUtils;
import org.geotools.mbstyle.layer.*;
import org.geotools.mbstyle.parse.MBObjectParser;
import org.geotools.styling.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Expression;
import org.opengis.style.GraphicalSymbol;

import javax.xml.transform.TransformerException;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * Test parsing and transforming a Mapbox fill layer from json.
 */
public class StyleTransformTest {

    static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

    Map<String, JSONObject> testLayersById = new HashMap<>();

    @Before
    public void setUp() throws IOException, ParseException {
        JSONObject jsonObject = MapboxTestUtils.parseTestStyle("functionParseTest.json");
        JSONArray layers = (JSONArray) jsonObject.get("layers");
        for (Object o : layers) {
            JSONObject layer = (JSONObject) o;
            testLayersById.put((String) layer.get("id"), layer);
        }
    }

    /**
     * Test parsing a Mapbox fill layer
     */
    @Test
    public void testFill() throws IOException, ParseException {

        JSONObject jsonObject = parseTestStyle("fillStyleTest.json");

        // Parse to MBStyle
        MBStyle mbStyle = new MBStyle(jsonObject);
        List<MBLayer> layers = mbStyle.layers("geoserver-states");

        assertEquals(1, layers.size());

        // Find the MBFillLayer and assert it contains the correct FeatureTypeStyle.
        assertTrue(layers.get(0) instanceof FillMBLayer);
        FillMBLayer mbFill = (FillMBLayer) layers.get(0);
        List<FeatureTypeStyle >fts = mbFill.transform(mbStyle);
        
        PolygonSymbolizer psym = SLD.polySymbolizer(fts.get(0));
        
        Expression expr =  psym.getFill().getColor();
        assertNotNull("fillColor set", expr);
        assertEquals( Color.decode("#FF595E"), expr.evaluate(null,Color.class) );
        assertEquals(Double.valueOf(.84),
                psym.getFill().getOpacity().evaluate(null, Double.class));
        
        Expression colorStroke = psym.getStroke().getColor();
        assertNotNull("stroke color set", colorStroke);
        assertEquals(Color.decode("#1982C4"), colorStroke.evaluate(null, Color.class));

        assertNotNull("displacement not null", psym.getDisplacement());
        assertNotNull("displacementX not null",  psym.getDisplacement().getDisplacementX());
        assertNotNull("displacementY not null",  psym.getDisplacement().getDisplacementY());
        assertEquals(Integer.valueOf(20), psym.getDisplacement().getDisplacementX().evaluate(null, Integer.class));
        assertEquals(Integer.valueOf(20), psym.getDisplacement().getDisplacementY().evaluate(null, Integer.class));
        
    }

    /**
     * Test parsing and generating a MapBox fill extrusion
     */
    @Test
    public void testFillExtrusion() throws IOException, ParseException {
        JSONObject jsonObject = parseTestStyle("fillExtrusionTest.json");

        // Parse to MBStyle
        MBStyle mbStyle = new MBStyle(jsonObject);
        List<MBLayer> layers = mbStyle.layers("composite");

        assertEquals(2, layers.size());

        // Find the MBFillLayer and assert it contains the correct FeatureTypeStyle.
        assertTrue(layers.get(1) instanceof FillExtrusionMBLayer);
        FillExtrusionMBLayer mbFill = (FillExtrusionMBLayer) layers.get(1);
        List<FeatureTypeStyle> fts = mbFill.transform(mbStyle);
        assertEquals(3, fts.size());

        PolygonSymbolizer psym = SLD.polySymbolizer(fts.get(0));
        PolygonSymbolizer sides = SLD.polySymbolizer(fts.get(1));
        PolygonSymbolizer roof = SLD.polySymbolizer(fts.get(2));

        Expression shadowGeometry = psym.getGeometry();
        assertEquals("offset", ((FilterFunction_offset)shadowGeometry).getName());
        assertEquals(3, ((FilterFunction_offset)shadowGeometry).getParameters().size());

        Expression roofGeometry = roof.getGeometry();
        assertEquals("offset", ((FilterFunction_offset)roofGeometry).getName());
        assertEquals(3, ((FilterFunction_offset)roofGeometry).getParameters().size());
//        assertEquals("the_geom", ((FilterFunction_offset) roofGeometry).getParameters().get(0).toString());
        assertEquals("0", ((FilterFunction_offset) roofGeometry).getParameters().get(1).toString());
        assertEquals("Height", ((FilterFunction_offset) roofGeometry).getParameters().get(2).toString());

        Expression sidesGeometry = sides.getGeometry();
        assertEquals("isometric", ((FilterFunction_isometric)sidesGeometry).getName());
        assertEquals(2, ((FilterFunction_isometric)sidesGeometry).getParameters().size());
//        assertEquals("the_geom", ((FilterFunction_isometric) sidesGeometry).getParameters().get(0).toString());
        assertEquals("Height", ((FilterFunction_isometric) sidesGeometry).getParameters().get(1).toString());

        Expression expr =  psym.getFill().getColor();
        assertNotNull("fillColor set", expr);
        assertEquals( Color.decode("#aaa"), expr.evaluate(null,Color.class) );
        assertEquals(Double.valueOf(0.6),
                psym.getFill().getOpacity().evaluate(null, Double.class));
    }

    /**
     * Test parsing a Mapbox fill layer using a sprite fill-pattern
     */
    @Test
    public void testFillSprite() throws IOException, ParseException {

        JSONObject jsonObject = parseTestStyle("fillStyleSpriteTest.json");<commit_msg>fix style transform test (#15)<commit_after>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2017, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.mbstyle.transform;

import org.geotools.TestData;
import org.geotools.data.property.PropertyDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.store.ContentFeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.filter.function.CategorizeFunction;
import org.geotools.filter.function.FilterFunction_isometric;
import org.geotools.filter.function.FilterFunction_offset;
import org.geotools.mbstyle.MBStyle;
import org.geotools.mbstyle.MapboxTestUtils;
import org.geotools.mbstyle.layer.*;
import org.geotools.mbstyle.parse.MBObjectParser;
import org.geotools.styling.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.junit.Before;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Expression;
import org.opengis.style.GraphicalSymbol;

import javax.xml.transform.TransformerException;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * Test parsing and transforming a Mapbox fill layer from json.
 */
public class StyleTransformTest {

    static FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

    Map<String, JSONObject> testLayersById = new HashMap<>();


    @Before
    public void setUp() throws IOException, ParseException {
        JSONObject jsonObject = MapboxTestUtils.parseTestStyle("functionParseTest.json");
        JSONArray layers = (JSONArray) jsonObject.get("layers");
        for (Object o : layers) {
            JSONObject layer = (JSONObject) o;
            testLayersById.put((String) layer.get("id"), layer);
        }
    }

    /**
     * Test parsing a Mapbox fill layer
     */
    @Test
    public void testFill() throws IOException, ParseException {

        JSONObject jsonObject = parseTestStyle("fillStyleTest.json");

        // Parse to MBStyle
        MBStyle mbStyle = new MBStyle(jsonObject);
        List<MBLayer> layers = mbStyle.layers("geoserver-states");

        assertEquals(1, layers.size());

        // Find the MBFillLayer and assert it contains the correct FeatureTypeStyle.
        assertTrue(layers.get(0) instanceof FillMBLayer);
        FillMBLayer mbFill = (FillMBLayer) layers.get(0);
        List<FeatureTypeStyle >fts = mbFill.transform(mbStyle);
        
        PolygonSymbolizer psym = SLD.polySymbolizer(fts.get(0));
        
        Expression expr =  psym.getFill().getColor();
        assertNotNull("fillColor set", expr);
        assertEquals( Color.decode("#FF595E"), expr.evaluate(null,Color.class) );
        assertEquals(Double.valueOf(.84),
                psym.getFill().getOpacity().evaluate(null, Double.class));
        
        Expression colorStroke = psym.getStroke().getColor();
        assertNotNull("stroke color set", colorStroke);
        assertEquals(Color.decode("#1982C4"), colorStroke.evaluate(null, Color.class));

        assertNotNull("displacement not null", psym.getDisplacement());
        assertNotNull("displacementX not null",  psym.getDisplacement().getDisplacementX());
        assertNotNull("displacementY not null",  psym.getDisplacement().getDisplacementY());
        assertEquals(Integer.valueOf(20), psym.getDisplacement().getDisplacementX().evaluate(null, Integer.class));
        assertEquals(Integer.valueOf(20), psym.getDisplacement().getDisplacementY().evaluate(null, Integer.class));
        
    }

    /**
     * Test parsing and generating a MapBox fill extrusion
     */
    @Test
    public void testFillExtrusion() throws IOException, ParseException {
        JSONObject jsonObject = parseTestStyle("fillExtrusionTest.json");
        MBObjectParser parse = new MBObjectParser(StyleTransformTest.class);

        // Parse to MBStyle
        MBStyle mbStyle = new MBStyle(jsonObject);
        List<MBLayer> layers = mbStyle.layers("composite");

        assertEquals(2, layers.size());

        // Find the MBFillLayer and assert it contains the correct FeatureTypeStyle.
        assertTrue(layers.get(1) instanceof FillExtrusionMBLayer);
        FillExtrusionMBLayer mbFill = (FillExtrusionMBLayer) layers.get(1);
        List<FeatureTypeStyle> fts = mbFill.transform(mbStyle);
        assertEquals(3, fts.size());

        PolygonSymbolizer psym = SLD.polySymbolizer(fts.get(0));
        PolygonSymbolizer sides = SLD.polySymbolizer(fts.get(1));
        PolygonSymbolizer roof = SLD.polySymbolizer(fts.get(2));

        Expression shadowGeometry = psym.getGeometry();
        assertEquals("offset", ((FilterFunction_offset)shadowGeometry).getName());
        assertEquals(3, ((FilterFunction_offset)shadowGeometry).getParameters().size());

        Expression roofGeometry = roof.getGeometry();
        assertEquals("offset", ((FilterFunction_offset)roofGeometry).getName());
        assertEquals(3, ((FilterFunction_offset)roofGeometry).getParameters().size());
//        assertEquals("the_geom", ((FilterFunction_offset) roofGeometry).getParameters().get(0).toString());
        assertEquals("0", ((FilterFunction_offset) roofGeometry).getParameters().get(1).toString());
        assertEquals("Height", ((FilterFunction_offset) roofGeometry).getParameters().get(2).toString());

        Expression sidesGeometry = sides.getGeometry();
        assertEquals("isometric", ((FilterFunction_isometric)sidesGeometry).getName());
        assertEquals(2, ((FilterFunction_isometric)sidesGeometry).getParameters().size());
//        assertEquals("the_geom", ((FilterFunction_isometric) sidesGeometry).getParameters().get(0).toString());
        assertEquals("Height", ((FilterFunction_isometric) sidesGeometry).getParameters().get(1).toString());

        Expression expr =  psym.getFill().getColor();
        assertNotNull("fillColor set", expr);
        assertEquals( parse.convertToColor("#aaa"), expr.evaluate(null,Color.class) );
        assertEquals(Double.valueOf(0.6),
                psym.getFill().getOpacity().evaluate(null, Double.class));
    }

    /**
     * Test parsing a Mapbox fill layer using a sprite fill-pattern
     */
    @Test
    public void testFillSprite() throws IOException, ParseException {

        JSONObject jsonObject = parseTestStyle("fillStyleSpriteTest.json");
{'fraction_non_alphanumeric': 0.11568613602655554, 'fraction_numerical': 0.0064976340137015325, 'mean_word_length': 3.8493150684931505, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5715831', 'n_tokens_mistral': 3898, 'n_tokens_neox': 3825, 'n_words': 849}