Skip to main content

Attribute Queries

Fuzzy text match

Let's say we're interested in a specific river, one we think is called the "Animas river". We can pass an attribute filter query to the mainstems collection and then use our dataframe and visualization library to filter and display the results.

# construct a query for a river name that includes the string "animas"
query <- URLencode("name_at_outlet ILIKE '%animas%'")
url <- paste0(base_url, "/collections/mainstems/items?f=json&filter=", query)
# Read the data into an sf object
animas_rivers <- st_read(url, quiet = TRUE)
# Display the results
animas_rivers |>
select(uri, name_at_outlet, outlet_drainagearea_sqkm) |>
datatable()
# Map the results
mapview(animas_rivers |>
select(uri, name_at_outlet), zcol = "name_at_outlet")

There are evidently 3 rivers that include the word "Animas". Let's say we were interested in the "Animas River", shown on the map in Green. We find that it's Geoconnex URI is https://geoconnex.us/ref/mainstems/35394.

Logical and quantitative

We can also do filters based on logical and quantitative filters on attributes.

Let's say we wanted to find all rivers with drainage areas (in this reference dataset, the attribute is outlet_drainagearea_sqkm) greater than 1,000,000 square kilometers:

# construct a query for a river with outlet_drainagearea_sqkm > 600,000
query <- URLencode("outlet_drainagearea_sqkm > 500000")
url <- paste0(base_url, "/collections/mainstems/items?f=json&filter=", query)
# Read the data into an sf object
large_mainstems <- st_read(url, quiet = TRUE)
# Display the results
large_mainstems |>
select(uri, name_at_outlet, outlet_drainagearea_sqkm) |>
datatable()
# Map the results
mapview(large_mainstems, zcol = "name_at_outlet")

Combining Queries

Queries over multiple attributes can also be made, combining with 'AND' or 'OR'. For example, let's find all dams that include the name "Hoover", but then also filter to only the ones with a drainage area of more than 100,000 square kilometers:

# Step 1: Find all dams named "Hoover"
query_hoover <- URLencode("name LIKE '%Hoover%'")
url_hoover <- paste0(base_url, "/collections/dams/items?f=json&filter=", query_hoover)
hoover_dams <- st_read(url_hoover, quiet = TRUE)
cat("Number of dams named 'Hoover':", nrow(hoover_dams), "\n")
# Create an interactive table of all Hoover dams
datatable(
hoover_dams |>
st_drop_geometry() |>
select(uri, name, drainage_area_sqkm) |>
arrange(desc(drainage_area_sqkm)),
options = list(pageLength = 10, scrollX = TRUE),
caption = "All Dams Named 'Hoover'",
rownames = FALSE
)
# Step 2: Query for large Hoover dams using a combined filter
query_large_hoover <- URLencode("name LIKE '%Hoover%' AND drainage_area_sqkm > 100000")
url_large_hoover <- paste0(base_url, "/collections/dams/items?f=json&filter=", query_large_hoover)
large_hoover_dams <- st_read(url_large_hoover, quiet = TRUE)
cat("\nNumber of large Hoover dams (Drainage Area > 100,000 sq km):", nrow(large_hoover_dams), "\n")
# Create an interactive table of large Hoover dams
datatable(
large_hoover_dams |>
st_drop_geometry() |>
select(uri, name, drainage_area_sqkm) |>
arrange(desc(drainage_area_sqkm)),
options = list(pageLength = 10, scrollX = TRUE),
caption = "Large Dams Named 'Hoover' (Drainage Area > 100,000 sq km)",
rownames = FALSE
)
# Create a map view of all Hoover dams, highlighting the large ones
m <- mapview(hoover_dams |>
select(uri, name, drainage_area_sqkm), layer.name = "All Hoover Dams", label="name")
m + mapview(large_hoover_dams |>
select(uri, name, drainage_area_sqkm), color = "red", col.regions="red", layer.name = "Large Hoover Dams", lwd=2, cex=15, label="Hoover")

We found 39 Dams in the US named "Hoover", but only 1 with a large drainage area, the famous one near Las Vegas, NV.