GIS programming (introduction to Raster data type)

KIPYEGON AMOS
5 min readJul 30, 2023

--

i n case you need to freshen up with vector data you can follow in this link, Introduction to GIS programming (vector)

Raster data type is collection of rows and columns of pixels where each pixel represent a value between 0 and 255.

In GIS, Remote Sensing Image (Raster) is made up pixel with Values that represent Digital Numbers (DN) which represent the intensity or strength of electromagnetic energy measured for ground.
- as a satellite vehicle orbits it carry a sensor that emits electromagnetic energy, features on the surface of the earth has different reflectance.
- python reads Raster image in form of 2D Numpy arrays, where each pixel represent different index.
- Numpy library is installed automatically when pandas is installed.

Numpy simply means numeric python num -numeric py — python
- — it is use in processing array of values/numbers all of the same data type and are indexed by tubles of positive integers.
- it can be installed independently by
pip install Numpy
Numpy array class is called ndarray or alias

Array

. it is an ordered sequence of values arranged in an n-dimention
np.nan represent NoData
. Transposing array means changing rows to column
array.

create a 2-d ARRAY

import numpy as np
array_values = np.array([
[1,2,3,4],
[5,6,7,8]
])
# checking type of array
print("Array is of type: " ,type(array_values))

#checking the dimention of array
print("Array dimention is :",array_values.ndim)

# checking array total size
print("Array has a total siza of", array_values.size)
# checking shape of an array
print("the shape of array is: ", array_values.shape)

ways of creating array.

arrays can be created from
1. list of values np.array([1,2,3])
2. create from tuples np.array((1,2,3,4))
3. from random values np.random.random((2,3))
np.nan is use in accessing null values

simple raster

import required libraries

import numpy as np
from matplotlib import pyplot as plt
import rasterio
from rasterio.plot import show
data= np.random.random((10,10)) # 2 represent dimention (height) , 3 represent number of bands and 6 represent widht (2,3) represent shape

# converting array to list
li=data.tolist()
# plotting
show(data)
Raster image from array of values

Dealing with Spatial Raster

. 2-d array in raster represent single band
. 3-d — multy band
characteristics of raster
- Area is subdivided into grids with equal cells
- each piel has only one value of DN
- X and Y of pixel is spatial resolution, so area is simply culculated as area of 1 pixel multibly by total number of pixels. (x*y) * Number of cells

. each pixel value in raster represent reflectance value of feature on the surface of the earth

Creating Raster from scratch

Geospatial raster is made up of array of values linked to geographic information with the following characteristics -:

1. Transformation matrix linking pixel index with coordintes (x minimum and y maximum, origin (0,0))

example

if raster values is ploted in a cartesian plane without transformation the north-west cordinate values is (0,0) since rasterio will plot based on index values,in order to plot this in its position on earth surface we need a transformation matrix that will transform this “cordinadte”(0,0) to exact cordinates on the surface,
- resolution (pixel size) it is delta x and delta uy, for a Landsat image it is 30m

2. coordinate reference system SRC
3. meta data which is pass as a dictionary

Note that : there is difference between Transformation and Projection.

projection is transformation of spherical earth into a planar surface
- when Transformation is done it causes different distortion on
1. shape
2. area
3. direction
4. shape
types of map projectio includes -:
- conformal- local shapes are preserved
- equal area — areas are preserved
- Azimuthal — direction from a single location to all location are preseved
- equidistrance — distance to all other location are preserved
- — projection surface include
1. Cylinder
2. cone
3. plane

Transformation take a matrix for changing values
- transform (1,5) with factor (2,-3) = new coordinates will be (3,2)

steps

creating random array of values values using Numpy and reshaping it into a two dimensional

we use round() function to round off to a 4 decimal place by passing 4 to the function

Another function to arrange values in in numpy is np.arrange(fro,to dtypes=uint8)

  • Create 10000 random numbers between 0 and 255
    1. np.random.uniform(0,255,100)
    2. reshape the numpy array to relfect 2d of raster
arr_val =np.random.uniform(0,255,10000)
data=arr_val.reshape(100,100).round(4)
#de # de.dtype=np.uint8 to convert to intergers
# plotting
show(data)
raster image of random array of values
raster of random points with values (0–255)
  1. count — — → Number of layers(bands)
  2. height — → Number of Rows
  3. width — — → Number of columns

transforming to position on earth

in this example we will use abitrary tranformation.

. let set origin “(0.0)” to be
1. xmin,ymax =(10000,5000)
2. Resolution to be delta x, delta y(2,-2)
- this will mean that x values will be increasing by 2 from the x transformation matrix and y will be reducing by 2 from transformation matrix
- Affine(delta_x, 0.0, xmin,
0.0, delta_y, ymax).

example

- [0,0]=10000,5000
- [0,1] =(10000, 4998); [0,2]= (10000,4996)
- [1,0] = (10002,5000); [2,0] = (10004,5000)
- [xn,yn] =((10000+(2*n)),( 5000-(2*n)))

. lets define our transformation in practice
- lets define corner coordinates and resolution

spatial_attributes=rasterio.transform.from_origin(
west= 10000,# xmin
north= 5000, # ymax
xsize= 2, # delta_x
ysize= 2) # delta_y

spatial_attributes
# plotting
show(data, transform= spatial_attributes)
image of transform data

above raster is the same with previous only that it has undergo transformation

Writing Raster

spatial raster has metadata

lets define our metadata for above data

meta={
'driver':'GTiff',
'count':1,
'height':data.shape[0],
'width': data.shape[1],
'dtype':data.dtype,
'transform': spatial_attributes
}
  • we are passing value and key arguments (kwargs) using ** name
new_dt=rasterio.open("datatif",'w', **meta)
new_dt.write(data,1)
new_dt.close()

alternatively; we may pass all data as:


new_data= rasterio.open("raster.tif", 'w',
driver='GTiff',
count=1,
height=data.shape[0],
width= data.shape[1],
dtype=data.dtype,
transform= spatial_attributes)
new_data.write(data,1)
new_data.close()

we can now open the saved raster

de_r=rasterio.open("raster.tif")
rd=de_r.read()
rd

fig,ax =plt.subplots()
rasterio.plot.show(de_r,transform= spatial_attributes,ax=ax,title="random")
raster image after writing

we can check meta data

de_r.meta
metadata
metadata

stay tuned for more and more # Geospatial to Transform view of The world. from Agriculture,Climate, Business, Infrastructure, communication, Politics and many more application

you cane get Jupiter notebook code here GIS Programming with raster

find and follow me at Twitter @kiptoamos

follow and connect with me at LinkedIn Kipyego Amos

--

--