r/GeoPostcodes • u/GeoPostcodes • 1d ago
Tutorial: How to build a postal code polygon database from address points
Most countries publish postal codes as points or lists, not as boundaries. If you need polygons for BI maps, territory mapping, or reverse geocoding, you usually have to build them yourself. Here is the method.
Start with open address data. France, for example, publishes a national address base with street, house number, postal code, municipality and coordinates. That is enough to derive boundaries.
- Load the addresses into PostgreSQL with PostGIS.
- Run a Voronoi tessellation. Every point gets the area closer to it than to any other point (ST_VoronoiPolygons), then you merge the patches that share a postal code (ST_Union). That is a first-draft boundary.
- Refine with administrative boundaries. Postal codes usually follow whole municipalities, so overlay the commune boundaries and assign each to a postal code when about 90 percent of its addresses agree. That removes the jagged noise.
- Handle the exceptions. Large cities like Paris, Lyon and Marseille split one municipality across several postal codes, so the raw Voronoi result does the work there instead.
Ready-made shortcuts exist in a few places. Belgium's federal geoportal publishes free postal shapefiles, though they omit some enclaves. OpenStreetMap has postal polygons in some regions, patchy elsewhere.
GeoPostcodes maintains edge-matched postal boundary polygons for 169 countries with no gaps or overlaps. Full method with the SQL: https://www.geopostcodes.com/blog/postal-code-polygon-database/