Using QGIS to convert .shp file to .geojson (GADM)
by Maria Jane S. Poncardas, May 16, 2019
At the moment of writing, there is no direct method on how to utilize raw file from Database of Global Administrative Areas (GADM) into Python script to create charts in Bokeh package. GADM provides maps and spatial data for all countries and their sub-divisions. The goal here is to extract administrative areas in Mindanao (Southern Philippines) as it will be used for spatial and temporal analyses on terrorism (with which utilizes Bokeh for visualization). Sure, there are plenty of workarounds (such as using Plotly or simply Seaborn), but this article focuses on such as it deals with maps. I hope a few portion of this article may help you.
CONVERT .SHP TO .GEOJSON USING QGIS
Download shapefile from Database of Global Administrative Areas https://gadm.org/download_country_v3.html
Extract downloaded file by right-clicking on the file then from the dropdown menu, select “Extract files to <filename>\ ”
In the case of Philippine shapefile, extracted files consist of four levels of information, each with five (5) file types (.shp, .dbf, .prj, .cpg, .shx):
filename_0 is for country’s boundary
filename_1 is for province boundaries
filename_2 is for cities/municipalities boundaries
filename_3 is for barangay boundaries
If QGIS is unavailable, you can download and install QGIS based on your machine’s bitness for free from https://qgis.org/en/site/forusers/download.html
Once setting up and installation is done, open QGIS software.
From the folder of extracted files, select the .shp files and drag it over to the QGIS window.
From the lower left panel of QGIS, you can toggle off or on the shapefile layers.
On the same panel, right-click on the necessary layer, select Export > Save Features As…
A window will prompt wherein you can fill in information such as Format and File names.
Click OK.
INSTALLATION OF ANACONDA, INSTALLATION OF EXTENSION PACKAGES, AND RUNNING JUPYTER NOTEBOOK
Download the Anaconda Installer from https://www.anaconda.com/distribution
Double click the installer to launch
Click Next.
Read the licensing terms and click “I Agree”
Select installation method “Just Me” or for all users
Select a destination folder to install Anaconda and proceed to the installation process
Installing packages in Python have dependencies on other libraries, especially the “Geopandas” and “OSMnx” library for this study, hence, it is necessary to specify the install order in python package management to satisfy its prerequisites.
Installation of extension packages for geospatial analysis:
From the start menu, open “Anaconda Prompt”
Install packages by typing in the prompt window: pip install <package>
Packages (load order):
numpy
GDAL
pyshp
Shapely
Fiona
geopy
pyproj
descartes
geopandas
bokeh
pandas-bokeh
There are packages that are not available in their respective repositories, however, they can be downloaded from an unofficial windows binaries https://www.lfd.uci.edu/~gohlke/pythonlibs/
If you have downloaded the package from an external source, type: pip install <file location>/<file name.extension> on the Anaconda prompt
To avoid importing issues with OSMnx (tool used to retrieve, construct, analyze and visualize street networks from OpenStreetMap’s APIs), install in Anaconda prompt by typing: conda install -c conda-forge osmnx
After completing the installations, run the Jupyter Notebook from the Start Menu.
REMOVE LUZON AND VISAYAS INFORMATION OF THE GADM MAP USING PYTHON
Run Jupyter Notebook
Import libraries: geopandas (as gpd), numpy (as np), matplotlib.pyplot (as plt)
Open file by using geopandas package: df = gpd.read_file(‘gadm36_PHL_2.dbf’)
Set index of the file as provinces: df.set_index(‘province_column’, inplace = True), ‘inplace’ is used to apply the indexing to the data frame.
Remove rows of the unnecessary provinces: df.drop([‘province_name1’, ’province_name2’, …], axis = 0, inplace = True)
MAP DATA VISUALIZATION
Run Jupyter Notebook
Import libraries: geopandas, numpy, pandas_bokeh
Create a python environment through os.environ
To show the map with the OpenStreetMap as a Geographic Layer, call .plot_bokeh() to the dataframe
You can play with the data visualizations such as figure size, category, show_colorbar, colormaps, hovertool_columns, tile_provider.
PANDAS-BOKEH
Take note that once you import the Geojson file of your geospatial map, you need to setup the type of projection that will show in your OpenStreetMap (live map).
First, you need to import the os module in jupyter notebook (import os)
Create an environment to place your map: os.environ[‘PROJ_LIB’] = ‘Testdata’
Define your shape projection
If you need to show square shapes only, you need to input the corresponding code: df.crs = {‘init’:’epsg:3857’}
If you need to show the actual map, use ‘epsg: 4326’
EPSG stands for European Petroleum Survey Group. They publish a database of coordinate system information, and the Projection Engine uses a modified version of the EPSG model.
RELEVANT PACKAGES
numpy – efficient container of multi-dimensional array of data
GDAL – Geospatial Data Abstraction Library, used for manipulating geospatial raster data
pyshp – used to read and write shapefiles
Shapely – manipulation and analysis of planar features/geometries
Fiona – used for reading and writing geospatial data formats
geopy – locate the coordinates of addresses, cities, countries, and landmarks using third-party geocoders
pyproj – performs cartographic transformations between geographic (lat/lon) and map projection (x/y) coordinates.
descartes – use geometric objects as matplotlib paths and patches
geopandas – allow spatial operations on geometric types
bokeh – interactive visualization library through web
pandas-bokeh – backend visualization of Bokeh.
Comments
Post a Comment