Because of the low data quality of the official UN/LOCODE dataset, determining which location is meant is an art. This document describes that art and is similar to how this project does most of this automatically.
Remember that no single field of the UN/LOCODE set is trustworthy. The art is in cross-referencing.
For example: CNTZO. The coordinates do point to a place matching the name, but in flexport and MarineTraffic, another location is used. Because the other city is the one used in practice, you could argue this is what the UN/LOCODE means now.
Example sources:
- NGA World Port Index (Pub 150)
- Sailing Directions Enroute
- The port list at AtoBviaC
- Marine traffic (though there is no overview you can access without paying).
- VesselFinder
None of these are perfect, but they are pretty good
Unfortunately, Wikidata doesn't quite count as a primary source: while quite accurate, most of them are tagged by (not quite very clever) bots.
UN/LOCODEs are meant to facilitate international trade. If there are multiple options, it is likely the bigger and/or more notable city of the 2.
Almost all UN/LOCODEs are places (though not all, like EGSCN for the Suez Canal, but these are quite rare).
Almost no UN/LOCODE points to a municipality. When a city and a municipality share a name, choose the city.
That's where the coordinates field is for. If the coordinates do point to a city with its name, that's probably the one being meant. Unfortunately, these can be flat-out wrong. Some have typos and others are just wrong. An example is the Suez Canal, whose coordinates point to somewhere in the Black Sea.
Look up what is meant by the code in subdivision-codes.csv. Unfortunately, a lot of subdivision codes are outdated and won't match the subdivisions CSV which is more up-to-date.
Still: a subdivision can give a good indication which location is meant.
Look at the functions. For example: if it is defined as a port (function 1), it's a safe bet it needs to be next to a sea or a river.
There are actually quite a few UN/LOCODEs for airports. Note that "Apt" in a name means Airport. In those cases, the UN/LOCODE means an actual airport.
Also, note that the UN/LOCODE dataset started out as the IATA dataset. So if the function is Airport and the last 3 characters of the UN/LOCODE is an IATA of an airport of the same name as the UN/LOCODE and it is in the country of the UN/LOCODE, then you have a hit. The UN/LOCODE then means the location the airport is named after.
For example: MASEK (Ksar Es Souk) is the old name of Errachidia. However, Errachidia already has its own UN/LOCODE (MAERH), so MASEK actually shouldn't exist anymore.
For example: ATKIE. There are 11 different places called Kiensberg across Austria, all tiny. There's no way to know which one is meant.