๐ŸŽฏ Inspiration

Walking through Manhattan during sunset one evening, I witnessed the famous Manhattanhenge phenomenonโ€”the moment when the setting sun perfectly aligns with the city's street grid, creating a stunning visual spectacle. Named by astrophysicist Neil deGrasse Tyson, this astronomical event only occurs twice a year in New York.

But then I wondered: What about other cities? Chicago has a grid layout. So does Toronto, Barcelona, and Buenos Aires. Could there be "Chicagohenge" or "Torontohenge" moments happening right now that nobody knows about?

That question sparked this project: A global Manhattanhenge finder that works for any city, anywhere in the world.

๐ŸŒ What it does

Manhattanhenge is an interactive web application that calculates and visualizes when the sunset aligns with street grids in any city globally. Here's what makes it special:

  • ๐Ÿ” Universal Search: Type any city name (New York, Tokyo, Paris, etc.) and instantly see its street layout
  • ๐ŸŒ… Sun Position Calculator: Uses astronomical algorithms to compute the sun's exact azimuth (compass direction) for any date and time
  • ๐Ÿ—บ๏ธ Dynamic Street Overlay: Fetches real street data from OpenStreetMap and calculates each street's orientation
  • ๐ŸŽจ Visual Magic: Highlights streets that align with the sunset in vibrant orange, creating a beautiful visualization
  • โฐ Time Travel: Choose any date/timeโ€”past, present, or futureโ€”to see when alignments occur
  • ๐Ÿ“ฑ Interactive UI: Draggable control panel with glassmorphism design

Use Cases:

  • ๐Ÿ“ท Plan the perfect sunset photography session
  • ๐ŸŽ“ Educational tool for teaching astronomy and urban planning
  • ๐ŸŒ† Discover hidden "henge" moments in your own city
  • ๐Ÿ—“๏ธ Find optimal dates for visiting cities as a tourist

๐Ÿ› ๏ธ How we built it

Architecture

The project uses a Next.js full-stack architecture with TypeScript:

Frontend:

  • Next.js 16 with React 19 for the UI
  • Mapbox GL JS for interactive map rendering
  • Tailwind CSS 4 for responsive, modern styling
  • Dynamic imports to optimize bundle size

Backend:

  • Next.js API Routes (serverless functions)
  • SunCalc library for astronomical calculations
  • OpenStreetMap Overpass API for real-time street data

The Math ๐Ÿงฎ

  1. Sun Azimuth Calculation:

    // Use SunCalc to get sun position
    const pos = SunCalc.getPosition(date, lat, lng);
    const sunAzimuth = (pos.azimuth * 180 / Math.PI + 180) % 360;
    
    • Converts sun's position to compass bearing (0ยฐ = North, 90ยฐ = East, etc.)
  2. Street Azimuth Calculation:

    // Calculate bearing between two GPS points
    azimuth = atan2(
     sin(ฮ”ฮป) ร— cos(ฯ†2),
     cos(ฯ†1) ร— sin(ฯ†2) - sin(ฯ†1) ร— cos(ฯ†2) ร— cos(ฮ”ฮป)
    )
    
    • Fetches street geometries from OpenStreetMap
    • Calculates each street segment's compass direction
    • Averages azimuths for entire street
  3. Alignment Detection:

    // Compare sun and street directions
    const isAligned = Math.abs(streetAzimuth - sunAzimuth) <= 5ยฐ
    
    • ยฑ5ยฐ tolerance accounts for street irregularities

Data Flow

User enters city โ†’ Mapbox Geocoding API โ†’ Get coordinates
User selects datetime โ†’ Frontend sends to /api/alignment
Backend calls /api/streets โ†’ OpenStreetMap Overpass API
Calculate street azimuths โ†’ Compare with sun azimuth
Return aligned streets โ†’ Frontend overlays on Mapbox

๐Ÿ˜… Challenges we ran into

  1. OpenStreetMap Rate Limiting

    • The Overpass API can be slow (10-30 seconds for dense cities)
    • Solution: Added loading states and caching strategies
  2. Coordinate System Confusion

    • Mapbox uses [lng, lat] but most APIs use [lat, lng]
    • Solution: Careful parameter ordering and TypeScript types
  3. Azimuth Calculation Accuracy

    • Streets with curves have varying directions
    • Solution: Calculate average azimuth across all segments
  4. React Hot Reload Issues

    • Map was re-initializing on every code change
    • Solution: Added useRef guards to prevent double initialization
  5. Math Debugging

    • Initial azimuth calculations were off by 180ยฐ
    • Solution: Extensive console logging and testing with known Manhattanhenge dates

๐Ÿ† Accomplishments that we're proud of

  • โœ… Global Coverage: Works for literally any city on Earth
  • โœ… Accurate Math: Successfully replicated real Manhattanhenge dates (May 28, July 12)
  • โœ… Beautiful UX: Glassmorphism design with smooth animations
  • โœ… Performance: Handles 200+ streets per city efficiently
  • โœ… Real Data: Uses actual street geometries, not approximations
  • โœ… Custom Domain: Deployed at manhattanhenge.tech
  • โœ… Educational: Makes complex astronomy accessible to everyone

๐Ÿ“š What we learned

Technical Skills

  • Astronomical Calculations: Understanding sun position algorithms
  • Geospatial Math: Working with coordinates, bearings, and map projections
  • API Integration: Combining multiple external APIs (Mapbox, OpenStreetMap, SunCalc)
  • TypeScript Best Practices: Type-safe API contracts and data flow
  • Performance Optimization: Lazy loading, debouncing, and caching strategies

Domain Knowledge

  • Urban Planning: How city grids are designed (Manhattan's 29ยฐ rotation!)
  • Astronomy: Solar azimuths, altitudes, and the analemma
  • Cartography: Map projections and coordinate systems

Soft Skills

  • Debugging Complex Systems: Tracing errors across frontend, backend, and external APIs
  • Documentation: Writing clear explanations of technical concepts
  • User-Centric Design: Making complex calculations feel magical

๐Ÿš€ What's next for Manhattanhenge

Phase 1: Community Features (Next Month)

  • ๐Ÿ“ธ Photo Gallery: Users upload their Manhattanhenge photos
  • ๐ŸŒŸ Best Spots Database: Crowdsourced best viewing locations
  • ๐Ÿ—“๏ธ Calendar Integration: Export alignment dates to Google Calendar

Phase 2: Enhanced Visualizations (3 Months)

  • ๐ŸŒˆ 3D Sun Path: Show sun trajectory throughout the day
  • ๐Ÿ“ฑ AR Mode: Point phone camera at street to see predicted alignment
  • ๐ŸŽฅ Time-Lapse Generator: Animate sun movement across the day

Phase 3: Social & Gamification (6 Months)

  • ๐Ÿ… Achievement System: "Witnessed 10 Manhattanhenges"
  • ๐Ÿ‘ฅ User Profiles: Share your henge experiences
  • ๐ŸŒ Global Leaderboard: Most alignments discovered

Phase 4: Advanced Features (1 Year)

  • ๐Ÿค– AI Predictions: Machine learning to predict best photo conditions
  • ๐ŸŒค๏ธ Weather Integration: Only show alignments on clear days
  • ๐Ÿ“Š Analytics Dashboard: Heatmap of alignment frequencies globally

Long-term Vision

Transform Manhattanhenge into the Spotify of urban sunsetsโ€”helping millions of people discover and experience the magic of celestial alignments in their own cities.


๐Ÿ› ๏ธ Built With

  • Next.js 16
  • React 19
  • TypeScript
  • Tailwind CSS 4
  • Mapbox GL JS
  • SunCalc
  • OpenStreetMap Overpass API
  • Vercel
  • .tech Domain

Try it now: manhattanhenge.tech ๐ŸŒ…

Built With

Share this project:

Updates