Skip to content

Commit

Permalink
Add user ip datails to dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
planemad committed Dec 4, 2023
1 parent a6d996b commit 097d427
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 77 deletions.
90 changes: 35 additions & 55 deletions js/chennai.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Simple map
var SELECTED_ROADS_SOURCE;
let SELECTED_ROADS_SOURCE;

let user = {
ip : null,
location : null
}

mapboxgl.accessToken = PUBLIC_ACCESS_TOKEN;
var map = new mapboxgl.Map({
Expand All @@ -8,37 +13,43 @@ var map = new mapboxgl.Map({
hash: true
});

// Find user details to add to contributed data
fetch('https://cloudflare.com/cdn-cgi/trace')
.then(resp => resp.text())
.then(data => {

userIpMatch = data.match(/ip=([\d.]+)/);
userLocationMatch = data.match(/colo=([\d.]+)/);

user.location = userLocationMatch ? userLocationMatch[1] : null;
user.ip = userIpMatch ? userIpMatch[1] : null;

})

map.off('tile.error', map.onError);

// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());

// Add geolocate control to the map.
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
// When active the map will receive updates to the device's location as it changes.
trackUserLocation: true,
// Draw an arrow next to the location dot to indicate which direction the device is heading.
showUserHeading: true
})
);

map.on('style.load', function (e) {

addSourcesAndLayers();

getDataSet();

// map.on('click', function (e) {
// map.featuresAt(e.point, {
// radius: 10,
// layer: LOAD_INFO_LAYERS,
// includeGeometry: true
// }, loadInfo);
// });

// Update map legend from styles
$('[data-map-layer]').each(function () {
// Get the color of the feature from the map
var obj = $(this).attr('data-map-layer');

try {
var color = map.getPaintProperty(obj, 'circle-color');
// Set the legend color
$(this).prepend('<div class="map-legend-circle" style="background:"' + array2rgb(color) + '></div>');
} catch (e) {
return;
}
});
});

function getDataSet(startID) {
Expand Down Expand Up @@ -105,6 +116,8 @@ function addRoad(data, addedRoads, addedFeatures, features) {
};
tempObj.properties['is_flooded'] = true;
tempObj.properties['timestamp'] = Date.now();
tempObj.properties['ip'] = user.ip;
tempObj.properties['location'] = user.location;
tempObj.id = md5(JSON.stringify(tempObj));

var url = DATASETS_BASE + 'features/' + tempObj.id + '?access_token=' + DATASETS_ACCESS_TOKEN;
Expand Down Expand Up @@ -204,42 +217,9 @@ function selectionHandler(data) {
});
}

function loadInfo(err, features) {
if (err) throw err;

if (features.length > 0) {
var popupHTML = '<h5>' + features[0].properties.Name + '</h5><p>' + $('[data-map-layer=' + features[0].layer.id + ']').html() + '</p>';

var popup = new mapboxgl.Popup();

popup
.setLngLat(features[0].geometry.coordinates)
.setHTML(popupHTML)
.addTo(map);
}
}

function updateFeatureCount(data) {
$('#feature-count').toggleClass('loading');
$('#feature-count').html(data.features.length);
$('#feature-count').toggleClass('loading');
}

function array2rgb(color) {
// Combine and return the values
return 'rgba(' + color.map(function (x) {
return x * 255;
}).join() + ')';
}

$(function () {
$('#sidebar').mCustomScrollbar({
theme: 'rounded-dots',
scrollInertia: 100,
callbacks: {
onInit: function () {
$('#sidebar').css('overflow', 'auto');
}
}
});
});
2 changes: 1 addition & 1 deletion js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ var DATASET_ID = 'clpqxs1d31ym31umqlq5yaop5';
var DATASETS_BASE = 'https://api.mapbox.com/datasets/v1/chennaiflood/' + DATASET_ID + '/';
var DATASETS_ACCESS_TOKEN = 'sk.eyJ1IjoiY2hlbm5haWZsb29kIiwiYSI6ImNscHF4dXpxdTA0cGMybG84NDIwd3Z6N24ifQ.R40WZ0N2V_C7HgfKOhCPKg';
var PUBLIC_ACCESS_TOKEN = 'pk.eyJ1IjoiY2hlbm5haWZsb29kIiwiYSI6ImNscHF4d20wdDA2cG8yaXA1aDIzaGNoYmcifQ.6gd6zLtZC3oSZj9nG5zxCQ';
var STYLESHEET = 'mapbox://styles/planemad/cih4qzr0w0012awltzvpie7qa';
var STYLESHEET = 'mapbox://styles/planemad/clpr7hshs013b01qt7t6ehr92';
var LOAD_INFO_LAYERS = ['chennai-relief-camps', 'chennai-relief-camps-22nov'];
22 changes: 1 addition & 21 deletions js/data/mapLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,6 @@ var MAP_LAYERS = {
"water": ["water", "waterway-river-canal", "waterway-small"],
"buildings": ["building"],
"road": [
"road-main",
"road-construction",
"road-rail",
"road-motorway",
"road-trunk",
"road-street",
"road-service-driveway",
"road-path",
"tunnel-motorway",
"tunnel-trunk",
"tunnel-main",
"tunnel-street",
"bridge-main",
"bridge-street",
"bridge-trunk",
"bridge-motorway",
"road-street_limited",
"aeroway-runway",
"aeroway-taxiway",
"road-rail",
"bridge-rail"
"road-simple"
]
};

0 comments on commit 097d427

Please sign in to comment.