Skip to content

Commit

Permalink
add sample to fetch API by points in CSV (#349)
Browse files Browse the repository at this point in the history
* add sample to fetch API by points in CSV

* add link to another example in doc
  • Loading branch information
danangmassandy authored Jan 14, 2025
1 parent 59c4f0d commit 35dbabc
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/src/developer/api/guide/access-api-using-r.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This tutorial demonstrates how to download a NetCDF file from GAP API, save it l

- You can download the full code from [here](https://github.com/kartoza/tomorrownow_gap/blob/main/examples/sample.R).
- Once you have downloaded the sample code, then you can open the file from RStudio (File -> Open File).
- Other example [fetch by points](https://github.com/kartoza/tomorrownow_gap/blob/main/examples/sample_points.R) is to show an example fetching the data by list of point from CSV file.

2. **Set the username and password**

Expand Down
5 changes: 1 addition & 4 deletions examples/sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ query_params <- list(
# location_name = ''
)

# Define user agent of the request
user_agent_string <- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"

# Set the output file path
output_file <- "data.nc"

Expand All @@ -47,7 +44,7 @@ response <- GET(
url = base_url,
authenticate(username, password),
query = query_params,
add_headers(`User-Agent` = user_agent_string),
user_agent("httr"),
write_disk(output_file, overwrite = TRUE)
)
# Check the response
Expand Down
81 changes: 81 additions & 0 deletions examples/sample_points.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# library for HTTP request
library(httr)

# working directory for output data
# NOTE: on windows, the path should use double backslashes ("\\")
working_dir <- '~/Documents/gap_data'
dir.create(working_dir, showWarnings = FALSE)

# GAP API Base URL
base_url <- 'https://tngap.sta.do.kartoza.com/api/v1/measurement/'

# Set your username and password
username <- ''
password <- ''

# the csv has columns: locationId,latitude,longitude
csv_file_path <- file.path(working_dir, 'points.csv')

# Load CSV into a DataFrame
input_points <- read.csv(csv_file_path, header = TRUE)

# Initialize an empty list to store dataframes
dataframes <- list()

for (i in 1:nrow(input_points)) {
# Define the request parameters
query_params <- list(
# product type and attribute list can be viewed in
# https://kartoza.github.io/tomorrownow_gap/developer/api/guide/measurement/#gap-input-data-table
product = 'cbam_historical_analysis',
# comma separated string (without space)
attributes = 'max_temperature,min_temperature',
# start and end dates in format YYYY-MM-DD
start_date = '2020-01-01',
end_date = '2020-01-03',
# available output type: json, csv, netcdf
# Note that json output is only for single location query
output_type = 'csv',
# area bounding box (long min, lat min, long max, lat max)
# bbox = '33.9, -4.67, 41.89, 5.5'
# for single point query, we can use lat and lon parameters
lat = input_points$latitude[i],
lon = input_points$longitude[i]
# for custom polygon/bounding box, you can upload a shapefile and provides the location_name
# location_name = ''
)

# Set the output file path
output_file <- file.path(working_dir, paste0("data", "_lat", input_points$latitude[i], "_lon", input_points$longitude[i], ".csv"))
print(output_file)

# Trigger the API call
response <- GET(
url = base_url,
authenticate(username, password),
query = query_params,
user_agent("httr"),
write_disk(output_file, overwrite = TRUE)
)
# Check the response
if (status_code(response) == 200) {
cat("File downloaded successfully as", output_file, "\n")

# Load the CSV into a dataframe
df <- read.csv(output_file)

# add locationId to the df
df$locationId <- input_points$locationId[i]

# Add the dataframe to the list
dataframes[[i]] <- df

# Clean up the temporary file if needed
# unlink(output_file)
} else {
cat("Failed to download. HTTP Status Code:", status_code(response), "\n")
cat(content(response, as = "text"))
}
}

print(dataframes)

0 comments on commit 35dbabc

Please sign in to comment.