plin-code / laravel-istat-geography
Laravel package for importing and managing Italian geography data from ISTAT
Package info
github.com/plin-code/laravel-istat-geography
pkg:composer/plin-code/laravel-istat-geography
Fund package maintenance!
Requires
- php: ^8.3
- illuminate/contracts: ^12.0 || ^13.0
- league/csv: ^9.24
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^3.0 || ^4.0
- pestphp/pest-plugin-arch: ^3.0 || ^4.0
- pestphp/pest-plugin-laravel: ^3.0 || ^4.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
Suggests
- laravel/boost: 2.5 or higher reads the AI guidelines shipped in resources/boost
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-12-03 17:41:14 UTC
README
A Laravel package for importing and managing Italian geographical data from ISTAT.
Features
- ๐ฎ๐น Import Italian regions, provinces, and municipalities from ISTAT
- ๐ฎ Import Italian postal codes (CAP) with support for multi-CAP municipalities
- ๐ Optional latitude and longitude for every municipality
- ๐ Incremental updates: add new records, update changes, soft-delete removed ones
- ๐ฃ
GeographyUpdatedevent after each update, to keep your own references in sync - ๐ Daily CSV caching to avoid unnecessary requests
- ๐ Eloquent models with hierarchical relationships
- โก Artisan commands for easy data import and synchronization
- ๐ง Fully configurable via configuration file
- ๐ UUID primary keys and soft deletes support
- ๐งช Comprehensive test suite with mocked HTTP requests
Requirements
- PHP 8.3+
- Laravel 12.0+ or 13.0+
- league/csv 9.0+
Installation
composer require plin-code/laravel-istat-geography
Quick Start
- Install the package:
composer require plin-code/laravel-istat-geography
- Publish the configuration:
php artisan vendor:publish --provider="PlinCode\IstatGeography\IstatGeographyServiceProvider"
- Run migrations:
php artisan migrate
- Import the data:
php artisan geography:import
That's it! You now have all Italian geographical data in your database.
Commands
geography:import
Performs a full import of all geographical data from ISTAT. Use this for the initial data load.
php artisan geography:import
Options
| Option | Description |
|---|---|
--cap |
Also import postal codes (CAP) after ISTAT data |
--cap-only |
Import only postal codes, skip ISTAT data (requires existing municipalities) |
--cap-file=<path> |
Use a local JSON file for CAP data instead of downloading |
--coordinates |
Also import municipality coordinates (latitude and longitude) after ISTAT data |
--coordinates-only |
Import only coordinates, skip ISTAT data (requires existing municipalities) |
--coordinates-file=<path> |
Use a local JSON file (plain or gzipped) for coordinates instead of downloading |
Examples
# Import ISTAT data only php artisan geography:import # Import ISTAT data + CAP (using local file - recommended) php artisan geography:import --cap --cap-file=cap-dataset.json # Update only CAP on existing municipalities php artisan geography:import --cap-only --cap-file=cap-dataset.json # Import ISTAT data + coordinates php artisan geography:import --coordinates # Update only coordinates on existing municipalities php artisan geography:import --coordinates-only
Note: The remote GeoJSON with geometries is ~464MB. Using
--cap-filewith a preprocessed JSON file (~3MB) is recommended for better performance.
geography:download-cap
Downloads CAP GeoJSON data and saves it locally for offline import. Useful when you want to download once and import multiple times.
# Download from default URL (config/env) php artisan geography:download-cap # Download from custom URL php artisan geography:download-cap --url=https://example.com/cap.json # Specify output path php artisan geography:download-cap --output=storage/app/my-cap.json
After downloading, import with:
php artisan geography:import --cap --cap-file=storage/app/cap-boundaries.geojson
geography:download-coordinates
Downloads the coordinates dataset and saves it locally (decompressed) for offline import.
# Download from default URL (config/env) php artisan geography:download-coordinates # Download from custom URL php artisan geography:download-coordinates --url=https://example.com/coordinates.json.gz # Specify output path php artisan geography:download-coordinates --output=storage/app/my-coordinates.json
After downloading, import with:
php artisan geography:import --coordinates-only --coordinates-file=storage/app/coordinates-dataset.json
geography:update
Incrementally synchronizes your database with the latest ISTAT data. It compares the current ISTAT CSV against your existing records and applies only the differences: new records are added, changed records are updated, and records no longer present in ISTAT are soft-deleted.
php artisan geography:update
Options
| Option | Description |
|---|---|
--dry-run |
Simulate the update without making any database changes. Shows what would be added, modified, or deleted. |
--force |
Continue execution even if non-critical errors occur (errors are logged as warnings). |
Verbosity Levels
| Flag | Output |
|---|---|
| (none) | Final summary only (e.g. 3 added, 1 modified, 0 deleted) |
-v |
Download progress, list of new/modified/suppressed records, progress bar |
-vv |
Field-level change details (e.g. name: Old Name โ New Name) |
-vvv |
Debug output with timing information for each operation |
Examples
# Preview changes without applying them php artisan geography:update --dry-run # Run with verbose output php artisan geography:update -v # Run with full debug output php artisan geography:update -vvv # Force continue on non-critical errors php artisan geography:update --force
All database operations are wrapped in a transaction. If any error occurs (and --force is not set), all changes are automatically rolled back.
Reacting to changes
After the changes are committed, geography:update dispatches PlinCode\IstatGeography\Events\GeographyUpdated. Use it to update records of your app that point to regions, provinces or municipalities, for example addresses of a suppressed municipality or the previous name of a renamed one.
The event is not dispatched on a dry run, when the update is rolled back, or when nothing changed.
use Illuminate\Support\Facades\Event; use PlinCode\IstatGeography\Events\GeographyUpdated; Event::listen(GeographyUpdated::class, function (GeographyUpdated $event) { foreach ($event->comparison->municipalities->suppressed as $istatCode => $record) { // $record['id'] is the soft deleted municipality, $record['name'] its name } foreach ($event->comparison->municipalities->modified as $istatCode => $record) { // $record['id'] is the updated municipality // $record['changes']['name'] = ['old' => 'Turin', 'new' => 'Torino'] // a moved municipality has $record['changes']['istat_code'] and keeps its id } foreach ($event->createdIds['municipalities'] as $istatCode => $id) { // municipalities created by this update } });
The event carries:
| Property | Content |
|---|---|
comparison |
The ComparisonResult the update applied. regions, provinces and municipalities each have new (CSV data indexed by ISTAT code), modified (id and changes with old and new per field), suppressed (id and name) and skipped (moves that could not be applied) |
added, modified, suppressed |
Counts of records actually created, updated and soft deleted, per entity |
createdIds |
Ids of the created records, per entity, indexed by ISTAT code |
With --force a failed step is skipped and the update goes on, so comparison can list changes that were not applied. Check the counts and createdIds for what really happened.
The shape of the event and of ComparisonResult is part of the public API and follows semantic versioning.
Upgrading from an older version
geography:updatealso syncsbel_code(the Belfiore/cadastral code), so theextend_municipalities_with_postal_codesmigration must have been run. Without the column the update fails with an unknown column error and rolls back.- On a database where
bel_codeis still empty, the first update reports almost every municipality as modified (about 7,900 records), because it fills the missing codes. Later runs report only real changes. - A non-empty Belfiore code that does not match the expected format (one letter and three digits) is skipped and logged as a warning with the municipality ISTAT code.
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="PlinCode\IstatGeography\IstatGeographyServiceProvider"
The config/istat-geography.php file allows you to customize:
- Database connection: Choose which database connection the package tables should use (defaults to the main connection)
- Table names: Customize the database table names (set them before running the migrations)
- Model classes: Use your own model classes by extending the base ones
- CSV URL: Change the ISTAT data source URL (also via
ISTAT_CSV_URLenv) - CAP GeoJSON URL: Change the CAP data source URL (also via
CAP_GEOJSON_URLenv) - Coordinates: Import coordinates on every
geography:import(also viaISTAT_IMPORT_COORDINATESenv) and change the dataset URL (also viaISTAT_COORDINATES_URLenv) - Temporary file name: Customize the cache file name
Database Connection
By default the package uses your application's default database connection. To store the geographical tables on a separate connection, set the connection key in config/istat-geography.php or the ISTAT_DB_CONNECTION environment variable:
ISTAT_DB_CONNECTION=geography
Note
The connection config key is additive and fully backward compatible. If you published the config file before this option existed, the package falls back to your default database connection (config('database.default')), so no action is required on upgrade. To opt into a custom connection, either republish the config file or set the ISTAT_DB_CONNECTION environment variable.
Table Names
The tables key controls the table names used by both the migrations and the models. Set it before running the migrations for the first time: the migrations create the tables (and their foreign keys) with the configured names, and the models read and write the same tables.
'tables' => [ 'regions' => 'geo_regions', 'provinces' => 'geo_provinces', 'municipalities' => 'geo_municipalities', ],
Changing the names after the tables already exist does not rename them. In that case rename the tables yourself with a migration, then update the config. A custom model that defines its own $table property keeps using that table.
Municipality Coordinates
Coordinates are optional. The extend_municipalities_with_coordinates migration adds two nullable columns, latitude and longitude, to the municipalities table, and they stay empty until you import them.
Import them once with --coordinates, or enable the import on every geography:import run:
ISTAT_IMPORT_COORDINATES=true
Each municipality gets one representative point of its administrative area: the centroid of the municipal boundary when it falls inside the municipality, otherwise a point guaranteed to be inside it. The point is not the town hall or the main inhabited centre, so for large or irregular municipalities it can be a few kilometres away from the town centre (and sometimes in open countryside). It is meant for maps, sorting by distance and proximity searches, not for navigation.
geography:update never overwrites coordinates, because they are not ISTAT fields. When ISTAT adds a new municipality, it gets coordinates the next time you run the coordinates import with an updated dataset.
$municipality = Municipality::where('istat_code', '058091')->first(); $municipality->latitude; // 41.8853588 $municipality->longitude; // 12.4607809
Example Configuration
return [ 'connection' => env('ISTAT_DB_CONNECTION', env('DB_CONNECTION')), 'tables' => [ 'regions' => 'my_regions', 'provinces' => 'my_provinces', 'municipalities' => 'my_municipalities', ], 'models' => [ 'region' => \App\Models\Region::class, 'province' => \App\Models\Province::class, 'municipality' => \App\Models\Municipality::class, ], 'import' => [ 'csv_url' => 'https://custom-url.com/data.csv', 'temp_filename' => 'my_istat_data.csv', ], ];
Models
The package provides three Eloquent models:
Region
use PlinCode\IstatGeography\Models\Geography\Region; $region = Region::where('name', 'Piemonte')->first(); $provinces = $region->provinces;
Province
use PlinCode\IstatGeography\Models\Geography\Province; $province = Province::where('code', 'TO')->first(); $municipalities = $province->municipalities; $region = $province->region;
Municipality
use PlinCode\IstatGeography\Models\Geography\Municipality; $municipality = Municipality::where('name', 'Torino')->first(); $province = $municipality->province;
ISTAT Fields
Each model exposes a static istatFields() method that returns the list of fields managed by ISTAT data. These are the fields that the geography:update command is allowed to overwrite. Any additional fields you add to your extended models will not be touched during updates.
Region::istatFields(); // ['name', 'istat_code'] Province::istatFields(); // ['name', 'code', 'istat_code', 'region_id'] Municipality::istatFields(); // ['name', 'istat_code', 'province_id', 'bel_code'] Municipality::capFields(); // ['postal_code', 'postal_codes'] Municipality::coordinateFields(); // ['latitude', 'longitude']
Extending Models
If you want to use the package models in your main project, you can extend them:
// app/Models/Region.php namespace App\Models; use PlinCode\IstatGeography\Models\Geography\Region as BaseRegion; class Region extends BaseRegion { // Add your project-specific logic here public function customMethod() { return $this->provinces()->count(); } }
// app/Models/Province.php namespace App\Models; use PlinCode\IstatGeography\Models\Geography\Province as BaseProvince; class Province extends BaseProvince { // Add your project-specific logic here }
// app/Models/Municipality.php namespace App\Models; use PlinCode\IstatGeography\Models\Geography\Municipality as BaseMunicipality; class Municipality extends BaseMunicipality { // Add your project-specific logic here }
Remember to update the models section in the configuration file to point to your custom classes. The import and update services and the model relationships (for example $region->provinces or $municipality->province) then use your classes.
Database Structure
Regions
id(UUID, primary key)name(string)istat_code(string, unique)created_at,updated_at,deleted_at
Provinces
id(UUID, primary key)region_id(UUID, foreign key)name(string)code(string, unique)istat_code(string, unique)created_at,updated_at,deleted_at
Municipalities
id(UUID, primary key)province_id(UUID, foreign key)name(string)istat_code(string, unique)bel_code(string, nullable) - Cadastral/Belfiore code for CAP matching.geography:importandgeography:updatesave it only when the ISTAT value has the expected format (one letter and three digits). An empty or malformed value never overwrites a stored code.postal_code(string, nullable) - Primary postal code (CAP)postal_codes(string, nullable) - Range of postal codes for large municipalities (e.g., "00118-00199")latitude(decimal 10,7, nullable): latitude of the representative point (WGS 84)longitude(decimal 10,7, nullable): longitude of the representative point (WGS 84)created_at,updated_at,deleted_at
Relationships
RegionโhasManyโProvinceProvinceโbelongsToโRegionProvinceโhasManyโMunicipalityMunicipalityโbelongsToโProvince
Replacing Existing Command
If you already have a geography:import command in your project, you can replace it with the package's command:
// In app/Console/Kernel.php or in your existing command Artisan::command('geography:import', function () { $this->info('Starting geographical data import...'); try { $count = \PlinCode\IstatGeography\Facades\IstatGeography::import(); $this->info("Import completed successfully! Imported {$count} municipalities."); } catch (\Exception $e) { $this->error('Error during import: ' . $e->getMessage()); } })->purpose('Import regions, provinces and municipalities from ISTAT');
AI guidelines (Laravel Boost)
This package ships AI guidelines for Laravel Boost at resources/boost/guidelines/core.blade.php, the path the Boost documentation gives package authors. It is a short, always in context brief on the three models, their tables, columns and relations, the geography:* commands and the config keys, so an agent uses the package models instead of creating its own regions, provinces or municipalities tables.
Install Boost 2.5 or higher with composer require laravel/boost --dev then php artisan boost:install, selecting this package when Boost asks which third party guidelines to install. On an app that already has Boost installed, php artisan boost:update --discover offers the newly detected package.
Testing
Run the test suite:
composer test
The package includes:
- โ Unit tests for models and relationships
- โ Feature tests for the import service
- โ Feature tests for the update command and services
- โ Mocked HTTP requests (no external dependencies)
- โ PHPStan static analysis
- โ Pest PHP testing framework
Test Coverage
- Models and their relationships
- Import service with CSV processing
- Compare service for detecting changes
- Update service for applying changes
- Artisan command functionality (import and update)
- Configuration handling
Data Sources
ISTAT Data
Geographic data (regions, provinces, municipalities) is sourced from ISTAT (Italian National Institute of Statistics), the official Italian government statistics agency.
Postal Codes (CAP) and Boundaries
Postal code data (data-properties-v1) and the full boundaries GeoJSON (data-geo-v1) are sourced from Zornade Data Downloads. They are derived from OpenStreetMap addresses, so they are licensed under the Open Database License (ODbL). If you use or redistribute them, keep this attribution and share derived databases under the same license:
ยฉ OpenStreetMap contributors (ODbL). Data processed by Zornade (https://zornade.com).
Municipality Coordinates
Coordinates are derived from the ISTAT municipal boundaries (Confini Amministrativi) published by Zornade Data Downloads, licensed under CC BY 4.0. If you show these coordinates in your product, keep this attribution visible:
Source: ISTAT, administrative boundaries (CC BY 4.0). Data processed by Zornade (https://zornade.com).
The points were computed by this package from the boundary polygons (centroid in EPSG:3035, or a point on surface when the centroid falls outside the polygon), so they are an adaptation of the original data. The dataset file includes the same attribution, license and generation date in its meta section.
A huge thanks to Zornade for their incredible work in making Italian public data freely available. Their dedication to open data helps developers build better applications for Italian users.
Regenerating the Coordinates Dataset
Maintainers can rebuild the dataset with scripts/build-coordinates-dataset.php (PHP with pdo_sqlite, plus Docker for the official GDAL image). It downloads the boundaries and the current ISTAT list, computes one point per municipality and writes build/municipality_coordinates_dataset.json.gz:
php scripts/build-coordinates-dataset.php
# Optional sanity check with the Zornade reverse geocoding API
ZORNADE_TOKEN=your-token php scripts/build-coordinates-dataset.php --spot-check
Municipalities created by a merger after the boundaries were published are computed on the union of their predecessors, listed in MERGED_MUNICIPALITIES inside the script. The script stops with an error when a municipality has no polygon and no merger entry.
Contributing
- Fork the project
- Turn on the commit hook (
git config core.hooksPath .githooks) - Create a branch named
type/short-kebab-description(git checkout -b fix/bel-code-on-update) - Commit your changes with a
type(scope): descriptionmessage (git commit -m 'fix(update): save bel_code on new municipalities') - Run
composer test,composer analyseandcomposer format - Push the branch and open a Pull Request
The commit title is lowercase, in the imperative mood, with a description of 50 characters at most. Every commit of a pull request is checked on GitHub, because pull requests are merged with a rebase.
The full rules, with the lists of types and scopes, are in CONTRIBUTING.md.
License
The MIT License (MIT). Please see License File for more information.
