Created
May 15, 2017 09:26
-
-
Save Athorcis/1a358c6ceccbba58e61188e050083c24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($, L, zoneController, map) { | |
'use strict'; | |
$.getScript('https://npmcdn.com/@turf/turf/turf.min.js'); | |
var Control = L.Control.extend({ | |
options: { | |
position: 'topleft' | |
}, | |
onAdd: function (map) { | |
var $container = $('<div></div>'), | |
$findButton = $('<button>Trouver les zones vides</button>'), | |
$nextButton = $('<button>Afficher la prochaine zone vide</button>'); | |
$findButton.click(function (event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
mbShowLoadingOverlay(); | |
$findButton.hide(); | |
findEmptyAreas(function () { | |
$nextButton.click(); | |
mbHideLoadingOverlay(); | |
if (emptyAreas.length > 0) { | |
$nextButton.show(); | |
} | |
}); | |
}); | |
$nextButton.click(function (event) { | |
event.stopPropagation(); | |
event.preventDefault(); | |
displayNextEmptyArea(); | |
if (emptyAreas.length === 0) { | |
$findButton.show(); | |
$nextButton.hide(); | |
} | |
}); | |
$nextButton.hide(); | |
$container.append($findButton); | |
$container.append($nextButton); | |
return $container.get(0); | |
} | |
}); | |
function latLngBoundsToTurfPolygon(bounds) { | |
return turf.polygon([[ | |
[bounds.getNorth(), bounds.getWest()], | |
[bounds.getNorth(), bounds.getEast()], | |
[bounds.getSouth(), bounds.getEast()], | |
[bounds.getSouth(), bounds.getWest()], | |
[bounds.getNorth(), bounds.getWest()] | |
]]); | |
} | |
function addTurfPolygonToMap(polygon) { | |
map.addLayer(L.multiPolygon(polygon.geometry.coordinates)); | |
} | |
function computeIntersection(id, coordinates, data) { | |
try { | |
var area, intersection, | |
view = latLngBoundsToTurfPolygon(map.getBounds()); | |
if (!coordinates[0][0][0]) { | |
coordinates = [coordinates]; | |
} | |
area = turf.polygon(coordinates); | |
intersection = turf.intersect(view, area); | |
if (intersection !== undefined) { | |
if (filledArea === null) { | |
filledArea = intersection; | |
} else { | |
filledArea = turf.merge({ type: 'FeatureCollection', features: [filledArea, intersection] }); | |
} | |
} | |
} catch (exception) { | |
console.error(exception); | |
} | |
} | |
function findEmptyAreas(callback) { | |
filledArea = null; | |
emptyAreas = []; | |
zoneController.zoneVilleLayer.load.hook = computeIntersection; | |
zoneController.requestMapUpdate(); | |
$(zoneController).one('villeLoaded', function () { | |
var coordinates = filledArea.geometry.coordinates, | |
length = coordinates.length; | |
delete zoneController.zoneVilleLayer.load.hook; | |
if (length > 1) { | |
for (var i = 1; i < length; ++i) { | |
emptyAreas.push(coordinates[i]); | |
} | |
} | |
if (typeof callback === 'function') { | |
callback(); | |
} | |
}); | |
} | |
function displayNextEmptyArea() { | |
if (emptyAreas.length) { | |
currentCoordinates = emptyAreas.shift(); | |
if (currentPolygon) { | |
map.removeLayer(currentPolygon); | |
} | |
currentPolygon = L.polygon(currentCoordinates); | |
map.addLayer(currentPolygon); | |
console.log('JSON: ' + JSON.stringify(currentCoordinates)); | |
} | |
} | |
var filledArea = null, | |
emptyAreas = null, | |
currentCoordinates = null, | |
currentPolygon = null; | |
map.addControl(new Control()); | |
}(jQuery, L, mbZoneController, cdf_map_zones)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment