{ "metadata": { "name": "", "signature": "sha256:34721fb75d1a69f20f368d7384d1c7b3d5b2f5874790b4eb673aed55e0b47177" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Geospatial Data in Python: Database, Desktop, and the Web\n", "## Tutorial (Part 3)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Convert vector data formats (Shapefile -> GeoJSON)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import fiona\n", "from fiona.crs import to_string\n", "import os\n", "\n", "boro_file = os.path.join(\"..\", \"data\", \"nybb\", \"nybb.shp\")\n", "out_file = os.path.join(\"..\", \"data\", \"nybb\", \"nybb.geojson\")\n", "\n", "# Register format drivers with a context manager\n", "with fiona.drivers():\n", " # Open the shapefile (can also open directly from zip files with vfs!)\n", " with fiona.open(boro_file) as source:\n", " print(\"Feature Count: %s\" % len(source))\n", " print(\"Input Driver: %s\" % source.driver)\n", " \n", " meta = source.meta\n", " meta.update(driver=\"GeoJSON\")\n", " \n", " if os.path.exists(out_file):\n", " os.remove(out_file)\n", " with fiona.open(out_file, 'w', **meta) as sink:\n", " print(\"Output Driver: %s\" % sink.driver)\n", " for rec in source:\n", " sink.write(rec)\n", "# Did it work?\n", "print(\"File Exists: %s\" % os.path.exists(out_file))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Read geospatial raster data\n", "\n", "Note: You can download the GeoTIFF of Manhattan used in this example [from here](https://www.dropbox.com/s/mba7obrfh2b2ucb/manhattan.tif). Make sure you put it in your `data` folder." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import rasterio\n", "from fiona.crs import to_string\n", "import os\n", "import numpy as np\n", "\n", "image_file = os.path.join('..', 'data', 'manhattan.tif')\n", "\n", "# Register format drivers with a context manager\n", "with rasterio.drivers():\n", " with rasterio.open(image_file, 'r') as source:\n", " print(source.count, source.shape)\n", " print(source.driver)\n", " print(to_string(source.crs))\n", " \n", " # Get data from each band (newer versions of rasterio use source.read())\n", " r, g, b = map(source.read_band, (1, 2, 3))\n", " data = np.dstack((r, g, b)) # Each band is just an ndarray!\n", " print(type(data))\n", " \n", " # Get the bounds of the raster (for plotting later)\n", " bounds = source.bounds[::2] + source.bounds[1::2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An alternative way (outside of `with` context manager as above):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "source = rasterio.open(image_file, 'r')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "## Simple plot of geospatial raster" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "fig = plt.figure(figsize=(8, 8))\n", "ax = plt.imshow(data, extent=bounds)\n", "plt.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Time to work on Notebook:\n", "\n", "[`Working with Rasters in Python`](../exercises/Working with Rasters in Python.ipynb)" ] } ], "metadata": {} } ] }