Infragistics
Close
Angular React Web Components Blazor React
Premium

React Binding and Overlaying Multiple Shape Files

In the Ignite UI for React map, you can add multiple geographic series objects to overlay a few shapefiles with geo-spacial data. For example, IgrGeographicSymbolSeries for plotting geographic locations of ports, the IgrGeographicPolylineSeries for plotting routes between ports, and the IgrGeographicShapeSeries for plotting shapes of countries.

React Binding and Overlaying Multiple Shape Files Example

This topic takes you step-by-step towards displaying multiple geographic series in the map component. All geographic series plot following geo-spatial data loaded from shape files using the IgrShapefileRecord class. Refer to the Binding Shape Files topic for more information about IgrShapefileRecord object.

You can use geographic series in above or other combinations to plot desired data.

Importing Components

First, let’s import required components and modules:

import { IgrGeographicMap, IgrGeographicShapeSeries, IgrGeographicPolylineSeries, IgrGeographicSymbolSeries } from 'igniteui-react-maps';
import { IgrShapeDataSource } from 'igniteui-react-core';
import { IgrGeographicMapModule } from 'igniteui-react-maps';
import { IgrDataChartInteractivityModule } from 'igniteui-react-charts';
// ...
const mods: any[] = [
    IgrGeographicMapModule,
    IgrDataChartInteractivityModule
];
mods.forEach((m) => m.register());

Creating Series

Next, we need to create a map with a few Geographic Series that will later load different type of shapefile.

<IgrGeographicMap
    ref={this.geoMapRef}
    height="100%"
    width="100%"
    zoomable={true}>
    <IgrGeographicShapeSeries
        name="polygonSeries"
        ref={ref => { this.polygonSeries = ref; }}
        shapeMemberPath="points"
        shapeFill="rgb(150, 150, 150)"
        shapeStroke="Black"
        shapeStrokeThickness={1} />
    <IgrGeographicPolylineSeries
        name="lineSeries"
        ref={ref => { this.lineSeries = ref; }}
        shapeMemberPath="points"
        shapeStroke="rgba(147, 15, 180, 0.5)"
        thickness={3} />
    <IgrGeographicSymbolSeries
        name="symbolSeries"
        ref={ref => { this.symbolSeries = ref; }}
        longitudeMemberPath="longitude"
        latitudeMemberPath="latitude"
        markerType="Circle"
        markerOutline="rgba(2, 102, 196, 1)"
        markerBrush="White" />
</IgrGeographicMap>

Loading Shapefiles

Next, in constructor of your page, add a IgrShapefileRecord for each shapefile that you want to display in the geographic map component. Each source is given the method that turns its records into data; those methods belong to a MapMultipleShapesReaders class, and the sections below show each of them.

import { IgrShapeDataSource } from 'igniteui-react-core';
// ...
private mapMultipleShapesReaders: MapMultipleShapesReaders = new MapMultipleShapesReaders();
// ...
var root = "https://static.infragistics.com/xplatform/shapes/";
var readers = this.mapMultipleShapesReaders;
var map = this.geoMap;

var sdsPolygons = new IgrShapeDataSource();
sdsPolygons.importCompleted = (s: IgrShapeDataSource, e: any) => readers.readPolygons(s, e, map);
sdsPolygons.shapefileSource = root + "WorldCountries.shp";
sdsPolygons.databaseSource = root + "WorldCountries.dbf";
sdsPolygons.dataBind();

var sdsPolylines = new IgrShapeDataSource();
sdsPolylines.importCompleted = (s: IgrShapeDataSource, e: any) => readers.readPolylines(s, e, map);
sdsPolylines.shapefileSource = root + "WorldCableRoutes.shp";
sdsPolylines.databaseSource = root + "WorldCableRoutes.dbf";
sdsPolylines.dataBind();

var sdsLocations = new IgrShapeDataSource();
sdsLocations.importCompleted = (s: IgrShapeDataSource, e: any) => readers.readPoints(s, e, map);
sdsLocations.shapefileSource = root + "WorldCities.shp";
sdsLocations.databaseSource = root + "WorldCities.dbf";
sdsLocations.dataBind();

Processing Polygons

Process shapes data loaded in IgrShapefileRecord with countries of the world and assign it to IgrGeographicShapeSeries object.

/** Country shapes, with the fields the tooltip shows taken from the .DBF file. */
public readPolygons(sds: IgrShapeDataSource, e: any, map: IgrGeographicMap): void {
    var geoPolygons: any[] = [];
    var pointData = sds.getPointData();
    // parsing shapefile data and creating geo-polygons
    for (var i = 0; i < pointData.length; i++) {
        var record = pointData[i];
        // using field/column names from .DBF file
        geoPolygons.push({
            points: record.points,
            name: record.fieldValues.NAME,
            gdp: record.fieldValues.GDP,
            population: record.fieldValues.POPULATION
        });
    }

    var shapeSeries = map.series.item(0) as IgrGeographicShapeSeries;
    shapeSeries.dataSource = geoPolygons;
}

Processing Polyline

Process shapes data loaded in IgrShapefileRecord with communication routes between major cities and assign it to IgrGeographicPolylineSeries object.

/** Connections between places, each record one path. */
public readPolylines(sds: IgrShapeDataSource, e: any, map: IgrGeographicMap): void {
    var geoPolylines: any[] = [];
    var pointData = sds.getPointData();
    // parsing shapefile data and creating geo-polylines
    for (var i = 0; i < pointData.length; i++) {
        var record = pointData[i];
        // using field/column names from .DBF file
        geoPolylines.push({
            points: record.points,
            name: record.fieldValues.Name,
            capacity: record.fieldValues.CapacityG,
            distance: record.fieldValues.DistanceKM,
            isOverLand: record.fieldValues.OverLand === 0,
            isActive: record.fieldValues.NotLive !== 0,
            service: record.fieldValues.InService
        });
    }

    var lineSeries = map.series.item(1) as IgrGeographicPolylineSeries;
    lineSeries.dataSource = geoPolylines;
}

Processing Points

Process shapes data loaded in IgrShapefileRecord with locations of major cities and assign it to IgrGeographicSymbolSeries object.

/** Cities with a known population, one point per record. */
public readPoints(sds: IgrShapeDataSource, e: any, map: IgrGeographicMap): void {
    var geoLocations: any[] = [];
    var pointData = sds.getPointData();
    // parsing shapefile data and creating geo-locations
    for (var i = 0; i < pointData.length; i++) {
        var record = pointData[i];
        var pop = record.fieldValues.POPULATION;
        if (pop > 0) {
            // each of these records holds a single point
            // using field/column names from .DBF file
            geoLocations.push({
                latitude: record.points[0][0].y,
                longitude: record.points[0][0].x,
                city: record.fieldValues.NAME,
                population: pop
            });
        }
    }

    var symbolSeries = map.series.item(2) as IgrGeographicSymbolSeries;
    symbolSeries.dataSource = geoLocations;
}

Map Background

Also, you might want to hide geographic imagery from the map background content if your shape files provided sufficient geographic context (e.g. shape of countries) for your application.

<IgrGeographicMap
    ref={this.geoMapRef}
    height="100%"
    width="100%"
    backgroundContent={null} />

Summary

For your convenience, all above code snippets are combined into one code block below that you can easily copy to your project.

<IgrGeographicMap
    ref={this.geoMapRef}
    height="100%"
    width="100%"
    zoomable={true}>
    <IgrGeographicShapeSeries
        name="polygonSeries"
        ref={ref => { this.polygonSeries = ref; }}
        shapeMemberPath="points"
        shapeFill="rgb(150, 150, 150)"
        shapeStroke="Black"
        shapeStrokeThickness={1} />
    <IgrGeographicPolylineSeries
        name="lineSeries"
        ref={ref => { this.lineSeries = ref; }}
        shapeMemberPath="points"
        shapeStroke="rgba(147, 15, 180, 0.5)"
        thickness={3} />
    <IgrGeographicSymbolSeries
        name="symbolSeries"
        ref={ref => { this.symbolSeries = ref; }}
        longitudeMemberPath="longitude"
        latitudeMemberPath="latitude"
        markerType="Circle"
        markerOutline="rgba(2, 102, 196, 1)"
        markerBrush="White" />
</IgrGeographicMap>
import { IgrShapeDataSource } from 'igniteui-react-core';
// ...
private mapMultipleShapesReaders: MapMultipleShapesReaders = new MapMultipleShapesReaders();
// ...
var root = "https://static.infragistics.com/xplatform/shapes/";
var readers = this.mapMultipleShapesReaders;
var map = this.geoMap;

var sdsPolygons = new IgrShapeDataSource();
sdsPolygons.importCompleted = (s: IgrShapeDataSource, e: any) => readers.readPolygons(s, e, map);
sdsPolygons.shapefileSource = root + "WorldCountries.shp";
sdsPolygons.databaseSource = root + "WorldCountries.dbf";
sdsPolygons.dataBind();

var sdsPolylines = new IgrShapeDataSource();
sdsPolylines.importCompleted = (s: IgrShapeDataSource, e: any) => readers.readPolylines(s, e, map);
sdsPolylines.shapefileSource = root + "WorldCableRoutes.shp";
sdsPolylines.databaseSource = root + "WorldCableRoutes.dbf";
sdsPolylines.dataBind();

var sdsLocations = new IgrShapeDataSource();
sdsLocations.importCompleted = (s: IgrShapeDataSource, e: any) => readers.readPoints(s, e, map);
sdsLocations.shapefileSource = root + "WorldCities.shp";
sdsLocations.databaseSource = root + "WorldCities.dbf";
sdsLocations.dataBind();

API References