Blog
BLOG

Geospatial data analysis in Angular

Santosh Viswanatham
   

Immersive experience has tapped into data analysis with a dazzling array of visualization techniques. The evolution of visualization-based data analysis influences business and sets apart from the competition since it can help provide the desired user experience. Users prefer data storytelling and demand data visualization beyond reports and dashboards. IT teams add visualization features to enable and standardize data visualization as it is a powerful mode for displaying the metrics.

Deck.gl is a WebGL-powered framework from Uber for visual exploratory data analysis of large datasets. In simple words, Deck.gl is a library that is used to create interactive visualizations on the web for large datasets and the library is powered by WebGL.

This blog will explain the method to create an interactive geospatial data visualization with Angular 8, MapboxGL and DeckGL. Here you can find a way to integrate Deck.gl, an Uber Visualization library into Angular Apps. After several trial and error approaches from multiple Vanilla JS/Non-Angular guides, this how-to procedure holds true with most of the Angular Versions (2+) but it is most accurate with Angular 8.

To ensure a workable environment for Angular CLI, prior installation of node and npm is essential.

Creating an Angular App

  • If you have Angular CLI Installed then you are good to go, else Install Angular CLI from here.
  • Create an Angular app using the CLI
    ng new visualization
  • If you are using CLI V8 then you will be prompted with a couple of questions about routing and CSS pre-processors configurations before creating the app, select the appropriate option that works for you and proceed to generate the app.
  • Now navigate to the directory of your project cd visualization
  • Run the command ng serve and open localhost:4200 on your browser. You will see the below screen (or a different one depending on your CLI/Angular Versions).
Angular app

Generate Mapbox Token

  • Go to Mapbox, create an account, and generate a Mapbox token.
  • Add the Mapbox token to your src/environments/environment.ts file
    export const environment = {
    production: false,
    mapboxToken: ‘YOUR_MAPBOX_TOKEN’
    };

Install and Initialize MapboxGL

  • Now Install MapboxGL packages into your project using npm i –save mapbox-gl @types/mapbox-gl
  • Delete everything in src/app/app.component.html and add the following HTML code
    <div id=”container”>
    <div id=”map”></div>
    </div>
  • Add the following CSS to your src/app/app.component.css
    #container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    }
    #container > * {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    }
  • Import MapboxGL, configure the mapbox token and Initialize mapbox-gl on the container as below in your src/app/app.component.ts
    // src/app/app.component.ts
    import { Component, AfterViewInit } from ‘@angular/core’;
    import { environment } from ‘src/environments/environment’;
    import * as mapboxgl from ‘mapbox-gl’;
    @Component({…}) // default code
    export class AppComponent implements AfterViewInit{
    ngAfterViewInit() {
    (mapboxgl as any).accessToken = environment.mapboxToken; // configure the mapbox token
    const INITIAL_VIEW_STATE = {
    latitude: 51.47,
    longitude: 0.45,
    zoom: 4,
    bearing: 0,
    pitch: 30
    };
    const map = new mapboxgl.Map({
    container: ‘map’, // should be the div id
    style: ‘mapbox://styles/mapbox/dark-v10’,
    interactive: false, // deck.gl will be in charge of interaction and event handling
    center: [INITIAL_VIEW_STATE.longitude, INITIAL_VIEW_STATE.latitude],
    zoom: INITIAL_VIEW_STATE.zoom,
    bearing: INITIAL_VIEW_STATE.bearing,
    pitch: INITIAL_VIEW_STATE.pitch
    });
    }
    }

Reload your browser window (if it isn’t auto-reloaded) and you will see the map loaded on to your screen. It isn’t interactive as you used the flag interactive: false in your Mapbox configuration above. We will explain this in the following steps.

MapboxGL Initialized

Install and Initialize DeckGL

  • Install the required packages for using deck.gl
    npm i –save deck.gl @deck.gl/core @deck.gl/layers
  • Add a canvas element to src/app/app.component.html, and your HTML code should look like
    <div id=”container”>
    <div id=”map”></div>
    <canvas id=”deck-canvas”></canvas>

    </div>

  • Import the required packages in src/app/app.component.ts
    import { Deck } from ‘@deck.gl/core’;
    import { GeoJsonLayer, ArcLayer } from ‘@deck.gl/layers’;
  • Add the data sources inside ngAfterViewInit
    const AIR_PORTS = ‘https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_10m_airports.geojson’;

The above data source if from official DeckGL examples.

    • Initialize deck.gl just below the MapboxGL Initialization const map = new mapboxgl.Map({…});
      const deck = new Deck({
      canvas: ‘deck-canvas’,
      width: ‘100%’,
      height: ‘100%’,
      initialViewState: INITIAL_VIEW_STATE,
      controller: true,
      onViewStateChange: ({ viewState }) => {
      map.jumpTo({
      center: [viewState.longitude, viewState.latitude],
      zoom: viewState.zoom,
      bearing: viewState.bearing,
      pitch: viewState.pitch
      });
      },
      layers: [
      new ArcLayer({
      id: ‘arcs’,
      data: AIR_PORTS,
      dataTransform: d => d.features.filter(f => f.properties.scalerank < 4),
      getSourcePosition: f => [-0.4531566, 51.4709959], // London
      getTargetPosition: f => f.geometry.coordinates,
      getSourceColor: [0, 128, 200],
      getTargetColor: [200, 0, 80],
      getWidth: 1
      })
      ]
      });

    Now, you will be able to see your deck.gl layers on the screen. If you miss anything, you can find the final code in here.

You can use the same approach to add another set of DeckGL layers to the map or integrate this map into other components.

Deck.gl has amazing set of prebuilt visualizations that you could use to visually represent large sets of data. A few of the screenshots of examples are listed below. If you want to try them out then you can find a list of examples here.

People moving in and out of Santa Clara, CA b/w 2009–2013
A visualization of the Uber trips in the city of Manhattan

With the above described building blocks of data visualizations, businesses can create better data visualization to tell compelling stories. The interactive ways in which businesses deliver real-time analytics create greater awareness of data and deliver insights in a way that engages with decision makers in an easily assimilated form.

Comment

Your email address will not be published. Required fields are marked *