Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Created October 5, 2022 19:49
Show Gist options
  • Save jdbcode/490b567812db4c95bbe6a3c19fe30a55 to your computer and use it in GitHub Desktop.
Save jdbcode/490b567812db4c95bbe6a3c19fe30a55 to your computer and use it in GitHub Desktop.
g4g22_feature_collection_to_csv.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNgdUapalBFXo56bh4NQfOU",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jdbcode/490b567812db4c95bbe6a3c19fe30a55/g4g22_feature_collection_to_csv.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "r6Lw9A7kCIXV"
},
"outputs": [],
"source": [
"import ee\n",
"ee.Authenticate()\n",
"ee.Initialize()"
]
},
{
"cell_type": "code",
"source": [
"import pandas as pd\n",
"import altair as alt\n",
"import numpy as np\n",
"import io\n",
"import requests"
],
"metadata": {
"id": "l6XrOYDkCbxo"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"today = ee.Date(pd.to_datetime('today'))\n",
"date_range = ee.DateRange(today.advance(-20, 'years'), today)\n",
"pdsi = ee.ImageCollection('GRIDMET/DROUGHT').filterDate(date_range).select('pdsi')\n",
"aoi = ee.FeatureCollection('EPA/Ecoregions/2013/L3').filter(\n",
" ee.Filter.eq('na_l3name', 'Sierra Nevada')).geometry()"
],
"metadata": {
"id": "z5TxdJANCNeV"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def reduce(img):\n",
" stat = img.reduceRegion(**{\n",
" 'reducer': ee.Reducer.mean(),\n",
" 'geometry': aoi,\n",
" 'scale': 5000\n",
" })\n",
" return ee.Feature(aoi, stat).set('date', img.date().format('YYYY-MM-dd'))\n",
"\n",
"pdsi_median = ee.FeatureCollection(pdsi.map(reduce))"
],
"metadata": {
"id": "dJnYb2U3Cz36"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"url = pdsi_median.getDownloadURL(\n",
" **{'filetype': 'csv', 'selectors': pdsi_median.first().propertyNames().getInfo()})"
],
"metadata": {
"id": "XVNvHE1aE8cp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def get_pd_from_url(url):\n",
" return pd.read_csv(io.StringIO(requests.get(url).content.decode('utf-8')))"
],
"metadata": {
"id": "ZiQ_WNBVGhBp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"pdsi_df = get_pd_from_url(url)"
],
"metadata": {
"id": "91DPj-hOXpvT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"pdsi_df"
],
"metadata": {
"id": "2YTO4FQhZQTP"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"alt.Chart(pdsi_df).mark_bar(size=1).encode(\n",
" x='date:T',\n",
" y='pdsi:Q',\n",
" color=alt.Color(\n",
" 'pdsi:Q', scale=alt.Scale(scheme='redblue', domain=(-5, 5))),\n",
" tooltip=[\n",
" alt.Tooltip('Timestamp:T', title='Date'),\n",
" alt.Tooltip('pdsi:Q', title='PDSI')\n",
" ]).properties(width=600, height=300)"
],
"metadata": {
"id": "1w8ft-QlXsj4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# In R!"
],
"metadata": {
"id": "jqvNoP-Ondin"
}
},
{
"cell_type": "code",
"source": [
"%reload_ext rpy2.ipython"
],
"metadata": {
"id": "oi648u0ErEvm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%R\n",
"library(\"readr\")"
],
"metadata": {
"id": "juUh10wEbyOw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%R -i url"
],
"metadata": {
"id": "OitneUnNcSZr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"%%R\n",
"data = read_csv(url)\n",
"head(data)"
],
"metadata": {
"id": "l_uloDkMcCRV"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment