<commit_before>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2004-2008, 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.referencing;

import static org.junit.Assert.*;

import java.awt.geom.Rectangle2D;
import java.util.Set;

import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.CRS.AxisOrder;
import org.geotools.referencing.crs.DefaultEngineeringCRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.resources.geometry.XRectangle2D;
import org.junit.Ignore;
import org.junit.Test;
import org.opengis.geometry.Envelope;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.CoordinateOperation;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.MathTransform2D;
import org.opengis.referencing.operation.TransformException;


/**
 * Tests the {@link CRS} utilities methods.
 *
 *
 * @source $URL$
 * @version $Id$
 * @author <NAME> (IRD)
 */
public final class CrsTest {
    /**
     * Tests the {@link CRS#getSupportedAuthorities} method.
     */
    @Test
    public void testSupportedAuthorities() {
        final Set<String> withoutAlias = CRS.getSupportedAuthorities(false);
        assertTrue (withoutAlias.contains("CRS"));
        assertTrue (withoutAlias.contains("AUTO2"));
        assertTrue (withoutAlias.contains("urn:ogc:def"));
        assertTrue (withoutAlias.contains("http://www.opengis.net"));
        assertFalse(withoutAlias.contains("AUTO"));
        assertFalse(withoutAlias.contains("urn:x-ogc:def"));

        final Set<String> withAlias = CRS.getSupportedAuthorities(true);
        assertTrue (withAlias.containsAll(withoutAlias));
        assertFalse(withoutAlias.containsAll(withAlias));
        assertTrue (withAlias.contains("AUTO"));
        assertTrue (withAlias.contains("urn:x-ogc:def"));
    }

    /**
     * Tests simple decode.
     */
    @Test
    public void testDecode() throws FactoryException {
        assertSame(DefaultGeographicCRS.WGS84, CRS.decode("WGS84(DD)"));
    }

    /**
     * Tests an ESRI code.
     *
     * @todo Not yet working.
     */
    @Test
    @Ignore
    public void testESRICode() throws Exception {
        String wkt = "PROJCS[\"Albers_Conic_Equal_Area\",\n"                  +
                     "  GEOGCS[\"GCS_North_American_1983\",\n"                +
                     "    DATUM[\"D_North_American_1983\",\n"                 +
                     "    SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],\n" +
                     "    PRIMEM[\"Greenwich\",0.0],\n"                       +
                     "    UNIT[\"Degree\",0.0174532925199433]],\n"            +
                     "  PROJECTION[\"Equidistant_Conic\"],\n"                 +
                     "  PARAMETER[\"False_Easting\",0.0],\n"                  +
                     "  PARAMETER[\"False_Northing\",0.0],\n"                 +
                     "  PARAMETER[\"Central_Meridian\",-96.0],\n"             +
                     "  PARAMETER[\"Standard_Parallel_1\",33.0],\n"           +
                     "  PARAMETER[\"Standard_Parallel_2\",45.0],\n"           +
                     "  PARAMETER[\"Latitude_Of_Origin\",39.0],\n"            +
                     "  UNIT[\"Meter\",1.0]]";
        CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final MathTransform crsTransform = CRS.findMathTransform(WGS84, crs, true);
        assertFalse(crsTransform.isIdentity());
    }
    
    /**
     * Checks X is equated to Easting and Y to Northing
     * @throws Exception
     */
    public void testAxisAliases() throws Exception {
        String wkt1 = "PROJCS[\"NAD_1927_Texas_Statewide_Mapping_System\"," +
        		"GEOGCS[\"GCS_North_American_1927\"," +
        		"DATUM[\"D_North_American_1927\"," +
        		"SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]]," +
        		"PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]," +
        		"PROJECTION[\"Lambert_Conformal_Conic\"]," +
        		"PARAMETER[\"False_Easting\",3000000.0]," +
        		"PARAMETER[\"False_Northing\",3000000.0]," +
        		"PARAMETER[\"Central_Meridian\",-100.0]," +
        		"PARAMETER[\"Standard_Parallel_1\",27.416666666666668]," +
        		"PARAMETER[\"Standard_Parallel_2\",34.916666666666664]," +
        		"PARAMETER[\"Latitude_Of_Origin\",31.166666666666668]," +
        		"UNIT[\"Foot\",0.3048]]";
        
        String wkt2 = "PROJCS[\"NAD_1927_Texas_Statewide_Mapping_System\"," +
                "GEOGCS[\"GCS_North_American_1927\"," +
                "DATUM[\"D_North_American_1927\"," +
                "SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]]," +
                "PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]," +
                "PROJECTION[\"Lambert_Conformal_Conic\"]," +
                "PARAMETER[\"False_Easting\",3000000.0]," +
                "PARAMETER[\"False_Northing\",3000000.0]," +
                "PARAMETER[\"Central_Meridian\",-100.0]," +
                "PARAMETER[\"Standard_Parallel_1\",27.416666666666668]," +
                "PARAMETER[\"Standard_Parallel_2\",34.916666666666664]," +
                "PARAMETER[\"Latitude_Of_Origin\",31.166666666666668]," +
                "UNIT[\"Foot\",0.3048]" +
                "AXIS[\"Easting\", EAST]," + 
                "AXIS[\"Northing\", NORTH]]";
        
        CoordinateReferenceSystem crs1 = CRS.parseWKT(wkt1);
        CoordinateReferenceSystem crs2 = CRS.parseWKT(wkt2);
        assertTrue(CRS.equalsIgnoreMetadata(crs1, crs2));
    }

    /**
     * Tests the transformations of an envelope.
     */
    @Test
    public void testEnvelopeTransformation() throws FactoryException, TransformException {
        final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.UTM_10N);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final MathTransform crsTransform = CRS.findMathTransform(WGS84, mapCRS, true);
        assertFalse(crsTransform.isIdentity());

        final GeneralEnvelope firstEnvelope, transformedEnvelope, oldEnvelope;
        firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43});
        firstEnvelope.setCoordinateReferenceSystem(WGS84);

        transformedEnvelope = CRS.transform(crsTransform, firstEnvelope);
        transformedEnvelope.setCoordinateReferenceSystem(mapCRS);

        oldEnvelope = CRS.transform(crsTransform.inverse(), transformedEnvelope);
        oldEnvelope.setCoordinateReferenceSystem(WGS84);

        assertTrue(oldEnvelope.contains(firstEnvelope, true));
        assertTrue(oldEnvelope.equals  (firstEnvelope, 0.02, true));
    }
    
    /**
     * Tests the transformations of an envelope when the two CRS have identify
     * transforms but different datum names 
     */
    @Test
    public void testEnvelopeTransformation2() throws FactoryException, TransformException {
        final CoordinateReferenceSystem WGS84Altered = CRS.parseWKT(WKT.WGS84_ALTERED);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final MathTransform crsTransform = CRS.findMathTransform(WGS84, WGS84Altered, true);
        assertTrue(crsTransform.isIdentity());

        final GeneralEnvelope firstEnvelope;
        firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43});
        firstEnvelope.setCoordinateReferenceSystem(WGS84);

        // this triggered a assertion error in GEOT-2934
        Envelope transformed = CRS.transform(firstEnvelope, WGS84Altered);
        
        // check the envelope is what we expect
        assertEquals(transformed.getCoordinateReferenceSystem(), WGS84Altered);
        double EPS = 1e-9;
        assertEquals(transformed.getMinimum(0), firstEnvelope.getMinimum(0), EPS);
        assertEquals(transformed.getMinimum(1), firstEnvelope.getMinimum(1), EPS);
        assertEquals(transformed.getMaximum(0), firstEnvelope.getMaximum(0), EPS);
        assertEquals(transformed.getMaximum(1), firstEnvelope.getMaximum(1), EPS);
    }

    /**
     * Tests the transformations of a rectangle using a coordinate operation.
     * With assertions enabled, this also test the transformation of an envelope.
     */
    @Test
    public void testTransformationOverPole() throws FactoryException, TransformException {
        final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.POLAR_STEREOGRAPHIC);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final CoordinateOperation operation =
                CRS.getCoordinateOperationFactory(false).createOperation(mapCRS, WGS84);
        final MathTransform transform = operation.getMathTransform();
        assertTrue(transform instanceof MathTransform2D);
        /*
         * The rectangle to test, which contains the South pole.
         */
        Rectangle2D envelope = XRectangle2D.createFromExtremums(
                -3943612.4042124213, -4078471.954436003,
                 3729092.5890516187,  4033483.085688618);
        /*
         * This is what we get without special handling of singularity point.
         * Note that is doesn't include the South pole as we would expect.
         */
        Rectangle2D expected = XRectangle2D.createFromExtremums(
                -178.49352310409273, -88.99136583196398,
                 137.56220967463082, -40.905775004205864);
        /*
         * Tests what we actually get.
         */
        Rectangle2D actual = CRS.transform((MathTransform2D) transform, envelope, null);<commit_msg>Test case for GEOT-3570; commented out until we can make it pass<commit_after>/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2004-2008, 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.referencing;

import static org.junit.Assert.*;

import java.awt.geom.Rectangle2D;
import java.util.Set;

import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.GeneralEnvelope;
import org.geotools.referencing.CRS.AxisOrder;
import org.geotools.referencing.crs.DefaultEngineeringCRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.resources.geometry.XRectangle2D;
import org.junit.Ignore;
import org.junit.Test;
import org.opengis.geometry.Envelope;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.CoordinateOperation;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.MathTransform2D;
import org.opengis.referencing.operation.TransformException;


/**
 * Tests the {@link CRS} utilities methods.
 *
 *
 * @source $URL$
 * @version $Id$
 * @author <NAME> (IRD)
 */
public final class CrsTest {
    /**
     * Tests the {@link CRS#getSupportedAuthorities} method.
     */
    @Test
    public void testSupportedAuthorities() {
        final Set<String> withoutAlias = CRS.getSupportedAuthorities(false);
        assertTrue (withoutAlias.contains("CRS"));
        assertTrue (withoutAlias.contains("AUTO2"));
        assertTrue (withoutAlias.contains("urn:ogc:def"));
        assertTrue (withoutAlias.contains("http://www.opengis.net"));
        assertFalse(withoutAlias.contains("AUTO"));
        assertFalse(withoutAlias.contains("urn:x-ogc:def"));

        final Set<String> withAlias = CRS.getSupportedAuthorities(true);
        assertTrue (withAlias.containsAll(withoutAlias));
        assertFalse(withoutAlias.containsAll(withAlias));
        assertTrue (withAlias.contains("AUTO"));
        assertTrue (withAlias.contains("urn:x-ogc:def"));
    }

    /**
     * Tests simple decode.
     */
    @Test
    public void testDecode() throws FactoryException {
        assertSame(DefaultGeographicCRS.WGS84, CRS.decode("WGS84(DD)"));
    }

    /**
     * Tests an ESRI code.
     *
     * @todo Not yet working.
     */
    @Test
    @Ignore
    public void testESRICode() throws Exception {
        String wkt = "PROJCS[\"Albers_Conic_Equal_Area\",\n"                  +
                     "  GEOGCS[\"GCS_North_American_1983\",\n"                +
                     "    DATUM[\"D_North_American_1983\",\n"                 +
                     "    SPHEROID[\"GRS_1980\",6378137.0,298.257222101]],\n" +
                     "    PRIMEM[\"Greenwich\",0.0],\n"                       +
                     "    UNIT[\"Degree\",0.0174532925199433]],\n"            +
                     "  PROJECTION[\"Equidistant_Conic\"],\n"                 +
                     "  PARAMETER[\"False_Easting\",0.0],\n"                  +
                     "  PARAMETER[\"False_Northing\",0.0],\n"                 +
                     "  PARAMETER[\"Central_Meridian\",-96.0],\n"             +
                     "  PARAMETER[\"Standard_Parallel_1\",33.0],\n"           +
                     "  PARAMETER[\"Standard_Parallel_2\",45.0],\n"           +
                     "  PARAMETER[\"Latitude_Of_Origin\",39.0],\n"            +
                     "  UNIT[\"Meter\",1.0]]";
        CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final MathTransform crsTransform = CRS.findMathTransform(WGS84, crs, true);
        assertFalse(crsTransform.isIdentity());
    }
    
    /**
     * Checks X is equated to Easting and Y to Northing
     * @throws Exception
     */
    public void testAxisAliases() throws Exception {
        String wkt1 = "PROJCS[\"NAD_1927_Texas_Statewide_Mapping_System\"," +
        		"GEOGCS[\"GCS_North_American_1927\"," +
        		"DATUM[\"D_North_American_1927\"," +
        		"SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]]," +
        		"PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]," +
        		"PROJECTION[\"Lambert_Conformal_Conic\"]," +
        		"PARAMETER[\"False_Easting\",3000000.0]," +
        		"PARAMETER[\"False_Northing\",3000000.0]," +
        		"PARAMETER[\"Central_Meridian\",-100.0]," +
        		"PARAMETER[\"Standard_Parallel_1\",27.416666666666668]," +
        		"PARAMETER[\"Standard_Parallel_2\",34.916666666666664]," +
        		"PARAMETER[\"Latitude_Of_Origin\",31.166666666666668]," +
        		"UNIT[\"Foot\",0.3048]]";
        
        String wkt2 = "PROJCS[\"NAD_1927_Texas_Statewide_Mapping_System\"," +
                "GEOGCS[\"GCS_North_American_1927\"," +
                "DATUM[\"D_North_American_1927\"," +
                "SPHEROID[\"Clarke_1866\",6378206.4,294.9786982]]," +
                "PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]," +
                "PROJECTION[\"Lambert_Conformal_Conic\"]," +
                "PARAMETER[\"False_Easting\",3000000.0]," +
                "PARAMETER[\"False_Northing\",3000000.0]," +
                "PARAMETER[\"Central_Meridian\",-100.0]," +
                "PARAMETER[\"Standard_Parallel_1\",27.416666666666668]," +
                "PARAMETER[\"Standard_Parallel_2\",34.916666666666664]," +
                "PARAMETER[\"Latitude_Of_Origin\",31.166666666666668]," +
                "UNIT[\"Foot\",0.3048]" +
                "AXIS[\"Easting\", EAST]," + 
                "AXIS[\"Northing\", NORTH]]";
        
        CoordinateReferenceSystem crs1 = CRS.parseWKT(wkt1);
        CoordinateReferenceSystem crs2 = CRS.parseWKT(wkt2);
        assertTrue(CRS.equalsIgnoreMetadata(crs1, crs2));
    }

    /**
     * Tests the transformations of an envelope.
     */
    @Test
    public void testEnvelopeTransformation() throws FactoryException, TransformException {
        final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.UTM_10N);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final MathTransform crsTransform = CRS.findMathTransform(WGS84, mapCRS, true);
        assertFalse(crsTransform.isIdentity());

        final GeneralEnvelope firstEnvelope, transformedEnvelope, oldEnvelope;
        firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43});
        firstEnvelope.setCoordinateReferenceSystem(WGS84);

        transformedEnvelope = CRS.transform(crsTransform, firstEnvelope);
        transformedEnvelope.setCoordinateReferenceSystem(mapCRS);

        oldEnvelope = CRS.transform(crsTransform.inverse(), transformedEnvelope);
        oldEnvelope.setCoordinateReferenceSystem(WGS84);

        assertTrue(oldEnvelope.contains(firstEnvelope, true));
        assertTrue(oldEnvelope.equals  (firstEnvelope, 0.02, true));
    }
    
    /**
     * Tests the transformations of an envelope when the two CRS have identify
     * transforms but different datum names 
     */
    @Test
    public void testEnvelopeTransformation2() throws FactoryException, TransformException {
        final CoordinateReferenceSystem WGS84Altered = CRS.parseWKT(WKT.WGS84_ALTERED);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final MathTransform crsTransform = CRS.findMathTransform(WGS84, WGS84Altered, true);
        assertTrue(crsTransform.isIdentity());

        final GeneralEnvelope firstEnvelope;
        firstEnvelope = new GeneralEnvelope(new double[] {-124, 42}, new double[] {-122, 43});
        firstEnvelope.setCoordinateReferenceSystem(WGS84);

        // this triggered a assertion error in GEOT-2934
        Envelope transformed = CRS.transform(firstEnvelope, WGS84Altered);
        
        // check the envelope is what we expect
        assertEquals(transformed.getCoordinateReferenceSystem(), WGS84Altered);
        double EPS = 1e-9;
        assertEquals(transformed.getMinimum(0), firstEnvelope.getMinimum(0), EPS);
        assertEquals(transformed.getMinimum(1), firstEnvelope.getMinimum(1), EPS);
        assertEquals(transformed.getMaximum(0), firstEnvelope.getMaximum(0), EPS);
        assertEquals(transformed.getMaximum(1), firstEnvelope.getMaximum(1), EPS);
    }
    /** 
     * Test "densification" during envelope transform in order to avoid clipping(GEOT-3634).
     */
    public void XtestEnvelopeTransformClipping() throws Exception {
        final CoordinateReferenceSystem source = DefaultGeographicCRS.WGS84;
        final CoordinateReferenceSystem target;
        target = CRS.parseWKT(
            "GEOGCS[\"GCS_North_American_1983\","+ 
            "DATUM[\"North_American_Datum_1983\", "+
            "SPHEROID[\"GRS_1980\", 6378137.0, 298.257222101]], "+
            "PRIMEM[\"Greenwich\", 0.0], "+
            "UNIT[\"degree\", 0.017453292519943295], "+
            "AXIS[\"Longitude\", EAST], "+
            "AXIS[\"Latitude\", NORTH]]");       
        // bounds from geotiff
        GeneralEnvelope geotiff = new GeneralEnvelope(source);
        geotiff.add(new DirectPosition2D(source,-179.9,-90.0));
        geotiff.add(new DirectPosition2D(source,180.0,89.9));
        
        Envelope transformed = CRS.transform(geotiff, target );
        assertNotNull( transformed );
        assertTrue( "clipped y",transformed.getUpperCorner().getOrdinate(1) > 88.0);
        assertTrue( "clipped y",transformed.getLowerCorner().getOrdinate(1) < -88.0);
        assertTrue( "clipped x",transformed.getUpperCorner().getOrdinate(0) > 170.0);
        assertTrue( "clipped x",transformed.getLowerCorner().getOrdinate(0) < -170.0);

    }

    /**
     * Tests the transformations of a rectangle using a coordinate operation.
     * With assertions enabled, this also test the transformation of an envelope.
     */
    @Test
    public void testTransformationOverPole() throws FactoryException, TransformException {
        final CoordinateReferenceSystem mapCRS = CRS.parseWKT(WKT.POLAR_STEREOGRAPHIC);
        final CoordinateReferenceSystem WGS84  = DefaultGeographicCRS.WGS84;
        final CoordinateOperation operation =
                CRS.getCoordinateOperationFactory(false).createOperation(mapCRS, WGS84);
        final MathTransform transform = operation.getMathTransform();
        assertTrue(transform instanceof MathTransform2D);
        /*
         * The rectangle to test, which contains the South pole.
         */
        Rectangle2D envelope = XRectangle2D.createFromExtremums(
                -3943612.4042124213, -4078471.954436003,
                 3729092.5890516187,  4033483.085688618);
        /*
         * This is what we get without special handling of singularity point.
         * Note that is doesn't include the South pole as we would expect.
         */
        Rectangle2D expected = XRectangle2D.createFromExtremums(
                -178.49352310409273, -88.99136583196398,
                 137.56220967463082, -40.905775004205864);
        /*
         * Tests what we actually get.
         */
        Rectangle2D actual = CRS.transform((MathTransform2D) transform, envelope, null);
{'fraction_non_alphanumeric': 0.11781282668969592, 'fraction_numerical': 0.05708831416753784, 'mean_word_length': 2.6615077383924115, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 12, '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': '1697641', 'n_tokens_mistral': 7429, 'n_tokens_neox': 6573, 'n_words': 1168}