{{announcement.body}}
{{announcement.title}}

Geospatial Data Analysis in Angular

DZone 's Guide to

Geospatial Data Analysis in Angular

This article will explain the method to create an interactive geospatial data visualization with Angular 8, MapboxGL and DeckGL.

· Web Dev Zone ·
Free Resource

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.

You may also like: What Data Analysis Tools Should I Learn to Start a Career as a Data Analyst?

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 approach 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).

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.
CSS
 




xxxxxxxxxx
1


1
export const environment = {
2
production: false,
3
mapboxToken: ‘YOUR_MAPBOX_TOKEN
4
};



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.
HTML
 




xxxxxxxxxx
1


 
1
<div id=”container”>
2
<div id=”map”></div>
3
</div>



  • Add the following CSS to your src/app/app.component.css.
CSS
 




xxxxxxxxxx
1
14


 
1
#container {
2
position: fixed;
3
top: 0;
4
left: 0;
5
right: 0;
6
bottom: 0;
7
}
8
#container > * {
9
position: absolute;
10
top: 0;
11
left: 0;
12
width: 100%;
13
height: 100%;
14
}



  • Import MapboxGL, configure the Mapbox token and Initialize Mapbox-gl on the container as below in your src/app/app.component.ts.
CSS
 




xxxxxxxxxx
1
26


 
1
// src/app/app.component.ts
2
import { Component, AfterViewInit } from@angular/core’;
3
import { environment } fromsrc/environments/environment’;
4
import * as mapboxgl frommapbox-gl’;
5
@Component({…}) // default code
6
export class AppComponent implements AfterViewInit{
7
ngAfterViewInit() {
8
(mapboxgl as any).accessToken = environment.mapboxToken; // configure the mapbox token
9
const INITIAL_VIEW_STATE = {
10
latitude: 51.47,
11
longitude: 0.45,
12
zoom: 4,
13
bearing: 0,
14
pitch: 30
15
};
16
const map = new mapboxgl.Map({
17
container: ‘map’, // should be the div id
18
style: ‘mapbox://styles/mapbox/dark-v10’,
19
interactive: false, // deck.gl will be in charge of interaction and event handling
20
center: [INITIAL_VIEW_STATE.longitude, INITIAL_VIEW_STATE.latitude],
21
zoom: INITIAL_VIEW_STATE.zoom,
22
bearing: INITIAL_VIEW_STATE.bearing,
23
pitch: INITIAL_VIEW_STATE.pitch
24
});
25
}
26
}



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.

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:
HTML
 




xxxxxxxxxx
1


 
1
<div id=”container”>
2
<div id=”map”></div>
3
<canvas id=”deck-canvas”></canvas>
4
</div>



  • Import the required packages in src/app/app.component.ts
CSS
 







  • Add the data sources inside ngAfterViewInit
JSON
 







The above data source if from official DeckGL examples.

  • Initialize deck.gl just below the MapboxGL Initialization const map = new mapboxgl.Map({…});
CSS
 




xxxxxxxxxx
1
27


 
1
const deck = new Deck({
2
canvas: ‘deck-canvas’,
3
width: ‘100%’,
4
height: ‘100%’,
5
initialViewState: INITIAL_VIEW_STATE,
6
controller: true,
7
onViewStateChange: ({ viewState }) => {
8
map.jumpTo({
9
center: [viewState.longitude, viewState.latitude],
10
zoom: viewState.zoom,
11
bearing: viewState.bearing,
12
pitch: viewState.pitch
13
});
14
},
15
layers: [
16
new ArcLayer({
17
id: ‘arcs’,
18
data: AIR_PORTS,
19
dataTransform: d => d.features.filter(f => f.properties.scalerank < 4),
20
getSourcePosition: f => [-0.4531566, 51.4709959], // London
21
getTargetPosition: f => f.geometry.coordinates,
22
getSourceColor: [0, 128, 200],
23
getTargetColor: [200, 0, 80],
24
getWidth: 1
25
})
26
]
27
});



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 an 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.

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.


Further Reading

Comparison of Data Analysis Tools: Excel, R, Python, and BI Tools

Principles of Data Analysis for Beginners

Top 5 Methods of Thinking in Data Analysis

Topics:
data analysis services ,data analysis and coding ,data analysis process ,data and location services ,data visualization ,data viz ,data visualization tutorial ,data ,tech blog ,technology

Published at DZone with permission of Imaginea Technologies . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}