Skip to content

Commit

Permalink
Implement Vector Tiling for SMAP (#1099)
Browse files Browse the repository at this point in the history
* implemented tile->lat/long converter

adding L.Deflate back

added L.TileLayer.GeoJSON and updated to be compatable with Leaflet 1.0

Currently only loaded spatial resources. Should update to drop SUs when out of view

* Lazy tile loading
  • Loading branch information
linzjax committed Apr 20, 2017
1 parent 49cecee commit cc81d02
Show file tree
Hide file tree
Showing 13 changed files with 686 additions and 94 deletions.
1 change: 0 additions & 1 deletion cadasta/core/static/css/single.scss
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@
#project-map {
width: 100%;
}

}


Expand Down
22 changes: 9 additions & 13 deletions cadasta/core/static/js/map_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ function renderFeatures(map, featuresUrl, options) {
} else {
$('#messages #loading').addClass('hidden');
if (options.fitBounds === 'locations') {
// var bounds = markers.getBounds();
var bounds = geoJson.getBounds();
var bounds = markers.getBounds();
if (bounds.isValid()) {
map.fitBounds(bounds);
map.fitBounds(bounds);
}
}
}
Expand Down Expand Up @@ -97,7 +96,7 @@ function renderFeatures(map, featuresUrl, options) {
} else {
map.fitBounds([[-45.0, -180.0], [45.0, 180.0]]);
}

var geoJson = L.geoJson(null, {
style: { weight: 2 },
onEachFeature: function(feature, layer) {
Expand All @@ -106,17 +105,18 @@ function renderFeatures(map, featuresUrl, options) {
"<h2><span>Location</span>" +
feature.properties.type + "</h2></div>" +
"<div class=\"btn-wrap\"><a href='" + feature.properties.url + "' class=\"btn btn-primary btn-sm btn-block\">" + options.trans['open'] + "</a>" +
"</div>");
"</div>");
}
}
});
// var markers = L.Deflate({minSize: 20, layerGroup: geoJson});
// markers.addTo(map);

var markers = L.Deflate({minSize: 20, layerGroup: geoJson});
markers.addTo(map);
geoJson.addTo(map);

if (options.location) {
options.location.addTo(map);
map.fitBounds(options.location.getBounds());
map.fitBounds(options.location.getBounds());
} else if (projectBounds) {
map.fitBounds(projectBounds);
}
Expand All @@ -142,7 +142,7 @@ function switch_layer_controls(map, options){
var groupedOptions = {
groupCheckboxes: false
};
// map.removeControl(map.layerscontrol);
map.removeControl(map.layerscontrol);
map.layerscontrol = L.control.groupedLayers(
baseLayers, groupedOptions).addTo(map);
}
Expand Down Expand Up @@ -186,7 +186,3 @@ function saveOnMapEditMode() {
saveButton.dispatchEvent(clickEvent);
}
}




141 changes: 141 additions & 0 deletions cadasta/core/static/js/smap/L.Map.Deflate4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
L.Deflate4 = L.LayerGroup.extend({
options: {
minSize: 10,
markerCluster: false
},

initialize: function (options) {
L.Util.setOptions(this, options);
this._allLayers = [];
this._featureGroup = this.options.markerCluster ? L.markerClusterGroup() : L.featureGroup();
},

_isCollapsed: function(path, zoom) {
var bounds = path.getBounds();

var ne_px = this._map.project(bounds.getNorthEast(), zoom);
var sw_px = this._map.project(bounds.getSouthWest(), zoom);

var width = ne_px.x - sw_px.x;
var height = sw_px.y - ne_px.y;
return (height < this.options.minSize || width < this.options.minSize);
},

_getZoomThreshold: function(path) {
var zoomThreshold = null;
var zoom = this._map.getZoom();
if (this._isCollapsed(path, this._map.getZoom())) {
while (!zoomThreshold) {
zoom += 1;
if (!this._isCollapsed(path, zoom)) {
zoomThreshold = zoom - 1;
}
}
} else {
while (!zoomThreshold) {
zoom -= 1;
if (this._isCollapsed(path, zoom)) {
zoomThreshold = zoom;
}
}
}
return zoomThreshold;
},

addLayer: function (layer) {
if (layer instanceof L.FeatureGroup) {
for (var i in layer._layers) {
this.addLayer(layer._layers[i]);
}
} else {
var layerToAdd = layer;
if (layer.getBounds && !layer.zoomThreshold && !layer.marker) {
var zoomThreshold = this._getZoomThreshold(layer);
var marker = L.marker(layer.getBounds().getCenter());

if (layer._popupHandlersAdded) {
marker.bindPopup(layer._popup._content)
}

var events = layer._events;
for (var event in events) {
if (events.hasOwnProperty(event)) {
var listeners = events[event];
for (var i = 0, len = listeners.length; i < len; i++) {
marker.on(event, listeners[i].fn)
}
}
}

layer.zoomThreshold = zoomThreshold;
layer.marker = marker;
layer.zoomState = this._map.getZoom();

if (this._map.getZoom() <= zoomThreshold) {
layerToAdd = layer.marker;
}
this._allLayers.push(layer);
}

this._featureGroup.addLayer(layerToAdd);
}
},

removeLayer(layer) {
if (layer instanceof L.FeatureGroup) {
for (var i in layer._layers) {
this.removeLayer(layer._layers[i]);
}
} else {
this._featureGroup.removeLayer(layer.marker);
this._featureGroup.removeLayer(layer);

const index = this._allLayers.indexOf(layer);
if (index !== -1) { this._allLayers.splice(index, 1); }
}
},

_switchDisplay: function(layer, showMarker) {
if (showMarker) {
this._featureGroup.addLayer(layer.marker);
this._featureGroup.removeLayer(layer);
} else {
this._featureGroup.addLayer(layer);
this._featureGroup.removeLayer(layer.marker);
}
},

_deflate: function() {
const bounds = this._map.getBounds();
const endZoom = this._map.getZoom();
var markersToAdd = [];
var markersToRemove = [];

for (var i = 0, len = this._allLayers.length; i < len; i++) {
if (this._allLayers[i].zoomState !== endZoom && this._allLayers[i].getBounds().intersects(bounds)) {
this._switchDisplay(this._allLayers[i], endZoom <= this._allLayers[i].zoomThreshold);
this._allLayers[i].zoomState = endZoom;
}
}
},

getBounds: function() {
return this._featureGroup.getBounds();
},

onAdd: function(map) {
this._featureGroup.addTo(map);
this._map.on('zoomend', this._deflate, this);
this._map.on('dragend', this._deflate, this);
},

onRemove: function(map) {
map.removeLayer(this._featureGroup);
this._map.off('zoomend', this._deflate, this);
this._map.off('dragend', this._deflate, this);
}
});

L.deflate4 = function (options) {
return new L.Deflate4(options);
};
Loading

0 comments on commit cc81d02

Please sign in to comment.