<commit_before><?php
/*
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

class Tagger_always extends Tagger {

    /*
     * Data references
     */
    public $references = array(
        array(
            'dataset' => 'Coastline',
            'author' => 'Natural Earth',
            'license' => 'Free of Charge',
            'url' => 'http://www.naturalearthdata.com/downloads/10m-physical-vectors/10m-coastline/'
        )
    );

    /*
     * Well known areas
     */
    private $areas = array(
        'equatorial' => array(
            'operator' => 'ST_Crosses',
            'geometry' => 'ST_GeomFromText(\'LINESTRING(-180 0,180 0)\', 4326)'
        ),
        'tropical' => array(
            'operator' => 'ST_Contains',
            'geometry' => 'ST_GeomFromText(\'POLYGON((-180 -23.43731,-180 23.43731,180 23.43731,180 -23.43731,-180 -23.43731))\', 4326)'
        ),
        'southern' => array(
            'operator' => 'ST_Contains',
            'geometry' => 'ST_GeomFromText(\'POLYGON((-180 0,-180 -90,180 -90,180 0,-180 0))\', 4326)'
        ),
        'northern' => array(
            'operator' => 'ST_Contains',
            'geometry' => 'ST_GeomFromText(\'POLYGON((-180 0,-180 90,180 90,180 0,-180 0))\', 4326)'
        )
    );

    /**
     * Constructor
     *
     * @param DatabaseHandler $dbh
     * @param array $config
     */
    public function __construct($dbh, $config) {
        parent::__construct($dbh, $config);
    }

    /**
     * TODO Tag metadata
     *
     * @param array $metadata
     * @param array $options
     * @return array
     * @throws Exception
     */
    public function tag($metadata, $options = array()) {

        /*
         * Relative location on earth
         */
        $locations = $this->getLocations($metadata['geometry']);
        $keywords = $locations;

        /*
         * Coastal status
         */
        if ($this->isCoastal($metadata['geometry'])) {
            $keywords[] = 'location' . iTag::TAG_SEPARATOR . 'coastal';
        }

        /*
         * Season
         */
        if (isset($metadata['timestamp']) && $this->isValidTimeStamp($metadata['timestamp']) ) {
            $keywords[] = $this->getSeason($metadata['timestamp'], in_array('location:southern', $locations));
        }

        return array(
            'area' => $this->getArea($metadata['geometry']),
            'keywords' => $keywords
        );

    }

    /**
     * Return geometry area in square meters
     *
     * @param string $geometry
     */
    private function getArea($geometry) {
        $query = 'SELECT ' . $this->postgisArea($this->postgisGeomFromText($geometry)) . ' as area';
        $result = $this->query($query);
        if ($result) {
          $row = pg_fetch_assoc($result);
          return isset($row) && isset($row['area']) ? $this->toSquareKm($row['area']) : 0;
        }
        return 0;
    }

    /**
     * Return locations of geometry i.e.
     *  - location:equatorial
     *  - location:tropical
     *  - location:northern
     *  - location:southern
     *
     * @param string $geometry
     */
    private function getLocations($geometry) {
        $locations = array();
        foreach ($this->areas as $key => $value) {
            if ($this->isETNS($geometry, $value)) {
                $locations[] = 'location'. iTag::TAG_SEPARATOR . $key;
            }
        }
        return $locations;
    }

    /**
     * Return true if geometry overlaps a coastline
     *
     * @param string $geometry
     */
    private function isCoastal($geometry) {
        $geom = $this->postgisGeomFromText($geometry);
        $query = 'SELECT gid FROM datasources.coastlines WHERE ST_Crosses(' . $geom . ', geom) OR ST_Contains(' . $geom . ', geom)';
        return $this->hasResults($query);
    }

    /**
     * Return true if geometry overlaps Equatorial, Tropical, Southern or Northern areas
     *
     * @param string $geometry
     * @param array $what
     */<commit_msg>Change 'location' to 'loc'<commit_after><?php
/*
 * Copyright 2013 <NAME>
 *
 * Licensed under the Apache License, version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

class Tagger_always extends Tagger {

    /*
     * Data references
     */
    public $references = array(
        array(
            'dataset' => 'Coastline',
            'author' => 'Natural Earth',
            'license' => 'Free of Charge',
            'url' => 'http://www.naturalearthdata.com/downloads/10m-physical-vectors/10m-coastline/'
        )
    );

    /*
     * Well known areas
     */
    private $areas = array(
        'equatorial' => array(
            'operator' => 'ST_Crosses',
            'geometry' => 'ST_GeomFromText(\'LINESTRING(-180 0,180 0)\', 4326)'
        ),
        'tropical' => array(
            'operator' => 'ST_Contains',
            'geometry' => 'ST_GeomFromText(\'POLYGON((-180 -23.43731,-180 23.43731,180 23.43731,180 -23.43731,-180 -23.43731))\', 4326)'
        ),
        'southern' => array(
            'operator' => 'ST_Contains',
            'geometry' => 'ST_GeomFromText(\'POLYGON((-180 0,-180 -90,180 -90,180 0,-180 0))\', 4326)'
        ),
        'northern' => array(
            'operator' => 'ST_Contains',
            'geometry' => 'ST_GeomFromText(\'POLYGON((-180 0,-180 90,180 90,180 0,-180 0))\', 4326)'
        )
    );

    /**
     * Constructor
     *
     * @param DatabaseHandler $dbh
     * @param array $config
     */
    public function __construct($dbh, $config) {
        parent::__construct($dbh, $config);
    }

    /**
     * TODO Tag metadata
     *
     * @param array $metadata
     * @param array $options
     * @return array
     * @throws Exception
     */
    public function tag($metadata, $options = array()) {

        /*
         * Relative location on earth
         */
        $locations = $this->getLocations($metadata['geometry']);
        $keywords = $locations;

        /*
         * Coastal status
         */
        if ($this->isCoastal($metadata['geometry'])) {
            $keywords[] = 'loc' . iTag::TAG_SEPARATOR . 'coastal';
        }

        /*
         * Season
         */
        if (isset($metadata['timestamp']) && $this->isValidTimeStamp($metadata['timestamp']) ) {
            $keywords[] = $this->getSeason($metadata['timestamp'], in_array('location:southern', $locations));
        }

        return array(
            'area' => $this->getArea($metadata['geometry']),
            'keywords' => $keywords
        );

    }

    /**
     * Return geometry area in square meters
     *
     * @param string $geometry
     */
    private function getArea($geometry) {
        $query = 'SELECT ' . $this->postgisArea($this->postgisGeomFromText($geometry)) . ' as area';
        $result = $this->query($query);
        if ($result) {
          $row = pg_fetch_assoc($result);
          return isset($row) && isset($row['area']) ? $this->toSquareKm($row['area']) : 0;
        }
        return 0;
    }

    /**
     * Return locations of geometry i.e.
     *  - location:equatorial
     *  - location:tropical
     *  - location:northern
     *  - location:southern
     *
     * @param string $geometry
     */
    private function getLocations($geometry) {
        $locations = array();
        foreach ($this->areas as $key => $value) {
            if ($this->isETNS($geometry, $value)) {
                $locations[] = 'loc'. iTag::TAG_SEPARATOR . $key;
            }
        }
        return $locations;
    }

    /**
     * Return true if geometry overlaps a coastline
     *
     * @param string $geometry
     */
    private function isCoastal($geometry) {
        $geom = $this->postgisGeomFromText($geometry);
        $query = 'SELECT gid FROM datasources.coastlines WHERE ST_Crosses(' . $geom . ', geom) OR ST_Contains(' . $geom . ', geom)';
        return $this->hasResults($query);
    }

    /**
     * Return true if geometry overlaps Equatorial, Tropical, Southern or Northern areas
     *
     * @param string $geometry
     * @param array $what
     */
{'fraction_non_alphanumeric': 0.1484106767083841, 'fraction_numerical': 0.029239118396278657, 'mean_word_length': 2.077709611451943, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5617250', 'n_tokens_mistral': 2914, 'n_tokens_neox': 2637, 'n_words': 649}