Skip to content

Commit

Permalink
contour map prep work
Browse files Browse the repository at this point in the history
  • Loading branch information
ianalexmac committed May 15, 2024
1 parent 01a4bc9 commit e2466a5
Show file tree
Hide file tree
Showing 7 changed files with 2,647 additions and 8 deletions.
82 changes: 82 additions & 0 deletions code/contourmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
document.addEventListener('DOMContentLoaded', (event) => {
mapboxgl.accessToken = 'MAPBOX_API';

var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
center: [-149.75, 61.22], // starting position [lng, lat]
zoom: 9, // starting zoom
maxZoom: 12
});

const years = [
'2017',
'2018',
'2019',
'2020',
'2021',
'2022',
'2023'
];

function filterBy(year) {
// Set a filter on the heatmap layer to only show data for the selected year
map.setFilter('contour-layer', ['==', ['get', 'year'], year]);
// Update the year display
document.getElementById('year-display').textContent = year;
}

map.on('load', function () {
fetch('data/permits_lat_long.geojson')
.then(response => {
return response.json();
})
.then(data => {
jsonCallback(null, data);
})
.catch(error => console.error('Error:', error));
});

function jsonCallback(err, data) {
if (err) {
throw err;
}

data.features = data.features.map((d) => {
d.properties.year = Number(d.properties.properties.year);
return d;
});

map.addSource('contour-source', {
'type': 'geojson',
'data': data
});

map.addLayer({
'id': 'contour-layer',
'type': 'contour',
'source': 'contour-source',
'paint': {
'contour-color': [
'step',
['get', 'Valuation'],
'#f1f075',
20,
'#eb4d5c'
]
}
});

filterBy(2017);
}

document.getElementById('year-slider').addEventListener('input', function(e) {
const year = parseInt(e.target.value, 10);
filterBy(year);
});

});




4 changes: 0 additions & 4 deletions code/heatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ function filterBy(year) {
map.on('load', function () {
fetch('data/permits_lat_long.geojson')
.then(response => {
console.log('Response:', response);
return response.json();
})
.then(data => {
console.log('Data:', data);
jsonCallback(null, data);
})
.catch(error => console.error('Error:', error));
Expand Down Expand Up @@ -88,9 +86,7 @@ function jsonCallback(err, data) {
}

document.getElementById('year-slider').addEventListener('input', function(e) {
console.log('Slider input event:', e); // Log the event
const year = parseInt(e.target.value, 10);
console.log('Slider year:', year); // Log the year
filterBy(year);
});

Expand Down
31 changes: 31 additions & 0 deletions code/value_interpolation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const turf = require('@turf/turf');
const fs = require('fs');

let rawdata = fs.readFileSync('data/permits_lat_long.geojson');
let geojsonData = JSON.parse(rawdata);

let points = geojsonData.features.filter(feature => {
return ['Point', 'LineString', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection'].includes(feature.geometry.type);
});
// Define the grid parameters
let cellSize = 0.1;
let gridUnits = 'degrees';

// Create a grid of points
let grid = turf.pointGrid(turf.bbox(points), cellSize, {units: gridUnits});

// Perform IDW interpolation
let interpolated = turf.interpolate(points, grid, {gridType: 'point', property: 'Valuation', units: gridUnits});

// Convert the GeoJSON object to a string
let data = JSON.stringify(interpolated);

// Write the string to a file
fs.writeFile('data/value_interpolated.geojson', data, 'utf8', function(err) {
if (err) {
console.log('An error occurred while writing JSON Object to File.');
return console.log(err);
}

console.log('JSON file has been saved.');
});
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ <h2>Anchorage Solar Installations</h2>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.6.1/mapbox-gl.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src='code/heatmap.js'></script>
<script src='code/contourmap.js'></script>
</body>
</html>
Loading

0 comments on commit e2466a5

Please sign in to comment.