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..
    shp.close()
    dbf.close()
    
    # 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

Hello,

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!

-------------------------------

Friday, December 22, 2017

Enable Legend window on QGIS Map Canvas

Hello there,
After styling your map analysis, you will want to add a legend or key to better communicate what is on the map to the map user/reader.

In QGIS, this is usually done through the "Print Composer". While this is excellent, the "Print Composer" dialog isn't an interactive mapping evironment. It is meant for final preparation of a map for printing or sharing in form of image/SVG/PDF.

In this post, we want an interactive legend layer separate from the "Layers Panel" on the map canvas so we can control map interactively to reveal some patterns with interfering with the main project "Layers Panel".

I will show you how to eanble the Legend dialog box using the python console with few lines of PyQGIS code.

Let get started...

Legend on QGIS Map Canvas
Start the "python console" enter the following codes...

from qgis.gui import *
root = QgsProject.instance().layerTreeRoot()
model = QgsLayerTreeModel(root)
view = QgsLayerTreeView()
view.setModel(model)
view.show()



This will start a GUI map Legend containing all the layer in the "Layers Panel". This especially good for interacting with the map canvas without obstructions from other setting on the "Layers Panel" (most especially if the map has large number of layers).

You can save the code as a .py file and run it anytime you want to start the Legend GUI window.

That is it!
Thanks for reading...



Wednesday, December 20, 2017

Creating 3D buildings model in QGIS

Creating 3D buildings model in QGIS

Hello there,
An easy way to wow your GIS client is to display his building mapping project modeled in 3D :).

Ok, on a more serious note, there are many reasons why you will model a map in 3D. In this article, an going to guide you on how to do it in QGIS software.

Let's get started..



Step 1:
Install the Qgis2threejs plugin through menu plugin manager (Plugins >> Manage and install plugins...). This plugin exports terrain data, map canvas image and vector data to your web browser. It allows you to view 3D objects in the web browser (You need a web browser which supports WebGL (Web Graphics Library)).


Step 2:
After isnalling the plugin, you can access it on the menu Web >> Qgis2threejs >> Qgis2threejs

Make sure you have you build layer loaded with attribute column for the builds height.






Step 3:
From the Qgis2threejs dialog window, select the layer you want to display, the building height attribute column, select a location to save the generated .html file and click on "Run" button.



That is it! The generated 3D model should now open on you default browser (as seen in the first image above) and you can always come back to manipulate other setting to get what you want.

Note: Your project coordinate reference system (CRS) should be in meters or the same unit of your building heights to avoid get a funny result.

thanks for reading.

Thursday, December 14, 2017

Switching to Monokai Color Scheme on SublimeText and RStudio


My Favorite IDE Color Scheme is: Monokai. The best color scheme, syntax highlighting and user interface theme for Sublime Text and Visual Studio Code, from the author of the original Monokai color scheme.

In this post, I will show you how to switch to this theme in both SublimeText and RStudio.


SublimeText: Go to Preferences >> Color Scheme and select Monokai





RStudio: Go to Tools >> Global Options >> Appearance >> Editor Theme and select Monokai


That is it!
Enjoy this beautiful  IDE Color Scheme.

Monday, December 11, 2017

Executing Python Script in RStudio - R + Python in one IDE

Hello there,

Without any doubt what-so-ever, the two most powerful data science programming tools are Python and R. How about if you could run these two in on Integrated Development Environment (IDE)?

That is what this post is all about. I will show you how you could run Python scripts right inside of a popular R environment.

R + Python in one IDE

A quick solution to this lies in RStudio. With the latest version of RStudio, you can use RStudio as a Python REPL. But first you need to download and install Python on your machine just the way you would do R.

RStudio is a free and open-source integrated development environment for R, a programming language for statistical computing and graphics. RStudio was founded by JJ Allaire, creator of the programming language ColdFusion (Source: WikiPedia).



To run R script in RStudio:
Because RStudio was originally made for R, it is fairly striaght forward to run R scripts. Just got to the "Comprehensive R Archive Network" (CRAN) web page to download R. Then also download and install RStudio.


To run Python script in RStudio:
To run Python in the same RStudio environment, go to the official Python web page and download it. During the installation, make sure that it is added to your system "Environment Variable" so that RStudio terminal could recognize it without you calling the full PATH all the time.

With the above in place, you can now write your python script in the RStudio "Script Pane" and save it with .py extension. Then run it and see the output on the "Console Pane".


As seen above, I import 'pandas' module and created a dataframe in the "Script Pane" and the result is displayed on the "Console Pane".

You can also use the interactive terminal to run python scripts by going to: Tools >> Terminal >> New Terminal. From the new terminal window that open, you can call the python interpreter and enter any python command just as you would do on cmd or python IDLE.



That is it!

Note: Apart fro R and Python, RStudio supports other programming languages such as JavaScript, SQL, etc.

Saturday, December 2, 2017

Downloading High Resolution Satellite Image Using Terra Incognita Software


Terra Incognita is a GPS mapping and management software that is available for download here. It is a program for downloading web source maps or local files maps for various programs or GPS devices.

In this post, I will show you how to use it to download High Resolution Satellite Images.



Monday, November 27, 2017

Bulk Install Python Libraries with a Single Command

Hello there,
Assuming you have a situation where you needed to install numerous library dependency on your python environment to be able to run an app. You can install the libraries one after the other using: pip install library_name.

However, where you where you have many libraries to install, then a handy command will be to run it once and installed all the dependency libraries without repeating same command/process over and over again.

Here I needed to install about 20 libraries so instead of doing one thing 20 times, i will use thais command to install all sequentially: pip install -r requirements.txt

How it works
First create a text file name it requirements.txt and type all the libraries with there versions as seen below...


Next, open cmd/terminal and run pip install -r requirements.txt

This will install all the libraries as typed in the requirements.txt text file.




That is it!