Where are new secondary schools most needed? Part I: open data and circles
How to place 136 new schools so no point in Uruguay is far from one: converting coordinates with pyproj and a first visual solution.
A Quanam consultant uses open data on Uruguay’s secondary schools to find where 136 new ones should go so that no point in the country is far from a school. In this first part he converts coordinates from EPSG:32721 to EPSG:4326 with pyproj and tries a visual solution: circles that grow around each school until only the uncovered spots remain.
TABLA DE CONTENIDOS
Series «Where are new secondary schools most needed?», part 1 of 3. Part 1 · Part 2 · Part 3
How do you place 136 new secondary schools in Uruguay so the whole country is as well covered as possible? This first part sets up the problem, explains how the coordinates of the existing schools were obtained and converted, and tries a first solution with spatial data science: drawing growing circles around each school until only the uncovered spots remain.
A concrete problem to try spatial data science
For a long time I had wanted to dig into spatial data science and play with it. Combining GIS (geographic information systems), DBMS (database management systems), data analytics and big data was fun for me, but I couldn’t find an excuse or a specific problem I was motivated to solve.
During Uruguay’s 2019 election campaign, a proposal called for building 136 public model secondary schools in the country’s most vulnerable neighborhoods. That gave me a problem I was interested in: where should those schools go? Targeting the most vulnerable areas makes sense, because the largest gaps in upper secondary graduation appear when socioeconomic background is taken into account.
I thought of another way to solve it: place the 136 schools so that Uruguay is covered as well as possible, that is, minimize the distance from any point in the country to a secondary school.
A first solution: circles around each school
The first intuitive solution was to plot every existing secondary school on a map of Uruguay and draw circles around them with a growing diameter. At some point, the circles would cover the whole country except for a single spot. That spot, outside every circle, would be the farthest from any existing school, and that is where the first new school should go. The process would be repeated 136 times, adding the new schools in each iteration.
Locating the existing schools
The first problem was geolocating every existing secondary school, and I didn’t know whether that data existed. At a meeting I ran into one of the co-founders of DATA Uruguay, an organization working on open government, open data and civic tech, who confirmed it did and sent me the link to the open data catalog.
The coordinates were in the EPSG:32721 WGS 84 / UTM zone 21S system. Its area of use lies between the 60° W and 54° W meridians and between the 80° S parallel and the Equator; in other words, it is used in Argentina, Bolivia, Brazil, Paraguay and Uruguay.
Converting the coordinates to EPSG:4326
I preferred EPSG:4326 WGS 84, the system used by GPS and Google Maps, which covers the whole world rather than a single region and is the one I was used to. I tried to write a function to transform a point from one system to the other, without success. A Quanam colleague found an approximate formula, but it assumes the Earth is a perfect sphere, and we know it isn’t.
I tried several approaches that didn’t work:
- Websites that do the conversion, called as a web service with Python libraries (http.client, urllib): they assumed I was a scraper and blocked me.
- Selenium, a tool for automating browser tasks, usually web application tests: the steps are recorded and the script is generated, but the output field has no store value, store text or store title method.
- BadBoy, an application with a purpose similar to Selenium: no luck there either.
- Several free APIs, which capped the number of requests. Messages like these were constant, and waiting between requests meant the process would never finish:
urllib.error.HTTPError: HTTP Error 429: Too Many Requests
or
<geonames><status message=”the hourly limit of 1000 credits for hectorcotelo has been exceeded. Please throttle your requests or use the commercial service.” value=”19″/></geonames>
In the end we went for the simple option I hadn’t thought of: a library, pyproj. It performs cartographic transformations between geographic coordinates (lat/lon) and map projections (x/y), and can also convert directly from one projected coordinate system to another. Coordinates can be passed as numpy arrays, Python arrays, lists and so on. Here is a code sample:
Growing the circles
With that solved, I could start on the solution.
We grow the circles…
A little more…
And a little bit more.
With circles of that size, three areas are still not blue. The spots are approximately (latitude, longitude) -31.798, -57.049 (in Paysandú, near Paso del Parque del Daymán), -30.886, -56.430 (in Salto, near the border with Artigas) and -32.220, -53.717 (in Cerro Largo, near the Yaguarón River).
That solution could be implemented. If a circle has its center at point C (a, b) and radius r, its standard equation is (x − a)² + (y − b)² = r². If we plug each point in Uruguay (x, y) into the equations of every school’s circle and (x − a)² + (y − b)² − r² is greater than 0 in all of them, the point lies outside every circle; if it is negative in at least one, the point is inside that school’s “radius of influence.”
Even though it can be done, it doesn’t lend itself well to computation. It is intuitively right, and fine if the goal is a visual solution, but it is the way a person would solve the problem, not the best way for a computer to solve it.
In upcoming articles I will keep untangling other ways to reach a solution. I won’t give up.
Héctor Cotelo
Business Analytics & Information Management consultant