GIS & Pokémon : Programatically search for rare Pokémon with R

Programatically search for Pokemon via R

Hey fellow trainers!

Although the Pokémon hype is slowly fading, I would like to show you, how you can programatically search for rare Pokémon with R. The final script will be able to either take coordinates or an adress and look for Pokémon within a given radius. Let’s start.

Step 1: Send a query via curl

To do a Pokémon search programatically we first need an API that allows us to do that. During my research on the web, I found the webpage which allows you to click on a map and within a couple of seconds all the Pokémon in an approximate radius of 40m are displayed. The neat thing is that they also have an API which enables you to do this programatically by using curl. You can read more about this topic on reddit. This is also where I got the curl query from:

curl '' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Origin:' -H 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36' --compressed

So what this query does, is to take a coordinate (lat, long) and returns all the Pokémon within an approximate radius of 40m. The final result is a  JSON which looks for example like this (when you plug it into your console you might get a different result depending on the time):


You can see the coordinates of the detected Pokémon, their names (RATTATA and PIDGEY) and a couple of other parameters.

Step 2: Wrap it up in R

Query with coordinates

R allows you to execute these queries from within a script. You can do this in many ways, I prefer the following implementation using system(). With this function you can invoke OS commands like this:

#Define Coordinates
x = 51.53054691352413
y = -0.08174179999999999

#Send query and store result in variable res
res = system(paste0("curl '",x,"&lng=",y,"' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Origin:' -H 'User-Agent: Mozilla/5.0' --compressed"), 

In the code above I already defined two variables where the coordinates are stored and later plugged into your query statement invoked by system(). By setting the argument intern=T the result from the query is stored in a user-defined R variable. This is how the result looks like in R:

[1] "{\"result\":[{\"spawn_point_id\":\"48761cbc919\",\"encounter_id\":\"17122260820454702845\",\"pokemon_id\":\"RATTATA\",\"latitude\":51.53169635521043,\"longitude\":-0.0801630351728887,\"expiration_timestamp_ms\":\"1474705518404\"},{\"spawn_point_id\":\"48761cbc9bb\",\"encounter_id\":\"13826549414915815469\",\"pokemon_id\":\"RATTATA\",\"latitude\":51.53120337908847,\"longitude\":-0.08025346621673418,\"expiration_timestamp_ms\":\"1474705359884\"}]}"

Query by place name (geocoding)

In most cases you don’t know the exact coordinates of a place. This is why I am going to show you how you can use the query above with place names. For this, we will be using the package {dismo}:

This package uses geocoding to get coordinates from a given address. For example:

#load library
#get coordintes of the street 'Lincoln Ave in Ames Iowa'
coords = geocode("Lincoln Ave, Ames Iowa)

This is how the result looks like:

> coords
           originalPlace           interpretedPlace longitude latitude      xmin     xmax     ymin     ymax uncertainty
1 Lincoln Ave, Ames Iowa Lincoln Way, Ames, IA, USA -93.65478 42.02272 -93.70192 -93.6084 42.01998 42.02479        3849

By using coords$longitude and coords$latitude you can extract the corresponding coordinates from the location and plug it into our query from above:

x = coords$longitude
y = coords$latitude

res = system(paste0("curl '",x,"&lng=",y,"' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Origin:' -H 'User-Agent: Mozilla/5.0' --compressed"), intern=T)

In this case however, I got an empty result. Apparently there were no Pokémon at this location at the time of query:


Taking it to the next level

So what else is possible? We could wrap the single queries up in a loop to automatically search within an entire city or neighbourhood. Other possibilities include a notification script that sends you an email when a Pokémon is detected. Or if you really want to push it, scan your city over and over again to detect spawning patterns and nests. The possibilities are endless!
If you liked this short tutorial, I will be glad to show you more. Just leave me a comment below.

Cheers Martin (Team Valor)


About This Author

Martin was born in Czech Republic and studied at the University of Natural Resources and Life Sciences, Vienna. He is currently working at GeoVille - an Earth Observation Company based in Austria, specialised in Land Monitoring. His main interests are: Open-source applications like R, (geospatial) statistics and data-management, web-mapping and visualization. He loves travelling, geocaching, photography and sports.

Post A Reply