Skip to content

Instantly share code, notes, and snippets.

@ArrEssJay
Last active February 12, 2016 00:18
Show Gist options
  • Save ArrEssJay/8258a0651f5089bd80b0 to your computer and use it in GitHub Desktop.
Save ArrEssJay/8258a0651f5089bd80b0 to your computer and use it in GitHub Desktop.
NSW 6-figure abbreviated UTM grid reference to 1:25k map sheet. Handles AGD66 and MGA94 datums.
//requires esri leaflet, leaflet, proj4js
//fiddle at https://jsfiddle.net/a8aLj0nk/13
var map = L.map('map').setView([-34.00, 151.00], 9);
var position = 0;
var marker;
L.esri.tiledMapLayer("http://mapsq.six.nsw.gov.au/arcgis/rest/services/sixmaps/LPITopoMap/MapServer", {
detectRetina: true,
reuseTiles: true
}).addTo(map);
var sheets = L.esri.featureLayer('http://mapsq.six.nsw.gov.au/arcgis/rest/services/sixmaps/Boundaries/MapServer/18', {
style: function (feature) {
return {
color: 'grey',
weight: 0.5,
opacity: 0.5,
fillOpacity: 0
};
}
}).addTo(map);
grRegEx = '^[0-9]{6,6}$';
var previousIds = [];
function processPoint(e){
console.log('Click: ' + e.latlng);
sheet = document.getElementById('sheet_extents');
var qr = sheets.query().nearby(e.latlng, 500);
qr.count(function (error, count, response) {
console.log('Found ' + count + ' features');
});
qr.ids(function (error, ids) {
for (var j = 0; j < previousIds.length; j++) {
sheets.resetStyle(previousIds[j]);
}
previousIds = ids;
for (var i = 0; i < ids.length; i++) {
console.log("Setting style for feature " + i);
sheets.setFeatureStyle(ids[i], {
color: 'blue',
weight: 2,
opacity: 0.5,
fillOpacity: 0.1
});
}
});
qr.run(function (error, featureCollection, response) {
for (var i = 0; i < featureCollection.features.length; i++) {
tile = featureCollection.features[i];
var poly = L.polygon(tile.geometry.coordinates);
sheet.innerHTML = tile.properties.maptitle + '<br>' + tile.properties.mapnumber + '<br>';
var projFrom = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs';
var projTo = '+proj=utm +zone=' + tile.properties.mgazone + ' +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs';
var proj66 = '+proj=utm +zone=' + tile.properties.mgazone + ' +south +ellps=aust_SA +towgs84=-117.808,-51.536,137.784,0.303,0.446,0.234,-0.29 +no_defs';
var bounds = poly.getBounds();
var neBoundUTM = proj4(projFrom, projTo).forward([bounds._northEast.lat, bounds._northEast.lng]);
sheet.innerHTML += 'NE LatLng :' + [bounds._northEast.lat, bounds._northEast.lng] + '<br>';
sheet.innerHTML += 'NE UTM :' + neBoundUTM + '<br>';
var markpos = proj4(projFrom, projTo).forward([e.latlng.lng, e.latlng.lat]);
if (typeof (marker) == 'object') {
map.removeLayer(marker);
}
var gr = fromGridRef(document.getElementById('pointInput').value, neBoundUTM);
console.log(gr);
var pointgr;
//get lat lng of the gr
console.log(document.getElementById('is66').value);
if (document.getElementById('is66').checked) {
console.log('Transform using AGD66 datum');
pointgr = proj4(projTo, projFrom).forward(gr);
}
else {
pointgr = proj4(proj66, projFrom).forward(gr);
}
sheet.innerHTML += 'Point UTM: ' + gr + '<br>';
sheet.innerHTML += 'Point ll: ' + pointgr + '<br>';
console.log(': ' + pointgr);
console.log(L.latLng([pointgr[1], pointgr[0]]));
marker = new L.Marker(L.latLng([pointgr[1], pointgr[0]]), {
draggable: true
});
console.log(marker);
console.log(e);
map.addLayer(marker);
}
});
}
var point;
map.on('mousedown', function (e) {
point = e;
processPoint(point);
});
//update when toggling checkbox
function fromGridRef(gridref, sheetref) {
console.log('Input ref: ' + gridref);
if (gridref.length > 1) {
if (!gridref.match(grRegEx)) {
alert("Not a valid grid ref: " + gridref);
}
var grE = +gridref.substring(0, 3);
var grN = +gridref.substring(3, 6);
var sE = Math.round(sheetref[0]);
var sN = Math.round(sheetref[1]);
console.log('sE = ' + sE);
console.log('sN = ' + sN);
//the 0,0 point of the sheet (for the 3 sig-figs of the GR we have)
//might be mid-sheet, in which case we need to be -1 on an order of
//magnitude above
var ssE = +('' + sE).substring(1, 4);
if (grE > ssE) {
console.log('3SF (E)' + ssE);
sE = sE - 100000;
console.log('Reduced sE to ' + sE);
}
var ssN = +('' + sN).substring(2, 5);
if (grN > ssN) {
console.log('3SF (N)' + ssN);
sN = sN - 100000;
console.log('Reduced sN to ' + sN);
}
var pointUTM_E = ('' + sE).substring(0, 1) + grE + '00';
var pointUTM_N = ('' + sN).substring(0, 2) + grN + '00';
return [+pointUTM_E, +pointUTM_N];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment