Spec 0.1.0
Madagascar administrative divisions: rules and sources
Finds, lists and searches the regions, districts, communes and fokontany of Madagascar by their official code.
- Four levels. A region has a 4-character code (
MG11), a district 7 (MG11102), a commune 10 and a fokontany 13. Every code starts with the code of its parent, so the hierarchy needs no separate table. - A code is
MGand digits. Letters are read in any case and spaces around it are ignored. Any other character, or a length that is not one of the four, isINVALID_FORMAT. A well formed code that is not in the list isUNKNOWN_PLACE. listPlaceswith no parent returns the regions. A fokontany has no children: the result is an empty list.- Searching folds both sides: case, accents, and
-,',.,/,(,)read as a space. A place matches when the folded name contains the folded text. Matches are ranked: the name equals the text, then the name starts with it, then the text starts a word of the name, then the text is found elsewhere in the name. Inside a rank the shorter name comes first, then the lower code. The firstmaxSearchResultsare returned. Nothing found is an empty list, not an error. - Limits (in
data.yaml): text of at mostmaxInputLengthcharacters, counted as Unicode code points (not bytes, not UTF-16 units); a search text of at leastminSearchLengthcharacters. An unknownlevelisINVALID_FORMAT. - Storage.
data.yamlwrites regions, districts and communes as[code, name]and the fokontany per commune: a list of names is numbered001,002, … and a map gives the numbers when they are not consecutive (138 communes). A port may keep the fokontany of each region apart and load them on demand; the vectors assume every region is loaded. JavaScript does: its functions know the regions, districts and communes, andloadPlacesadds the fokontany of the regions it reads, with the same functions on the result. - Names are kept as published. Many fokontany share a name (
Tanambao); the code is the identity, never the name. - Search options.
optionsis an object with the keyslevelandwithin; any other key isINVALID_FORMAT, and a value that is not an object isINVALID_TYPE. A language with typed arguments, such as PHP, has no unknown option or level to reject.withinis a place code, read the wayfindPlacereads it, and keeps only the places below it: a place is not below itself, and a level that is not below the code finds nothing, which is an empty list. A code that is not text isINVALID_TYPE, a badly formed oneINVALID_FORMAT, an unknown oneUNKNOWN_PLACE.levelandwithincombine. formatPlacewrites the name of the place, then the name of each parent up to the region, joined bypathSeparator(indata.yaml):Antananarivo Avaradrano, Analamanga. Its errors are those offindPlace.- Points and areas. Every place has one point and one area, from the COD-AB points and areas
(
geoindata.yaml): latitude and longitude in degrees rounded half up to 5 decimals (about 1 m), area in km2 rounded half up to 2 decimals. The point is the one the dataset publishes for the place; it is a reference point, not a proof that the place contains it. locatePlacereads the code the wayfindPlacedoes and has its errors.distanceBetweenreads both codes the wayfindPlacedoes. The distance is the great circle (haversine) distance between the two points on a sphere of radiusearth.radiusKm, in km, rounded half up toearth.distanceDecimalsdecimals. The same place is0.nearestPlacereturns the place of the givenlevelwhose point is nearest to the given point, by the distance of rule 12 before rounding; a tie goes to the lower code. It answers “nearest point”, not “contains”: near a boundary the place that contains the point can be another one. Boundaries are not part of this version. Checks, in this order: latitude and longitude must be finite numbers andleveltext, elseINVALID_TYPE;levelmust be one of the four, elseINVALID_FORMAT; latitude within -90 to 90 and longitude within -180 to 180, elseOUT_OF_RANGE; and the point insidebounds(whole degrees around Madagascar), elseOUT_OF_RANGE, so a point in Paris is never given a Malagasy place.
Errors
Section titled “Errors”| Input | Code |
|---|---|
Not text (and not nothing, for listPlaces) |
INVALID_TYPE |
| Empty, too long, wrong shape, unknown level or option | INVALID_FORMAT |
| Well formed code that is not in the list | UNKNOWN_PLACE |
| Latitude or longitude out of range, or outside bounds | OUT_OF_RANGE |
Sources
Section titled “Sources”- OCHA COD-AB Madagascar, HDX dataset
cod-ab-mdg, CC BY-IGO: https://data.humdata.org/dataset/cod-ab-mdg (24 regions, 120 districts, 1 645 communes, 17 465 fokontany, checked 2026-12-03). It is a UN humanitarian reference dataset, not a list published by the Malagasy government: the module never calls it “official”.
Recorded disagreements
Section titled “Recorded disagreements”- The dataset notes that levels 2 to 4 may have changed since its last review (March 2024). Commercial sources count 22 regions, 114 districts and 1 578 communes. The COD-AB is used as the single reference.
- CENI, number of voters per fokontany on 2023-08-01 (https://www.ceni-madagascar.mg/wp-content/uploads/2023/08/NOMBRE-DES-ELECTEURS-PAR-FOKONTANY-DU-01-AOUT-2023.pdf, checked 2026-12-03): 22 regions, 116 districts, 1 653 communes and 19 340 fokontany, names only and no codes. It is an electoral register, not a list of divisions, and it predates the region Ambatosoa. Only 10 822 of the 17 465 COD-AB name paths appear in it, partly because the same names are spelled differently (Roman numerals against ordinals, for example). The 1 875 fokontany difference is open: COD-AB may be incomplete, or the two may count differently.
Recorded decisions
Section titled “Recorded decisions”- 2026-12-03: coordinates and areas come from the COD-AB v01 points and area columns, kept to 5 and
2 decimals.
boundsis the extent of all points (latitude -25.6 to -12.0, longitude 43.2 to 50.5) rounded out to whole degrees. Distances use a sphere of the mean Earth radius 6371.0088 km. Boundary shapes, region capitals and the old provinces (faritany) are left for a later version. - 2026-12-03, project lead: COD-AB is the reference and stays the only data source. Names, not
codes, are what a form shows and stores; the code is kept because 337 fokontany are called
Tanambaoand the code is the only way to tell them apart. An official list from INSTAT or the Ministry of the Interior would replace the data without changing the API. - Postal codes and the written order of a postal address are not part of this module: no official source has been found yet. Open, waiting for the project lead.