Skip to content
GitHub

Malagasy Address

Places

Find, list and search the regions, districts, communes and fokontany of Madagascar by code or by name.

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

  1. RegionAnalamangaMG11
  2. DistrictAntananarivo AtsimondranoMG11117
  3. CommuneItaosyMG11117110
  4. FokontanyAmbodifasikaMG11117110005
A code is read from left to right: each level adds its part to the code of the level above.

Know a code, want the place and where it is

You haveMG11117
You callfindPlace('MG11117')
You getAntananarivo Atsimondrano, Analamanga

Want to go down one level, for a picker

You haveMG11
You calllistPlaces('MG11')
You get13: Antananarivo Avaradrano, Ambohidratrimo, Ankazobe, …

The user types a name, you show the best matches first

You haveitaosy
You callsearchPlaces('itaosy')
You getItaosy, Itaosy

Want one line to print, with the parents

You haveMG11117
You callformatPlace('MG11117')
You getAntananarivo Atsimondrano, Analamanga
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

@mdgascar/places: Works in the browser and on a server. The fokontany load on demand.

Terminal window
npm install @mdgascar/places

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 Analamanga
searchPlaces('avaradrano', { level: 'district' })[0]?.code; // 'MG11102'
searchPlaces('ambohi', { within: 'MG11102' })[0]?.name; // a place below that district
formatPlace('MG11102'); // 'Antananarivo Avaradrano, Analamanga'

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 commune

read 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.

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.

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 km2
nearestPlace(-18.9, 47.52, 'region').name; // 'Analamanga'
distanceBetween('MG11', 'MG12'); // the km between the two regions

With loadPlaces, the fokontany points are read from geo/<region>.json next to the names.

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];
}

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.

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.