For our first real geospatial analysis program, we are going to write a Python script that identifies neighboring countries. The basic concept is to extract the polygon or multipolygon for each country and see which other countries each polygon or multipolygon touches. For each country, we will display a list of other countries that border that country.
Let's start by creating the Python script. Create a new file named
borderingCountries.py and place it in the same directory as the
TM_WORLD_BORDERS-0.3.shp shapefile you downloaded earlier. Then enter the following into this file:
import osgeo.ogr import shapely.wkt def main(): shapefile = osgeo.ogr.Open("TM_WORLD_BORDERS-0.3.shp") layer = shapefile.GetLayer(0) ...