Published
Edited
Nov 9, 2019
Insert cell
md`# Leaflet kml with omnivore mapping practice`
Insert cell
L = require("leaflet")
Insert cell
Tabletop = require("tabletop")
Insert cell
omnivore = require('https://bundle.run/leaflet-omnivore@0.3.4')
Insert cell
html ` <link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<link rel="stylesheet" href="http://code.ionicframework.com/ionicons/1.5.2/css/ionicons.min.css">`
Insert cell
html ` <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""></script>

<!-- Tabletop allows us to connect to Google Sheets -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/tabletop.js/1.5.1/tabletop.min.js'></script>

<!-- Font awesome has options for icons and stuff -->
<script src="https://kit.fontawesome.com/1e3fb69e1e.js" crossorigin="anonymous"></script>

<!-- Omnivore lets us ingest lots of different kinds of data -->
<script src='//api.tiles.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.3.1/leaflet-omnivore.min.js'></script>`
Insert cell
html `<div id="mapid" style="height: 600px;"></div>`
Insert cell
{ // Set viewing box coordinates
var mymap = L.map('mapid').setView([41.9049617,-99.6452142], 7);
// Add tile layer
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
maxZoom: 18,
// Set the mapbox base layer style here
id: 'mapbox.outdoors',
// Access token comes from Mapbox account
accessToken: 'pk.eyJ1IjoiY29kYW5iYXIiLCJhIjoiY2syaHg4ZGs4MGY1MDNtbGlkeWV4YmpnMyJ9.Rv6IL_aRXHNjKcw2irDz3g'
}).addTo(mymap);
// Add an individual point to a map
//var marker = L.marker([40.8152976,-96.7014155]).addTo(mymap);
// Define a function to call data from Google Sheets

/*
Tabletop.init({
key: "https://docs.google.com/spreadsheets/d/16Z466d6FhGJBKnyFZymja6b0hLx0Lin60KPLTbq6OZY/edit?usp=sharing",
callback: addPoints,
simpleSheet: true
});
*/

/*
// Populate the map with markers from your data - we're looping through each row
function addPoints(data, tabletop) {
for (var row in data) {
var marker = L.marker([
data[row].lat,
data[row].long
]).addTo(mymap);
// Now we're adding popups - plain version, bolded and colored
marker.bindPopup("<p>" + "<strong style='color: #84b819'>" + data[row].market_name + "</strong style>" + "<br/>" + data[row].operation + "<br/>" + data[row].weekday + "<br/>" + "</p>");
}
}
*/
// If we want a bolded header
//"<strong>" and then "</strong>"
// If we want a bold header with color
//"<strong style='color: #84b819'>" and then "</strong style>"
/*
// Populate the map with markers with images from your data - we're still looping through each row
function addPoints(data, tabletop) {
for (var row in data) {
var marker = L.marker([
data[row].lat,
data[row].long
]).addTo(mymap);
// Now we're adding popups - plain version, bolded and colored
marker.bindPopup("<p>" + data[row].market_name + "<br/>" + data[row].operation + "<br/>" + data[row].weekday + "<br/>" + "<img src='" + data[row].image_url + "' height='100' />" + "</p>");
// Note that the Google Sheet that data[row].image_url is pulling from is pulled from every time, so you can modify the images in the Google Sheet without having to mess with the code. But if the Google Sheet info disappears, the code won't be able to pull the images/data/whatever.
}
}
*/

// If we want other images for our icons

// Specify marker style

/*
var LeafIcon = L.Icon.extend({
options: {
// iconSize: [25, 41],
iconAnchor: [12, 30],
// adjust iconAnchor if the icon appear to be floating above, below, or to the side of the actual point on the map (lat and long) that you are trying to mark on the map
popupAnchor: [1, -34]
}
});
/*

/*
// Specify icon url
var greenIcon = new LeafIcon({
iconUrl: 'https://img.icons8.com/offices/30/000000/leaf.png'
})
function addPoints(data, tabletop) {
for (var row in data) {
var marker = L.marker([
data[row].lat,
data[row].long
], {icon:greenIcon}).addTo(mymap);
// Now we're adding popups - plain version, bolded and colored
marker.bindPopup("<p>" + data[row].market_name + "<br/>" + data[row].operation + "<br/>" + data[row].weekday + "</p>");
}
}
*/


/*
//color markers based on some additional value
//specify colors
var colors = {
orange: '#ffa500',
blue: '#0000ff',
green: '#008000',
pink: '#ffc0cb',
};

//set marker opacity
var markerOptions = {
opacity: 1,
};

switch(Data[i].value) {
case "A":
markerOptions.color = colors.orange;
break;
case "B":
markerOptions.color = colors.blue;
break;
case "C":
markerOptions.color = colors.green;
break;
default:
markerOptions.color = colors.pink;
break;
}

var marker = new L.circleMarker(markerLocation,markerOptions).addTo(map);
*/
// Style for kml layer
var whoopingLayer = L.geoJson(null, {
// http://leafletjs.com/reference.html#geojson-style
style: function(feature) {
return { color: '#bcbddc', weight: .5, opacity: 1.5 };
// #aaa is just white. Use Colorbrewer2.com to find a color that you like.
}
});
// Style for kml layer
var beetleLayer = L.geoJson(null, {
// http://leafletjs.com/reference.html#geojson-style
style: function(feature) {
return { color: '#fdae6b', weight: 1, opacity: 1 };
// #aaa is just white. Use Colorbrewer2.com to find a color that you like.
}
});
// Style for kml layer
var squirrelLayer = L.geoJson(null, {
// http://leafletjs.com/reference.html#geojson-style
style: function(feature) {
return { color: '#43a2ca', weight: 1, opacity: 1.5 };
// #aaa is just white. Use Colorbrewer2.com to find a color that you like.
}
});

/*
// Import kml with omnivore
omnivore.kml("https://mrdata.usgs.gov/geology/state/kml/mtgeol.kml", null, customLayer).addTo(mymap);
*/
//Map whooping crane habitat
omnivore.kml("https://opendata.arcgis.com/datasets/74997701f62f468d9a097e9062fb9dea_68.kml?where=CommonName%20%3D%20%27Whooping%20Crane%27&geometry=%7B%22xmin%22%3A-116.376%2C%22ymin%22%3A38.584%2C%22xmax%22%3A-83%2C%22ymax%22%3A44.341%2C%22type%22%3A%22extent%22%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%7D%7D&outSR=%7B%22latestVcsWkid%22%3A5703%2C%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%2C%22vcsWkid%22%3A5703%7D", null, whoopingLayer).addTo(mymap)
//Map beetle habitat
omnivore.kml("https://opendata.arcgis.com/datasets/74997701f62f468d9a097e9062fb9dea_68.kml?where=CommonName%20%3D%20%27Salt%20Creek%20Tiger%20Beetle%27&geometry=%7B%22xmin%22%3A-116.376%2C%22ymin%22%3A38.584%2C%22xmax%22%3A-83%2C%22ymax%22%3A44.341%2C%22type%22%3A%22extent%22%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%7D%7D&outSR=%7B%22latestVcsWkid%22%3A5703%2C%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%2C%22vcsWkid%22%3A5703%7D", null, beetleLayer).addTo(mymap)
//Map Flying Squirrel habitat
omnivore.kml("https://opendata.arcgis.com/datasets/74997701f62f468d9a097e9062fb9dea_68.kml?where=CommonName%20%3D%20%27Southern%20Flying%20Squirrel%27&geometry=%7B%22xmin%22%3A-116.376%2C%22ymin%22%3A38.584%2C%22xmax%22%3A-83%2C%22ymax%22%3A44.341%2C%22type%22%3A%22extent%22%2C%22spatialReference%22%3A%7B%22wkid%22%3A4326%7D%7D&outSR=%7B%22latestVcsWkid%22%3A5703%2C%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%2C%22vcsWkid%22%3A5703%7D", null, squirrelLayer).addTo(mymap)
}


// Import kml with omnivore practice
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more