landcover/index.html

703 строки
26 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">
<title>Land Cover Mapping</title>
<!-- Core CSS -->
<link href="css/leaflet.css" rel="stylesheet" />
<link href="css/leaflet-slider.css" rel="stylesheet" />
<link href="css/leaflet-sidebar.css" rel="stylesheet" />
<link href="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet-easybutton@2/src/easy-button.css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<style type="text/css">
html, body {
height: 100%;
height: 100%;
}
body {
padding: 0;
margin: 0;
}
#map {
width: 100%;
height: 100%;
min-height: 100%;
}
.leaflet-control-slider{
border-radius: 2px;
}
.sidebar{
border: 0 !important;
right: 20px !important;
}
.sidebar-content{
right: 0 !important;
}
.sidebar-header{
background-color: #19715E;
font-family: "Segoe UI Web (West European)",Segoe UI,-apple-system,BlinkMacSystemFont,Roboto,Helvetica Neue,sans-serif;
font-size: 1.2em;
letter-spacing: .1rem;
font-weight: 400;
color: white;
}
.sidebar-tabs{
background-color: rgba(255, 255, 255, 0.95);
}
#inputNAIP{
width: 125px;
height: 125px;
margin: 10px;
}
.exampleImage{
width: 125px;
height: 125px;
margin: 10px;
}
.exampleImage.active{
border: 2px solid red;
}
.leaflet-image-layer.gray{
filter: grayscale(66%);
}
.slider{
height: 25px;
width: 100%;
}
input[type=button] {
color: #ccc;
background-color: #595959;
border: solid 3px #6e6e6e;
width: 85%;
height: 40px;
}
input[type=button]:hover{
cursor: pointer;
cursor: hand;
text-decoration: none;
color: #fff;
}
a.leaflet-control-slider-toggle {
width: 171px;
}
.logo-area{
background-color: #19715E;
width:300px;
height:70px;
display: flex !important;
flex-basis: auto;
flex-grow: 1;
align-items: center;
font-family: "Segoe UI Web (West European)",Segoe UI,-apple-system,BlinkMacSystemFont,Roboto,Helvetica Neue,sans-serif;
}
.logo-text{
font-size: 1.7em;
letter-spacing: .1rem;
font-weight: 400;
line-height: 1.5;
color: white;
padding: 2px;
padding-right: 2rem;
padding-left: 2rem;
}
.logo-link{
font-size: 100%;
font-weight: 400;
color: white;
padding-right: 1.5rem;
padding-left: 1.5rem;
}
.m-0{
margin: 0 !important;
}
.leaflet-container .logo-area a {
color: #19715E;
}
.leaflet-container .logo-area a:hover {
color: #25A782;
}
</style>
</head>
<body>
<!--
<nav class="navbar navbar-solid navbar-fixed-top">
<a class="" href=".">
<figure class="m-0">
<span class="logo-text"> AI for Earth </span>
</figure>
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-sm-auto">
<li class="nav-item mx-xl-5">
<a class="nav-link text-capitalize text-white px-sm-4 small" href="https://www.microsoft.com/en-us/ai-for-earth/land-cover-mapping-api">About</a>
</li>
</ul>
</div>
</nav>
-->
<div id="map"></div>
<div id="sidebar" class="sidebar">
<!-- Tab panes -->
<div class="sidebar-content">
<!-- Start of "home" tab -->
<div class="sidebar-pane" id="home">
<h1 class="sidebar-header">
Land Cover Mapping
<!--
<span class="sidebar-close">
<i class="fa fa-caret-right"></i>
</span>
-->
</h1>
<div style="text-align: center; margin-bottom:10px;" id="inputImages">
<h3>NAIP Input</h3>
<img id="inputNAIP">
</div>
<div style="text-align: center; margin-bottom:10px; width:100%;" id="exampleImages">
<h3>Land Cover Predictions</h3>
<div id="exampleImageList">
</div>
</div>
<div style="text-align: center; margin-bottom:10px;" id="classAdjustments">
<h3>Change Class Weights</h3>
<table style="width:100%;">
<tr>
<td style="text-align: center;">Water</td>
<td>
<img src="css/images/Water.png" />
</td>
<td style="width:30px;" id="waterVal">25</td>
<td>
<input type="range" min="1" max="100" value="25" class="slider" id="waterRange">
</td>
</tr>
<tr>
<td style="text-align: center;">Forest</td>
<td>
<img src="css/images/Forest.png" />
</td>
<td style="width:30px;" id="forestVal">25</td>
<td>
<input type="range" min="1" max="100" value="25" class="slider" id="forestRange">
</td>
</tr>
<tr>
<td style="text-align: center;">Field</td>
<td>
<img src="css/images/Field.png" />
</td>
<td style="width:30px;" id="fieldVal">25</td>
<td>
<input type="range" min="1" max="100" value="25" class="slider" id="fieldRange">
</td>
</tr>
<tr>
<td style="text-align: center;">Built</td>
<td>
<img src="css/images/Built.png" />
</td>
<td style="width:30px;" id="builtVal">25</td>
<td>
<input type="range" min="1" max="100" value="25" class="slider" id="builtRange">
</td>
</tr>
</table>
</div>
</div>
<!-- End of "home" tab -->
</div>
</div>
<!-- Core JavaScript
================================================== -->
<script src="js/jquery-3.3.1.min.js"></script>
<!-- Leaflet JavaScript
================================================== -->
<script src="js/leaflet.js" type="text/javascript"></script>
<script src="js/leaflet-slider.js" type="text/javascript"></script>
<script src="js/leaflet-sidebar.min.js" type="text/javascript"></script>
<script src="https://unpkg.com/leaflet-control-geocoder/dist/Control.Geocoder.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/leaflet-easybutton@2/src/easy-button.js" type="text/javascript"></script>
<!-- List of backend URLS to query
================================================== -->
<script src="endpoints.js" type="text/javascript"></script>
<script type="text/javascript">
var processingServiceURL = ENDPOINTS[0]["url"];
var START_CENTER = [38, -94]; // lat, lon
var START_ZOOM = 4;
var SELECTION_SIZE = 200; // in meters, probably
var selectionBox = null;
var currentSelection = null;
var exampleImgsList = [];
var currentPatches = [];
var currentPatchSrcs = [];
var currentPatchModelIDs = [];
var soft0_hard1 = 1;
var active_im_id = 0;
var weights = [0.25, 0.25, 0.25, 0.25];
var requestPatches = function(polygon, weights_save, map){
// Request input image from the first server we know about
requestInputPatch(polygon, ENDPOINTS[0]["url"]);
// Setup placeholders for the predictions from the current click to be saved to
currentPatches.push(null);
currentPatchSrcs.push(Object());
currentPatchModelIDs.push(Object());
var idx = currentPatches.length-1;
for(var i=0; i<ENDPOINTS.length; i++){
//console.debug("Running requestPatch on " + ENDPOINTS[i]["url"]);
currentPatchSrcs[idx][i] = null;
currentPatchModelIDs[idx][i] = null;
requestPatch(polygon, weights_save, map, idx, i, ENDPOINTS[i]["url"]);
}
};
var requestInputPatch = function(polygon, serviceURL){
var topleft = L.latLng(polygon[0][0], polygon[0][1]);
var topleftProjected = L.CRS.EPSG3857.project(topleft);
var bottomright = L.latLng(polygon[2][0], polygon[2][1]);
var bottomrightProjected = L.CRS.EPSG3857.project(bottomright);
var request = {
"extent": {
"xmax": bottomrightProjected.x,
"xmin": topleftProjected.x,
"ymax": topleftProjected.y,
"ymin": bottomrightProjected.y,
"spatialReference": {
"latestWkid": 3857
}
},
};
$.ajax({
type: "POST",
url: serviceURL + "getInput",
data: JSON.stringify(request),
success: function(data, textStatus, jqXHR){
var resp = data;
$("#inputNAIP").attr("src", "data:image/png;base64," + resp.input_naip);
},
error: function(jqXHR, textStatus, errorThrown){
var response = $.parseJSON(jqXHR.responseText);
console.log("Error in processing server: " + response.error);
},
dataType: "json",
contentType: "application/json"
});
};
var requestPatch = function(polygon, weights_save, map, requestIdx, patchId, serviceURL=processingServiceURL){
var topleft = L.latLng(polygon[0][0], polygon[0][1]);
var topleftProjected = L.CRS.EPSG3857.project(topleft);
var bottomright = L.latLng(polygon[2][0], polygon[2][1]);
var bottomrightProjected = L.CRS.EPSG3857.project(bottomright);
var request = {
"extent": {
"xmax": bottomrightProjected.x,
"xmin": topleftProjected.x,
"ymax": topleftProjected.y,
"ymin": bottomrightProjected.y,
"spatialReference": {
"latestWkid": 3857
}
},
"weights": weights_save,
};
$.ajax({
type: "POST",
url: serviceURL + "predPatch",
data: JSON.stringify(request),
success: function(data, textStatus, jqXHR){
var resp = data;
var srcs = [
"data:image/png;base64," + resp.output_soft,
"data:image/png;base64," + resp.output_hard,
];
var img = $("#exampleImage_"+patchId);
if (patchId == active_im_id) {
img.addClass("active");
}
img.attr("src", srcs[soft0_hard1]);
img.attr("data-name", resp.model_name);
img.attr("data-weights", JSON.stringify(resp.weights));
exampleImgsList[patchId] = img;
if(patchId == active_im_id){
var imageLayer = L.imageOverlay(srcs[soft0_hard1],
L.polygon(polygon).getBounds()).addTo(map);
currentPatches[requestIdx] = imageLayer;
}
currentPatchSrcs[requestIdx][patchId] = srcs;
currentPatchModelIDs[requestIdx] = active_im_id;
},
error: function(jqXHR, textStatus, errorThrown){
var response = $.parseJSON(jqXHR.responseText);
console.log("Error in processing server: " + response.error);
},
dataType: "json",
contentType: "application/json"
});
};
var getPolyAround = function(latlon, radius){
// We convert the input lat/lon into the EPSG3857 projection,
// define our square, then re-convert to lat/lon
var latlonProjected = L.CRS.EPSG3857.project(latlon);
var x = latlonProjected.x;
var y = latlonProjected.y;
var top = y + radius/2;
var bottom = y - radius/2;
var left = x - radius/2;
var right = x + radius/2;
top = Math.round(top);
bottom = Math.round(bottom);
left = Math.round(left);
right = Math.round(right);
// left/right are "x" points while top/bottom are the "y" points
var topleft = L.CRS.EPSG3857.unproject(L.point(left, top));
var bottomright = L.CRS.EPSG3857.unproject(L.point(right, bottom));
return [[topleft.lat, topleft.lng],
[topleft.lat, bottomright.lng],
[bottomright.lat, bottomright.lng],
[bottomright.lat, topleft.lng]];
};
$(document).ready(function(){
var sliderRanges = [
$("#waterRange"),
$("#forestRange"),
$("#fieldRange"),
$("#builtRange")
];
var sliderVals = [
$("#waterVal"),
$("#forestVal"),
$("#fieldVal"),
$("#builtVal")
];
//----------------------------------------------------------------------
// Setup map layers
//----------------------------------------------------------------------
var OpenStreetMap_Mapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
});
var Esri_WorldImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles &copy; Esri &mdash; Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community'
});
var interesting_locations = [
L.marker([47.60, -122.15]).bindPopup('Bellevue, WA'),
L.marker([39.74, -104.99]).bindPopup('Denver, CO'),
L.marker([37.53, -77.44]).bindPopup('Richmond, VA'),
];
var cities = L.layerGroup(interesting_locations);
var map = L.map('map', {
zoomControl: false,
crs: L.CRS.EPSG3857, // this is a default, but I'm setting it to be explicit about what CRS we are in
center: START_CENTER,
zoom: START_ZOOM,
layers: [Esri_WorldImagery, cities]
});
//---------------------------------------------------------------------
// Setup AI4E Branding
//----------------------------------------------------------------------
var logoControl = $("<div class='leaflet-control logo-area'></div>");
logoControl.append("<a href='.' style='text-decoration: underline;' class='.m-0'><span class='logo-text'> AI for Earth </span></a>");
logoControl.append("<a href='https://www.microsoft.com/en-us/ai-for-earth/land-cover-mapping-api' style='text-decoration: underline;'><span class='logo-link'>About</span></a>");
$(".leaflet-top.leaflet-left").append(logoControl)
//----------------------------------------------------------------------
// Setup leaflet-control-geocoder plugin
//----------------------------------------------------------------------
L.Control.geocoder({
collapsed: false,
position:'topleft',
defaultMarkGeocode: true
}).addTo(map);
//----------------------------------------------------------------------
// Custom initialization of the map zoom controls so that we can
// position it where we want
//----------------------------------------------------------------------
L.control.zoom({
position:'topleft'
}).addTo(map);
//----------------------------------------------------------------------
// Setup the leaflet-easybutton plugin to reset the map to its initial
// position
//----------------------------------------------------------------------
L.easyButton(
'fa-undo', function(btn, map) {
map.closePopup();
map.setView(START_CENTER, START_ZOOM);
},
).addTo(map);
//----------------------------------------------------------------------
// Setup layer picker and initialize the "interesting points" click
// handlers
//----------------------------------------------------------------------
var baseMaps = {
"OpenStreetMap Mapnik": OpenStreetMap_Mapnik,
"ESRI World Imagery": Esri_WorldImagery
};
var overlayMaps = {
"Interesting Points": cities
};
for(var i=0;i<interesting_locations.length;i++){
interesting_locations[i].on('click', function(e){
map.setView(e.latlng, 13);
});
}
L.control.layers(
baseMaps, overlayMaps, {
collapsed:false,
position:"bottomleft"
}
).addTo(map);
//----------------------------------------------------------------------
// Setup leaflet-slider plugin, we make two sliders to control opacity
// and selection window size
//----------------------------------------------------------------------
L.control.slider( // opacity slider
function(value){
map.getPane('overlayPane').style.opacity = value / 100.0;
}, {
position: 'bottomleft',
id: 'opacity_slider',
orientation: 'horizontal',
collapsed: true,
syncSlider: true,
min: 0,
max: 100,
value: 100,
logo: "Opacity",
size: "171px"
}
).addTo(map);
L.control.slider( // sharpness slider
function(value){
soft0_hard1 = value;
for(idx=0; idx<currentPatches.length; idx++){
active_im_id = currentPatchModelIDs[idx];
currentPatches[idx].setUrl(currentPatchSrcs[idx][active_im_id][soft0_hard1]);
}
if(currentPatchModelIDs.length>0){
last_idx = currentPatchSrcs.length-1;
for(idx=0; idx<exampleImgsList.length; idx++){
exampleImgsList[idx].attr("src",
currentPatchSrcs[last_idx][idx][soft0_hard1]);
}
}
}, {
position: 'bottomleft',
id: 'soft_hard_slider',
orientation: 'horizontal',
collapsed: true,
syncSlider: true,
min: 0,
max: 1,
value: 1,
logo: "Sharpness",
size: "171px"
}
).addTo(map);
L.control.slider( // window size slider
function(value){
SELECTION_SIZE = value;
}, {
position: 'bottomleft',
id: 'selection_size_slider',
orientation: 'horizontal',
collapsed: true,
syncSlider: true,
min: 50,
max: 700,
value: 500,
logo: "Patch Size",
size: "171px"
}
).addTo(map);
//----------------------------------------------------------------------
// Setup leaflet-sidebar-v2 and open the "#home" tab
//----------------------------------------------------------------------
var sidebar = L.control.sidebar(
'sidebar', {
position: 'right'
}
).addTo(map);
sidebar.open("home")
//----------------------------------------------------------------------
// Setup map selection handlers
//----------------------------------------------------------------------
map.addEventListener('mousemove', function(e){
curSelPoly = getPolyAround(e.latlng, SELECTION_SIZE)
if(selectionBox === null){
selectionBox = L.polygon(curSelPoly, {
color: "#000000",
fillColor: "#ffffff",
weight: 2
});
selectionBox.addTo(map);
}else{
selectionBox.setLatLngs(curSelPoly);
}
});
map.addEventListener('click', function(e){
curSelPoly = getPolyAround(e.latlng, SELECTION_SIZE);
if(currentSelection === null){ // This condition creates the red selection box on the first click
currentSelection = L.polygon(curSelPoly, {
color: "#ff0000",
fillColor: "#ffffff",
weight: 2
});
currentSelection.addTo(map);
}else{
currentSelection.setLatLngs(curSelPoly);
}
requestPatches(curSelPoly, weights, map);
});
//----------------------------------------------------------------------
// Setup adjustment sliders
//----------------------------------------------------------------------
var updateWeights = function(){
var sumVal = 0;
for(var j=0;j<sliderRanges.length;j++){
sumVal += parseFloat(sliderRanges[j].val());
}
for(var j=0;j<weights.length;j++){
weights[j] = parseFloat(sliderRanges[j].val()) / sumVal;
}
for(var j=0;j<weights.length;j++){
var value = Math.round((Math.round(weights[j]*100)/100)*100);
sliderRanges[j].val(value);
sliderVals[j].html(value);
}
}
for(var i=0;i<sliderRanges.length;i++){
sliderRanges[i].on("input", function(){
updateWeights();
});
}
//----------------------------------------------------------------------
// Setup the example images list
//----------------------------------------------------------------------
for(var i=0; i<ENDPOINTS.length; i++){
var img = $("<img class='exampleImage'>");
img.attr("im-id", i);
img.attr("id", "exampleImage_"+i);
$("#exampleImageList").append(img);
exampleImgsList.push(img);
}
$(".exampleImage").click(function(){
$(".exampleImage").removeClass("active");
$(this).addClass("active");
active_im_id = $(this).attr("im-id")
idx = currentPatches.length-1;
currentPatches[idx].setUrl(currentPatchSrcs[idx][active_im_id][soft0_hard1]);
$(this).attr("src", currentPatchSrcs[idx][active_im_id][soft0_hard1]);
currentPatchModelIDs[idx] = active_im_id;
});
});
</script>
</body>
</html>