summaryrefslogtreecommitdiff
path: root/_downloads/78c112310e3baf2494000ec899fe1d8c/plot_polygons.ipynb
blob: 7ba12d166720c800655661612bd5b1497cc5223c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Graphs from Polygons\n\nThis example shows how to build a graph from a set of polygons\nusing PySAL and geopandas. We'll focus on the Queen contiguity \ngraph, but constructors are also provided for Rook contiguity, \nas well as other kinds of graphs from the polygon centroids. \n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from libpysal import weights\nimport matplotlib.pyplot as plt\nimport networkx as nx\nimport geopandas\nimport numpy as np\n\n# read in example data from geojson. GeoJSON is a file format\n# for encoding geographic data based on JSON. It is useful for\n# presenting geographic data on the web, and is increasingly\n# used as a file format for geographic data.\nfilepath = \"nuts1.geojson\"\neuropean_regions = geopandas.read_file(filepath)\n\n# extract the centroids for connecting the regions, which is\n# the average of the coordinates that define the polygon's boundary\ncentroids = np.column_stack((european_regions.centroid.x, european_regions.centroid.y))\n\n# construct the \"Queen\" adjacency graph. In geographical applications,\n# the \"Queen\" adjacency graph considers two polygons as connected if\n# they share a single point on their boundary. This is an analogue to\n# the \"Moore\" neighborhood nine surrounding cells in a regular grid.\nqueen = weights.Queen.from_dataframe(european_regions)\n\n# Then, we can convert the graph to networkx object using the\n# .to_networkx() method.\ngraph = queen.to_networkx()\n\n# To plot with networkx, we need to merge the nodes back to\n# their positions in order to plot in networkx\npositions = dict(zip(graph.nodes, centroids))\n\n# plot with a nice basemap\nax = european_regions.plot(linewidth=1, edgecolor=\"grey\", facecolor=\"lightblue\")\nax.axis([-12, 45, 33, 66])\nax.axis(\"off\")\nnx.draw(graph, positions, ax=ax, node_size=5, node_color=\"r\")\nplt.show()\n\n# An alternative method to construct graphs from polygons may use\n# pygeos. This package is a high-performance interface to the GEOS C\n# library, used in computing geographical relationships. These let us\n# describe the relationships between \"point sets,\" like polygons whether\n# or not a line \"crosses\" a polygon, or whether two polygons \"touch.\"\n# These relationships, called \"predicates\", are extensive, and are documented\n# by the pygeos package."
      ]
    }
  ],
  "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.9.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}