Skip to main content

Where are new secondary schools most needed? Part II: a point grid with PostGIS

A 259,600-point grid over Uruguay, reverse geocoding and spatial queries to find the areas farthest from a secondary school.

Part two of the search for where to place 136 new secondary schools in Uruguay. The author builds a grid of equidistant points, tries geopy, Nominatim and Cognos 11 maps to keep only the points inside the country, and ends up solving it with PostgreSQL, PostGIS and QGIS. The takeaway: distance alone isn’t enough without population and socioeconomic data.

Series «Where are new secondary schools most needed?», part 2 of 3. Part 1 · Part 2 · Part 3

In the first part of this series, a visual solution with growing circles for placing 136 new secondary schools in Uruguay wasn’t convincing (and sparked some debate on social media). This second part tries another approach: a 259,600-point grid over the country, reverse geocoding and spatial queries with PostgreSQL, PostGIS and QGIS, noting which tools worked and which didn’t.

A grid of equidistant points over Uruguay

I couldn’t come up with a good way to solve it, so I talked it over with two friends from university and high school, and both agreed that the simplest version of the problem is to find the points farthest from any school: take a grid of equidistant points and sort them by their distance to the nearest school.

So I looked up Uruguay’s boundaries to build the grid:

More precisely: ymax = -30.0508, ymin = -34.9376, xmin = -58.4200 and xmax = -53.1107. With those boundaries I could write two nested loops, increasing both x and y by 0.01 to build the grid.

That makes 259,600 points in total, roughly one every ten blocks.

Zooming in on Montevideo:

With the grid in place, the next step is to find the distance from each point to its nearest school. The point whose nearest school is farthest away is where the first new school would go. Repeating this 136 times gives the location of every school to build.

Reverse geocoding: which points fall inside Uruguay

But there was still a problem: Uruguay isn’t square! As the grid image shows, many points fall in Argentina, others in Brazil and others in the middle of the Atlantic Ocean. I needed reverse geocoding, something that takes a latitude and longitude and returns the address (or at least the country).

Once again I didn’t want to use paid web services or APIs, because if you don’t pay they won’t let you run bulk queries. I asked three Quanam colleagues who had worked with geolocation on some projects:

  • One suggested the web service provided by Agesic, Uruguay’s e-government agency. It has several features, but it searches by street names and the like, and what I had was a point with latitude and longitude.
  • Another suggested the map visualization in Cognos 11. I’ve used it and it’s very good: custom controls and the advanced functions of the Mapbox API through JavaScript make it very capable. But when I tested it with a large volume it truncated at around 30,000 points, far from the 260,000 I had.
  • The third suggested geopy, which has a function that returns the country for a given pair of coordinates:

It was exactly what I needed, but I would also run into the request limit. The workaround was to install Nominatim on my own server. Nominatim (from the Latin “by name”) is a tool for searching OpenStreetMap (OSM) data. The problem is that it apparently only installs on Linux distributions (I use Windows), and I didn’t feel like setting up virtual machines.

PostgreSQL, PostGIS and QGIS

Just as I was about to give up, an acquaintance told me he had used PostgreSQL with the PostGIS extension for his thesis.

The point is that it adds geometric data types (points, lines, polygons, etc.) and a large set of functions for spatial queries: distance between points, whether a point lies inside a polygon, and so on.

Looking into it, I saw that QGIS works very well with PostGIS. QGIS is an open source geographic information system (GIS) that, besides connecting to PostgreSQL, handles shapefile vector files and much more.

I was almost there: all I needed was a shapefile with the outline of Uruguay, to import it into QGIS, load it into a PostGIS table and start querying. Luckily, the open data catalog has shapefiles for many things (postal codes, localities, etc.).

With the database and the geospatial extension installed, and tables with points, postal codes, neighborhoods, census sections and more, there is a lot you can do. For example, find the distance between two points:

Find the area of Uruguay:

Find large towns without a secondary school:

See which secondary school is closest to the largest town without one:

Which schools are closest to the corner of 21 de Setiembre and Bulevar Artigas in Montevideo?

Candidate points only where people live

Since I had a map of Uruguay’s localities, it occurred to me that instead of using the whole grid I could cross it with this layer, so that candidate points fall inside a locality (where people live) and not in the middle of nowhere.

Map of Uruguay’s localities
Grid crossed with localities to reduce the number of candidate points

Now a simple query returns the places farthest from any school:

Let’s dig a little deeper. According to Wikipedia, Pueblo Quintana has 67 inhabitants and Morato 218, while Paso de las Piedras de Arerunga isn’t listed. We would also need to know how many of those people are of secondary school age.

Looking only at whether a point falls in a locality and how far it is from the nearest school isn’t very useful. The analysis needs the number of people of school age living near each candidate point, plus socioeconomic data on them. Quanam’s data science specialists had already warned me it wouldn’t be that easy.

But nothing will stop me: in the next installment I’ll dig into another approach that convinces me more.

Héctor Cotelo
Business Analytics & Information Management consultant

¿Listo para dar
el próximo paso?

Inicia tu camino
Otras publicaciones

Before asking how to use AI in a utility, it is worth asking whether the data is ready to support better decisions.

Six connected stages take a public power finance function from assessment to adoption, so AI can support better, faster and more traceable decisions.

Affordability, capital investment and resilience are now moving together. Utilities need scenarios to make decisions before the future becomes clear.

Quanam, a digital transformation, data and AI consultancy headquartered in Montevideo, Uruguay, is Certified in the Claude Partner Network. Its team holds Claude certifications, part of a path guided by one conviction: AI delivers more when people work alongside it.

Leave a Reply