Skip to content

Commit

Permalink
Use native:zonalstatisticsfb in challenge 3
Browse files Browse the repository at this point in the history
See r-spatial/qgisprocess#193. Thanks @florisvdh

Co-Authored-By: Floris Vanderhaeghe <[email protected]>
  • Loading branch information
damianooldoni and florisvdh committed Dec 22, 2023
1 parent d2776f9 commit d9f3033
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/20231214/20231214_challenges_solutions.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,26 @@ tmax_statistics

# 3

qgis_get_argument_specs("native:zonalstatistics") %>% View()
qgis_search_algorithms("zonal")

qgis_get_output_specs("native:zonalstatistics")
qgis_get_argument_specs("native:zonalstatisticsfb") %>% View()

zonal_st <- qgis_run_algorithm("native:zonalstatistics",
INPUT_RASTER = land_use,
RASTER_BAND = 1,
INPUT_VECTOR = prot_areas,
STATISTICS = c(8,9)) # Majority, Minority
# Notice that the other algorithm, native:zonalstatistics, is DEPRECATED and

This comment has been minimized.

Copy link
@florisvdh

florisvdh Jan 6, 2024

Author Member

Current dev version of {qgisprocess}, as provided by https://inbo.r-universe.dev, will not show this deprecated algorithm anymore, by default. If you'd like to still get it, you need to do qgis_search_algorithms("zonal", include_deprecated = TRUE).

# works in place: the temporary file contains the solution, but
# is destroyed subsequently. More info in https://github.com/r-spatial/qgisprocess/issues/193

zonal_sf_output <- qgis_extract_output(zonal_st, "INPUT_VECTOR")
zonal_st <- sf::st_as_sf(zonal_st)
qgis_get_output_specs("native:zonalstatisticsfb")

zonalstatisticsfb <- qgis_function("native:zonalstatisticsfb")

prot_areas_zonal <- zonalstatisticsfb(INPUT = prot_areas,
INPUT_RASTER = land_use,
RASTER_BAND = 1,
COLUMN_PREFIX = "zonal_", # optional, default: "_"
STATISTICS = c("Majority", "Minority")) %>%
qgis_extract_output("OUTPUT") %>%

This comment has been minimized.

Copy link
@florisvdh

florisvdh Jan 6, 2024

Author Member

You could skip this line, as st::st_as_sf() does this for you if OUTPUT is present.

sf::st_as_sf()

View(prot_areas_zonal) # notice the two new columns: zonal_majority and zonal_minority

mapview::mapview(prot_areas_zonal, zcol = "zonal_majority")

0 comments on commit d9f3033

Please sign in to comment.