Download - Session 08 geospatial data
![Page 1: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/1.jpg)
Handling Geospatial DataINAFU6513 Lecture 8b
![Page 2: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/2.jpg)
Lab 8: Your 5-7 things:
• Geospatial data
• Geospatial data tools
• Vector map data
• Raster map data
• Geospatial calculations
![Page 3: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/3.jpg)
Geospatial Data
![Page 4: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/4.jpg)
Geospatial Data
o Geographical: related to the Earth’s surface
o Spatial: about space (locations, distances etc)
o Data: yep, this is also data
o Usually handled by Geographical Information Systems (GIS) tools
o …many of which are written in Python…
![Page 5: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/5.jpg)
Printed Maps
![Page 6: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/6.jpg)
Georeferenced satellite and aerial data
![Page 7: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/7.jpg)
Vector Map
![Page 8: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/8.jpg)
Adding features to a vector map
![Page 9: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/9.jpg)
Abstract Schematics
![Page 10: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/10.jpg)
Schematics
![Page 11: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/11.jpg)
Map Projections
![Page 12: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/12.jpg)
Coordinate systems
• WGS84 (GIS)
• OSGB36 (UK)
• ED50 (Nato)
• Etc.
![Page 13: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/13.jpg)
Mapping Tools
![Page 14: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/14.jpg)
Tableau: Symbol Maps
![Page 15: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/15.jpg)
Tableau: Choropleth maps
![Page 16: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/16.jpg)
Geospatial data tools
• Google Maps
• OpenStreetMap
• QGIS
• CartoDB
• ArcGIS
• LeafletJS
• MapBox
Python libraries:
• GDAL toolset
• Shapely
• Fiona
• Basemap
![Page 17: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/17.jpg)
QGIS
![Page 18: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/18.jpg)
QGIS: reading shapefiles
![Page 19: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/19.jpg)
QGIS: Tanzania Wards
![Page 20: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/20.jpg)
QGIS: Looking at the dataset
![Page 21: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/21.jpg)
QGIS: Attribute Table
![Page 22: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/22.jpg)
QGIS: Reading Raster map data
![Page 23: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/23.jpg)
QGIS: Getting Raster Map information
![Page 24: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/24.jpg)
QGIS: Raster map histogram
![Page 25: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/25.jpg)
Vector Map Data
![Page 26: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/26.jpg)
Vector data formats
• Spreadsheet (.csv, .xls)
• Shapefile (.shp)
• Keyhole markup language (.kml, .kmz)
• Geojson and Topojson (.json)
• GPS exchange format (.gpx)
• OSM output files (.osm.bz2)
![Page 27: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/27.jpg)
Shapefiles• .shp: feature geometry (e.g. your dataset)
• .shx: shape index
• .dbf: shape attributes
• .prj: projection
• .shp.xml: metadata
![Page 28: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/28.jpg)
The GDAL toolsetExample terminal-line tools:
• OGR2OGR: convert between vector formats
• GDALwarp: cookie-cut raster files
• gdal_polygonize: convert raster to vector
Python libraries:
• gdal, ogr2ogr etc
• fiona (“pythonic GDAL”)
![Page 29: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/29.jpg)
Reading shapefiles: the Fiona library
from fiona import collection
with collection('example_data/TZwards/TZwards.shp', 'r') as input:
for f in input:
print(f)
![Page 30: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/30.jpg)
Convert vector file formats: OGR2OGR
From the terminal window:
ogr2ogr f GeoJSON where "ADM0_A3 = 'YEM'" outfile.json ne_10m_admin_1_states_provinces.shp
![Page 31: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/31.jpg)
Raster Map Data
![Page 32: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/32.jpg)
Why raster data?
![Page 33: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/33.jpg)
Raster Data Formats
• GeoTiff (.tif)
• Jpeg (.jpg)
• NITF
• HTF5
![Page 34: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/34.jpg)
Geotiff features
![Page 35: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/35.jpg)
Reading raster map data with GDAL
import gdal import numpy as np
dataset = gdal.Open(infile, GA_ReadOnly)cols = dataset.RasterXSizerows = dataset.RasterYSizenbands = dataset.RasterCountdriver = dataset.GetDriver().LongNamegeotransform = dataset.GetGeoTransform()for b in range(1,nbands+1): band = dataset.GetRasterBand(b) bandtype = gdal.GetDataTypeName(band.DataType) banddata = band.ReadAsArray(0,0,band.XSize, band.YSize).astype(np.float)
![Page 36: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/36.jpg)
Raster + Vector: cookie-cut a raster map
From the command line, type
gdalwarp cutline yourshapefile.shp yourgeotiff.tif yourresult.tif
![Page 37: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/37.jpg)
GIS data Calculations
![Page 38: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/38.jpg)
Vector data calculations
• Point location (e.g. lat/long from address)
• Area and area overlap sizes (e.g. overlap between village and protected area)
• Belonging (e.g. finding which district a lat/long is in)
• Straight-line distance between points (e.g. great circles)
• Practical distance and time between points (e.g. using roads)
![Page 39: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/39.jpg)
Geopy: get lat/ longs from addresses
from geopy.geocoders import Nominatimfrom geopy.geocoders import GoogleV3geolocator = Nominatim()googlocator = GoogleV3()
address = '1600 Pennsylvania Ave NW, Washington, DC'result = geolocator.geocode(address, timeout=10) if result is None:
result = googlocator.geocode(address, timeout=10) if result is None:
latlon = (0.0, 0.0) else:
latlon = (float(result.latitude), float(result.longitude))
![Page 40: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/40.jpg)
Geopy: Calculate distance
from geopy.distance import vincenty
dist = vincenty(loc1, loc2)
![Page 41: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/41.jpg)
Fiona point-in-polygon: finding “belonging”import fionaImport shapely
mapdata = fiona.open(‘map_data/nyc_neighbourhoods_wgs84.shp’, 'r') for f in mapdata: props = f['properties'] geom = shapely.geometry.asShape(f['geometry']) neighs[props['NTACode']] = geom
stationpt = Point(stationlon, stationlat)for neigh, neighshape in neighs.iteritems(): if neighshape.contains(stationpt): stationneigh = neigh
![Page 42: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/42.jpg)
Basemap: Map visualisations in Python%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
fig = plt.figure(figsize=(8, 8))
usmap = Basemap(projection='lcc', resolution=None,
width=8000000, height=8000000,lat_0=45, lon_0=-100,)
usmap.etopo(scale=0.5, alpha=0.5)
![Page 43: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/43.jpg)
Exercises
![Page 44: Session 08 geospatial data](https://reader031.vdocuments.mx/reader031/viewer/2022030308/58ecd8d01a28ab0e278b4697/html5/thumbnails/44.jpg)
If this interested you…
There’s more in workbooks 8.x