Skip to content
Snippets Groups Projects
Commit 6ceba590 authored by Jakob Severin Steffensen Hjelseth's avatar Jakob Severin Steffensen Hjelseth
Browse files

Polygon OK, ikke Line

parent 1e3df4d7
No related branches found
No related tags found
2 merge requests!63Dev,!62Resolve "Rydd opp i riktig filformat ved opplasting av egne geojson-filer"
......@@ -4,30 +4,43 @@ function handleFile() {
const selectedFile = fileHandler.files[0];
if (selectedFile == null) {
return alert("No chosen file!")
return alert("No chosen file!");
}
//console.log(selectedFile); // Printer bare selve filen til loggen
fileHandler.value = "";
document.getElementById("loadButton").style.backgroundColor = "orangered";
var read = new FileReader();
read.readAsDataURL(selectedFile);
var newLayer = L.geoJSON(null, {style: getStyle()})
var newLayer = L.geoJSON(null, {style: getStyle()});
var info = null;
read.onloadend = function() {
fetch(read.result).then(function(response) {
return response.json();
}).then(function(data) {
newLayer.addData(data);
info = isInputMultiPolygon(data);
if (info[0]) {
if (info[1] == "P") {
data = inputMultiPolygon(data);
} else if (info[1] == "L") {
data = inputMultiLine(data);
}
}
console.log(JSON.stringify(data));
//newLayer.addData(data);
})
}
/*
overlayMaps[selectedFile.name] = newLayer;
updateSidebar();
handleLayer(selectedFile.name);
*/
}
......@@ -33,3 +33,98 @@ function multiPolygonToFeatureCollection(layer) {
}
return turf.featureCollection(features);
}
/*
Håndtering av input-filer fra bruker for å få de på rett format egnet for turf-operasjoner
*/
function isInputMultiPolygon(layer) {
liste = layer["features"];
for (var i = 0; i < liste.length; i++) {
if (liste[i]["geometry"]["type"] == "MultiPolygon") {
return [true, "P"];
} else if (liste[i]["geometry"]["type"] == "MultiLineString") {
return [true, "L"];
}
}
return [false, null];
}
function turnList(liste) {
var more = false;
try {
var test = liste[0][0][0];
if (test != null) {
more = true;
}
} catch {
more = false;
}
if (more) {
var newList = [];
for (var i = 0; i < layer.length; i++) {
newList.push(turnList(liste[i]));
}
} else {
var newList = [];
for (var i = liste.length - 1; i > -1; i--) {
newList.push(liste[i]);
}
}
return newList;
}
function inputMultiPolygon(layer) {
liste = layer["features"];
k = liste.length;
for (var i = 0; i < k; i++) {
if (liste[i]["geometry"]["coordinates"].length == 1) {
liste[i]["geometry"]["type"] = "Polygon";
liste[i]["geometry"]["coordinates"] = [turnList(liste[i]["geometry"]["coordinates"][0][0])];
}
else if (liste[i]["geometry"]["coordinates"].length > 1) {
var coordinates = liste[i]["geometry"]["coordinates"];
liste[i]["geometry"]["type"] = "Polygon";
liste[i]["geometry"]["coordinates"] = [turnList(coordinates[0][0])];
for (var j = 1; j < coordinates.length; j++) {
newElement = {"type": liste[i]["type"], "properties": liste[i]["properties"], "geometry": {"type": "Polygon", "coordinates": [turnList(coordinates[j][0])]}};
liste.push(newElement);
}
}
}
layer["features"] = liste;
return layer;
}
function inputMultiLine(layer) {
liste = layer["features"];
k = liste.length;
for (var i = 0; i < k; i++) {
if (liste[i]["geometry"]["coordinates"].length == 1) {
liste[i]["geometry"]["type"] = "LineString";
liste[i]["geometry"]["coordinates"] = turnList(liste[i]["geometry"]["coordinates"][0][0]);
}
else if (liste[i]["geometry"]["coordinates"].length > 1) {
var coordinates = liste[i]["geometry"]["coordinates"];
liste[i]["geometry"]["type"] = "LineString";
liste[i]["geometry"]["coordinates"] = turnList(coordinates[0][0]);
for (var j = 1; j < coordinates.length; j++) {
newElement = {"type": liste[i]["type"], "properties": liste[i]["properties"], "geometry": {"type": "Polygon", "coordinates": turnList(coordinates[j][0])}};
liste.push(newElement);
}
}
}
layer["features"] = liste;
return layer;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment