Coffee Digital - Mapping and Analysis Project π±β
This repository contains R scripts for analyzing and visualizing coffee farm data, including static maps with satellite imagery and interactive web maps. The project focuses on mapping coffee farms across different communities and categories using geographic data.
π Table of Contents
π― Overview
This project analyzes coffee farm data across different communities and categories, creating both static and interactive visualizations. The analysis includes:
- Static Maps: High-quality satellite maps with farm locations
- Interactive Maps: Web-based maps with filtering capabilities
- Community Analysis: Individual maps for each community
- Category Visualization: Different symbols and colors for farm categories
Key Features
- πΊοΈ Satellite imagery integration using Google Maps API
- π Geographic visualization with custom symbols and colors
- ποΈ Community-based analysis and mapping
- π¨ Category differentiation through symbols and colors
- π Interactive web maps with Leaflet
- π Automated map generation for multiple communities
π Project Structure
coffee-digital/
βββ README.md # Project documentation
βββ LICENSE # License file
βββ .gitignore # Git ignore rules
βββ config.R.example # Configuration template
βββ config.R # API keys (not in version control)
β
βββ data/ # Data directory
β βββ derived/ # Processed data
β β βββ coffee_sample_final_20250709.rds
β β βββ coffee_sample_final_20250709.xlsx
β βββ org/ # Original data files
β β βββ 3.Fincas_seleccionadas.xlsx
β β βββ Base_vieja.xlsx
β β βββ Fincas_actualizado_final2.xlsx
β β βββ pioners.xlsx
β β βββ TREATMENT VS CONTROL_2.R
β βββ Shapes/ # Shapefiles for geographic boundaries
β βββ shapes.shp
β βββ shapes.dbf
β βββ shapes.prj
β βββ shapes.cpg
β βββ shapes.shx
β
βββ syntaxis/ # R scripts
β βββ 01_Sampling_Random_Selection.R
β βββ 02_create_maps.R # Main mapping script
β βββ 03_interactive_maps.R # Interactive map creation
β βββ aux_func_distacia.R # Distance calculation functions
β βββ old_check_old_ds.R # Data validation
β
βββ out/ # Output directory (generated)
βββ map_all.png # General overview map
βββ map_[community].png # Individual community maps
βββ interactive_map.html # Interactive web map
π οΈ Setup and Installation
Prerequisites
- R (version 4.0 or higher)
- RStudio (recommended)
- Google Maps API key
Required R Packages
The project uses pacman for package management. The following packages are automatically installed:
# Core packages
- ggplot2 # Data visualization
- ggmap # Google Maps integration
- sf # Spatial data handling
- leaflet # Interactive maps
- htmlwidgets # Web widgets for R
Installation Steps
- Clone the repository:
git clone https://github.com/yourusername/coffee-digital.git
cd coffee-digital
-
Set up Google Maps API key:
Option A: Environment Variable (Recommended)
export GOOGLE_MAPS_API_KEY="your_api_key_here"
Option B: Configuration File
cp config.R.example config.R
# Edit config.R and add your API key
- Create output directory:
- Run the scripts in RStudio or R console
π Usage
Quick Start
- Generate all maps:
source("syntaxis/02_create_maps.R")
- Create interactive map:
source("syntaxis/03_interactive_maps.R")
Detailed Workflow
- Data Preparation: Load and process coffee farm data
- Static Maps: Generate satellite maps with farm locations
- Community Maps: Create individual maps for each community
- Interactive Maps: Build web-based interactive visualizations
π Scripts Description
01_Sampling_Random_Selection.R
- Purpose: Random sampling and selection of coffee farms
- Input: Original data files from
data/org/
- Output: Processed sample data
- Key Functions: Statistical sampling, data validation
02_create_maps.R β Main Script
- Purpose: Creates static maps with satellite imagery
- Features:
- Google Maps satellite integration
- Shapefile overlay for geographic boundaries
- Community-based color coding
- Category-based symbol differentiation
- Automated map generation for all communities
- Outputs:
out/map_all.png - Overview map of all farms
out/map_[community].png - Individual community maps
03_interactive_maps.R π Interactive Maps
- Purpose: Creates interactive web-based maps
- Features:
- Leaflet integration for web interactivity
- Toggle between street and satellite views
- Color-coded communities
- Symbol-coded categories
- Popup information for each farm
- Legend for easy interpretation
- Output:
out/interactive_map.html
aux_func_distacia.R
- Purpose: Distance calculation utilities
- Functions: Geographic distance computations between farms
old_check_old_ds.R
- Purpose: Data validation and quality checks
- Functions: Compare old vs new datasets
π¨ Output Examples
Static Maps
Overview Map (map_all.png)
- Shows all coffee farms across all communities
- Satellite background with shapefile boundaries
- Color-coded by community
- Symbol-coded by category
Individual maps for each community with:
- Closer zoom level for detailed view
- Category-based color and symbol coding
- Farm locations with coordinates
Interactive Map (interactive_map.html)
Features:
- Base Layers: Street view and satellite imagery
- Interactive Elements:
- Clickable markers with farm information
- Community color coding
- Category symbol differentiation
- Legends: Clear identification of communities and categories
- Popups: Detailed information including:
- Community name
- Farm category
- Geographic coordinates
π Data
Farm Categories
- Demostrativa β (Demonstration farms)
- Al Invest βΆοΈ (Investment category)
- Pioneros βΉοΈ (Pioneer farms)
- Croppie π (Crop-focused farms)
- Only Platform βοΈ (Platform-only participants)
- Control π (Control group)
- Reemplazo β (Replacement farms)
- Sin Asignar β (Unassigned)
Data Sources
- Primary Dataset:
coffee_sample_final_20250709.rds
- Geographic Boundaries: Shapefiles in
data/Shapes/
- Original Data: Various Excel files in
data/org/
βοΈ Configuration
Google Maps API Setup
- Get API Key:
- Configure in Project:
# In config.R
google_maps_api_key <- "YOUR_API_KEY_HERE"
- Security Note:
- Never commit API keys to version control
- Use environment variables in production
- Restrict API key usage by domain/IP
Customization Options
- Map Zoom Levels: Adjust zoom parameters in scripts
- Color Schemes: Modify color palettes for communities/categories
- Symbol Sets: Change category symbols in symbol definitions
- Output Formats: Adjust map dimensions and file formats
π€ Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
Guidelines
- Follow R coding standards
- Document new functions
- Test with sample data
- Update README for new features
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Troubleshooting
Common Issues
- Google Maps API Errors:
- Check API key validity
- Verify API quotas
- Ensure Maps JavaScript API is enabled
- Missing Packages:
- Run
install.packages("pacman") first
- Check R version compatibility
- File Path Issues:
- Ensure working directory is project root
- Check file permissions
- Verify data file existence
Support
For issues and questions:
- Open an issue on GitHub
- Check existing documentation
- Review error messages for debugging
Last Updated: July 9, 2025
Project Status: Active Development
R Version: 4.0+