You are here

class File in Geocoder 8.3

Same name in this branch
  1. 8.3 modules/geocoder_field/src/Geocoder/Provider/File.php \Drupal\geocoder_field\Geocoder\Provider\File
  2. 8.3 modules/geocoder_field/src/Plugin/Geocoder/Provider/File.php \Drupal\geocoder_field\Plugin\Geocoder\Provider\File
  3. 8.3 modules/geocoder_field/src/Plugin/Geocoder/Preprocessor/File.php \Drupal\geocoder_field\Plugin\Geocoder\Preprocessor\File

Provides a file handler to be used by 'file' plugin.

Hierarchy

  • class \Drupal\geocoder_field\Geocoder\Provider\File extends \Geocoder\Provider\AbstractProvider implements \Geocoder\Provider\Provider

Expanded class hierarchy of File

File

modules/geocoder_field/src/Geocoder/Provider/File.php, line 20

Namespace

Drupal\geocoder_field\Geocoder\Provider
View source
class File extends AbstractProvider implements Provider {

  /**
   * {@inheritdoc}
   */
  public function getName() : string {
    return 'file';
  }

  /**
   * {@inheritdoc}
   */
  public function geocodeQuery(GeocodeQuery $query) : Collection {
    $filename = $query
      ->getText();

    // Check file type exists and is a JPG (IMAGETYPE_JPEG) before exif_read.
    if (file_exists($filename) && exif_imagetype($filename) == 2 && ($exif = @exif_read_data($filename))) {
      if (isset($exif['GPSLatitude']) && isset($exif['GPSLatitudeRef']) && isset($exif['GPSLongitude']) && $exif['GPSLongitudeRef']) {
        $latitude = $this
          ->getGpsExif($exif['GPSLatitude'], $exif['GPSLatitudeRef']);
        $longitude = $this
          ->getGpsExif($exif['GPSLongitude'], $exif['GPSLongitudeRef']);
        $result = Address::createFromArray([
          'providedBy' => $this
            ->getName(),
          'latitude' => $latitude,
          'longitude' => $longitude,
        ]);
        return new AddressCollection([
          $result,
        ]);
      }
    }
    throw new LogicException(sprintf('Could not find geo data in file: "%s".', basename($filename)));
  }

  /**
   * Retrieves the latitude and longitude from exif data.
   *
   * @param array $coordinate
   *   The coordinate.
   * @param string $hemisphere
   *   The hemisphere.
   *
   * @return float
   *   Return value based on coordinate and Hemisphere.
   */
  protected function getGpsExif(array $coordinate, string $hemisphere) {
    for ($i = 0; $i < 3; $i++) {
      $part = explode('/', $coordinate[$i]);
      if (count($part) == 1) {
        $coordinate[$i] = $part[0];
      }
      elseif (count($part) == 2) {
        $coordinate[$i] = floatval($part[0]) / floatval($part[1]);
      }
      else {
        $coordinate[$i] = 0;
      }
    }
    list($degrees, $minutes, $seconds) = $coordinate;
    $sign = $hemisphere == 'W' || $hemisphere == 'S' ? -1 : 1;
    $value = $sign * ($degrees + $minutes / 60 + $seconds / 3600);
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function reverseQuery(ReverseQuery $query) : Collection {
    throw new UnsupportedOperation('The File plugin is not able to do reverse geocoding.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
File::geocodeQuery public function
File::getGpsExif protected function Retrieves the latitude and longitude from exif data.
File::getName public function
File::reverseQuery public function