44
Handling Geospatial Data INAFU6513 Lecture 8b

Session 08 geospatial data

Embed Size (px)

Citation preview

Page 1: Session 08 geospatial data

Handling Geospatial DataINAFU6513 Lecture 8b

Page 2: Session 08 geospatial data

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

Geospatial Data

Page 4: Session 08 geospatial data

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

Printed Maps

Page 6: Session 08 geospatial data

Georeferenced satellite and aerial data

Page 7: Session 08 geospatial data

Vector Map

Page 8: Session 08 geospatial data

Adding features to a vector map

Page 9: Session 08 geospatial data

Abstract Schematics

Page 10: Session 08 geospatial data

Schematics

Page 11: Session 08 geospatial data

Map Projections

Page 12: Session 08 geospatial data

Coordinate systems

• WGS84 (GIS)

• OSGB36 (UK)

• ED50 (Nato)

• Etc.

Page 13: Session 08 geospatial data

Mapping Tools

Page 14: Session 08 geospatial data

Tableau: Symbol Maps

Page 15: Session 08 geospatial data

Tableau: Choropleth maps

Page 16: Session 08 geospatial data

Geospatial data tools

• Google Maps

• OpenStreetMap

• QGIS

• CartoDB

• ArcGIS

• LeafletJS

• MapBox

Python libraries:

• GDAL toolset

• Shapely

• Fiona

• Basemap

Page 17: Session 08 geospatial data

QGIS

Page 18: Session 08 geospatial data

QGIS: reading shapefiles

Page 19: Session 08 geospatial data

QGIS: Tanzania Wards

Page 20: Session 08 geospatial data

QGIS: Looking at the dataset

Page 21: Session 08 geospatial data

QGIS: Attribute Table

Page 22: Session 08 geospatial data

QGIS: Reading Raster map data

Page 23: Session 08 geospatial data

QGIS: Getting Raster Map information

Page 24: Session 08 geospatial data

QGIS: Raster map histogram

Page 25: Session 08 geospatial data

Vector Map Data

Page 26: Session 08 geospatial data

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

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

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

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

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

Raster Map Data

Page 32: Session 08 geospatial data

Why raster data?

Page 33: Session 08 geospatial data

Raster Data Formats

• GeoTiff (.tif)

• Jpeg (.jpg)

• NITF

• HTF5

Page 34: Session 08 geospatial data

Geotiff features

Page 35: Session 08 geospatial data

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

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

GIS data Calculations

Page 38: Session 08 geospatial data

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

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

Geopy: Calculate distance

from geopy.distance import vincenty

dist = vincenty(loc1, loc2)

Page 41: Session 08 geospatial data

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

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

Exercises

Page 44: Session 08 geospatial data

If this interested you…

There’s more in workbooks 8.x