You are here

abstract class GeolocationGeometryDataBase in Geolocation Field 8.3

Class GeolocationGeometryDataBase.

@package Drupal\geolocation_geometry_data

Hierarchy

Expanded class hierarchy of GeolocationGeometryDataBase

5 files declare their use of GeolocationGeometryDataBase
CanadaProvinces.php in modules/geolocation_geometry/modules/geolocation_geometry_open_canada_provinces/src/Plugin/geolocation/GeolocationGeometryData/CanadaProvinces.php
GermanyZipCodes.php in modules/geolocation_geometry/modules/geolocation_geometry_germany_zip_codes/src/Plugin/geolocation/GeolocationGeometryData/GermanyZipCodes.php
NaturalEarthCountries.php in modules/geolocation_geometry/modules/geolocation_geometry_natural_earth_countries/src/Plugin/geolocation/GeolocationGeometryData/NaturalEarthCountries.php
UnescoWorldHeritage.php in modules/geolocation_geometry/modules/geolocation_geometry_world_heritage/src/Plugin/geolocation/GeolocationGeometryData/UnescoWorldHeritage.php
UsStates.php in modules/geolocation_geometry/modules/geolocation_geometry_natural_earth_us_states/src/Plugin/geolocation/GeolocationGeometryData/UsStates.php

File

modules/geolocation_geometry/modules/geolocation_geometry_data/src/GeolocationGeometryDataBase.php, line 13

Namespace

Drupal\geolocation_geometry_data
View source
abstract class GeolocationGeometryDataBase {

  /**
   * URI to archive.
   *
   * @var string
   */
  public $sourceUri = '';

  /**
   * Filename of archive.
   *
   * @var string
   */
  public $sourceFilename = '';

  /**
   * Directory extract of archive.
   *
   * @var string
   */
  public $localDirectory = '';

  /**
   * Extracted filename.
   *
   * @var string
   */
  public $shapeFilename = '';

  /**
   * Shape file.
   *
   * @var \Shapefile\ShapefileReader|null
   */
  public $shapeFile;

  /**
   * Return this batch.
   *
   * @return array
   *   Batch return.
   */
  public function getBatch() {
    $operations = [
      [
        [
          $this,
          'download',
        ],
        [],
      ],
      [
        [
          $this,
          'import',
        ],
        [],
      ],
    ];
    return [
      'title' => t('Import Shapefile'),
      'operations' => $operations,
      'progress_message' => t('Finished step @current / @total.'),
      'init_message' => t('Import is starting.'),
      'error_message' => t('Something went horribly wrong.'),
    ];
  }

  /**
   * Download batch callback.
   *
   * @return string
   *   Batch return.
   */
  public function download() {
    $destination = \Drupal::service('file_system')
      ->getTempDirectory() . '/' . $this->sourceFilename;
    if (!is_file($destination)) {
      $client = \Drupal::httpClient();
      $client
        ->get($this->sourceUri, [
        'save_to' => $destination,
      ]);
    }
    if (!empty($this->localDirectory) && substr(strtolower($this->sourceFilename), -3) === 'zip') {
      $zip = new \ZipArchive();
      $res = $zip
        ->open($destination);
      if ($res === TRUE) {
        $zip
          ->extractTo(\Drupal::service('file_system')
          ->getTempDirectory() . '/' . $this->localDirectory);
        $zip
          ->close();
      }
      else {
        return t('ERROR downloading @url', [
          '@url' => $this->sourceUri,
        ]);
      }
    }
    return t('Successfully downloaded @url', [
      '@url' => $this->sourceUri,
    ]);
  }

  /**
   * Import batch callback.
   *
   * @param mixed $context
   *   Batch context.
   *
   * @return bool
   *   Batch return.
   */
  public function import(&$context) {
    $logger = \Drupal::logger('geolocation_geometry_data');
    if (empty($this->shapeFilename)) {
      return FALSE;
    }
    try {
      $this->shapeFile = new ShapefileReader(\Drupal::service('file_system')
        ->getTempDirectory() . '/' . $this->localDirectory . '/' . $this->shapeFilename);
    } catch (ShapefileException $e) {
      $logger
        ->warning($e
        ->getMessage());
      return FALSE;
    }
    return TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GeolocationGeometryDataBase::$localDirectory public property Directory extract of archive. 4
GeolocationGeometryDataBase::$shapeFile public property Shape file.
GeolocationGeometryDataBase::$shapeFilename public property Extracted filename. 4
GeolocationGeometryDataBase::$sourceFilename public property Filename of archive. 5
GeolocationGeometryDataBase::$sourceUri public property URI to archive. 5
GeolocationGeometryDataBase::download public function Download batch callback.
GeolocationGeometryDataBase::getBatch public function Return this batch.
GeolocationGeometryDataBase::import public function Import batch callback. 5