Monday, March 19, 2018

Geolocating INEC State Offices

Hello there,
Counting from today (19th of March, 2018), Nigeria: General Elections 2019 (Time until Saturday, 16 February 2019) is less than 360 days away.


In this post, I will share with you how you can Geocode and Map INEC State Offices. The final product will be useful for finding direction to the INEC State Offices using Google map navigation system.

The Data

The dataset is a CSV file that contains list of addresses of INEC State Offices. This list has no geographical information (as seen below), so I will be converting the address into geographical location in form of latitude and longitude (a process know as geocoding) after which I will map the addresses on an interactive map. With this map, you can find you direction to any of the addresses of INEC State Offices by panning and zooming around the map.

The Tools

I will use a python module to geocode the addresses and then use Google maps to map the addresses.

Let's get started...

Wednesday, March 14, 2018

Convert OpenStreetMap of Abuja in Raster to Vector map

Hello there,
In this post, I will demo how to easily download the vector map of the city center of Abuja, Nigeria. We will be converting and OpenStreetMap Raster map to Vector map suing QGIS and related plugins.

Simply download and install QGIS with the following plugins: OpenStreetMap Downloader and OpenLayer plugins

Steps to follow...

Step 1:
From QGIS menu go to Web >> OpenLayers plugin >> OpenStreetMap >> OpenStreetMap

When the OpenStreetMap layer finish loading, zoom to Abuja metropolis or and area you want to download it vector map.

Step 2: Next, go to: Vector >> OpenStreetMap >> Download Data

On the dialog box that opened, specify the extent and select where to save the .OSM file, then click on OK to run the process. This will download all points, line and polygon feature with the area selected.

Step 3: Load the .osm file into QGIS just as you will load any other vector file. The result is as seen below....

That is it!

Note: Another useful plugin to get information from OSM is the OSMInfo plugin.

Thursday, March 8, 2018

An Interactive Map of Nigeria showing State Governors

Hello there,

Here I present to you an Interactive Map of Nigeria that showcase the states and their Governors in picture (we can add more states' attribute if we want to).

Just point your mouse over any state to reveal the name of the state, the governor's name, the governor's picture etc.

The following interactive map presents a visualization of Nigeria's state governors. Governors are elected for a term of four years (maximum of two terms). A person shall be eligible for the office of Governor if he or she is a citizen of Nigeria by birth, at least 35 years of age, is a member of a political party and is sponsored by that political party. The constitution limits state governors to only two four-year terms in office.

Happy viewing....

Do you like the map you saw above? Would you like to have a similar map made for you? Give me a call or send me an email: +2348039508010 or

Tuesday, March 6, 2018

Distance Between Two Geographic Coordinates (Latitude and Longitude)

Using python to compute distance between two latitude and longitude points

Latitude and Longitude coordinates assume the earth is sphere in shape (i.e NOT flat). So, since the earth is a sphere, you can't use the distance formula that works for two points in a plane. Instead you will use a formular that approximates and assumes the earth is a sphere or ellipsoid namely:-
1- Vincenty formula
2- Haversine formula
3- Great-circle distance
4- Spherical Law of Cosines formula for distance calculations

Here are some Python based Geographic Coordinates distance calculations using Haversine formula and Vincenty formula.

First lets see how to calculate the distance between two point on a flat plane using the distance formula

In python it should look like this...

def distance(x1, y1, x2, y2):
     dist = ((x1 - x2)**2 + (y1-y2)**2)**.5 
     return dist

x1, y1, x2, y2 = 3.52, 7.02, 10.55, 9.60

print (distance(x1, y1, x2, y2)) # Result is 7.49meters

Friday, March 2, 2018

QGIS Multipart to Singleparts

Here we have an administrative polygon layer with series of Islands as seen below...

Now, we don't want the Islands to be part of the main polygon, so we have to split them apart. Thanks to the "Multipart to Singleparts" command tool under the "Vector >> Geometry Tools" menu. This command takes a vector layer with multipart geometries and generates a new one in which all geometries contain a single part. Features with multipart geometries are divided in as many different features as parts the geometry contain, and the same attributes are used for each of them.

Happy reading :)

Monday, February 26, 2018

Export Shapefile Attribute to Text File using PyQGIS

Hello there,

Lets use QGIS python scripting (PyQGIS) to create a text file of attributes from a shapefile.

Here I loaded onto QGIS a point layer representing capitals of Nigeria states with four attributes namely: "State_name", "Capital", "Area" and "Population".

Open the python console and with the point layer selected on the layers panel, enter the code below on the console to create an active layer object.
layer = iface.activeLayer()

Wednesday, February 14, 2018

Deploying python Flask web app on Amazon Lightsail

Hello there,

Let me start off this with this question "What is Flask and Amazon Lightsail"?

Chances are you already know what a Flask web framework is (a light weight python web framework), so I will only give a brief on Amazon Lightsail before we deploy a simple flask app on it.

Amazon Lightsail is a Virtual Private Servers (VPS) which enables you to easily set up servers on the Amazon Web Services (AWS) cloud. It is also a fast and simple VPS hosting service from Amazon.

Alternatives to Amazon Lightsail include Heroku, DigitalOcean, Linode, Microsoft Azure etc

How to deploy Flask applications on a Virtual Private Server (VPS) running Ubuntu OS, Apache server, and WSGI

Step 1: Use your existing Amazon account or sign up for a new to subscribe for a LightSail VPS service. You can get this for as low as $5 per month.

Step 2: By now you should have you flask app fully developed and ready for deployment. My app is a simple one that doesn't require a database, so I will skip and for of database configuration.

Wednesday, January 17, 2018

Identifying the type of shapefile in a given folder

Python script to separate shapefile based on type (Point, Line and Polygon)

A shapefile can be point, line or polygon. When in a folder there is no way to know what type it is until you opened it in a GIS environment such as QGIS or ArcGIS. That means you have to install a heavy duty software just to know what type of shapefiles you have in a given directory/folder. Also if you have thousands of shapefiles to identify their type, that will be a lot of work and also time consuming to access each shapefile via a GIS environment to identity its type.

Here is a smart solution in using Python that will allow you to determine the shapefile type in an efficient manner. Run the script once and it tells you what type of shapefile it is.

Lets get started...

The code

The script makes use of a python module called "PyShp". It reads the shapefiles contained in a given folder and classify them based on there type.

import glob
import shapefile
# Read in all the SHP and DBF files in the directory/folder
shp_files = glob.glob("path_to_folder\\foldername\\*.shp")
dbf_files = glob.glob("path_to_folder\\foldername\\*.dbf")

# loop through the files
for s, d in zip(shp_files, dbf_files):
    shp = open(s, 'rb')
    dbf = open(d, 'rb')
    complete_shp = shapefile.Reader(shp=shp, dbf=dbf)
    # Close the files for further usage..
    # Checking the type of shapefile (Point=1, Line=3 or Polygon=5/Polygonz=15)?
    #     complete_shp.shapes()[0].shapeType
    # --------------POINT SHP ----------------------
    if complete_shp.shapeType == shapefile.POINT:
        print ("This is a POINT Shapefile")

    # --------------LINE SHP ----------------------        
    if complete_shp.shapeType == shapefile.POLYLINE:
        print ("This is a LINE Shapefile")
    # --------------POLYGON SHP ----------------------        
    if complete_shp.shapeType == shapefile.POLYGONZ:
        print ("This is a POLYGON Shapefile")

First we import the glob module to read contents of a directory and the shapefile module to read shapefiles. Next, we loop through the files and use if conditional statement to check if the shapefile is a point, line or polygon type.

Note: you can perform additional actions after you have identified the type by adding the relevant code snippet just below the conditional statements.

The list above was extracted from the PyShp github repo. It shows that Shapefile types are represented by numbers between 0 and 31 as defined by the shapefile specification.

That is it!

The video shows demo of the script...

Sunday, December 31, 2017

UnicodeDecodeError while reading CSV into Pandas


A common error that frustrates a developer while reading CSV file with Pandas is this:-
UnicodeDecodeError Traceback (most recent call last)...UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 9: invalid start byte
The Cause

The usual cause is because the CSV has some hidden special characters and pandas is unable to detect the right encoding system to read the file correctly.

The Solution

I was able to over come this error most of the time by open the CSV file in a text editor such as Sublime Text and re-save it with and encode system.

In Sublime Text, go to: File >> Save with Encoding >> UTF-8 (chose the right encoding for the file in question.)

Doing this means you have defined the encoding system for the file. In other word, you re-defined the file's encoding system which will allow pandas read it correctly.

That is it.
Good Luck

Thursday, December 28, 2017

The way forward for Land Surveying Profession in Nigeria

The way forward for Land Surveying Profession in Nigeria

There is no doubt, Land Surveying profession has experienced huge changes due to advancement in technology. This has lead experts in the field to modify its curriculum and nomenclature to Geomatics Engineering, Surveying and Geoinformatics, Geomatics etc.

Here is an extract from NASGL WhatsApp Group - National Association of Surveying and Geo-Informatics Lecturers (December, 2017)

Happy reading!