Tuesday, May 22, 2018

Jupyter Notebook Python user input() function


Jupyter notebook has added a feature I have always wished it had long ago. Before now, writing a script that accepts input from user with the input() function in python3 or raw_ input() function in python2 was almost impossible and you have to switch between the terminal window and the jupyter notebook all the time.

The newer version of jupyter notebook has made available a text-box right inside the browser whenever you call the input() function as seen below;-




Enjoy the new GUI feature of the jupyter notebook.







Wednesday, April 11, 2018

Scrape data from Google maps

Scrapping data from Google maps made by third party

Often, you will find Google maps published by someone you don't have access to and you really want to extract some data from the map.

Lets consider this map below, assuming it was published by someone you don't know and you have to have access to the raw data behind the map.



When you click on individual features on the map, the required data of that point is displayed on the left side panel. Now, the task is to extract or collect those data for every point feature on the map into a spreadsheet for further use/consumption.




Step to extract the data

Here is how to go about extracting such dataset.

Step 1: On the main page, click on the three vertical dots to expand the menu


Step 2: Select "Download KML" to download a file that can be read by Google Earth software, text editor and other GIS software.




Step 3: Open this downloaded KML file in any software of your choice to access the data contained there in. In my case, I used QGIS to open the kml file and exported the content into a CSV/Excel spreadsheet.

That is it!
Now if you come across any Google maps that you want to access the data used in preparing the map, that is how to go about accessing it. Enjoy!

Sunday, April 8, 2018

GPS coordinates of INEC Offices across the country

Find GPS coordinates of INEC Offices in Nigeria

Few days ago, I wrote on finding addresses of INEC states' offices. I even showed the locations of each INEC state headquarters on Google maps.

On this post, you will find list of Latitude and Longitude GPS coordinates of INEC offices across the country. There is at least on major office in each state of the federation, here is the list below:-

STATE
Name
Latitude
Longitude
Abia
INEC Abia Hqtr, Umuahia
5.525982
7.513276
Adamawa
INEC Adamawa Hqtr, Jimeta
9.28333
12.46667
Akwa-Ibom
INEC Akwa-Ibom Hqtr, Calabar
4.9988714
8.334555
Anambra
INEC Anambara Hqtr, Awka
6.234931
7.080384
Bauchi
INEC Bauchi Hqtr, Bauchi
10.31689
9.8296
Benue
INEC Benue Hqtr, Makurdi
7.709681
8.527267
Borno
INEC Borno Hqtr, Maiduguri
11.8436599
13.0861767
Bayelsa
INEC Bayelsa Hqtr, Yenagoa
4.9378742
6.2714292
Cross-River
INEC Cross-River Hqtr, Calabar
5.011425
8.329815
Delta
INEC Delta Hqtr, Asaba
6.218859
6.683576
Ebonyi
INEC Ebonyi Hqtr, Abakaliki
6.3267112
8.1099312
Edo
INEC Edo Hqtr, Benin City
6.3496306
5.6609366
Ekiti
INEC Ekiti Hqtr, Ekiti
7.63232
5.21787
Enugu
INEC Enugu Hqtr, Enugu
6.4562096
7.5294245
FCT
INEC Nigeria Hqtr, Abuja
9.039736
7.4846524
Gombe
INEC Gombe Hqtr, Gombe
10.290956
11.133903
Imo
INEC Imo Hqtr, Owerri
5.476379
7.010559
Jigawa
INEC Jigawa Hqtr, Dutse
11.71923
9.35991
Kebbi
INEC Kebbi Hqtr, Birnin Kebbi
12.4527
4.20065
Kaduna
INEC Kaduna Hqtr, Kaduna
10.5478485
7.4566833
Kogi
INEC Kogi Hqtr, Lokoja
7.80137152
6.746167975
Kano
INEC Kano Hqtr, Kano
12.027758
8.522139
Katsina
INEC Katsina Hqtr, Katsina
12.991684
7.638025
Kwara
INEC Kwara Hqtr, Ilorin
8.4801977
4.4988242
Lagos
INEC Lagos Hqtr, Lagos
6.5089256
3.3801858
Niger
INEC Niger Hqtr, Minna
9.615976
6.5656279
Nassarawa
INEC Nasarawa Hqtr, Lafia
8.53333
8.55
Ondo
INEC Ondo Hqtr, Alagbaka
7.24653
5.217
Ogun
INEC Ogun Hqtr, Abeokuta
7.1259889
3.3588871
Osun
INEC Osun Hqtr, Osogbo
7.791994
4.54179
Oyo
INEC Oyo Hqtr, Ibadan
7.398063938
3.978493573
Plateau
INEC Plateau Hqtr, Jos
9.8629268
8.8662419
Rivers
INEC Rivers Hqtr, Port Harcourt
4.817084248
7.009455249
Sokoto
INEC Sokoto Hqtr, Sokoto
13.06092
5.23902
Taraba
INEC Taraba Hqtr, Jalingo
8.879033
11.385662
Yobe
INEC Yobe Hqtr, Damaturu
11.7444
11.96255
Zamfara
INEC Zamfara Hqtr, Gusau
12.190819
6.661709

If you find any inconsistent or missing data, please let me know to correct immediately at: umaryusuf49 (at) gmail.com.


Thank you for following.

Friday, April 6, 2018

How to setup MySQL Database on PythonAnyWhere.com

Hello there,
PythonAnyWhere.com is one of the platforms that allows you to easily host scripts written in python. In this post, I will show how to setup a database server on the platform and load in data from a simple python script.

There are several databases supported on the PythonAnyWhere.com platform. the common once are:-
1- SQLite (a server less database that requires not setup at all)
2- MySQL (a server based database available for free account holders)
3- PostgreSQL (a server based database available for paid account holders)

I will be using the MySQL database for this demonstration as it is the one that requires setup and is available to every on account (free and paid accounts).

Step 1:
If you haven't already, create a free account on PythonAnyWhere.com


Step 2:
Login to your account dashboard and go to the "Databases" tab. Then Create a database by specifying the Database name and MySQL password




Step 3:
After creating a Database name and MySQL password, you should have a MySQL Console added to you list of consoles under the console tab. The name will look like this: MySQL: username$dbname



Open it and you should have your MySQL console running just like mine...



Step 4:
Now, you can enter MySQL commands to interact with your MySQL database via the console. For example, lets display the databases and tables in our account by entering: show databases; and show tables;



Step 5:
To create a table, enter this query....
CREATE TABLE test_table (
id int,
member_id int,
status varchar(255)
);
The above query creates a new table named "test_table" with three fields namely; 'id', 'member_id' and 'status'. The 'id' and 'member_id' fields are integer fields while 'status' is a varchar.



Connecting your app/script:
To connect your web application or any python .py script to the database, you simply
host='username.mysql.pythonanywhere-services.com'
user='username'
pass='password'
database='username$databasename'
For example if you are using the MySQLdb module, you connection will look like this:-
conn = MySQLdb.connect(
    host='username.mysql.pythonanywhere-services.com',
    user='username',
    passwd='pass123',
    db='username$your_db_name')


That is it!
You have learned how to create a MySQL database on PythonAnyWhere.

Note: To create another database, just repeat step 2 above. Also, note that whatever database name and password you specified at that point are the credentials you will use if you a connecting to the database from an external script such as python .py script.

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.

 INEC SCHEDULES 2019 GENERAL ELECTIONS FOR SATURDAY 16TH FEBRUARY AND SATURDAY 2ND MARCH 2019


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 finished loading, zoom to Abuja metropolis or any area you want to download its 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 umaryusuf49@gmail.com

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
            Distance=(x2x1)2+(y2y1)2



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