Created
October 25, 2024 05:20
-
-
Save calebrob6/2a2705027764d17fdbeb2b7a4cf4887c to your computer and use it in GitHub Desktop.
A notebook for benchmarking `fiona.transform.transform_geom` vs. `pyproj.Transformer` for reprojecting polygons.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pyproj\n", | |
"import shapely.geometry\n", | |
"from shapely.ops import transform\n", | |
"import fiona\n", | |
"import fiona.transform\n", | |
"from tqdm import tqdm" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"('3.6.1', '9.3.0', '1.10.1', '3.9.2')" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"pyproj.__version__, pyproj.__proj_version__, fiona.__version__, fiona.__gdal_version__" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"--2024-10-25 05:13:43-- https://gist.githubusercontent.com/sdwfrost/d1c73f91dd9d175998ed166eb216994a/raw/e89c35f308cee7e2e5a784e1d3afc5d449e9e4bb/counties.geojson\n", | |
"Resolving gist.githubusercontent.com (gist.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.109.133, ...\n", | |
"Connecting to gist.githubusercontent.com (gist.githubusercontent.com)|185.199.111.133|:443... connected.\n", | |
"HTTP request sent, awaiting response... 200 OK\n", | |
"Length: 2962196 (2.8M) [text/plain]\n", | |
"Saving to: ‘counties.geojson’\n", | |
"\n", | |
"counties.geojson 100%[===================>] 2.82M 671KB/s in 5.7s \n", | |
"\n", | |
"2024-10-25 05:13:50 (504 KB/s) - ‘counties.geojson’ saved [2962196/2962196]\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"!wget https://gist.githubusercontent.com/sdwfrost/d1c73f91dd9d175998ed166eb216994a/raw/e89c35f308cee7e2e5a784e1d3afc5d449e9e4bb/counties.geojson" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"geoms = []\n", | |
"with fiona.open(\"counties.geojson\") as f:\n", | |
" for row in f:\n", | |
" geoms.append(row['geometry'])\n", | |
"geoms = geoms * 3 # make more polygons" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"9660" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"len(geoms)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"epsg_4326 = pyproj.CRS('EPSG:4326')\n", | |
"epsg_6933 = pyproj.CRS('EPSG:6933')\n", | |
"\n", | |
"project = pyproj.Transformer.from_crs(epsg_4326, epsg_6933, always_xy=True).transform" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 9660/9660 [00:01<00:00, 5545.97it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1.72 s, sys: 27.5 ms, total: 1.75 s\n", | |
"Wall time: 1.74 s\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for i in tqdm(range(len(geoms))):\n", | |
" shape = shapely.geometry.shape(geoms[i])\n", | |
" result = transform(project, shape)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 9660/9660 [00:04<00:00, 2349.27it/s]" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 4.06 s, sys: 57.2 ms, total: 4.11 s\n", | |
"Wall time: 4.12 s\n" | |
] | |
}, | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"for i in tqdm(range(len(geoms))):\n", | |
" result = fiona.transform.transform_geom(epsg_4326, epsg_6933, geoms[i])\n", | |
" result = shapely.geometry.shape(result)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "bda", | |
"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.10.14" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment