Skip to content

Instantly share code, notes, and snippets.

@scubbx
Created April 17, 2019 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scubbx/829f788832565d9dd0587336f410174e to your computer and use it in GitHub Desktop.
Save scubbx/829f788832565d9dd0587336f410174e to your computer and use it in GitHub Desktop.
Reprojection in MapBender on the client (prototype, buggy)

This code is to be added to a "coordinates" element in the digitizer. There still are numerous bugs, but this code proves that something along those lines can be done

Since a (probably older) version of the proj4js library is automatically delivered and loaded with MapBender, one can directrly make use of it.

The window element is used to carry the content of variables from one event to another.

click: |
console.log("click on coordinates detected");
var inputField = el;
var form = inputField.closest(".modal-body");
var epsgField = form.find(".-fn-active-epsgCode> .form-control");
window.liveReprSourceEpsg = epsgField.val();
change: |
function ConvertDDToDMS(D, lng){
return {
dir : D<0?lng?'W':'S':lng?'E':'N',
deg : 0|(D<0?D=-D:D),
min : 0|D%1*60,
sec :(0|D*60%1*6000)/100
};
}
console.log("change on coordinates");
var inputField = el;
var form = inputField.closest(".modal-body");
var epsgField = form.find(".-fn-active-epsgCode> .form-control");
var xField = form.find("[name='x']");
var yField = form.find("[name='y']");
var desiredEpsg = epsgField.val();
if (desiredEpsg != window.liveReprSourceEpsg) {
if (window.liveReprSourceEpsg = "EPSG:4326") {
var splitreprpointx = xField.val().split(/\'|°/);
var splitreprpointy = yField.val().split(/\'|°/);
console.log(splitreprpointx);
console.log(splitreprpointy);
var reprpoint = new Proj4js.Point( xField.val() , yField.val() );
} else {
var reprpoint = new Proj4js.Point( xField.val() , yField.val() );
}
console.log(window.liveReprSourceEpsg + " -> \n" + desiredEpsg );
Proj4js.defs["EPSG:31255"]='+proj=tmerc +lat_0=0 +lon_0=13.33333333333333 +k=1 +x_0=0 +y_0=-5000000 +ellps=bessel +towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232 +units=m +no_defs';
var toCRS = new Proj4js.Proj(desiredEpsg);
var fromCRS = new Proj4js.Proj(window.liveReprSourceEpsg);
var reprCoords = Proj4js.transform(fromCRS ,toCRS ,reprpoint);
if (desiredEpsg == "EPSG:4326") {
var ccordx = ConvertDDToDMS(reprCoords.x);
var ccordy = ConvertDDToDMS(reprCoords.y);
xField.val( ccordx.deg + "°" + ccordx.min + "'" + ccordx.sec + "''" );
yField.val( ccordy.deg + "°" + ccordy.min + "'" + ccordy.sec + "''" );
} else {
xField.val(reprCoords.x);
yField.val(reprCoords.y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment