Tuesday, November 1, 2016

GeoJSON in Python

Introduction


Few days ago, I wrote a blog post on using QGIS to convert a Shapefile to GeoJSON.

In this post, I will talk about working with GeoJSON object in Python programming language.

Basically we know a GeoJSON is an object contains spatially related datasets. Conventions used by GeoJSON are known to programmers, which include C, C++, Java, Python, Perl, etc.

GeoJSON has the benefit of having implementations in many languages (especially JavaScript), making it suitable for inter-application communication of spatial data.


GeoJSON in Python


There are many python libraries that allow you to load and manipulate JSON/GeoJSON objects within the python programming ecosystem. The common and default library is the JSON which comes by default with any python installation.

All you need to do to use it is to call this line "import json".

Code Demo


Am going to use a GeoJSON file containing polygon features and saved with the name "Polygon_3.geojson" on my local disk for this demonstration.



There are two most important functions in the json library for converting python objects to json/geojson or vise-versa named: dumps() and load() functions.

In this demo, I will just read in the geojson file above into the python development environment using the with open() python command. This will allow us have access to the individual polygon elements within the geojson file as follow:-


.As you can see from above, when you load a GeoJSON file using the JSON library, you get a dictionary that contains an entry features, which contains the list of features.

Each feature in turn consists of a dictionary, which among other things, contains an entry geometry.

The geometry is a dictionary containing the entries type (polygon in this case) and coordinates of the vertices. So you can traverse the GeoJSON file like this:

import json

with open('Polygon_3.geojson') as f:
    data = json.load(f)

for feature in data['features']:
    print (feature['geometry']['type'])
    print (feature['geometry']['coordinates'])



As you can see, the GeoJSON file contains five polygons of different number of vertices and shape as indicated by the amount of pairs of coordinates for each polygon.

That is it.
Thanks for reading.

Tuesday, October 25, 2016

Modern Database Models and Technologies

A data models define how the logical structure of a database is modeled. Data Models are fundamental entities to introduce abstraction in a Database Management Systems
 (DBMS). According to WikiPedia, a database model is a type of data model that determines the logical structure of a database and fundamentally determines in which manner data can be stored, organized, and manipulated. The most popular example of a database model is the relational model, which uses a table-based format.

(Source: WikiPedia)

Thursday, October 20, 2016

Background Maps in QGIS

Hi there,

For any GIS task, you need a Basemap to kick start the GIS project. Basemaps are often presented through different GIServices in the form of: TMS, WMS, WMTS, ESRI ArcGIS Services or simply as XYZ tiles.

QGIS has different tools for adding basemaps.



If you need a background map tiles for your QGIS projects, then the two awesome options I do recommend you checkout are OpenLayer and QuickMapServices.

With any of the above plugins, you can import background basemaps in the form of satellite images, line maps, topographic maps, landsat images or combination of the two (hybrid maps) from any of the service providers listed below into your project:-

1) Google
2) Bing
3) OSM - Open Street Map
4) ESRI - ArcGIS Online
5) eAtlas
6) Yandex
7) NASA
8) CartoDB
9) MapQuest
10) MapBox
11) 2gis
12) MapSurfer
13) Apple Maps

You can find all these under QGIS web menu.




Happy mapping.

Monday, October 17, 2016

The new GUI Interface for PostgreSQL - pgAdmin4

Hello,

The eagerly awaited new version of pgAdmin 4 is finally here with features that are more flexible and reliable than its predecessor (pgAdmin 3).

pgAdmin is the leading graphical Open Source management, development and administration tool for PostgreSQL object relational database management system.

For a very long time pgAdmin has been on version 3 (pgAdminlll). However, just of recent (in October, 2016) it was upgraded to version 4 (pgAdmin4).

This new application (pgAdmin4) is designed for operation on both the desktop and a webserver. It is written in Python using the Flask framework for the backend, and Javascript/jQuery/Backbone for the frontend.

The core functionality of pgAdmin4 remains similar to pgAdmin3, but there are a number of changes made. Read more on Dave's blog.

To learn step-by-step how to install pgAdmin4 and also install PostGIS on it, check this link here.


Saturday, October 15, 2016

Number of rows in an SQLite database with python

Hello there,

In this post, I will demonstrate a common issue of concern when working with an SQLite database in python.

As you already know, SQLite database is powered in python with a default module name: sqlite3. You don't need to install this as it already comes with every python installations since python version 2.5.

To use SQLite database in python there are basically six steps to take as follow:-

Step 1: Import sqlite3 module
Step 2: Connect to a database
Step 3: Create a cursor object
Step 4: Write an SQL query
Step 5: Commit changes
Step 6: Close database connection

Getting to know the number of rows in an SQLite database with python


This could be easily determined by use any SQLite GUI browser like SQLiteStudio, DB Browser, Navicat or SQLite Firefox browser add-on. But there is drawback in using the GUI, as you have to install a tool on your working machine and it the dataset within the database in big, it will take some effort to scroll down the table just to know the number of rows in the table.

If you already working in python, this can easily be done with few lines of code as follow.



As you can see above, my database "flights.db" has 6048 rows in its "airlines" table.

You will need to change step 2 and 4 above to match your database name and table name respectively.


Thanks for reading.

Wednesday, October 12, 2016

Difference Between Spatial Database And Non-Spatial Database

Basically, a database consists of an organized collection of data for one or more uses, typically in digital form.

Spatial Database: has the ability to store and access both Location/Spatial Information and Attributes/Non-Spatial Information.

Non-Spatial Database: has the ability to store and access only Attributes/Non-Spatial Information.

The info-graphics below shows the difference in the concepts of Spatial and Non-Spatial databases.



Basically, the differences are in the storage, function, and query capabilities between the two. The notable differences are listed below;-

a)  A spatial database supports special data types for geometric objects and allows you to store geometric data (usually of a geographic nature) in tables while a non-spatial database doesn't support such.

b)  A spatial database provides special functions and indexes for querying and manipulating geospatial data using something like Structured Query Language (SQL) while non-spatial database doesn't provide such functions and indexes.

c)  A spatial database is often used as a storage container for geospatial data, but it can do much more than that. While non-spatial database is often used as a storage container for non-spatial data.

d)  A spatial database uses spatial query in geometric functions to answer questions about space and objects in space. While non-spatial database don't support spatial queries.

e)  In addition to being able to answer questions about the use of space, spatial database functions allow you to create and modify objects in space. This portion of spatial analysis is often referred to as geometric or spatial processing.

f)  A spatially enabled database can intrinsically work with data types like rivers (modeled as linestrings), land parcels (modeled as polygons), and trees (modeled as points). While non-spatial database can’t work with these forms of models.


Examples of Spatial and Non-spatial databases


There are several examples of Spatial and Non-spatial database servers available, but the top common once include:
a) SpatiaLite - SQLite
b) Oracle Spatial/Locator - Oracle
c) MySQL Spatial - MySQL
d) PostGIS - PostgreSQL
e) DB2 Spatial - IBM DB2/Informix
f) MsSQL Spatial - MsSQL server
g) Spatial Hadoop - Hadoop
h) MS Access - this is Non-spatial database, it has no support for Spatial database at the time of writing.

Note that Spatial databases are actually Non-spatial databases with spatial extension added onto them (I hope this makes sense). Anyway, study the pictures below, the first contains Non-spatial databases. After extending them with location/spatial capabilities, they then become Spatial databases.

Before getting location/spatial capabilities




After getting location/spatial capabilities



Thanks for reading.

Monday, October 3, 2016

Working with Spatial Database in QGIS and Python Part 1


Hello,
I will explain how to get up and running with PostGIS Database in QGIS and Python.

This blog post will be in series of three parts. This first part will give an overview and installation steps of PostGIS on the local windows machine. Then Part 2 and Part 3 will discuss on working with the PostGIS database server/engine in QGIS and Python respectively.

Lets get started...


Tuesday, September 27, 2016

Converting Shapefile to GeoJSON


Introduction


If you are like me that often produce web based maps often, you will definitely come across situations were you have to convert the most popular geospatial file format (Shapefile) into the most popular web programming language file format - JSON (thta is: GeoJSON in our case since we care more about location elements).

Before I gone to show how you will convert a shapefile to a GeoJSON, let me briefly explain what a JSON file format is and why it is important to us.


What is JSON?


JSON is a short hand for "JavaScript Object Notation". Though JSON has the name JavaScript it can be used in all different languages since most languages have libraries to parse the JSON data.

JSON is a minimal, readable format for structuring data. It is used primarily to transmit data between a server and web application, as an alternative to XML. It is a lightweight format that is used for data interchanging. It is based on a subset of JavaScript language (the way objects are built in JavaScript). It is a much-more compact way of transmitting sets of data across network connections as compared to XML.

JSON values can consist of: objects (collections of name-value pairs) arrays (ordered lists of values) strings (in double quotes) numbers true, false, or null. JSON is language independent.


What are JSON files?


JSON files are the files which contain JSON data. They are saved with a file extension:  .json

Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. For instance, in Python programming the awareness of the built-in data structures such as Tuple, List, and Dictionary will help you understand JSON/GeoJSON object.



What does it look like?


A simple json structure may look like this:-

"author":
{
"name" : "Umar Yusuf",
"age" : "23",
"gender" : "male",
"url" : "http://www.UmarYusuf.com"
}

Storing JSON: data as a simple example, information about me might be written in JSON as above.

Storing JSON Data in Arrays: a slightly more complicated example involves storing information about two people. To do this, we enclose multiple objects in square brackets, which signifies an array. For instance, if I needed to include information about myself and my brother, it will look like this below:-

[{
"name" : "Umar Yusuf",
"age" : "23",
"gender" : "male"
"url" : "http://www.UmarYusuf.com"
},
{
"name" : "Musa Kallamu",
"age" : "37",
"gender" : "male"
"url" : "http://www.mk.com"
}, ...]


Nesting JSON Data: Another way to store information about multiple people would be to nest objects. To do this, we would create something similar to the following:

{
"Umar":{
"name" : "Umar Yusuf",
"age" : "23",
"gender" : "male"
"url" : "http://www.UmarYusuf.com"
},

"Musa":{
"name" : "Musa Kallamu",
"age" : "37",
"gender" : "male"
"url" : "http://www.mk.com"
}, ...

}


Uses of JSON


~ It is used while writing JavaScript based applications that includes browser extensions and websites.
~ JSON format is used for serializing and transmitting structured data over network connection.
~ It is primarily used to transmit data between a server and web applications.
~ Web services and APIs use JSON format to provide public data.
~ It can be used with modern programming languages.

Saturday, September 24, 2016

Spatial Analysis of Nigeria 2015 Presidential Election Result Using Tableau

Introduction


This is another spatial version of analysis on the "2015 Nigerian Presidential Election Result using python programming language".

Unlike the way we performed the spatial in QGIS with two data sets (Shapefile and CSV files), using Tableau the approach is different.

Spatial and Non-spatial (Attribute) Datasets


With Tableau, you just need to connect to an attribute dataset and the spatial information is generated on the fly as long as it contains some fields/columns identify by Tableau as have geographic role.

The attribute dataset of the 2015 presidential election result from INEC website. Convert is to data source format recognized by Tableau (e.g: csv, excel, database, sql, sas etc). In this case, am converting it to a csv file format.


Friday, September 23, 2016

Spatial Analysis of Nigeria 2015 Presidential Election Result Using QGIS


Introduction


This is the spatial version of analysis on the "2015 Nigerian Presidential Election Result using python programming language".

In this spatial analysis, I will analyze and visualize the “Presidential Election Result” dataset on a map of Nigeria showing individual states boundary. Basically that is what spatial analysis is all about.



Let’s get started…