You are here

public function NaturalEarthCountries::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_natural_earth_countries/src/Plugin/geolocation/GeolocationGeometryData/NaturalEarthCountries.php, line 42

Class

NaturalEarthCountries
Import Countries of the world.

Namespace

Drupal\geolocation_geometry_natural_earth_countries\Plugin\geolocation\GeolocationGeometryData

Code

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

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

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