GIS programming for Beginners
Introduction to GIS programming (vector)
GIS is a technological field that is use to store, creates, manages, analyzes, and display spatial data along with none spatial data called attributes
Types of spatial data
- vector data types (points,lines and polygons)
- raster( Stored in Grid/pixel)
GIS programming
it is writing code or programs that automate (storage,creation,analysis , management and display) of spatial data.
Area of specialization in GIS programming
- web mapping (web developer)
- Desktop GIS
- GIS database
why do i need GIS programming
in most cases existing software and programs was written to ‘fit’ individual needs at that particular time hence some functionalities might not address current problem on hand.make work easy by automating boring process (combining different process that you could have been waiting and clicking in existing software)
2. flexibility — you can be able to manipulate process to fit your desire
3. safes on time
Best libraries to start with and not limited to
1. Pandas
2. matplotlib
3. geopandas
4. shapely
5. rasterio
6. numpy
Pandas
it is a open source easy to use rich python library desighned for data anallysis
Reading data from Different file types to pandas
- CSV — → read_csv()
2. JASON — ->read_json()
3. HTML — -> READ_HTML()
4. XML — → read_xml()
5. SQL — → read_sql()
6. excel — → read_excel()
Creating a DataFrame and Series
DataFrame is a 2-d (table -like) and is 1–D data structure
import required libraries
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
import shapely.geometry
import numpy as np
from shapely.geometry import Point,Polygon
Lets create an empty dataframe
data=pd.DataFrame()
print(data)
ways of creating dataframe
Method 1. dictionary of list
coordinates={
"x":[2,3,4,5,6,],
"y":[7,8,9,0,11]
}
#DataFrame creation
df=pd.DataFrame(coordinates)
df
Method 2. pandas series
#creating a dataframe from a dictionary of series
x=pd.Series([1,2,3,4,5,6,7,8,9])
y=pd.Series([1,2,3,4,5,6,7,8,9])
#DtataFrame
df1=pd.DataFrame({"x":x,'y':y})
df1
Method 3. list
#creating from list
data=[
[1,2],
[2,2],
[3,4],
[2,7]
]
df2=pd.DataFrame(data,columns=["x","y"])
df2
Method 4. Dictionaries
#fromlist of dictionaries
data=[
{"name":"Amos","age":23},
{"name":"John","age":20}
]
df3=pd.DataFrame(data,index=['a','b'])
df3
exploring numpy functionalities.
creating a function that create random points from a polygon it takes 2 arguments
1. polygon
2. number of random points
polygon has xmin,ymin, xmax,ymax
def randm(poly, number):
xmin,ymin, xmax,ymax = poly.bounds
x=np.random.uniform(xmin,xmax,number)
y=np.random.uniform(ymin,ymax,number)
return x,y
polygon = Polygon([[0,0],[0,20],[30,20],[30,-0.5],[0,0]]) #create a polygon using shapely geometry
x,y=randm(polygon,100)
x = x.round(3)
y= y.round(3)
convert random points to list of points each representing location in Cartesian plane. first create an empty dataframe and pass there results
df = pd.DataFrame()
df['geometry'] = list(zip(x,y))
df
pandas dataframe from random points
convert the list of coordinates to Shapely points using the following line of code. then convert pandas dataframe to Geopandas dataframe
df['geometry'] = df['geometry'].apply(Point)
dta=gpd.GeoDataFrame(df)
dta
plotting
dta.plot()
# plotting specific column and adding legend
#dta.plot(column='column_name',legend=True)
Find code from GitHub KIPYEGON
find and follow me at Twitter @kiptoamos
follow me at LinkedIn Kipyego Amos