Skip to content

Commit

Permalink
Port demo notebook to R
Browse files Browse the repository at this point in the history
  • Loading branch information
zacdezgeo committed Aug 20, 2024
1 parent 49b7f2a commit da87ba4
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 31 deletions.
62 changes: 31 additions & 31 deletions notebooks/space2stats_api_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -21,7 +21,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 16,
"metadata": {},
"outputs": [
{
Expand All @@ -54,7 +54,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -115,7 +115,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -138,7 +138,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -171,31 +171,31 @@
" <tr>\n",
" <th>0</th>\n",
" <td>866a4a48fffffff</td>\n",
" <td>POINT (35.76352 2.99589)</td>\n",
" <td>POINT (39.87766 1.69187)</td>\n",
" <td>399.860905</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>866a4a497ffffff</td>\n",
" <td>POINT (40.58048 -3.79365)</td>\n",
" <td>POINT (37.18818 -2.47520)</td>\n",
" <td>582.555159</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>866a4a49fffffff</td>\n",
" <td>POINT (41.10421 3.37873)</td>\n",
" <td>POINT (36.94975 4.02855)</td>\n",
" <td>749.911237</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>866a4a4d7ffffff</td>\n",
" <td>POINT (37.26153 3.74581)</td>\n",
" <td>POINT (37.92927 -3.32703)</td>\n",
" <td>863.888290</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>866a5820fffffff</td>\n",
" <td>POINT (40.01148 1.53124)</td>\n",
" <td>POINT (39.96057 -0.72907)</td>\n",
" <td>525.085147</td>\n",
" </tr>\n",
" <tr>\n",
Expand All @@ -207,31 +207,31 @@
" <tr>\n",
" <th>16212</th>\n",
" <td>867b5dd77ffffff</td>\n",
" <td>POINT (34.94474 1.24558)</td>\n",
" <td>POINT (37.04080 2.92349)</td>\n",
" <td>-36.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16213</th>\n",
" <td>867b5dd87ffffff</td>\n",
" <td>POINT (40.95343 -1.83280)</td>\n",
" <td>POINT (39.46713 -3.53058)</td>\n",
" <td>-36.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16214</th>\n",
" <td>867b5dd8fffffff</td>\n",
" <td>POINT (35.20290 -0.29666)</td>\n",
" <td>POINT (36.52175 -2.04593)</td>\n",
" <td>-36.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16215</th>\n",
" <td>867b5dd9fffffff</td>\n",
" <td>POINT (41.28333 -1.08552)</td>\n",
" <td>POINT (36.12156 1.22229)</td>\n",
" <td>-36.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16216</th>\n",
" <td>867b5ddafffffff</td>\n",
" <td>POINT (36.63048 1.35038)</td>\n",
" <td>POINT (37.25642 1.10361)</td>\n",
" <td>-36.000000</td>\n",
" </tr>\n",
" </tbody>\n",
Expand All @@ -241,22 +241,22 @@
],
"text/plain": [
" hex_id geometry sum_pop_2020\n",
"0 866a4a48fffffff POINT (35.76352 2.99589) 399.860905\n",
"1 866a4a497ffffff POINT (40.58048 -3.79365) 582.555159\n",
"2 866a4a49fffffff POINT (41.10421 3.37873) 749.911237\n",
"3 866a4a4d7ffffff POINT (37.26153 3.74581) 863.888290\n",
"4 866a5820fffffff POINT (40.01148 1.53124) 525.085147\n",
"0 866a4a48fffffff POINT (39.87766 1.69187) 399.860905\n",
"1 866a4a497ffffff POINT (37.18818 -2.47520) 582.555159\n",
"2 866a4a49fffffff POINT (36.94975 4.02855) 749.911237\n",
"3 866a4a4d7ffffff POINT (37.92927 -3.32703) 863.888290\n",
"4 866a5820fffffff POINT (39.96057 -0.72907) 525.085147\n",
"... ... ... ...\n",
"16212 867b5dd77ffffff POINT (34.94474 1.24558) -36.000000\n",
"16213 867b5dd87ffffff POINT (40.95343 -1.83280) -36.000000\n",
"16214 867b5dd8fffffff POINT (35.20290 -0.29666) -36.000000\n",
"16215 867b5dd9fffffff POINT (41.28333 -1.08552) -36.000000\n",
"16216 867b5ddafffffff POINT (36.63048 1.35038) -36.000000\n",
"16212 867b5dd77ffffff POINT (37.04080 2.92349) -36.000000\n",
"16213 867b5dd87ffffff POINT (39.46713 -3.53058) -36.000000\n",
"16214 867b5dd8fffffff POINT (36.52175 -2.04593) -36.000000\n",
"16215 867b5dd9fffffff POINT (36.12156 1.22229) -36.000000\n",
"16216 867b5ddafffffff POINT (37.25642 1.10361) -36.000000\n",
"\n",
"[16217 rows x 3 columns]"
]
},
"execution_count": 20,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -269,24 +269,24 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "00bbfea95ae440d3a73ebb161e3142ab",
"model_id": "d8817abae9f54705b2b7966fd24a82e6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(layers=[ScatterplotLayer(get_fill_color=<pyarrow.lib.FixedSizeListArray object at 0x1631ef160>\n",
"Map(layers=[ScatterplotLayer(get_fill_color=<pyarrow.lib.FixedSizeListArray object at 0x169bf7e20>\n",
"[\n",
" [\n",
" 2…"
]
},
"execution_count": 21,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
125 changes: 125 additions & 0 deletions notebooks/space2stats_api_demo_R.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: "Space2Stats API Demo in R"
output: html_notebook
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(httr)
library(jsonlite)
library(sf)
library(dplyr)
library(leaflet)
library(viridis)
```

## Set Up API Endpoints

```{r}
base_url <- "https://space2stats.ds.io"
fields_endpoint <- paste0(base_url, "/fields")
summary_endpoint <- paste0(base_url, "/summary")
```

## Fetch Available Fields

```{r}
response <- GET(fields_endpoint)
if (status_code(response) != 200) {
stop("Failed to get fields: ", content(response, "text"))
}
available_fields <- content(response, "parsed")
print("Available Fields:")
print(unlist(available_fields))
```

## Define Area of Interest (AOI)

```{r}
# Define Area of Interest (AOI) with NULL for properties to ensure it's treated as a valid dictionary
aoi <- list(
type = "Feature",
properties = NULL, # Empty properties
geometry = list(
type = "Polygon",
coordinates = list(
list(
c(33.78593974945852, 5.115816884114494),
c(33.78593974945852, -4.725410543134203),
c(41.94362577283266, -4.725410543134203),
c(41.94362577283266, 5.115816884114494),
c(33.78593974945852, 5.115816884114494)
)
)
)
)
```

## Request Summary Data

```{r}
request_payload <- list(
aoi = aoi,
spatial_join_method = "centroid",
fields = list("sum_pop_2020"),
geometry = "point"
)
response <- POST(summary_endpoint, body = toJSON(request_payload, auto_unbox = TRUE), encode = "json")
if (status_code(response) != 200) {
stop("Failed to get summary data: ", content(response, "text"))
}
summary_data <- content(response, "parsed")
```

## Convert to Spatial DataFrame

```{r}
# Assuming summary_data is a list of features
df_list <- lapply(summary_data, function(x) {
# Extract the coordinates and create a POINT geometry
geom <- st_point(c(x$geometry$coordinates[[1]], x$geometry$coordinates[[2]]))
# Convert the geometry into a simple feature geometry (sfc)
geom <- st_sfc(geom, crs = 4326) # Assuming the coordinates are in WGS 84
# Combine the hex_id, properties, and geometry into a data frame
data.frame(
hex_id = x$hex_id, # Include hex_id
sum_pop_2020 = x$sum_pop_2020, # Include other properties
geometry = geom
)
})
# Combine all the individual data frames into one
df <- do.call(rbind, df_list)
# Convert to a spatial data frame
gdf <- st_sf(df)
```

## 6. Visualization

```{r}
# Replace NA values in sum_pop_2020 with 0
gdf$sum_pop_2020[is.na(gdf$sum_pop_2020)] <- 0
# Create a binned color palette with a fixed number of intervals (e.g., 7)
bin_pal <- colorBin(palette = "viridis", domain = gdf$sum_pop_2020, bins = 7, pretty = TRUE)
# Create the leaflet map with binned coloring
leaflet(gdf) %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addCircleMarkers(
radius = 3, # Adjust size as needed
color = ~bin_pal(sum_pop_2020),
stroke = FALSE, fillOpacity = 0.7,
popup = ~paste("Hex ID:", hex_id, "<br>", "Population 2020:", sum_pop_2020) # Add a popup with details
) %>%
addLegend(
pal = bin_pal, values = gdf$sum_pop_2020, title = "Population 2020 (Binned Scale)",
opacity = 1
) %>%
setView(lng = 37.5, lat = 0, zoom = 6)
```

0 comments on commit da87ba4

Please sign in to comment.