File size: 7,454 Bytes
c67e57e
 
02c6b99
 
 
 
 
 
 
 
c67e57e
 
a47ab28
02c6b99
c67e57e
 
 
02c6b99
 
c67e57e
02c6b99
 
c67e57e
02c6b99
c67e57e
02c6b99
45d9637
 
c67e57e
 
 
 
 
a47ab28
899836e
c67e57e
 
 
02c6b99
 
 
 
 
 
 
 
899836e
 
 
 
 
 
 
 
 
 
 
 
c67e57e
02c6b99
 
45d9637
02c6b99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a47ab28
02c6b99
 
 
 
 
2cc9bf8
 
02c6b99
 
 
 
 
 
 
 
 
 
 
c67e57e
6a1a3b0
02c6b99
 
 
 
 
 
 
 
 
 
a47ab28
02c6b99
6a1a3b0
a47ab28
6a1a3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02c6b99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a1a3b0
 
02c6b99
 
 
a47ab28
02c6b99
 
a47ab28
02c6b99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a47ab28
02c6b99
 
a47ab28
02c6b99
 
 
 
 
c67e57e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45d9637
c67e57e
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9fe02137-ba1c-4ced-9909-b5b7c39ed6d4",
   "metadata": {},
   "source": [
    "# Merging state/county/city polygons with party affiliation and landvote data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6452373c-d10f-472c-9174-cd05a3363587",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ibis\n",
    "from ibis import _\n",
    "\n",
    "import streamlit as st\n",
    "import ibis.expr.datatypes as dt  # Make sure to import the necessary module\n",
    "\n",
    "conn = ibis.duckdb.connect(extensions=[\"spatial\"])\n",
    "\n",
    "landvote_url = \"https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/landvote_polygons.parquet\"\n",
    "# party_url = \"https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/party_polygons.parquet\"\n",
    "party_url = \"https://huggingface.co/datasets/boettiger-lab/landvote/resolve/main/party_polygons_all.parquet\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd9ffc64-b4cf-4e5b-9c96-703e91a77837",
   "metadata": {},
   "outputs": [],
   "source": [
    "landvote = (conn\n",
    "            .read_parquet(landvote_url)\n",
    "            .cast({\"geometry\": \"geometry\"})\n",
    "            .mutate(county = _.county.upper())\n",
    "            .mutate(municipal = _.municipal.upper())\n",
    "            .mutate(elect_year = _.year - _.year % 4) # get most recent election year \n",
    "            .cast({\"municipal\": \"string\",\"county\":\"string\"})\n",
    "            .mutate(municipal=ibis.case()\n",
    "                 .when(_.jurisdiction.isin(['State','County']), ibis.literal(\"-\")) \n",
    "                 .else_(_.municipal) \n",
    "                 .end()\n",
    "                 )\n",
    "            .mutate(county=ibis.case()\n",
    "                 .when(_.jurisdiction.isin(['State']), ibis.literal(\"-\"))\n",
    "                 .else_(ibis.case()\n",
    "                         .when(_.county.endswith('COUNTY'), _.county)\n",
    "                         .else_(_.county + ' COUNTY')\n",
    "                         .end())\n",
    "                 .end())\n",
    "           )\n",
    "\n",
    "party = (conn\n",
    "        .read_parquet(party_url)\n",
    "        .cast({\"geometry\": \"geometry\",\"municipal\":\"string\"})\n",
    "        .mutate(municipal=ibis.case()\n",
    "                .when(_.jurisdiction.isin(['State','County']), ibis.literal(\"-\")) \n",
    "                .else_(_.municipal) \n",
    "                .end()\n",
    "                )\n",
    "        .mutate(county=ibis.case()\n",
    "                .when(_.jurisdiction.isin(['State']), ibis.literal(\"-\")) \n",
    "                .else_(_.county) \n",
    "                .end()\n",
    "                )\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fc40e96-fffd-4b23-9963-c931fdce96f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "votes = (landvote\n",
    "        .join(party,[\"state\",\"county\",\"municipal\",\"jurisdiction\",\"geometry\", _.elect_year == party[\"year\"]],how = \"left\")\n",
    "        .drop('elect_year','state_right','county_right','municipal_right','year_right',\"geometry_right\",\"jurisdiction_right\")\n",
    "        .mutate(municipal=ibis.case()\n",
    "                .when(_.municipal == ibis.literal(\"-\"), None) \n",
    "                .else_(_.municipal) \n",
    "                .end()\n",
    "                )\n",
    "        .mutate(county=ibis.case()\n",
    "                .when(_.county == ibis.literal(\"-\"), None) \n",
    "                .else_(_.county) \n",
    "                .end()\n",
    "                )\n",
    "        )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e80cfd2e-40eb-4065-9ae6-dcaf83319d9a",
   "metadata": {},
   "source": [
    "# Make PMTiles. Each jurisdiction type is its own layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1cd8f44-57fa-49a8-b438-f9e4aab747c5",
   "metadata": {},
   "outputs": [],
   "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, input_file2, input_file3, 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",
    "        \"-L\",\"state:\"+input_file,\n",
    "        \"-L\",\"county:\"+input_file2,\n",
    "        \"-L\",\"municipal:\"+input_file3\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": null,
   "id": "7061577e-0632-4395-8ac5-241a1fab53b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf_state = votes.filter(_.jurisdiction == 'State').execute().set_crs(\"EPSG:4326\")\n",
    "gdf_state.to_file(\"votes_state.geojson\")\n",
    "\n",
    "gdf_county = votes.filter(_.jurisdiction == 'County').execute().set_crs(\"EPSG:4326\")\n",
    "gdf_county.to_file(\"votes_county.geojson\")\n",
    "\n",
    "gdf_city = votes.filter(_.jurisdiction == 'Municipal').execute().set_crs(\"EPSG:4326\")\n",
    "gdf_city.to_file(\"votes_municipal.geojson\")\n",
    "\n",
    "generate_pmtiles(\"votes_state.geojson\", \"votes_county.geojson\",\"votes_municipal.geojson\", \"votes.pmtiles\")\n",
    "hf_upload(\"votes.pmtiles\", \"boettiger-lab/landvote\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2979624-bcdf-4a8a-899a-c22fc3cdaf0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save as parquet\n",
    "votes.execute().set_crs(\"EPSG:4326\").to_parquet(\"votes.parquet\")\n",
    "hf_upload(\"votes.parquet\", \"boettiger-lab/landvote\")\n"
   ]
  }
 ],
 "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.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}