{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Vector data cubes\n", "\n", "Exploration of possible implementation of vector data cubes in Python based on xarray and geopandas. The goal is to mimic what stars package in R can do - https://r-spatial.github.io/stars/articles/stars1.html#vector-data-cube-example\n", "\n", "\n", "\n", "Because we need to use geometries as an index, we need to ensure that geopandas is using shapely 2.0 (beta) as a geometry engine. Shapely 1.8 geometries are not hashable, while shapely 2.0 are." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import geopandas\n", "import pandas\n", "import xarray\n", "import numpy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to recreate [this example from stars documentation](https://r-spatial.github.io/stars/articles/stars1.html#vector-data-cube-example).\n", "\n", "Load geometries using geopandas:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "nc = geopandas.read_file(\"https://github.com/r-spatial/sf/raw/main/inst/gpkg/nc.gpkg\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the GeometryArray. Note that this also contains CRS information at this point." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "origin = destination = nc.geometry.array" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create dimensions and dummy data." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "mode = [\"car\", \"bike\", \"foot\"]\n", "day = pandas.date_range(\"2015-01-01\", periods=100)\n", "hours = range(24)\n", "data = numpy.random.randint(1, 100, size=(3, 100, 24, 100, 100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create `xarray.DataArray`." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (mode: 3, day: 100, time: 24, origin: 100, destination: 100)>\n", "array([[[[[ 3, 22, 90, ..., 1, 39, 76],\n", " [31, 59, 8, ..., 62, 20, 45],\n", " [23, 1, 21, ..., 88, 37, 35],\n", " ...,\n", " [ 4, 43, 45, ..., 55, 44, 46],\n", " [22, 78, 26, ..., 41, 3, 65],\n", " [98, 35, 77, ..., 28, 72, 47]],\n", "\n", " [[ 5, 4, 87, ..., 45, 28, 85],\n", " [58, 81, 9, ..., 29, 35, 46],\n", " [17, 11, 99, ..., 77, 29, 88],\n", " ...,\n", " [30, 37, 17, ..., 1, 69, 82],\n", " [94, 21, 4, ..., 73, 10, 63],\n", " [98, 3, 24, ..., 19, 88, 88]],\n", "\n", " [[10, 29, 63, ..., 75, 6, 70],\n", " [23, 20, 13, ..., 2, 44, 38],\n", " [60, 19, 98, ..., 40, 11, 17],\n", " ...,\n", "...\n", " ...,\n", " [19, 97, 35, ..., 14, 41, 54],\n", " [ 7, 16, 64, ..., 29, 75, 74],\n", " [66, 62, 62, ..., 67, 39, 36]],\n", "\n", " [[61, 63, 53, ..., 63, 40, 85],\n", " [78, 61, 62, ..., 32, 59, 43],\n", " [41, 68, 64, ..., 6, 51, 6],\n", " ...,\n", " [51, 89, 55, ..., 82, 61, 63],\n", " [46, 20, 85, ..., 76, 41, 52],\n", " [77, 68, 81, ..., 40, 75, 99]],\n", "\n", " [[53, 80, 42, ..., 77, 1, 1],\n", " [78, 76, 2, ..., 51, 64, 11],\n", " [ 2, 76, 35, ..., 87, 55, 91],\n", " ...,\n", " [31, 23, 7, ..., 69, 3, 91],\n", " [24, 51, 44, ..., 48, 85, 20],\n", " [81, 49, 65, ..., 46, 77, 31]]]]])\n", "Coordinates:\n", " * mode (mode) <U4 'car' 'bike' 'foot'\n", " * day (day) datetime64[ns] 2015-01-01 2015-01-02 ... 2015-04-10\n", " * time (time) int64 0 1 2 3 4 5 6 7 8 9 ... 15 16 17 18 19 20 21 22 23\n", " * origin (origin) object MULTIPOLYGON (((-81.4727554321289 36.2343559...\n", " * destination (destination) object MULTIPOLYGON (((-81.4727554321289 36.23...
<xarray.DataArray (destination: 100)>\n", "array([61, 37, 67, 7, 8, 68, 28, 23, 79, 36, 56, 37, 90, 33, 94, 91, 20,\n", " 38, 29, 21, 93, 75, 43, 67, 57, 3, 73, 69, 84, 79, 49, 72, 74, 87,\n", " 94, 31, 11, 86, 42, 72, 53, 93, 19, 42, 81, 36, 7, 80, 24, 21, 89,\n", " 1, 82, 49, 21, 9, 96, 13, 52, 79, 80, 41, 71, 53, 30, 96, 4, 83,\n", " 95, 84, 4, 45, 98, 11, 21, 1, 95, 21, 69, 79, 35, 14, 16, 73, 67,\n", " 9, 1, 47, 28, 17, 41, 59, 98, 77, 90, 6, 99, 5, 53, 24])\n", "Coordinates:\n", " mode <U4 'car'\n", " day datetime64[ns] 2015-01-01\n", " time int64 12\n", " origin object MULTIPOLYGON (((-81.4727554321289 36.23435592651367, ...\n", " * destination (destination) object MULTIPOLYGON (((-81.4727554321289 36.23..." ], "text/plain": [ "