THIS BLOGPOST AND THE SCRIPTS ON GITLAB WERE UPDATED ON DECEMBER 29, 2020, BECAUSE THE OLD API DIDN’T WORK ANYMORE. FROM NOW ON AN OFFICIAL GOOGLE API CAN BE USED. CHANGES WERE MARKED IN RED ON DECEMBER 29, 2020.
In the past I have created a map of Antwerp, Kontich and Edegem that shows when the most recent Google Streetview images were taken. To do so I use a MySQL database and some PHP scripts. I created multiple blog posts on how I make these maps:
- A map with the most recent Streetview capture dates per street: how to get started!
- A map with the most recent Streetview capture dates per street: the PHP and SQL scripts I use
As a comment on the Dutch version of that last blog post, I got the question if it was possible to do the same with a GeoJSON or a Geopackage file and something else than PHP. So I translated my MySQL – PHP combo to GeoJSON and Python 3.
The scripts and an example dataset I made can be found on GitLab. You can download all files using this link.The batch file is specific for use in Windows, but the GeoJSON and Python can be used in any operating system.
To use these scripts you’ll need to have Python installed on your system. I tested this script with Python 3.7.0 that was installed using OSGeo4W, but it probably also works in other versions of Python 3.
As you can see in the Windows Batch file, I used the Python 3 from QGIS that was installed using OSGeo4W. How you do that, I previously explained in my blog post about how to copy WFS data to PostGIS using GDAL/OGR. Python is installed in exactly the same way as I installed GDAL in that blog post. If you installed QGIS 3.x, you probably already have almost all the components you’ll need. You only have to add the Fiona Python 3 package. How you can do that, can be read in this blog post.
You can also use any other Python installation on your computer, but you have to change the content of the batch file and you have to make sure you also have the following Python packages installed:
fiona (for Python 3)- json
- urllib3
- python3-lxml
In this example I use Python 3 from my 64bit OSgeo4W QGIS installation. That is located at C:\OSGeo4W64\bin\python3.exe
. If you use Python 3 from another location, you have to change the location in the check.bat file and also update the PYTHONHOME setting.
Because a Google API key is personal and linked to a credit card, I replaced my API key by MyApiKey, but if you want to use these scripts, you’ll have to replace that with your own API key.
The Python script was written to use the “testdata.geojson” file that contains all the points you want to be checked by the script. The GeoJSON file needs to contain the following fields:
- fid: a unique number per point
- xgoogle: the x coordinate where the Google API finds a Streetview image (this can be left empty, the script will fill this field)
- ygoogle: the y coordinate where the Google API finds a Streetview image (this can be left empty, the script will fill this field)
- lastcheck: the last date this point was checked. If the point never was checked, the value should be 0! Don’t leave it empty. The value in this field has the form YearMonthDay.
- datestreetview: the moment the image in Google Streetview was taken (this can be left empty, the script will fill this field).
- municipality: the municipality the point is in (this is an optional field).
The Gitlab repository also contains a second geojson file that contains only 1 point (to maintain the fields in the file) that is a template for anybody to use. You can add your own points to that file and remove that 1 point that I added before. After that, change the name of the geojson file to be checked in check.py to the filename of your file or rename your file to testdata.geojson.
After that is done, you can just run check.bat and it will check 10 points for you. You can change that number on the line set checks=10
in check.bat. If you run check.bat from the command line, you can see which points have been checked and what the results are. If you want you can use Windows Task Scheduler to run the batch file so you can automate the checking. In the past I wrote a blog post about how to do this for my WFS backup scripts. You can also use that for these scripts here.
How the scripts work, is explained in the comments in the files. So I hope that will help to understand what the scripts do.