{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Benchmark CopyFromBuffer" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import geopandas\n", "import pygeos\n", "import pyogrio\n", "\n", "import pyproj\n", "pyproj.datadir.set_data_dir(\"/home/joris/miniconda3/envs/geo-dev/share/proj/\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# arr = pygeos.from_shapely(df.geometry.array.data)\n", "arr = df.geometry.array.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get flat array of rings:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "arr_rings = pygeos.get_rings(pygeos.get_parts(arr))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "coords = pygeos.get_coordinates(arr_rings)\n", "offsets = np.insert(np.cumsum(pygeos.get_num_coordinates(arr_rings)*2), 0, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Recreate the linearrings:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "ring_lengths = np.diff((offsets / 2).astype(int))\n", "ring_indices = np.repeat(np.arange(len(ring_lengths)), ring_lengths)\n", "rings = pygeos.linearrings(coords, indices=ring_indices)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pygeos.equals(arr_rings, rings).all()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test performance on larger dataset" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | GEOID10 | \n", "geometry | \n", "
|---|---|---|
| 0 | \n", "43451 | \n", "POLYGON ((-83.70873 41.32733, -83.70815 41.327... | \n", "
| 1 | \n", "43452 | \n", "POLYGON ((-83.08698 41.53780, -83.08256 41.537... | \n", "
| 2 | \n", "43456 | \n", "MULTIPOLYGON (((-82.83558 41.71082, -82.83515 ... | \n", "
| 3 | \n", "43457 | \n", "POLYGON ((-83.49650 41.25371, -83.48382 41.253... | \n", "
| 4 | \n", "43458 | \n", "POLYGON ((-83.22229 41.53102, -83.22228 41.532... | \n", "
| ... | \n", "... | \n", "... | \n", "
| 33139 | \n", "84044 | \n", "POLYGON ((-112.26022 40.76909, -112.25333 40.7... | \n", "
| 33140 | \n", "84045 | \n", "MULTIPOLYGON (((-111.92421 40.17034, -111.9240... | \n", "
| 33141 | \n", "84046 | \n", "POLYGON ((-110.00072 40.99745, -110.00036 40.9... | \n", "
| 33142 | \n", "84047 | \n", "POLYGON ((-111.92141 40.62772, -111.92134 40.6... | \n", "
| 33143 | \n", "84049 | \n", "POLYGON ((-111.59394 40.57707, -111.59386 40.5... | \n", "
33144 rows × 2 columns
\n", "