CanadaProvinces.php in Geolocation Field 8.3
File
modules/geolocation_geometry/modules/geolocation_geometry_open_canada_provinces/src/Plugin/geolocation/GeolocationGeometryData/CanadaProvinces.php
View source
<?php
namespace Drupal\geolocation_geometry_open_canada_provinces\Plugin\geolocation\GeolocationGeometryData;
use Shapefile\ShapefileException;
use Drupal\geolocation_geometry_data\GeolocationGeometryDataBase;
class CanadaProvinces extends GeolocationGeometryDataBase {
public $sourceUri = 'https://www.weather.gov/source/gis/Shapefiles/Misc/province.zip';
public $sourceFilename = 'province.zip';
public $localDirectory = 'geolocation_geometry_open_canadian_provinces';
public $shapeFilename = 'province.shp';
public function import(&$context) {
parent::import($context);
$taxonomy_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$logger = \Drupal::logger('geolocation_provinces_of_canada');
try {
while ($record = $this->shapeFile
->fetchRecord()) {
if ($record
->isDeleted()) {
continue;
}
$name = $record
->getData('NAME');
if (empty($name)) {
continue;
}
$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.');
}
}
}