{ "cells": [ { "cell_type": "code", "execution_count": 9, "id": "b313a218-4778-4d5b-9036-f0370d4212a0", "metadata": {}, "outputs": [], "source": [ "import ibis\n", "from ibis import _\n", "import streamlit as st\n", "import ibis.expr.datatypes as dt # Make sure to import the necessary module\n", "\n", "\n", "conn = ibis.duckdb.connect(extensions=[\"spatial\"])\n", "\n", "state_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-state-territory.parquet\"\n", "county_boundaries = \"https://data.source.coop/cboettig/us-boundaries/us-county.parquet\"\n", "states = conn.read_parquet(state_boundaries).rename(state_id = \"STUSPS\", state = \"NAME\")\n", "county = conn.read_parquet(county_boundaries).rename(county = \"NAMELSAD\", state = \"STATE_NAME\")\n", "\n", "localities_boundaries = \"us_localities.parquet\"\n", "locality = conn.read_parquet(localities_boundaries)\n", "\n", "\n", "votes = conn.read_csv(\"landvote.csv\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "ba4d8915-cde3-4ef9-ad8c-7759ed2c8a13", "metadata": {}, "outputs": [], "source": [ "vote_county = (votes\n", " .filter(_[\"Jurisdiction Type\"] == \"County\")\n", " .rename(county = \"Jurisdiction Name\", state_id = \"State\")\n", " .mutate(key = _.county + ibis.literal('-') + _.state_id)\n", " .rename(amount = 'Conservation Funds at Stake', yes = '% Yes')\n", " .mutate(amount_n=_.amount.replace('$', '').replace(',', '').cast('float'))\n", " .mutate(log_amount=_.amount_n.log())\n", " .mutate(year=_['Date'].year().cast('int32'))\n", " .mutate(\n", " yes=ibis.case()\n", " .when(_.yes.isin(['Pass', 'None','Fail']), None) # Handle non-numeric cases\n", " .when(_.yes.notnull(), (_.yes.replace('%', '').cast('float').round(2).cast(dt.float64)).cast(dt.string) + '%') # Convert valid percentages and add %\n", " .else_(None) # Default to None for other cases\n", " .end()\n", " )\n", " .mutate(log_amount = _.log_amount.round(4))\n", " .select('key', 'Status', 'yes', 'year', 'amount', 'log_amount', )\n", " )\n", "df_county = (county\n", " .join(states.select(\"state\", \"state_id\"), \"state\")\n", " .mutate(key = _.county + ibis.literal('-') + _.state_id)\n", " .select('key', 'geometry')\n", " .right_join(vote_county, \"key\")\n", " .drop('key_right')\n", " .mutate(jurisdiction = ibis.literal(\"County\"))\n", " .cast({\"geometry\": \"geometry\"})\n", " )\n" ] }, { "cell_type": "code", "execution_count": 11, "id": "0cce23c9-245c-4c28-9523-0231eb5acc17", "metadata": {}, "outputs": [], "source": [ "vote_local = (votes\n", " .filter(_[\"Jurisdiction Type\"] == \"Municipal\")\n", " .rename(city = \"Jurisdiction Name\", state_id = \"State\")\n", " .mutate(key = _.city + ibis.literal('-') + _.state_id)\n", " .rename(amount = 'Conservation Funds at Stake', yes = '% Yes')\n", " .mutate(amount_n=_.amount.replace('$', '').replace(',', '').cast('float'))\n", " .mutate(log_amount=_.amount_n.log())\n", " .mutate(year=_['Date'].year().cast('int32'))\n", " .mutate(\n", " yes=ibis.case()\n", " .when(_.yes.isin(['Pass', 'None','Fail']), None) # Handle non-numeric cases\n", " .when(_.yes.notnull(), (_.yes.replace('%', '').cast('float').round(2).cast(dt.float64)).cast(dt.string) + '%') # Convert valid percentages and add %\n", " .else_(None) # Default to None for other cases\n", " .end()\n", " )\n", " .mutate(log_amount = _.log_amount.round(4))\n", " .select('key', 'Status', 'yes', 'year', 'amount', 'log_amount', )\n", " )\n", "\n", "df_local = (locality\n", " .mutate(key = _.name + ibis.literal('-') + _.state_id)\n", " .select('key', 'geometry')\n", " .right_join(vote_local, \"key\")\n", " .drop('key_right')\n", " .mutate(jurisdiction = ibis.literal(\"Municipal\"))\n", " .cast({\"geometry\": \"geometry\"})\n", " .mutate(geometry = _.geometry.buffer(.07))\n", " )\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "a1e81807-8ce3-44bf-9a1c-8563fa33817c", "metadata": {}, "outputs": [], "source": [ "df = df_county.union(df_local)\n", "# df.execute()" ] }, { "cell_type": "code", "execution_count": 13, "id": "5d3bee26-7ca8-490c-be5b-fc69a6c3db2a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The token has not been saved to the git credentials helper. Pass `add_to_git_credential=True` in this function directly or `--add-to-git-credential` if using via `huggingface-cli` if you want to set the git credential as well.\n", "Token is valid (permission: write).\n", "Your token has been saved to /home/rstudio/.cache/huggingface/token\n", "Login successful\n" ] } ], "source": [ "import subprocess\n", "import os\n", "from huggingface_hub import HfApi, login\n", "import streamlit as st\n", "\n", "login(st.secrets[\"HF_TOKEN\"])\n", "# api = HfApi(add_to_git_credential=False)\n", "api = HfApi()\n", "\n", "def hf_upload(file, repo_id):\n", " info = api.upload_file(\n", " path_or_fileobj=file,\n", " path_in_repo=file,\n", " repo_id=repo_id,\n", " repo_type=\"dataset\",\n", " )\n", "def generate_pmtiles(input_file, output_file, max_zoom=12):\n", " # Ensure Tippecanoe is installed\n", " if subprocess.call([\"which\", \"tippecanoe\"], stdout=subprocess.DEVNULL) != 0:\n", " raise RuntimeError(\"Tippecanoe is not installed or not in PATH\")\n", "\n", " # Construct the Tippecanoe command\n", " command = [\n", " \"tippecanoe\",\n", " \"-o\", output_file,\n", " \"-zg\",\n", " \"--extend-zooms-if-still-dropping\",\n", " \"--force\",\n", " \"--projection\", \"EPSG:4326\", \n", " input_file\n", " ]\n", "\n", " # Run Tippecanoe\n", " try:\n", " subprocess.run(command, check=True)\n", " print(f\"Successfully generated PMTiles file: {output_file}\")\n", " except subprocess.CalledProcessError as e:\n", " print(f\"Error running Tippecanoe: {e}\")\n", "\n" ] }, { "cell_type": "code", "execution_count": 14, "id": "902ece1d-6e57-4f83-9286-709dbb549fae", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2b18fcc2775845c4a3b40eedbe32475b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FloatProgress(value=0.0, layout=Layout(width='auto'), style=ProgressStyle(bar_color='black'))" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c7314c0c7d9948c9a3feaddadccde55b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "vote.geojson: 0%| | 0.00/92.9M [00:00