General functions

Create .poly files

scripts.functions.create_poly_files(base_path, global_shape, save_shapefile=True)[source]

This function will create the .poly files from the world shapefile. These .poly files are used to extract data from the openstreetmap files.

This function is adapted from the Export OSM Poly function in QGIS.

Args:

base_path : The base path to location of all files.

global_shape : The exact path to the global shapefile used to create the poly files.

save_shape_file : When set to True, the new shapefile with the countries that we include in this analysis will be saved.

Returns:
A .poly file for each country in the ‘poly_files’ directory in the global working directory.

Do a spatial overlay

scripts.functions.spatial_overlays(df1, df2, how='intersection')[source]

Compute overlay intersection of two GeoPandasDataFrames df1 and df2. This function is much faster compared to the original geopandas overlay method.

Adapted from https://github.com/geopandas/geopandas/issues/400

Args:

df1 : The dataframe to be either intersected or ‘erased’, using the difference function.

df2 : The dataframe to intersect or to erase, using the difference function.

Return:
df1: an either intersected or (partly) erased geopandas dataframe.

Get country file

scripts.functions.get_country(country, continent_osm, base_path, overwrite=False, RAI=False)[source]

Extraction of the road data for the specified country from OSM. We use the continental OSM file, downloaded from http://download.geofabrik.de.

Args:

country : The country for which we calculate the RAI.

continent_osm : The continent the country ‘belongs’ to. This is required for the osm extraction.

base_path : The base path to location of all files and scripts.

overwrite : This is set on True by default, set on False if you are sure the input files are correct (it will save a lot of computation time).

RAI : This is set on False by default. set on True if this country extracting is used for the RAI analysis. It will skip the road length calculation (saving computation time).

Returns:
load_country : A geodataframe containing all the roads and their length.

Save a map of the roads of a country

scripts.functions.create_figure(country, load_country, base_path, report=False)[source]

Create a figure of the road network

Args:

country : The ISO-code of the country.

load_country : The geodataframe of the country.

base_path : The base path to the location of all files and scripts.

report : Set to False by default. When set to True, the figure will not be saved with a figure title.

Returns:
A .png file with a classified road system of the specified country.

Estimate length of line

scripts.functions.line_length(line, ellipsoid='WGS-84')[source]

Length of a line in meters, given in geographic coordinates.

Adapted from https://gis.stackexchange.com/questions/4022/looking-for-a-pythonic-way-to-calculate-the-length-of-a-wkt-linestring#answer-115285

Args:

line : A shapely LineString object with WGS-84 coordinates.

ellipsoid : The string name of an ellipsoid that geopy understands (see http://geopy.readthedocs.io/en/latest/#module-geopy.distance).

Returns:
The length of the line in meters.

Map to aggregate set of roads

scripts.functions.map_roads(load_country)[source]

To create a new column with an aggregated list of road types.

Args:
load_country : A geodataframe containing all the roads of a country.
Returns:
load_country : The same geodataframe but with an additional ‘roads’ column containing the aggregated road types.

Extract country from Openstreetmap

scripts.functions.extract_osm(country_shp, country_pbf)[source]

Extract a shapefile with all the road information from the openstreetmap file.

Args:

country_shp : The path string indicating the final output directory and output name of the new shapefile.

country_pbf : The path string indicating the directory and name of the .osm.pbf file.

Returns:
A shapefile with all the roads of the clipped country. The shapefile will be in WGS84 (epsg:4326). This is the same coordinate system as Openstreetmap.

Clip country in Openstreetmap

scripts.functions.clip_osm(continent_osm, country_poly, country_pbf)[source]

Clip the country osm file from the larger continent (or planet) file and save to a new osm.pbf file. This is much faster compared to clipping the osm.pbf file while extracting through ogr2ogr.

This function uses the osmconvert tool, which can be found at http://wiki.openstreetmap.org/wiki/Osmconvert.

Either add the directory where this executable is located to your environmental variables or just put it in the scripts directory.

Args:

continent_osm : path string to the osm.pbf file of the continent associated with the country.

country_poly : path string to the .poly file, made through the ‘create_poly_files’ function.

country_pbf : path string indicating the final output dir and output name of the new .osm.pbf file.

Returns:
A clipped .osm.pbf file.

Unzip worldpop data from 7zip file

scripts.functions.unzip_worldpop(country, base_path, temp_path)[source]

Function to unzip the worldpop files for the following islands (not included in the main worldpop file):

  • Fiji
  • Kiribati
  • Marshall Islands
  • Micronesia (Federated States of)
  • Palau
  • Papua New Guinea
  • Samoa
  • Solomon Islands
  • Tonga
  • Vanuatu

This should work out of the box if 7zip is installed.

If not: the easiest way to get this to run, is to move the 7z.exe into the scripts directory. The other option would be to add the 7zip directory to your environmental variables.

Args:

country : The ISO-code of the country.

base_path : The base path to the location of all files and scripts.

temp_path : The path to which we temporarily write the unzipped file.

Returns:
An unzipped worldpop GeoTIFF file for the specified country.