visit
Everyone knows and loves maps, but what’s GIS, you say:
Geospatial Information Systems (GIS) is an area of cartography and information technology concerned with the storage, manipulation, analysis, and presentation of geographic and spatial data. You are probably most familiar with GIS services that produce dynamic, two-dimensional tile maps which have been prominent on the web since the days of .
Note: The CodePen examples embedded in this post are best viewed on CodePen directly.
CSS JSResult Skip Results Iframe
EDIT ON
// Render GeoJSON features on a spherical object.
// Create Three.js scene, camera, & light
var WIDTH = window.innerWidth - 30,
HEIGHT = window.innerHeight - 30;
var angle = 75,
aspect = WIDTH / HEIGHT,
near = 0.5,
far = 1000;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(angle, aspect, near, far);
// Renderer the canvas
var renderer = new THREE.WebGLRenderer();
renderer.setSize( WIDTH, HEIGHT);
renderer.setClearColor( 0x555555, 1);
document.body.appendChild(renderer.domElement);
// create a point light (goes in all directions)
scene.add(new THREE.AmbientLight(0x71ABEF));
var pointLight = new THREE.PointLight(0x666666);
// set its position
pointLight.position.x = 60;
pointLight.position.y = 50;
pointLight.position.z = 230;
scene.add(pointLight);
// Create a sphere to make visualization easier.
var geometry = new THREE.SphereGeometry(10,32,32);
var material = new THREE.MeshPhongMaterial({
color: 0xDDDDDD,
wireframe: false,
transparent: true
});
var sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);
sphere.castShadow = true;
sphere.receiveShadow = true;
//Draw the GeoJSON at THREE.ParticleSystemMaterial
var countries = $.getJSON("//s3-us-west-2.amazonaws.com/s.cdpn.io/230399/countries_states.geojson", function (data) {
drawThreeGeo(data, 10, 'sphere', {
color: 0x8B572A,
skinning: true
});
});
var rivers = $.getJSON("//s3-us-west-2.amazonaws.com/s.cdpn.io/230399/rivers.geojson", function (data) {
drawThreeGeo(data, 10, 'sphere', {
color: '#4A90E2'
});
});
//Set the camera position
camera.position.z = 20;
//Enable controls
controls = new THREE.TrackballControls(camera);
// Slow down zooming
controls.zoomSpeed = 0.1;
//Render the image
function render() {
controls.update();
requestAnimationFrame(render);
renderer.render(scene, camera);
}
render();
is simply the best option for working with the display of points, symbols, and all types of features on web and mobile devices. The library supports , , , , , and a wide variety of layers. It performs quickly and handles a variety of formats. The library also has .
is a library from for geospatial analysis. One of the great things about Turf.js is that you can create a collection of features and then spatially analyze, modify (geoprocess), and simplify them, before using Leaflet to present the data. Like Geolib, Turf.js will calculate the path length, feature center, points inside a feature.
creates and other symbology by simply defining a GeoJSON object and data attribute.
// Public auth for initializing the map
var mapMain = 'stevepepple.lkojhhoh';
L.mapbox.accessToken = 'pk.eyJ1Ijoic3RldmVwZXBwbGUiLCJhIjoiTmd4T0wyNCJ9.1-jWg2J5XmFfnBAhyrORmw';
// Quantile or Jenks classification
var type = $("#type").val();
var breaks;
// Color scale using chroma.js
var scale = chroma.scale('YlOrBr');
// Create the base map using Mapbox
map = L.mapbox.map('map', mapMain, {
attributionControl: false,
maxZoom: 12,
minZoom: 5
}).setView([34.0261899, -118.2455643], 8);
// Calculate population density for each county
_.each(counties.features, function(feature) {
// Population from 2010 Census
var pop = feature.properties.POP;
// Calcualate desnity as popluation / square meters
var area = turf.area(feature.geometry);
var sq_miles = area * 0.000000386102158542;
feature.properties.pop_density = (pop / sq_miles);
});
updateMap();
// Show the Choropleth of Population Density
function updateMap() {
// Make a Turf collection for analysis
collection = turf.featurecollection(counties.features);
// Basic UI for select Jenks or Quantile classification
if (type == "jenks") {
breaks = turf.jenks(collection, "pop_density", 8);
} else {
breaks = turf.quantile(collection, "pop_density", [25, 50, 75, 99]);
}
// Get the color when adding to the map
var layer = L.geoJson(counties, {
style: getStyle
})
layer.addTo(map);
// Fit to map to counties and set the legend
map.fitBounds(layer.getBounds());
updateLegend();
function getStyle(feature) {
var pop = feature.properties.POP;
var pop_density = feature.properties.pop_density;
return {
fillColor: getColor(pop_density),
fillOpacity: 0.7,
weight: 1,
opacity: 0.2,
color: 'black'
}
}
function updateLegend() {
$(".breaks").html();
for (var i = 0; i < breaks.length; i++) {
var density = Math.round(breaks[i] * 100) / 100;
var background = scale(i / (breaks.length));
$(".breaks").append("<p><div class='icon' style='background: " + background + "'></div><span> " + density + " <span class='sub'>pop / mi<sup>2</sup></span></span></p>");
}
}
}
/* Get color depending on population density value */
function getColor(d) {
// Select a color scale from Color Brewer
var color = scale(0);
// Place the feature based upon breaks
for (var i = breaks.length - 1; i >= 0; i--) {
if (d < breaks[i]) {
// Automatic way to select the color by class
var percentage = (i / (breaks.length));
color = scale(percentage);
}
}
return color;
}
collection = turf.featurecollection(counties.features);
breaks = turf.jenks(collection, "pop_density", 8);
navigator.geolocation.getCurrentPosition(function(result){
// do something with result.coords
);
Location-aware web applications can use Turf.js spatial analysis methods for advanced techniques such as geofencing a location inside or outside of a map feature. For instance, you can take the result from the above example and use the method to see if that coordinate is within the boundaries of a given neighborhood.
/* Calculate the center of all points */
function findCenter() {
var centroidPt = turf.centroid(points);
var marker = L.geoJson(centroidPt)
markers.push(marker)
marker.addTo(map);
}
/* Find the rectangular boundary of all points */
function findBounds() {
bbox = turf.extent(points);
bboxPolygon = turf.bboxPolygon(bbox);
var bounds = L.geoJson(bboxPolygon);
map.fitBounds(bounds.getBounds());
}
// Show a pseudo-random sample of points in the box
function randomSample() {
var sample = L.geoJson(turf.sample(points, 100), {
style: circle_options
})
sample.addTo(map);
markers.push(sample);
}
/* Find the Most Central POI by Neighborhood */
function findCentralPOIs() {
hoods = turf.featurecollection(neighborhoods.features);
display = L.geoJson(hoods, {
style: hood_options
}).addTo(map);
_.each(hoods.features, function(feature) {
try {
var center = turf.centroid(feature);
var nearest = turf.nearest(center, points);
var marker = L.geoJson(center)
markers.push(marker)
marker.addTo(map);
} catch (e) {
console.log(e)
}
});
}
/* Find the Most Central POI by Neighborhood */
function hoodByPOI() {
hoods = turf.featurecollection(neighborhoods.features);
display = L.geoJson(hoods, {
style: hood_options
}).addTo(map);
_.each(hoods.features, function(feature) {
try {
console.log(feature)
var collection = turf.featurecollection(feature);
//var aggregated = turf.sum( hoods, points, 'population', 'sum');
var counted = turf.count(collection, points, 'pt_count');
console.log(counted)
} catch (e) {
console.log(e)
}
});
}
/* Triangular Irregular Network */
function makeTIN() {
// THis collection was pre-made using the spatial join (turf.tag) feature
var collection = westlake;
var tin = turf.tin(collection);
//var markers = L.geoJson(collection).addTo(map);
var display = L.geoJson(tin, tin_options);
display.addTo(map);
markers.push(display);
map.fitBounds(display.getBounds());
_.each(collection.features, function(x) {
// Convert lat/long to Leaflet coord
var coord = L.latLng(x.geometry.coordinates[1], x.geometry.coordinates[0]);
var radius = 30;
var circle = L.circle(coord, radius, circle_options).addTo(map);
markers.push(circle);
});
}
// Heat map of all points using Leaflet Heatmap
function makeHeatMap() {
coords = []; //define an array to store coordinates
heat_options = {
radius: 14,
gradient: {
0.4: '#80cdc1',
0.65: '#b8e186',
1.0: '#d01c8b'
}
}
_.each(points.features, function(feature) {
coords.push([feature.geometry.coordinates[1], feature.geometry.coordinates[0]]);
});
var scale = chroma.scale('PuBuGn');
try {
var heat = L.heatLayer(coords, heat_options)
markers.push(heat);
heat.addTo(map);
map.setZoom(14);
} catch (e) {
console.log(e)
}
}
// Show or Hide LA Neighborhoods
function showNeighborhoods(hide) {
if (hide == true) {
map.removeLayer(display_hoods)
} else {
display_hoods = L.geoJson(hoods, {
style: hood_options
});
display_hoods.addTo(map);
}
}
// Util function to clear all features/markers
function clearMap() {
for (i = 0; i < markers.length; i++) {
map.removeLayer(markers[i])
}
}
// UI for calling the different functions
function bindActions() {
doAnalysis(showNeighborhoods);
$("#show_hoods").change(function() {
var checked = $(this).prop("checked");
if (checked == true) {
showNeighborhoods();
} else {
showNeighborhoods(true);
}
});
}
function doAnalysis(which, arg) {
clearMap();
which.call(this, arg);
}
/* Map Setup, which is less interesting */
var mapMain = 'osaez.kp2ddba3';
L.mapbox.accessToken = 'pk.eyJ1Ijoib3NhZXoiLCJhIjoiOExKN0RWQSJ9.Hgewe_0r7gXoLCJHuupRfg';
map = L.mapbox.map('map', mapMain, {
attributionControl: false,
maxZoom: 14,
minZoom: 5
}).setView([34.0261899, -118.2455643], 10);
var markers = [];
var layers = [];
var hoods, points, display_hoods;
var circle_options = {
color: '#053275',
opacity: 0,
weight: .5,
fillColor: '#3490E7 ',
fillOpacity: .5
};
var hood_options = {
color: "#9013FE",
weight: .4,
opacity: 0.65,
fillColor: "#9013FE",
fillOpacity: .01
};
var tin_options = {
color: "#417505",
weight: .4,
opacity: 0.65,
fillColor: "#417505 ",
fillOpacity: .1
};
//Convert Shapefile to GeoJSON using Shapefile.js
shp("//s3-us-west-2.amazonaws.com/s.cdpn.io/230399/la-poi-only.zip").then(function(geojson) {
json = geojson;
points = turf.featurecollection(json.features);
hoods = turf.featurecollection(neighborhoods.features);
prepAnalysis();
});
function prepAnalysis() {
// Show a box around all possible points of interest
hull = turf.convex(points);
layer = L.geoJson(hull);
bindActions();
}
Also published on: