Skip to content

Instantly share code, notes, and snippets.

@pjurczynski
Last active May 16, 2025 08:31
Show Gist options
  • Save pjurczynski/6137d3a4ca0642b18cfdae65c6cf29de to your computer and use it in GitHub Desktop.
Save pjurczynski/6137d3a4ca0642b18cfdae65c6cf29de to your computer and use it in GitHub Desktop.
script that lets you import geojson in the project coordinate system
javascript:(async function(){
try {
const authToken = JSON.parse(localStorage.getItem("pix4d-session") || "{}").access_token;
if (!authToken) {
alert("No 'pix4d-session' token found in localStorage.");
return;
}
const input = document.createElement('input');
input.type = 'file';
input.accept = '*/*';
input.style.display = 'none';
document.body.appendChild(input);
input.onchange = async function(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = async function(e) {
const fileContents = e.target.result;
// You should define this function elsewhere or inline if small
const serializeData = function(data) {
const projectId = window.location.href.match(/dataset\/(\d+)\//)[1];
const annotations = data.features.map(( feature, index ) => {
return {
entity_type: "Project",
entity_id: projectId,
geometry: feature.geometry,
properties: {
name: "Annotation - " + index,
visible: true
}
}
})
return JSON.stringify({ annotations });
};
const body = serializeData(JSON.parse(fileContents));
try {
const response = await fetch("https://api.webgis.pix4d.com/v1/annotations", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + authToken
},
body: body
});
const result = await response.text();
alert("Upload successful: " + result);
} catch (err) {
alert("Upload failed: " + err.message);
}
};
reader.readAsText(file);
};
input.click();
} catch (err) {
alert("Unexpected error: " + err.message);
}
})();
javascript:(function()%7B!async%20function()%7Btry%7Bconst%20e%3DlocalStorage.getItem(%22pix4d-session%22)%3Bif(!e)return%20void%20alert(%22No%20'pix4d-session'%20token%20found%20in%20localStorage.%22)%3Bconst%20t%3Ddocument.createElement(%22input%22)%3Bt.type%3D%22file%22%2Ct.accept%3D%22*%2F*%22%2Ct.style.display%3D%22none%22%2Cdocument.body.appendChild(t)%2Ct.onchange%3Dasync%20function(t)%7Bconst%20o%3Dt.target.files%5B0%5D%3Bif(!o)return%3Bconst%20a%3Dnew%20FileReader%3Ba.onload%3Dasync%20function(t)%7Bconst%20o%3Dt.target.result%2Ca%3D((i%3Do).features.map(((e%2Ct)%3D%3E(%7Bentity_type%3A%22Project%22%2Centity_id%3An%2Cgeometry%3Ae.geometry%2Cproperties%3A%7Bname%3A%22Annotation%20-%20%22%2Bt%2Cvisible%3A!0%7D%7D)))%2CJSON.stringify(%7Bfile%3Ai%7D))%3Bvar%20i%3Btry%7Bconst%20t%3Dawait%20fetch(%22https%3A%2F%2Fapi.webgis.pix4d.com%2Fv1%2Fannotations%22%2C%7Bmethod%3A%22POST%22%2Cheaders%3A%7B%22Content-Type%22%3A%22application%2Fjson%22%2CAuthorization%3A%22Bearer%22%2Be.accessToken%7D%2Cbody%3Aa%7D)%2Cn%3Dawait%20t.text()%3Balert(%22Upload%20successful%3A%20%22%2Bn)%7Dcatch(e)%7Balert(%22Upload%20failed%3A%20%22%2Be.message)%7D%7D%2Ca.readAsText(o)%7D%2Ct.click()%3Bconst%20n%3Dprompt(%22Enter%20the%20project%20ID%3A%22)%7Dcatch(e)%7Balert(%22Unexpected%20error%3A%20%22%2Be.message)%7D%7D()%3B%7D)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment