GermanyZipCodes.php in Geolocation Field 8.3
File
modules/geolocation_geometry/modules/geolocation_geometry_germany_zip_codes/src/Plugin/geolocation/GeolocationGeometryData/GermanyZipCodes.php
View source
<?php
namespace Drupal\geolocation_geometry_germany_zip_codes\Plugin\geolocation\GeolocationGeometryData;
use Shapefile\ShapefileException;
use Drupal\geolocation_geometry_data\GeolocationGeometryDataBase;
class GermanyZipCodes extends GeolocationGeometryDataBase {
public $sourceUri = 'https://www.suche-postleitzahl.org/download_v1/wgs84/mittel/plz-5stellig/shapefile/plz-5stellig.shp.zip';
public $sourceFilename = 'plz-5stellig.shp.zip';
public $localDirectory = 'plz';
public $shapeFilename = 'plz-5stellig.shp';
public function import(&$context) {
parent::import($context);
$taxonomy_storage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$logger = \Drupal::logger('geolocation_geometry_germany_zip_codes');
try {
while ($record = $this->shapeFile
->fetchRecord()) {
if ($record
->isDeleted()) {
continue;
}
$term = $taxonomy_storage
->create([
'vid' => 'germany_zip_codes',
'name' => $record
->getData('PLZ'),
]);
$term
->set('field_geometry_data_geometry', [
'geojson' => $record
->getGeoJSON(),
]);
$term
->set('field_city', $record
->getData('NOTE'));
$term
->save();
}
return t('Done importing PLZ');
} catch (ShapefileException $e) {
$logger
->warning($e
->getMessage());
return t('Error importing PLZ');
}
}
}