Sunday, October 21, 2018

Accessing JSON nested object with python

This blog post highlights the key components to look at when parsing a JSON file with deep level of nested objects and variables.

First thing first, is to load in the file using: with statement...

The nested/child objects can then be accessed like this...
obj1['obj1a']['obj1a_1']['obj1a_2']['obj1a_3']...,
obj2['obj2a']['obj2a_1']['obj2a_2']['obj2a_3']...,
obj3['obj3a']['obj3a_1']['obj3a_2']['obj3a_3']... etc


Take this example, where we have a JSON object with parent object "requests" which contains "0" which also contains "responseCode" which also contains more children (in this case 3 children as seen above). Another child object at the same level with "responseCode" is "testPassFailCounts" which contains 217 more children.

That JSON structure is nested as follow:- requests >> 0 >> responseCode >> ... >> ... >> ... etc. Use a JSON viewer or reader such as JSON Editor Online to visualize the structural arrangement of your specific JSON file.

Now, to access any level of nested child in python follow this code snippet:-

import json

with open('name_of_file.json', encoding='utf-8') as f:
    data = json.load(f)
    
responseCode = data['requests'][0]['responseCode']
testPassFailCounts = data['requests'][0]['testPassFailCounts']

len(responseCode) # will return 3
len(testPassFailCounts) # will return 217


Tuesday, October 16, 2018

TkInter Open select file dialog windows from CMD

Are you writting a script for non tech savvy people that will require them to supply some file path like this: C:\Users\PC Name\Desktop\folder1\folder2\folder3\file-name.csv

As you already guess, many non-tech savvy individuals wont be able to type in such kind of file path to use you script. A work around this is to provide a Graphical User Interface (GUI) to the script, where is easier and user friendly to work with.

Here below is a simple solution using default GUI library that comes with python. This script gets an input file from user interactively via GUI, instead of using the CMD. It is written for both python 2 and 3:-

try:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter
    from tkFileDialog import askopenfilename

    Tk().withdraw()
    polyfile1 = askopenfilename(title="Select original line file:", filetypes = (("comma-separated value (CSV) files","*.csv"),("all files","*.*"))) # show an "Open" dialog box and return the path to the selected file

    # Using the file...
    print ('Do something with the file in Py2', polyfile1)


except ImportError:
    # for Python3
    from tkinter import *   ## notice lowercase 't' in tkinter here
    from tkinter import filedialog

    Tk().withdraw()
    polyfile1 = filedialog.askopenfilename(title="Select original line file:", filetypes = (("CSV files","*.csv"),("all files","*.*"))) # show an "Open" dialog box and return the path to the selected file

    # Using the file...
    print ('Do something with the file in Py3', polyfile1)




Running the script above should open the select file window as seen below.

That is enjoy the script.

Wednesday, October 10, 2018

How to Upload CSV file of Projected Local Coordinates onto ArcGIS Online

The cloud/web based version of ESRI ArcMap (ArcGIS Online) has come to stay. However, unlike the desktop version (ArcMap) that support many local coordinates systems, ArcGIS Online currently supports geographic coordinates in WGS84 but hardly reads local projected coordinates such as Nigeria Minna Datum UTM Zone 32N, StatePlane coordinates NAD 1983 (e.g StatePlane_Illinois_East epsg: 102671), Hong Kon 1980 Grid System, etc

If you try to upload such CSV file that have projected coordinates on "ArcGIS Online", you will encounter this error that says: "The layer was not created because no locations could be found. Make sure your data has valid location information".


The is because the coordinates in the CSV file are projected in UTM Zone 32 - Minna Datum (used in Nigeria region - EPSG: 26332).

Note: If you getting this required screen error: Location fields not fully specified. Then it means you did not select the correct location fields from the CSV file.


So, be sure to select the correct fields, that is Eastings for Longitude and Northings for Latitude.



If you are getting this error that says: "The layer was not created because no locations could be found. Make sure your data has valid location information", then you have to do one of the following to successfully upload you CSV data file.

Solution 1: Convert the projected coordinates to Geographical coordinates (that is convert from easting & northing to longitude & latitude).

Solution 2: Convert the CSV file itself to GeoJSON. The reason why this GeoJSON work even though the coordinates are still in projected form is because of the attribute or object in the GeoJSON file that defines the correct coordinates system that the coordinates represented.

Wednesday, October 3, 2018

The Basics of Map making With QGIS, GIMP and Inkscape

Making a modern print ready map will require one or both of the following, namely:
1) Scientific analysis
2) Art and design

In other words, the maps you see are both products of scientific charts as well as works of art and design.

GIS software such as QGIS does the science part better, illustration software such as GIMP and Inkscape handles the art part better.

If you are making a map to accurately/precisely visualize some variables based on scale, then you probably need a scientific map done with the QGIS/ArcGIS. If on the other hand, your map is simply to illustrate some variables not accurately/precisely to scale, then an illustration software like GIMP/Adobe Photoshop for raster editing or Inkscape/Adobe Illustrator for vector editing should be enough to get the job done.

With that said, it is a common practice these days to combine both the scientific and art & design software to finish a map layout. That is you will accurately prepare your map to scale in a scientific software and then complete the aesthetic layout using illustration software. Let's take a look at how this can be done below.

QGIS can handle the majority of vector and raster operations and most of the cartographic work. However, from a cartographic perspective, Inkscape and GIMP are primarily used to finish maps created within a GIS. Inkscape handles vector maps, while GIMP is best suited for raster maps. People often use GIMP to fine-tune coloration of a map. Inkscape is often used to add, for example, custom arrows to maps or add shadows to lines and polygons.

Basic Steps for making maps with QGIS, GIMP and Inkscape:-
1) Get the data
2) Load the data into QGIS
3) Symbolise and style the map according to the information the map wants to communicate
4) Prepare and compose it for printing
5) Export it as image for further fine-tuning in GIMP or export is as SVG file for fine-tuning in Inkscape

Note: If you need to have the map in print-ready magazines, brochures, posters, newsletters or other forms of page layouts and typographic quality text and images, then you will need use a desktop publishing application such as Scribus/Microsoft Publisher/Adobe Indesign or Pagemaker.



Conclusion
You should always bear in mind that QGIS understand all the science of map making which includes various GIS, Geodesic and Cartographic processes. GIMP, Inkscape and Scribus are there to support you in archiving a quality print/publish ready info-graphic maps.