GeoJsonImporter.js

GeoJsonImporter()


constructor

The GeoJsonImporter class is responsible for transforming GeoJson format into renderable geometries

Options:
  • style(Object | Function)The style used for coloring the geometries. If it is a function the function is called for each geometry added with the geometry's GeoJson properties as the function's parameter. The function must return the style object.
  • success(Function)The function called after the GeoJson was loaded from URL, usually in this function we would add the importer to the vector layer (ex.: importer.addTo(vectorLayer)). The function is also called when a GeoJson object is provided, but in this case adding the importer to the vector layer can be performed immediatly without the success implementation.
Parameter Type Description
geoJson String | Object An URL to the GeoJson file or the actual GeoJson Javascript object
options Object The options for initializing the GeoJson class

Example

$(function () {

    'use strict';
    
    // Params
    var mapContainer = T.DomUtil.get("map"),
        vectorLayer = new T.VectorLayer({useCanvas: false}),
        mapoLayer = new T.TibcoLayer();

    //Map creation
    var map = new T.Map(
        mapContainer,
        {
            zoom: 4,
            center: new T.LatLng(39, -98)
        }
    );

    //LayerCreation
    map.addLayer(mapoLayer);
    map.addLayer(vectorLayer);

    // Helper function that returns a color for a given value
    function getColor(value) {
        return  value > 1000 ? '#800026' :
                value > 500  ? '#BD0026' :
                value > 200  ? '#E31A1C' :
                value > 100  ? '#FC4E2A' :
                value > 50   ? '#FD8D3C' :
                value > 20   ? '#FEB24C' :
                value > 10   ? '#FED976' :
                               '#FFEDA0';
    }

    // Function called for each geometry with its GeoJson properties as the parameter
    function style(properties) {
        return {
            color: '#ffffff',
            opacity: 1,
            weight: 2,
            fillOpacity: 0.7,
            dashArray: 3,
            fillColor: properties ? getColor(properties.density) : '#ff0000'
        };
    }

    // The definition of the importer object
    var importer = new T.GeoJsonImporter('../../assets/us-states.json', {
        style: style,
        success: function (importer) {
            importer.addTo(vectorLayer);
        }
    });

    // Fit map to geometry's bounds on click
    vectorLayer.events.on("press", function (obj) {
        map.fitToBounds(obj.getBounds());
    });

    // Highlights the shape on mouse over and displays the state info 
    vectorLayer.events.on("over", function (obj) {
        var data = obj.options.geojsonProperties;
        obj.setStyle({
            weight: 5,
            color: '#666',
            dashArray: ''
        });

        vectorLayer.bringToFront(obj);
        
        $('.info .description .state').html(data.name);
        $('.info .description .density').html(data.density + ' people / mi²');
    });
    
    // Resets the shape style and the state info box
    vectorLayer.events.on("out", function (obj) {
        obj.setStyle(style(obj.options.geojsonProperties));
        
        $('.info .description .state').html('');
        $('.info .description .density').html('Hover over a state');
    });

    // Renders the legend
    var div = $('.legend'),
        grades = [0, 10, 20, 50, 100, 200, 500, 1000],
        labels = [],
        from, to, i;

    for (i = 0; i < grades.length; i++) {
        from = grades[i];
        to = grades[i + 1];

        labels.push('<i style="background:' + getColor(from + 1) + '"></i> ' + from + (to ? '&ndash;' + to : '+'));
    }

    div.html(labels.join('<br>'));
});
<html>    
    <head>
        <title>TibcoMaps - Simple map</title>
        <meta charset="UTF-8">

        <!-- CSS -->
        <link rel="stylesheet" href="choropleth.css"/>

        <!-- Javascript -->
        <script type="text/javascript" src='../lib/GeoAnalytics.js'></script>
        <script type="text/javascript" src='../lib/jquery-1.9.1.min.js'></script>	
        <script type="text/javascript" src="choropleth.js"></script>	


    </head>

    <body>
        <div class="custom-container">
            <div id="map"></div>
            <div class="box info">
                <div class="title">US Population Density</div>
                <div class="description">
                    <div class="state"></div>
                    <div class="density">Hover over a state</div>
                </div>
            </div>
            <div class="box legend"></div>
        </div>
    </body>
</html>
body, html { 
    height: 100%;
    width: 100%;
    margin: 0px;
}

.custom-container  {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;   
}

#map {			
    width: 100%;
    height: 100%;
}

#map path {
    cursor: pointer;
}

.box {
    position: absolute;
    background-color: rgba(255, 255, 255, 0.9);
    padding: 10px;
    border-radius: 5px;
    font-size: 14px;
    line-height: 18px;
    font-family: Helvetica;
}

.info {
    top: 40px;
    left: 20px;
}

.info .title {
    font-weight: bold;
    color: #666;
}

.info .state {
    font-weight: bold;
}

.legend {
    right: 20px;
    bottom: 20px;
    color: #555;
}

.legend i {
    width: 18px;
    height: 18px;
    float: left;
    margin-right: 8px;
    opacity: 0.7;
}

addTo()


method

Adds the imported geometries to the vector layer or heatmap layer.
Heatmap layer will only consider features of type point.
Ex. importer.addTo(vectorLayer)

Parameter Type Description
layer VectorLayer | HeatmapLayer The layer on which to add the geometries