15th anniversary logo

Geocoding: Building a Zip Code to Coordinates Converter

Geocoding blog

Table of Contents

Depending on the country, a zip code might be called a postcode, code postal, código postal, PLZ, PIN code, 邮政编码, and the list undoubtedly goes on.

Whatever you call it, a zip code is a sequence of alphanumeric characters, sometimes formatted with spaces or hyphens. The primary use of zip codes is to facilitate the routing and sorting letters and parcels. Still, they’re also used for supply chain logistics, address cleaning, statistical research, sales, marketing, spatial analysis… again, the list continues.

For each of those reasons, a business would need to identify the location of the zip codes. We’ll walk you through a few use cases, discuss some of the challenges of converting zip codes to coordinates, and finally, how to build your zip code to coordinates converter.

What’s the Use of a Zip Code to Coordinates Converter?

If you know the coordinates of a zip code, you can determine the price of delivery or service level terms. You can identify which areas can be reached in one business day or two. It can also be convenient information to relate offers and demand based on distance criteria, for example, for jobs.

Converting postcodes to coordinates is also critical for building maps and performing spatial analysis, like looking for geographical patterns in your customer base. Are your customers mainly coming from specific areas or directions? How does that relate to transportation infrastructure, socio-demographic patterns, the presence (or absence) of competition, and so on?

To identify the physical location of a zip code, you need a tool that assigns a coordinate to every (geographic) zip code, something that converts a zip code into a set of coordinates.

Bear in mind that some postcodes are non-geographic (like the H0 H0 H0 for Santa Claus in Canada).

What Are the Challenges of Converting Zip Codes to Coordinates?

Postcode granularity differs widely from country to country. It can directly identify a building, a part of a street, a town district, an entire town, or even a larger zone such as an administrative region. That means you’ll have a lot of different types of data to process for an effective converter. The converter will have to deal with polygons, lines, and points and convert them into a coordinate

The logical choice would be to use the “center” or the centroid of the data representing the postcode. But unfortunately, nothing is simple. There are cases where the geometric centroid of a polygon does not fall into that polygon:

Centroid falling outside a polygon

The postcode could be composed of several polygons:

A postcode of more than one polygon

Or only points:

A postcode of points

You need a solution to ensure that the coordinate falls into the polygon or is one of the points.

So How Do You Build a Zip Code to Coordinates Converter?

Software engineers know perfectly well what a coordinate is. It’s a number that determines the position of a point in a space. Generally, we use xy, and sometimes z, but there are others—for example, the Geographic Coordinate System (GCS).

“Oh right,” you say, “I’ve seen Google Maps; I know what latitude and longitude look like.”

Latitude and longitude are angles that refer to a point on an ellipsoid (an abstraction of the earth), but even they are not the only way to represent geographic coordinates.

You must know the geodetic datum to know exactly what kind of reference is used for the coordinates. The one used by Google Maps and GPS is the World Geodetic System or WGS 84.

Their EPSG reference usually identifies GCSEs. For WGS 84, it’s EPSG:4326. Another commonly used reference is EPSG:3857, also called Pseudo-Mercator. It’s projected onto a square and widely used on maps everywhere online.

Of course, there are many other reference systems that are specific to countries or regions; in this article, we’ll show you how to transform data from one system to another.

Select Your Tools

It’s time to define which tools you’ll use for your zip code to coordinates converter. You’ll need to store data, process it, extract a pseudo-centroid for different data collections (polygons or points), and visualize it.

Storing and Handling Data

As you’re dealing with geographical coordinates, it seems obvious that you need a Geographical Information System (GIS) and a place to store data to work with. Why not a database?

A perfect (and free) choice is PostgreSQL with the PostGIS extension. We won’t cover the PostgreSQL installation here, but once you’ve installed it, you only need to execute CREATE EXTENSION postgis; in a psql prompt to set up PostGIS.

Note that other database systems propose similar solutions.

Computing Pseudo-centroids

A common way to find the centroid and to constrain it to be inside the geometry is to use the PostGIS function ST_PointOnSurface. It’s not the only algorithm available and not always the best choice, but it’s a good default.

Let’s try it with the previous examples we illustrated:

SELECT ST_PointOnSurface(g), g
FROM ST_GeomFromText('POLYGON((0 0, 0 1, 0.2 1, 0.2 0.2, 0.8 0.2, 0.8 1, 1 1, 1 0, 0 0))') AS g;
Coordinate within a polygon
SELECT ST_PointOnSurface(g) , g
FROM ST_GeomFromText('MULTIPOLYGON(((0 0, 0 1, 1 1, 1 0, 0 0),(2 0, 2 1, 3 1, 3 0, 2 0)))') AS g;
Coordinate selected between more than one polygon
SELECT ST_PointOnSurface(g), g
FROM ST_GeomFromText('MULTIPOINT((0 0), (0 0.21), (0.5 0.6), (0.4 0.3))') AS g;
Coordinate on a point

Visualizing Progress

This tutorial uses QGIS to load and view data, but you can also use geometry viewers of your favorite Postgres tool, like PgAdmin or DBeaver.

Take Your Tools to the Field

Now, it’s time to apply your tools to real-world examples!

A big challenge here is to find accurate and reliable sources. This tutorial only uses open data; post companies in some countries still sell them under license.

Polygons

For the first use case, let’s use ZIP Code Tabulation Areas from the census bureau of the United States. Click Download for the Shapefile Zip File, unzip it, and drop the .shp file into QGIS.

It should look like this (I’ve added an OpenStreetMap background to have some context):

Viewing data with QGIS

To insert the polygons into a Postgres database, use command line tools such as shp2pgsqlogr2ogr, or QGIS (via the menu Processing > Toolbox > Database > Export to PostgreSQL).

As mentioned, this tutorial uses the QGIS processing, but in a production environment, you’ll probably use command line tools that can be scripted.

Name your table (this tutorial uses us_zcta for the table name). Next, you have to add a geometry column to store the centroid of each zip. It’s constrained to store only point geometry with the WGS84 coordinate system (4326).

You can find more information on the coordinate systems in PostGIS here.

ALTER TABLE us_zcta ADD COLUMN pos geometry(point, 4326);

To compute the centroid, perform a transformation from the source spatial reference system; in this case, transform 4269 into the column we added, which is 4326.

UPDATE us_zcta
SET pos = ST_Transform(ST_PointOnSurface(geom), 4326);

Add the column us_zcta.pos to QGIS to see the following result:

Viewing data points on QGIS

To extract the latitude/longitude, perform the following select:

SELECT zcta5ce10 AS postcode, ST_Y(pos) AS latitude, ST_X(pos) AS longitude 
FROM us_zcta;

Note that the file already contained a latitude/longitude column.

Points

For the next example, let’s go to Austria. Addresses with postcodes are available from the Bundesministerium für Arbeit und Wirtschaft. Download the zip file directly and unzip it.

The zip contains a series of .csv files. There are no geometric formats, so you must load flat data first and then create the corresponding geometry.

First, create a table:

CREATE TABLE at_plz (
    adrcd varchar,
    gkz varchar,
    okz varchar,
    plz varchar,
    skz varchar,
    zaehlsprengel varchar,
    hausnrtext varchar,
    hausnrzahl1 varchar,
    hausnrbuchstabe1 varchar,
    hausnrverbindung1 varchar,
    hausnrzahl2 varchar,
    hausnrbuchstabe2 varchar,
    hausnrbereich varchar,
    hnr_adr_zusammen varchar,
    gnradresse varchar,
    hofname varchar,
    rw double precision,
    hw double precision,
    epsg integer,
    quelladresse varchar,
    bestimmungsart varchar
);

Open a psql session and load the ADRESSE.csv:

\copy at_plz from 'ADRESSE.csv' with delimiter ';' quote '"' CSV HEADER;

Add a geometry column and create the point from the .csv. There is a little subtlety here—the file uses three different coordinate systems, as indicated in the epsg column.

ALTER TABLE at_plz ADD COLUMN geom geometry(point, 4326);
UPDATE at_plz SET geom = ST_Transform(st_SetSRID(st_makepoint(rw, hw), epsg), 4326);

And here is the table in QGIS. The points are automatically colored according to the postcode to give you an idea of the zones they cover.

Color-coded postcode points in QGIS

Now, you have to compute the centroids of each postcode. As there are many rows with the same postcode, group them beforehand and save the results in a new table:

CREATE TABLE at_plz_agg AS 
SELECT plz, ST_PointOnSurface(ST_Collect(geom))::geometry(point, 4326) pos
FROM at_plz
GROUP BY plz;

View again in QGIS, note the red dots, and you’re done!

Data points viewed as red dots in QGIS

No Coordinates

Sometimes—or more often than sometimes—a post company publishes postcodes without coordinates. In that situation, you have to cross the data with other sources to extrapolate the coordinates. Usually, countries have geographic or statistical institutes that maintain such references, or you can also use OpenStreetMap or GeoNames in some circumstances.

Remember, when using multiple sources, have caution! You’ll face matching problems, erroneous data, normalization issues, multiple languages and scripts, and other complications. Addressing these issues may require many steps, such as exploring and cleaning the data and removing outliers, to name the first few.

Conclusion

While you’ve learned how to convert multiple object types into coordinates, specifically zip codes and their data sources and particularities, your converter is still unfinished.

To create an exhaustive worldwide dataset, you’d need to centralize multiple countries in one table and keep the data up to date. There are about 180 countries using postcodes; some are updated yearly, some monthly, some weekly…

But don’t be discouraged! You can work with a provider to handle those details for you, like GeoPostcodes.

We maintain a worldwide list of postcodes and related data, from coordinates to administrative divisions, including geocodes and time zones. We’ve updated our data hundreds of times and built advanced processing pipelines to deal with the particularities of each country. For example, 99.6 percent of our postal codes have accurate coordinates. The remaining 0.4 percent inherit coordinates from the administrative divisions they belong to. We enjoy solving these problems, and we’d be happy to save you the trouble! Reach out to us for further information, or you can directly explore our data from our website.

Additional Resources

What is a zip code to coordinates converter?

A zip code to coordinates converter is a tool that converts postal codes into latitude and longitude coordinates. It enables users to determine the precise geographic location associated with a specific zip code, facilitating mapping and geolocation services.

How do I convert ZIP codes to coordinates?
  1. Install PostgreSQL with the PostGIS extension
  2. Download the polygons of the zips and load them into the PostgreSQL database using ogr2ogr or QGIS.
  3. Compute the centroid of the polygons using PostGIS function ST_PointOnSurface() and store the result in the database
  4. Extract the latitude and longitude from the centroid using ST_Y() and ST_X()
How is a zip code to coordinates converter useful?

If you know the coordinates of a zip code, you can:

  • Determine the price of delivery or service level terms
  • Identify which areas can be reached in one or two business days
  • Relate offer and demand based on distance criteria
  • Build maps and perform spatial analysis

Related posts