# Working with WFS/WMS Services

## Understanding WFS and WMS Services

### What are WFS and WMS?

WFS (Web Feature Service) and WMS (Web Map Service) are standardized protocols for serving georeferenced map data over the internet:

- WFS provides access to actual geographic features with geometry and attributes
- WMS provides map images rendered from geographic data

### WFS Services in Detail

When you use Argentum to import WFS layers, you’re getting actual vector data that you can analyze and manipulate in R:

    library(Argentum)
    library(sf)

    # Get organization data
    org <- argentum_select_organization(search = "Buenos Aires")

    # List available layers
    layers <- argentum_list_layers(org$Name)

    # Import a specific layer
    sf_layer <- argentum_import_wfs_layer(org$WFS_URL, layers$Name[1])

    # Now you can work with the data using sf functions
    st_crs(sf_layer)  # Check the coordinate reference system
    plot(sf_layer)    # Basic plot of the geometry

### Working with Service Capabilities

Before importing data, you can check what capabilities a service offers:

    # Get capabilities document
    capabilities <- argentum_get_capabilities(org$WFS_URL)

    # The capabilities document contains information about:
    # - Available layers
    # - Supported operations
    # - Coordinate reference systems
    # - Output formats

## Best Practices

### 1. Error Handling

Always implement proper error handling:

    tryCatch({
      # Attempt to import data
      sf_layer <- argentum_import_wfs_layer(org$WFS_URL, layers$Name[1])
    }, error = function(e) {
      # Handle any errors that occur
      message("Error importing layer: ", e$message)
    })

### 2. Performance Considerations

When working with WFS services:

    # Use appropriate timeout values for large datasets
    capabilities <- argentum_get_capabilities(
      url = org$WFS_URL,
      timeout = 60  # Increase timeout for slow connections
    )

### 3. Data Processing

After importing WFS data:

    library(sf)
    library(dplyr)

    # Check the data structure
    str(sf_layer)

    # Basic statistics
    summary(sf_layer)

    # Spatial operations
    sf_layer_transformed <- st_transform(sf_layer, 4326)

    # Calculate areas if working with polygons
    if (all(st_geometry_type(sf_layer) %in% c("POLYGON", "MULTIPOLYGON"))) {
      sf_layer$area <- st_area(sf_layer)
    }

## Advanced Usage

### Custom Queries

While Argentum provides high-level functions, you can also work with WFS services directly:

    # Example of constructing a custom WFS URL
    base_url <- org$WFS_URL
    query_params <- list(
      service = "WFS",
      version = "1.1.0",
      request = "GetFeature",
      typeName = layers$Name[1],
      outputFormat = "application/json"
    )

    # Build the URL
    query_url <- httr::modify_url(
      url = base_url,
      query = query_params
    )

## Troubleshooting

Common issues and solutions:

1. Connection Timeouts
   - Increase timeout value
   - Check internet connection
   - Verify service availability
2. Invalid Layer Names
   - Use `argentum_list_layers()` to get exact layer names
   - Check for case sensitivity
   - Verify layer still exists in service
3. Data Format Issues
   - Check supported output formats
   - Verify coordinate reference systems
   - Ensure data compatibility with sf package

## Future Development

Planned features for future versions:

- Spatial filtering support
- Temporal queries
- WMS integration
- Caching mechanism
- Batch export capabilities

## Session Information

    sessionInfo()
    #> R version 4.3.1 (2023-06-16 ucrt)
    #> Platform: x86_64-w64-mingw32/x64 (64-bit)
    #> Running under: Windows 10 x64 (build 19045)
    #>
    #> Matrix products: default
    #>
    #>
    #> locale:
    #> [1] LC_COLLATE=C                       LC_CTYPE=Spanish_Argentina.utf8
    #> [3] LC_MONETARY=Spanish_Argentina.utf8 LC_NUMERIC=C
    #> [5] LC_TIME=Spanish_Argentina.utf8
    #>
    #> time zone: America/Buenos_Aires
    #> tzcode source: internal
    #>
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base
    #>
    #> loaded via a namespace (and not attached):
    #>  [1] digest_0.6.37     R6_2.5.1          fastmap_1.2.0     xfun_0.50
    #>  [5] cachem_1.1.0      knitr_1.49        htmltools_0.5.8.1 rmarkdown_2.29
    #>  [9] lifecycle_1.0.4   cli_3.6.1         sass_0.4.9        jquerylib_0.1.4
    #> [13] compiler_4.3.1    rstudioapi_0.17.1 tools_4.3.1       evaluate_1.0.3
    #> [17] bslib_0.8.0       yaml_2.3.10       rlang_1.1.1       jsonlite_1.8.9