Home

Awesome

geokz

This project downloads a set of shapefiles relevant to the Republic of Kazakhstan.

geokz-package provides access to multiple dataset of different types and for different use.

Package installation

geokz can be installed from Github using:

library("devtools")

devtools::install_github("arodionoff/geokz")

Vignette Making maps using {geokz}-package provides multiple real-world examples of their usage.

To use vignettes, you should use the installation with vignettes compilation:

devtools::install_github("arodionoff/geokz", build_vignettes = TRUE)

In these vignettes we introduce the different datasets and explain their use cases.

The following spatial objectsare included:

administrative division as of January 2024 and June 2018:

(Oblast Rayons, City of Oblast Significance and Rayons of Cities of Republican Significance)

natural in 2020:

You can use ESRI ArcGIS shapefiles (*.cpg, *.dbf, *.prj, *.shp, *.shx) load as shape subdirectory.

Using with Python

If necessary, a similar operations can be performed in Python.

If the required packages are not available, then they should be installed in the required Python instance. {GeoPandas} depends on the following packages:

fiona, in turn, depends on attrs, click, cliji, click_plugins, munch and, of couse, GDAL packages.

matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

import sys
print(f'\nPython Ver. =  {sys.version}')

# importing necessary libraries
import pandas as pd
import geopandas as gpd

gpd.show_versions()

kaz_ADM1_gdf = gpd.read_file('C:/R/R-4.4/library/geokz/shape/kaz_admbnda_adm1_2024.shp',
                             driver = 'ESRI Shapefile', encoding = 'utf-8')

kaz_ADM1_gdf.crs
print(kaz_ADM1_gdf)

import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Categories in legends produced by geopandas are sorted and this is hardcoded
# https://stackoverflow.com/questions/54370302/changing-the-order-of-entries-for-a-geopandas-choropleth-map-legend

# Set Number Code for Regions and Customer Legend
kaz_ADM1_gdf.loc[(kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ11') | 
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ39') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ55') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ59') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ71'), 'Regions'] = 0    # 'North'
kaz_ADM1_gdf.loc[(kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ15') | 
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ23') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ27') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ47'), 'Regions'] = 1    # 'West'
kaz_ADM1_gdf.loc[(kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ35') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ62'), 'Regions']   = 2  # 'Center'
kaz_ADM1_gdf.loc[(kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ10') | 
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ63'), 'Regions']   = 3  # 'East'
kaz_ADM1_gdf.loc[(kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ19') | 
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ31') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ33') | 
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ43') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ61') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ75') |
                 (kaz_ADM1_gdf['ADM1_PCODE'] == 'KZ79'), 'Regions'] = 4    # 'South'

#Re-Projection
kaz_ADM1_gdf = kaz_ADM1_gdf.to_crs(epsg = 2502)  # Pulkovo 1942 / Gauss-Kruger CM 69E. See <https://epsg.io/2502>
kaz_ADM1_gdf.crs

# Create color dictionaries for Regions- Set colors for Customer Palette in R: tmaptools::get_brewer_pal("Set3", n = 5)
palette_symb = {0: '#8DD3c7', 1: '#FFFFB3', 2: '#BEBADA', 3: '#FB8072', 4: '#80B1D3'}
regions_symb={0: 'North', 1: 'West', 2: 'Center', 3: 'East', 4: 'South'}

# Plot Geographic Coverage
fig, ax = plt.subplots(figsize=(7, 4))

kaz_ADM1_gdf.plot(
  # column = 'Regions',
  # cmap = 'Set3',
  color = kaz_ADM1_gdf['Regions'].map(palette_symb),
  edgecolor = 'black',
  linewidth = 0.15,
  categorical = True,
  legend = False,
  ax = ax
  )

kaz_ADM1_gdf.apply(lambda x: ax.annotate(text=x.ADM1_EN, xy=x.geometry.centroid.coords[0], ha='center', size=9), axis=1)

# Create Customer Legend for Category Legend
# see https://matplotlib.org/stable/gallery/text_labels_and_annotations/custom_legends.html
legend_elements = []
for x in range(len(regions_symb)):
    legend_elements.append(patches.Patch(facecolor=palette_symb[x],
                                         edgecolor='black',
                                         label=regions_symb[x]))
    
ax.legend(handles=legend_elements, bbox_to_anchor=(1.1, 1.1), title='Regions', prop={'size': 9})
ax.set(title = 'Regions of Kazakhstan')
ax.set_axis_off()

plt.show()

A call for action

The project is actively maintained, and ideas & suggestions to improve the package are greatly welcome. Should you feel more at ease with old fashioned email than the GitHub ticketing system - do drop me a line.