{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lecture 10: Topology" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Install the shapely Python package!** \n", "\n", "If you have Anaconda installed, open the *Anaconda Prompt* and type in:\n", "```\n", "conda install -c conda-forge shapely\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic usage of shapely" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shapely import geometry\n", "\n", "point = geometry.Point(5,5)\n", "print(point)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rectangle1 = geometry.Polygon([[0,0], [10,0], [10,10], [0,10]])\n", "print(rectangle1)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from shapely import wkt\n", "\n", "rectangle2 = wkt.loads('POLYGON ((-4 -4, 4 -4, 4 4, -4 4, -4 -4))')\n", "print(rectangle2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Rectangle1 contains Point: %s' % rectangle1.contains(point))\n", "print('Rectangle2 contains Point: %s' % rectangle2.contains(point))\n", "print('Rectangle1 contains Rectangle2: %s' % rectangle1.contains(rectangle2))\n", "print('Rectangle1 intersects Rectangle2: %s' % rectangle1.intersects(rectangle2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read Shapefile into Shapely objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import shapefile\n", "\n", "sf = shapefile.Reader('04_megye_region.shp', encoding = 'latin1')\n", "print(sf.fields)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for sr in sf.shapeRecords():\n", " record = sr.record\n", " name = record[0]\n", " \n", " shape = sr.shape\n", " geom = geometry.shape(shape)\n", " \n", " print('%s, Area: %.1f km2, Centroid: %s' % (name.title(), geom.area / 1e6, geom.centroid))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pos_budapest = geometry.Point(649918.4, 238457.1)\n", "for sr in sf.shapeRecords():\n", " record = sr.record\n", " name = record[0]\n", " \n", " shape = sr.shape\n", " geom = geometry.shape(shape)\n", " \n", " if geom.contains(pos_budapest):\n", " print(name.title())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 1.** Verify that neither of the county polygons in `04_megye_region.shp` intersect with each other." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise 2.** Verify that for each city / town in `08_telepulesek.shp` the county metadata in the attribute table is correct, by checking that the city is in the given county according to the `04_megye_region.shp` file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }