You are here

public function CanadaProvinces::import in Geolocation Field 8.3

Import batch callback.

Parameters

mixed $context: Batch context.

Return value

bool Batch return.

Overrides GeolocationGeometryDataBase::import

File

modules/geolocation_geometry/modules/geolocation_geometry_open_canada_provinces/src/Plugin/geolocation/GeolocationGeometryData/CanadaProvinces.php, line 42

Class

CanadaProvinces
Import Provinces of Canada.

Namespace

Drupal\geolocation_geometry_open_canada_provinces\Plugin\geolocation\GeolocationGeometryData

Code

public function import(&$context) {
  parent::import($context);
  $taxonomy_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $logger = \Drupal::logger('geolocation_provinces_of_canada');
  try {

    /** @var \Shapefile\Geometry\Geometry $record */
    while ($record = $this->shapeFile
      ->fetchRecord()) {
      if ($record
        ->isDeleted()) {
        continue;
      }
      $name = $record
        ->getData('NAME');
      if (empty($name)) {
        continue;
      }

      /** @var \Drupal\taxonomy\TermInterface $term */
      $term = $taxonomy_storage
        ->create([
        'vid' => 'geolocation_provinces_of_canada',
        'name' => $name,
      ]);
      $term
        ->set('field_geometry_data_geometry', [
        'geojson' => $record
          ->getGeoJSON(),
      ]);
      $term
        ->save();
    }
    return 'Done importing Provinces of Canada.';
  } catch (ShapefileException $e) {
    $logger
      ->warning($e
      ->getMessage());
    return t('Error importing Provinces of Canada.');
  }
}