Lecture Note - GIS 821 - Programming in Geoinformatics - Weeks 3
Lecture Note - GIS 821 - Programming in Geoinformatics - Weeks 3
Practical Exercise:
⚫ Write a script to classify temperature data (e.g., "Hot", "Moderate",
"Cold") using thresholds.
2. Loops & Repetition
Python Example (For Loop):
Practical Exercise:
⚫ Use a loop to iterate over a list of coordinates and filter latitudes above
30 degrees.
3. Data in Lists/Arrays
Python (List Operations):
1. File Input/Output
Python Example (Read/Write CSV):
Practical Exercise:
⚫ Convert a CSV of river coordinates to a text file using Python.
2. Libraries and Scripts
Python Script Example (custom_geo.py):
```python
# Import custom functions
def calculate_distance(lat1, lon1, lat2, lon2):
# Haversine formula implementation
return distance
# Load shapefile
gdf = gpd.read_file('roads.shp')
print(gdf.head())
# Plot
gdf.plot(color='blue', edgecolor='black')
```
3. Data Conversion & Processing
Python (CSV to Shapefile):
```python
import geopandas as gpd
from geopandas import GeoDataFrame
from shapely.geometry import Point
# Read CSV
data = pd.read_csv('cities.csv')
# Create geometry
geometry = [Point(xy) for xy in zip(data.lon, data.lat)]
gdf = GeoDataFrame(data, geometry=geometry, crs="EPSG:4326")
# Save as shapefile
gdf.to_file('cities.shp')
```
Practical Exercises
1. Python:
- Load a GeoTIFF using GDAL, extract metadata, and convert it to a
CSV of elevation values.
- Use Pandas to clean a CSV with missing geospatial values.
Next Steps:
- Practice with real-world datasets (e.g., Natural Earth
(https://www.naturalearthdata.com/)).
Resources:
- GeoPandas Documentation (https://geopandas.org/)