Skip to content
GitHub

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.

  1. 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.
  2. A code is MG and 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, is INVALID_FORMAT. A well formed code that is not in the list is UNKNOWN_PLACE.
  3. listPlaces with no parent returns the regions. A fokontany has no children: the result is an empty list.
  4. 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 first maxSearchResults are returned. Nothing found is an empty list, not an error.
  5. Limits (in data.yaml): text of at most maxInputLength characters, counted as Unicode code points (not bytes, not UTF-16 units); a search text of at least minSearchLength characters. An unknown level is INVALID_FORMAT.
  6. Storage. data.yaml writes regions, districts and communes as [code, name] and the fokontany per commune: a list of names is numbered 001, 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, and loadPlaces adds the fokontany of the regions it reads, with the same functions on the result.
  7. Names are kept as published. Many fokontany share a name (Tanambao); the code is the identity, never the name.
  8. Search options. options is an object with the keys level and within; any other key is INVALID_FORMAT, and a value that is not an object is INVALID_TYPE. A language with typed arguments, such as PHP, has no unknown option or level to reject. within is a place code, read the way findPlace reads 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 is INVALID_TYPE, a badly formed one INVALID_FORMAT, an unknown one UNKNOWN_PLACE. level and within combine.
  9. formatPlace writes the name of the place, then the name of each parent up to the region, joined by pathSeparator (in data.yaml): Antananarivo Avaradrano, Analamanga. Its errors are those of findPlace.
  10. Points and areas. Every place has one point and one area, from the COD-AB points and areas (geo in data.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.
  11. locatePlace reads the code the way findPlace does and has its errors.
  12. distanceBetween reads both codes the way findPlace does. The distance is the great circle (haversine) distance between the two points on a sphere of radius earth.radiusKm, in km, rounded half up to earth.distanceDecimals decimals. The same place is 0.
  13. nearestPlace returns the place of the given level whose 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 and level text, else INVALID_TYPE; level must be one of the four, else INVALID_FORMAT; latitude within -90 to 90 and longitude within -180 to 180, else OUT_OF_RANGE; and the point inside bounds (whole degrees around Madagascar), else OUT_OF_RANGE, so a point in Paris is never given a Malagasy place.
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
  • 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”.
  • 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.
  • 2026-12-03: coordinates and areas come from the COD-AB v01 points and area columns, kept to 5 and 2 decimals. bounds is 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 Tanambao and 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.