UWI-Zess / COMP6940-Project

https://comp-6940-project.vercel.app
0 stars 0 forks source link

Implement an Interactive Heatmap Visualization for High-Risk Areas in Next.js #20

Closed jefroy closed 1 month ago

jefroy commented 1 month ago

Description

We are creating a web application to analyze hate crime trends and patterns across geographical regions. The objective is to provide a heatmap visualization that identifies high-risk areas using data obtained from the FBI. The heatmap will help users understand geographical distribution and patterns in hate crime incidents.

Objectives

Implementation Plan

  1. Data Preparation:

    • Extract relevant columns from the CSV (e.g., state_name, incident_date, offense_name, bias_desc).
    • Aggregate data to focus on high-risk areas by summarizing incidents per region or state.
    • Convert the processed data to JSON format for ease of visualization.
  2. Install Required Libraries:

    • Mapbox GL JS and React Map GL for interactive mapping.
    • papaparse to load and parse the CSV data.
      npm install mapbox-gl @urbica/react-map-gl papaparse
  3. Map Component Setup:

    • Create a Map component in Next.js using @urbica/react-map-gl.
    • Set up the base map with Mapbox, ensuring credentials are securely stored in .env.local via NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN.
  4. Load and Process CSV Data:

    • Write a function to load CSV data via papaparse.
    • Convert the parsed CSV data into a format suitable for visualization, adding any necessary geographic coordinates.
  5. Create Heatmap Layer:

    • Use the processed data to create a heatmap layer in Mapbox.
    • Adjust properties like intensity, color, and radius based on the density and distribution of incidents.
    • Experiment with different data aggregation levels to optimize the visualization.
  6. Interactivity Features:

    • Implement tooltips to display incident details on hover.
    • Add zooming and panning to explore the map dynamically.
    • Integrate filters for users to refine results based on the region, year, or type of crime.
  7. Map Styling:

    • Style the heatmap and base map using Mapbox's map styles and customization options.
    • Ensure the visual presentation is consistent with the rest of the web app's theme.

Acceptance Criteria

Notes

jefroy commented 1 month ago

Data Preparation

  1. Extract Relevant Data: Focus on geographic data (state_name or division_name for regional granularity), incident_date for temporal analysis, and counts of incidents and biases (total_offender_count, offense_name, bias_desc) to identify patterns.

  2. Aggregate Data: Depending on the scale of the heatmap, you might want to aggregate data by state or region, summing up the incidents or categorizing them by type of offense and bias.

Visualization Tool Selection

Implementation Steps

  1. Set Up Mapbox in Next.js:

    • Install Mapbox GL JS and React Map GL.
      npm install mapbox-gl @urbica/react-map-gl
    • Set up a Map component using Mapbox, integrating it within your Next.js page.
  2. Load and Process CSV Data:

    • Use a library like papaparse to load and parse the CSV data.
      npm install papaparse
    • Convert the CSV data into a JSON format suitable for visualization (e.g., convert state_name to geographic coordinates if using a U.S. map).
  3. Create Heatmap Layer:

    • Use the data to create a heatmap layer. Adjust properties like intensity, color, and radius based on the density and distribution of data.
  4. Add Interactivity:

    • Implement interactive features such as tooltips for detailed information on hover, filters for specific years or types of crimes, and controls for zooming and panning.
  5. Style the Map:

    • Customize the map's appearance using Mapbox's styling options to match the application's design.

Sample Code for Map Component

Here's a basic outline of what the component might look like in Next.js:

import React, { useEffect, useState } from 'react';
import MapGL from '@urbica/react-map-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

const MapComponent = () => {
  const [viewport, setViewport] = useState({
    latitude: 37.7749,
    longitude: -122.4194,
    zoom: 11
  });

  useEffect(() => {
    // Load and process your CSV data here
  }, []);

  return (
    <MapGL
      style={{ width: '100%', height: '400px' }}
      mapStyle='mapbox://styles/mapbox/streets-v11'
      accessToken={process.env.NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN}
      latitude={viewport.latitude}
      longitude={viewport.longitude}
      zoom={viewport.zoom}
      onViewportChange={setViewport}
    >
      {/* Add your heatmap layer here */}
    </MapGL>
  );
};

export default MapComponent;

This setup uses environment variables for Mapbox credentials (ensure you have a .env.local file with your NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN).

Next Steps