Newer
Older
soil_moisture / point_downloader_leaflet / leaflet_point_downloader.js
@Evan Linde Evan Linde on 26 Jul 2022 2 KB use https
var map;
var markers = [];
var ok_bounds = L.latLngBounds([[37.002312, -103.002434], [33.615898, -94.43101]]);

function initMap() {
    var osu = L.latLng(36.12242, -97.06967);

    //map = L.map('map').setView(osu, 6);
    map = L.map('map').fitBounds(ok_bounds);

    var oklahoma = L.polyline(ok_polygon_coords, {
		color: '#FF7300',
		weight: 2,
		opacity: 1.0
	}).addTo(map);

    var osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
	maxZoom: 19,
	attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
	}).addTo(map);


    // This event listener will call addMarker() when the map is clicked
    map.on('click', function(event) {
        // only do anything for points clicked inside Oklahoma
        //if(ok_bounds.contains(event.latlng)) {
        /*
           Testing whether a polygon contains a point requires
           https://github.com/hayeswise/Leaflet.PointInPolygon
        */
        if(oklahoma.contains(event.latlng)) {
            deleteMarkers();         // get rid of the existing marker
            addMarker(event.latlng); // add a new marker at the clicked point
            var lat = event.latlng.lat.toFixed(5);
            var lon = event.latlng.lng.toFixed(5);
            document.getElementById("lat").innerHTML = lat;
            document.getElementById("lon").innerHTML = lon;
            update_download_url(lat,lon);
        }
    });

    // Adds a marker at the center of the map.
    addMarker(osu);
    document.getElementById("lat").innerHTML = osu.lat;
    document.getElementById("lon").innerHTML = osu.lng;
    update_download_url(osu.lat,osu.lng);
}

// Adds a marker to the map and push to the array.
function addMarker(location) {
    var marker = L.marker(location).addTo(map);
    markers.push(marker);
}

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
    for (var i = 0; i < markers.length; i++) {
        markers[i].remove();
    }
}

// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
    clearMarkers();
    markers = [];
}

function update_download_url(lat,lon) {
    //var url = 'http://soilmapnik.hpc.okstate.edu/'+lat+'/'+lon+'/vwc/data_request.csv';
    //var htmlstring = '<a href="'+url+'">'+url+'</a>';
    var url2 = 'https://tiger.hpc.okstate.edu/thredds/ncss/grid/agg-soil-moisture/agg_vwc.nc?var=vwc&temporal=all&latitude='+lat+'&longitude='+lon+'&accept=csv_file';
    //var htmlstring2 = '<br/><a href="'+url2+'">'+url2+'</a>';
    var htmlstring2 = '<a href="'+url2+'">'+url2+'</a>';
    document.getElementById("download-url").innerHTML = htmlstring2;
}

window.onload = function () { initMap() };