Skip to content

Chronicle18/TrailOpt

Repository files navigation

TrailOpt - Intelligent Trip Planning & Optimization

A full-stack hiking trip optimizer that computes calorie expenditure, evaluates weather risk, generates packing lists, and performs gear weight minimization via dynamic programming.

Features

  • Trip Analytics: Advanced calorie estimation using modified Naismith's rule + MET-based adjustments
  • Weather Risk Engine: Evaluates cold exposure, heat exposure, storm probability, and wind risk
  • Auto-Generated Packing Lists: Intelligent gear suggestions based on trip parameters
  • Gear Optimization: 0/1 Knapsack DP algorithm to maximize usefulness while minimizing weight
  • Visualizations: Charts showing weight breakdown, calorie burn, and gear distribution
  • Google OAuth: Secure authentication with NextAuth
  • 10 Pre-loaded Trails: Including Yosemite, Inca Trail, John Muir Trail, and more

Tech Stack

  • Frontend: Next.js 14 (App Router), React, TypeScript, Tailwind CSS, Recharts
  • Backend: Next.js API Routes, Node.js, TypeScript
  • Database: MongoDB with Mongoose
  • Authentication: NextAuth (Auth.js v5) with Google OAuth
  • Validation: Zod schemas

Getting Started

Prerequisites

  • Node.js 18+
  • MongoDB (local or Atlas)
  • Google OAuth credentials

Installation

  1. Clone the repository:
git clone <repository-url>
cd TrailOpt
  1. Install dependencies:
npm install
  1. Set up environment variables:
cp .env.example .env

Edit .env with your values:

MONGODB_URI=mongodb://localhost:27017/trailopt
NEXTAUTH_SECRET=your-secret-key-here
NEXTAUTH_URL=http://localhost:3000
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
  1. Generate NextAuth secret:
openssl rand -base64 32
  1. Set up Google OAuth:

    • Go to Google Cloud Console
    • Create a new project or select existing
    • Enable Google+ API
    • Create OAuth 2.0 credentials
    • Add authorized redirect URI: http://localhost:3000/api/auth/callback/google
  2. Start MongoDB (if running locally):

mongod
  1. Run the development server:
npm run dev
  1. Open http://localhost:3000

Project Structure

TrailOpt/
├── app/                    # Next.js App Router pages
│   ├── api/               # API routes
│   ├── auth/              # Authentication pages
│   ├── dashboard/         # Dashboard page
│   ├── trips/             # Trip management pages
│   └── gear/              # Gear inventory page
├── components/            # React components
│   ├── ui/               # Reusable UI components
│   └── layout/           # Layout components
├── lib/                   # Utilities and configurations
│   ├── db/               # Database connection
│   ├── models/           # Mongoose models
│   ├── schemas/          # Zod validation schemas
│   ├── data/             # Static trail data
│   └── utils/            # Core algorithms
└── middleware.ts         # Route protection

Core Algorithms

Calorie Estimation

  • Modified Naismith's rule for time estimation
  • MET-based adjustments for pack weight and terrain
  • Temperature-based energy expenditure adjustments

Weather Risk Engine

  • Cold exposure scoring based on temperature thresholds
  • Heat exposure penalties for high temperatures
  • Storm probability based on elevation and location
  • Wind risk assessment

Packing List Generator

  • Rule-based gear suggestions
  • Temperature-based insulation recommendations
  • Distance-based water and food calculations
  • Multi-day trip shelter requirements

Knapsack DP Optimizer

  • 0/1 Knapsack dynamic programming algorithm
  • Maximizes priority/usefulness score
  • Ensures essential items are always included
  • Optimizes within weight constraints

API Routes

  • POST /api/analytics - Calculate calorie estimates and fatigue scores
  • GET /api/weather-risk - Evaluate weather risk for a trip
  • POST /api/optimize-gear - Run knapsack optimization on gear list
  • GET /api/trips - List user's trips
  • POST /api/trips - Create a new trip
  • GET /api/trips/[id] - Get trip details
  • DELETE /api/trips/[id] - Delete a trip
  • GET /api/gear - List user's gear items
  • POST /api/gear - Create a gear item
  • PUT /api/gear/[id] - Update a gear item
  • DELETE /api/gear/[id] - Delete a gear item

Protected Routes

The following routes require authentication (handled by middleware):

  • /dashboard
  • /trips/*
  • /gear/*
  • /optimize/*

Design System

The application follows a comprehensive design system with:

  • Primary blue color scheme (#4B6BFF)
  • Soft pastel accent colors
  • Consistent spacing (4px multiples)
  • Rounded corners (12-20px for cards)
  • Subtle shadows (z1-z3 elevation)
  • Inter font family

Development

Build for production:

npm run build
npm start

Lint:

npm run lint

Environment Variables

Variable Description Required
MONGODB_URI MongoDB connection string Yes
NEXTAUTH_SECRET Secret for JWT signing Yes
NEXTAUTH_URL Base URL of the application Yes
GOOGLE_CLIENT_ID Google OAuth client ID Yes
GOOGLE_CLIENT_SECRET Google OAuth client secret Yes
OPENWEATHER_API_KEY OpenWeather API key (optional) No

Deployment to Vercel

Prerequisites

  • GitHub account
  • Vercel account
  • MongoDB Atlas account (recommended for production)

Steps

  1. Push to GitHub:

    git init
    git add .
    git commit -m "Initial commit"
    git branch -M main
    git remote add origin https://github.com/yourusername/trailopt.git
    git push -u origin main
  2. Deploy to Vercel:

    • Go to vercel.com
    • Click "New Project"
    • Import your GitHub repository
    • Configure environment variables (see below)
    • Click "Deploy"
  3. Environment Variables in Vercel: Add these in Vercel project settings → Environment Variables:

    • MONGODB_URI - Your MongoDB Atlas connection string
    • NEXTAUTH_SECRET - Generate with: openssl rand -base64 32
    • NEXTAUTH_URL - Your Vercel deployment URL (e.g., https://trailopt.vercel.app)
    • GOOGLE_CLIENT_ID - Your Google OAuth client ID
    • GOOGLE_CLIENT_SECRET - Your Google OAuth client secret
  4. Update Google OAuth Redirect URI:

    • Go to Google Cloud Console
    • Update authorized redirect URI to: https://your-domain.vercel.app/api/auth/callback/google
  5. MongoDB Atlas Setup:

    • Create a free cluster at MongoDB Atlas
    • Create a database user
    • Whitelist Vercel IPs (or use 0.0.0.0/0 for development)
    • Copy connection string and use as MONGODB_URI

Build Settings

Vercel will automatically detect Next.js and use these settings:

  • Framework Preset: Next.js
  • Build Command: npm run build
  • Output Directory: .next
  • Install Command: npm install

License

MIT

Author

Pranav

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published