{ "cells": [ { "cell_type": "markdown", "id": "1d11e6d1-8b7c-4a29-a75e-f6963e625fc9", "metadata": {}, "source": [ "# Loading GeoTiffs" ] }, { "cell_type": "code", "execution_count": 1, "id": "625150b3-bf52-4a11-92f7-c0f5767c8b01", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:27.651751Z", "iopub.status.busy": "2022-09-15T05:05:27.651444Z", "iopub.status.idle": "2022-09-15T05:05:29.213680Z", "shell.execute_reply": "2022-09-15T05:05:29.212750Z" } }, "outputs": [], "source": [ "import starepandas\n", "import rasterio\n", "import numpy\n", "import geopandas\n", "import pyproj\n", "import pystare" ] }, { "cell_type": "markdown", "id": "30cd8f67-1fef-495c-b81a-f29b6a0f17ed", "metadata": {}, "source": [ "## Usage" ] }, { "cell_type": "code", "execution_count": 2, "id": "55d6abb3-7aea-477e-8a73-890382800e7e", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.216731Z", "iopub.status.busy": "2022-09-15T05:05:29.216180Z", "iopub.status.idle": "2022-09-15T05:05:29.219155Z", "shell.execute_reply": "2022-09-15T05:05:29.218789Z" } }, "outputs": [], "source": [ "file_path = '../tests/data/wind.tif'" ] }, { "cell_type": "code", "execution_count": 3, "id": "6e2599a3-65aa-4a34-810a-49555d47c594", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.221475Z", "iopub.status.busy": "2022-09-15T05:05:29.221236Z", "iopub.status.idle": "2022-09-15T05:05:29.315198Z", "shell.execute_reply": "2022-09-15T05:05:29.314690Z" } }, "outputs": [], "source": [ "sdf = starepandas.read_geotiff(file_path, add_pts=True, add_latlon=True,\n", " add_coordinates=True, add_xy=True, add_trixels=True)" ] }, { "cell_type": "code", "execution_count": 4, "id": "8e294abd-0050-4619-9fd4-f127f846bbcc", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.317681Z", "iopub.status.busy": "2022-09-15T05:05:29.317411Z", "iopub.status.idle": "2022-09-15T05:05:29.337898Z", "shell.execute_reply": "2022-09-15T05:05:29.337467Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
band_1lonlatixiyxygeometry
06.272615-120.65005534.9726644.01.0-59314.902344-338019.9375POINT (-120.65006 34.97266)
16.252211-120.64731634.9726795.01.0-59064.902344-338019.9375POINT (-120.64732 34.97268)
26.258501-120.65004034.9704094.02.0-59314.902344-338269.9375POINT (-120.65004 34.97041)
36.258501-120.64730134.9704255.02.0-59064.902344-338269.9375POINT (-120.64730 34.97043)
46.232330-120.64456234.9704406.02.0-58814.902344-338269.9375POINT (-120.64456 34.97044)
...........................
885.839790-120.62251334.95480014.09.0-56814.902344-340019.9375POINT (-120.62252 34.95480)
895.825665-120.61977434.95481515.09.0-56564.902344-340019.9375POINT (-120.61978 34.95482)
905.817735-120.61703534.95483016.09.0-56314.902344-340019.9375POINT (-120.61704 34.95483)
915.817735-120.61429634.95484517.09.0-56064.902344-340019.9375POINT (-120.61430 34.95484)
925.815444-120.61155734.95486118.09.0-55814.902344-340019.9375POINT (-120.61156 34.95486)
\n", "

93 rows × 8 columns

\n", "
" ], "text/plain": [ " band_1 lon lat ix iy x y \\\n", "0 6.272615 -120.650055 34.972664 4.0 1.0 -59314.902344 -338019.9375 \n", "1 6.252211 -120.647316 34.972679 5.0 1.0 -59064.902344 -338019.9375 \n", "2 6.258501 -120.650040 34.970409 4.0 2.0 -59314.902344 -338269.9375 \n", "3 6.258501 -120.647301 34.970425 5.0 2.0 -59064.902344 -338269.9375 \n", "4 6.232330 -120.644562 34.970440 6.0 2.0 -58814.902344 -338269.9375 \n", ".. ... ... ... ... ... ... ... \n", "88 5.839790 -120.622513 34.954800 14.0 9.0 -56814.902344 -340019.9375 \n", "89 5.825665 -120.619774 34.954815 15.0 9.0 -56564.902344 -340019.9375 \n", "90 5.817735 -120.617035 34.954830 16.0 9.0 -56314.902344 -340019.9375 \n", "91 5.817735 -120.614296 34.954845 17.0 9.0 -56064.902344 -340019.9375 \n", "92 5.815444 -120.611557 34.954861 18.0 9.0 -55814.902344 -340019.9375 \n", "\n", " geometry \n", "0 POINT (-120.65006 34.97266) \n", "1 POINT (-120.64732 34.97268) \n", "2 POINT (-120.65004 34.97041) \n", "3 POINT (-120.64730 34.97043) \n", "4 POINT (-120.64456 34.97044) \n", ".. ... \n", "88 POINT (-120.62252 34.95480) \n", "89 POINT (-120.61978 34.95482) \n", "90 POINT (-120.61704 34.95483) \n", "91 POINT (-120.61430 34.95484) \n", "92 POINT (-120.61156 34.95486) \n", "\n", "[93 rows x 8 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sdf" ] }, { "cell_type": "markdown", "id": "5cf5192b-df36-48e2-99b9-a6b30f6505da", "metadata": {}, "source": [ "## Detail:" ] }, { "cell_type": "markdown", "id": "f004c8b3-3bd9-4a7b-a61b-ff3e483e2c6c", "metadata": {}, "source": [ "This is a geotiff containing average wind speeds in Santa Barbara county. It is projected in California Albers (EPSG:3310).\n", "Note that the raster is clipped to the county outline, thus containing NaNs" ] }, { "cell_type": "code", "execution_count": 5, "id": "0542db9f-e3cf-4409-8335-2e6d47ae029a", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.339793Z", "iopub.status.busy": "2022-09-15T05:05:29.339628Z", "iopub.status.idle": "2022-09-15T05:05:29.346617Z", "shell.execute_reply": "2022-09-15T05:05:29.345932Z" } }, "outputs": [], "source": [ "with rasterio.open(file_path) as src:\n", " values = {}\n", " for band in range(1, src.count+1): \n", " values[f'band_{band}'] = src.read(band)\n", " height = values['band_1'].shape[0]\n", " width = values['band_1'].shape[1] \n", " cols, rows = numpy.meshgrid(numpy.arange(width), numpy.arange(height))\n", " xs, ys = rasterio.transform.xy(src.transform, rows, cols)\n", " xs = numpy.array(xs)\n", " ys = numpy.array(ys)\n", " src_crs = src.crs" ] }, { "cell_type": "code", "execution_count": 6, "id": "d5e31f7f-6d4d-42c7-832c-b5b332f2d157", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.348655Z", "iopub.status.busy": "2022-09-15T05:05:29.348468Z", "iopub.status.idle": "2022-09-15T05:05:29.394495Z", "shell.execute_reply": "2022-09-15T05:05:29.394000Z" } }, "outputs": [], "source": [ "dst_crs = 'EPSG:4326'\n", "transformer = pyproj.Transformer.from_crs(src_crs, dst_crs)\n", "lats, lons = transformer.transform(xs, ys)\n", "sids = pystare.from_latlon_2d(lats, lons, adapt_level=True)" ] }, { "cell_type": "code", "execution_count": 7, "id": "f0f7b44d-c675-44b5-84fa-6724f5fff425", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.396728Z", "iopub.status.busy": "2022-09-15T05:05:29.396449Z", "iopub.status.idle": "2022-09-15T05:05:29.399471Z", "shell.execute_reply": "2022-09-15T05:05:29.399035Z" } }, "outputs": [], "source": [ "for key in values:\n", " values[key] = values[key].flatten()" ] }, { "cell_type": "code", "execution_count": 8, "id": "5efbb3b4-f388-461f-baf9-da77ac97402d", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.401548Z", "iopub.status.busy": "2022-09-15T05:05:29.401336Z", "iopub.status.idle": "2022-09-15T05:05:29.414113Z", "shell.execute_reply": "2022-09-15T05:05:29.413579Z" } }, "outputs": [], "source": [ "pts = geopandas.points_from_xy(lons.flatten(), lats.flatten())\n", "gdf = geopandas.GeoDataFrame(values, geometry=pts, crs=dst_crs)\n", "gdf = starepandas.STAREDataFrame(gdf, sids=sids.flatten())\n", "gdf.dropna(inplace=True)\n", "gdf.reset_index(inplace=True, drop=True)" ] }, { "cell_type": "code", "execution_count": 9, "id": "4e998c2d-92d3-4af7-b7fa-22fce6a419eb", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.416654Z", "iopub.status.busy": "2022-09-15T05:05:29.416435Z", "iopub.status.idle": "2022-09-15T05:05:29.418879Z", "shell.execute_reply": "2022-09-15T05:05:29.418472Z" } }, "outputs": [], "source": [ "#gdf.to_file('wind.gpkg', driver='GPKG')" ] }, { "cell_type": "code", "execution_count": 10, "id": "7f924d52-9f96-4a66-8880-14feb52712ed", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:05:29.420894Z", "iopub.status.busy": "2022-09-15T05:05:29.420586Z", "iopub.status.idle": "2022-09-15T05:05:29.432306Z", "shell.execute_reply": "2022-09-15T05:05:29.431805Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
band_1geometrysids
06.272615POINT (-120.65006 34.97266)3331746293595969199
16.252211POINT (-120.64732 34.97268)3331745340069319375
26.258501POINT (-120.65004 34.97041)3331746259443112015
36.258501POINT (-120.64730 34.97043)3331745271579949615
46.232330POINT (-120.64456 34.97044)3331745341877499695
............
885.839790POINT (-120.62252 34.95480)3331745323129218159
895.825665POINT (-120.61978 34.95482)3331745322842829231
905.817735POINT (-120.61704 34.95483)3331745305821847439
915.817735POINT (-120.61430 34.95484)3331745306553027855
925.815444POINT (-120.61156 34.95486)3331745573111838703
\n", "

93 rows × 3 columns

\n", "
" ], "text/plain": [ " band_1 geometry sids\n", "0 6.272615 POINT (-120.65006 34.97266) 3331746293595969199\n", "1 6.252211 POINT (-120.64732 34.97268) 3331745340069319375\n", "2 6.258501 POINT (-120.65004 34.97041) 3331746259443112015\n", "3 6.258501 POINT (-120.64730 34.97043) 3331745271579949615\n", "4 6.232330 POINT (-120.64456 34.97044) 3331745341877499695\n", ".. ... ... ...\n", "88 5.839790 POINT (-120.62252 34.95480) 3331745323129218159\n", "89 5.825665 POINT (-120.61978 34.95482) 3331745322842829231\n", "90 5.817735 POINT (-120.61704 34.95483) 3331745305821847439\n", "91 5.817735 POINT (-120.61430 34.95484) 3331745306553027855\n", "92 5.815444 POINT (-120.61156 34.95486) 3331745573111838703\n", "\n", "[93 rows x 3 columns]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gdf" ] }, { "cell_type": "code", "execution_count": null, "id": "c40f5ed7-a858-44ab-9e06-04eb1a8629c9", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }