Malagasy Address
Places
Find, list and search the regions, districts, communes and fokontany of Madagascar by code or by name.
How places work
Section titled “How places work”A place is one of four levels: a region contains districts, a district contains communes, a commune contains fokontany. Every place has a code, and the code starts with the code of its parent. That is why one code is enough to find a place and everything around it.
MG11117110005
- RegionAnalamanga
MG11 - DistrictAntananarivo Atsimondrano
MG11117 - CommuneItaosy
MG11117110 - FokontanyAmbodifasika
MG11117110005
What do you want to do?
Section titled “What do you want to do?”Know a code, want the place and where it is
MG11117findPlace('MG11117')Want to go down one level, for a picker
MG11listPlaces('MG11')The user types a name, you show the best matches first
itaosysearchPlaces('itaosy')Want one line to print, with the parents
MG11117formatPlace('MG11117')Quick reference
Section titled “Quick reference”findPlace(code) | Code → the place and its parents, or throws |
listPlaces(parentCode?) | Parent code, or nothing → the places directly below |
searchPlaces(text, options?) | Name text → places whose name contains it, best match first |
formatPlace(code) | Code → the name and its parents in one line |
locatePlace(code) | Code → latitude, longitude and area in km2 |
nearestPlace(latitude, longitude, level) | Latitude, longitude, level → the place whose point is nearest |
distanceBetween(codeA, codeB) | Two codes → the distance between their points in km |
Places::find($code) | Code → the place and its parents, or throws |
Places::list($parentCode) | Parent code, or nothing → the places directly below |
Places::search($text, $level, $within) | Name text → places whose name contains it, best match first |
Places::format($code) | Code → the name and its parents in one line |
Places::locate($code) | Code → latitude, longitude and area in km2 |
Places::nearest($latitude, $longitude, $level) | Latitude, longitude, level → the place whose point is nearest |
Places::distance($codeA, $codeB) | Two codes → the distance between their points in km |
Install
Section titled “Install”@mdgascar/places: Works in the browser and on a server. The fokontany load on demand.
npm install @mdgascar/placesmdgascar/malagasy-address: Plain PHP, no framework. The fokontany are built in.
composer require mdgascar/malagasy-addressmdgascar/malagasy-address-laravel: Adds the mg_place rule, the @mgPlace directive and the AsPlace cast. It installs the PHP package for you.
composer require mdgascar/malagasy-address-laravelmdgascar/malagasy-address-symfony: Adds the MalagasyPlace constraint, the mg_place Twig filter and the mg_place Doctrine type. It installs the PHP package for you.
composer require mdgascar/malagasy-address-symfonyBasic usage
Section titled “Basic usage”A place has a code (MG11 for a region, MG11102 for a district, then a commune and a fokontany) that starts with the code of its parent. Several fokontany share a name, so the code is what tells them apart.
import { findPlace, formatPlace, listPlaces, searchPlaces } from '@mdgascar/places';
findPlace('MG11102430').name; // 'Ankadinandriana' (a commune)findPlace('MG11102430').parents[0]?.name; // 'Analamanga' (its region)listPlaces('MG11'); // the districts of AnalamangasearchPlaces('avaradrano', { level: 'district' })[0]?.code; // 'MG11102'searchPlaces('ambohi', { within: 'MG11102' })[0]?.name; // a place below that districtformatPlace('MG11102'); // 'Antananarivo Avaradrano, Analamanga'use Mdgascar\MalagasyAddress\Places\PlaceLevel;use Mdgascar\MalagasyAddress\Places\Places;
Places::find('MG11102430')->name; // 'Ankadinandriana' (a commune)Places::find('MG11102430')->parents[0]->name; // 'Analamanga' (its region)Places::list('MG11'); // the districts of AnalamangaPlaces::search('avaradrano', PlaceLevel::District)[0]->code; // 'MG11102'Places::search('ambohi', within: 'MG11102')[0]->name; // a place below that districtPlaces::format('MG11102'); // 'Antananarivo Avaradrano, Analamanga'Fokontany
Section titled “Fokontany”The regions, districts and communes are part of the package (about 18 KB gzipped). The 17,000 fokontany are raw files, one per region, loaded when you ask: an app that needs one region does not carry the other 23. PHP has them built in, so the same calls work without loading.
import { loadPlaces } from '@mdgascar/places';
const places = await loadPlaces({ read, regions: ['MG11'] });places.findPlace('MG11102430001').name; // 'Ankadinandriana' (a fokontany)places.listPlaces('MG11102430'); // the fokontany of the communeread gets a file by its path: fetchBlobReader('https://example.com/malagasy-data/') in a browser or on a server, or your own function that reads node_modules/@mdgascar/places/data/<path> from the disk. Without regions, all 24 regions load.
Search
Section titled “Search”Searching ignores case, accents and punctuation and finds names that contain the text. The best match comes first: the exact name, then names that start with the text, then the rest. At most 50 places are returned. Nothing found is an empty list, not an error.
Two options narrow a search: level keeps one level, and within keeps the places below a code, which is how you find one Tanambao among the 337. formatPlace writes a result with its parents, to show in a list.
Points, areas and distances
Section titled “Points, areas and distances”Every place has a point (latitude and longitude) and an area in km2, from the COD-AB. nearestPlace finds the place of a level whose point is nearest to a point in Madagascar, and distanceBetween measures the km between two places. Nearest means the nearest point, not the place that contains it: near a boundary they can differ. A point out of range or outside Madagascar is OUT_OF_RANGE.
import { distanceBetween, locatePlace, nearestPlace } from '@mdgascar/places';
locatePlace('MG11').areaKm2; // the area of Analamanga in km2nearestPlace(-18.9, 47.52, 'region').name; // 'Analamanga'distanceBetween('MG11', 'MG12'); // the km between the two regionsWith loadPlaces, the fokontany points are read from geo/<region>.json next to the names.
Places::locate('MG11')->areaKm2; // the area of Analamanga in km2Places::nearest(-18.9, 47.52, PlaceLevel::Region)->name; // 'Analamanga'Places::distance('MG11', 'MG12'); // the km between the two regionsLaravel and Symfony
Section titled “Laravel and Symfony”Both adapters have the same three features: a validation rule, a way to print a place with its parents, and a way to store a place as its code.
$request->validate([ 'commune' => ['required', 'mg_place:commune,MG11102'],]);@mgPlace($user->commune) {{-- Ankadinandriana, Antananarivo Avaradrano, Analamanga --}}protected function casts(): array{ return ['commune' => AsPlace::class];}#[MalagasyPlace(level: PlaceLevel::Commune, within: 'MG11102')]public ?string $commune = null;{{ user.commune|mg_place }} {# Ankadinandriana, Antananarivo Avaradrano, Analamanga #}#[ORM\Column(type: 'mg_place')]private ?Place $commune = null;The level is optional, and within accepts only the places below a code. The cast and the column type store the code and read it back as a Place with its parents.
Where the data comes from
Section titled “Where the data comes from”The list is the OCHA COD-AB for Madagascar, published on the Humanitarian Data Exchange under CC BY-IGO (24 regions, 120 districts, 1 645 communes, 17 465 fokontany), a UN reference dataset, not a list published by the Malagasy government. Other lists count differently: see the rules and sources.