---
title: "Working with WFS/WMS Services"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Working with WFS/WMS Services}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

## 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:

```{r eval=FALSE}
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:

```{r eval=FALSE}
# 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:

```{r eval=FALSE}
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:

```{r eval=FALSE}
# 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:

```{r eval=FALSE}
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:

```{r eval=FALSE}
# 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

```{r}
sessionInfo()
```
