You are here

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

Class

UsStates
Import US states.

Namespace

Drupal\geolocation_geometry_natural_earth_us_states\Plugin\geolocation\GeolocationGeometryData

Code

public function import(&$context) {
  parent::import($context);
  $taxonomy_storage = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term');
  $logger = \Drupal::logger('geolocation_us_states');
  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_us_states',
        'name' => $record
          ->getData('NAME'),
      ]);
      $term
        ->set('field_geometry_data_geometry', [
        'geojson' => $record
          ->getGeoJSON(),
      ]);
      $term
        ->save();
    }
    return t('Done importing US States.');
  } catch (ShapefileException $e) {
    $logger
      ->warning($e
      ->getMessage());
    return t('ERROR importing US States.');
  }
}