Last active
August 1, 2016 17:29
-
-
Save easherma/99e2ecbad0eb93fce2d927f593b62c48 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
///////////////////////////////////////////////////////////////////////////////////////////// | |
//setting up the map http://jsfiddle.net/fq7vu2rn/3/// | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
// set center coordinates | |
var centerlat = 41.81173; | |
var centerlon = -87.666227; | |
// set default zoom level | |
var zoomLevel = 10; | |
// initialize map | |
var map = L.map('map').setView([centerlat,centerlon], zoomLevel); | |
// set source for map tiles | |
ATTR = '© <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' + | |
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a> | ' + | |
'© <a href="http://cartodb.com/attributions">CartoDB</a>'; | |
CDB_URL = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png'; | |
// add tiles to map | |
L.tileLayer(CDB_URL, {attribution: ATTR}).addTo(map); | |
var bbox =[-87.75613096987291,41.7218260301271,-87.5763230301271,41.9016339698729]; | |
var bb = {xmin: -87.75613096987291, ymin: 41.7218260301271,xmax:-87.5763230301271,ymax: 41.9016339698729}; | |
var xmin = -87.75613096987291; | |
var ymin = 41.7218260301271; | |
var xmax = -87.5763230301271; | |
var ymax = 41.9016339698729; | |
var bboxPoly = L.geoJson(turf.bboxPolygon(bbox)); | |
//L.geoJson(turf.bboxPolygon(bbox)).addTo(map); | |
var area = turf.area(turf.bboxPolygon(bbox)); | |
console.log(Math.sqrt(area)/1000); | |
//L.geoJson(area).addTo(map); | |
//var points = turf.explode(turf.bboxPolygon(bbox)); | |
var units = 'kilometers'; | |
//var distance = turf.distance(from, to, units); | |
var points = turf.multiPoint([[xmin,ymin], [xmax,ymax]]); | |
//L.geoJson(points).addTo(map); | |
var distance = turf.distance([xmin,ymax], [xmin,ymin], 'kilometers'); | |
console.log(distance); | |
var cellWidth = distance/2; | |
//create hex grid and count points within each cell | |
var squaregrid = turf.squareGrid(bbox, cellWidth, units); | |
//collect points into array | |
/* | |
features = []; | |
var midpointed1 = turf.midpoint([xmin,ymin], [xmin,ymax]); | |
L.geoJson(midpointed1).addTo(map); | |
features.push(midpointed1); | |
var midpointed2 = turf.midpoint([xmax,ymin], [xmax,ymax]); | |
L.geoJson(midpointed2).addTo(map); | |
features.push(midpointed2); | |
var midpointed3 = turf.midpoint([xmax,ymin], [xmin,ymin]); | |
L.geoJson(midpointed3).addTo(map); | |
features.push(midpointed3); | |
var midpointed4 = turf.midpoint([xmin,ymax], [xmax,ymax]); | |
features.push(midpointed4); | |
L.geoJson(midpointed4).addTo(map); | |
var centroid = turf.centroid(turf.bboxPolygon(bbox)); | |
//L.geoJson(centroid).addTo(map); | |
features.push(centroid); | |
L.geoJson(turf.lineString([midpointed3['geometry']['coordinates'], centroid['geometry']['coordinates']])).addTo(map); | |
var fc = turf.featureCollection(features);*/ | |
function splitBbox(bbox) { | |
var halfWidth = (bbox.xmax - bbox.xmin) / 2.0, | |
halfHeight = (bbox.ymax - bbox.ymin) / 2.0; | |
return [ | |
{xmin: bbox.xmin, ymin: bbox.ymin, ymax: bbox.ymin + halfHeight, xmax: bbox.xmin + halfWidth}, | |
{xmin: bbox.xmin + halfWidth, ymin: bbox.ymin, ymax: bbox.ymin + halfHeight, xmax: bbox.xmax}, | |
{xmin: bbox.xmin, ymin: bbox.ymin + halfHeight, xmax: bbox.xmin + halfWidth, ymax: bbox.ymax}, | |
{xmin: bbox.xmin + halfWidth, ymin: bbox.ymin + halfHeight, xmax: bbox.xmax, ymax: bbox.ymax} | |
]; | |
} | |
function getQuads (bb) { | |
var split = splitBbox(bb); | |
var quads = []; | |
for (var i = 0; i < split.length; i++) { | |
var test = quads.push(turf.bboxPolygon([split[i]['xmin'],split[i]['ymin'], split[i]['xmax'],split[i]['ymax']] )); | |
console.log(quads); | |
} | |
var fc = turf.featureCollection(quads); | |
L.geoJson(fc).addTo(map); | |
console.log(fc); | |
return quads; | |
} | |
var split1 = getQuads(bb); | |
var split2 = getQuads(split1[0]); | |
console.log(getQuads); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment