src/sidebar/Layers.js
// Adding file reading code in Layers as this is the only place it is used.
Adding file reading code in Layers as this is the only place it is used.
Adding file reading code in Layers as this is the only place it is used .
readGeoJSONFile(file_upload){
    var file = file_upload.target.files[0];
    const newest_file_key = Math.random().toString(36).substr(2, 9);
    const newest_file_name = file.name;
    if (file) {
      var reader = new FileReader();
      reader.onload = function(e) {
        const new_geojson = JSON.parse(e.target.result)
        if (new_geojson.type == 'FeatureCollection') {
          var geojson_features = new_geojson.features
          var new_features = []
          for (var i = 0; i < geojson_features.length; i++) {
            if(geojson_features[i].geometry.type == 'Polygon') {
              new_features.push(geojson_features[i])
            }
            else if(geojson_features[i].geometry.type == 'MultiPolygon') {
              for (var j = 0; j < geojson_features[i].geometry.coordinates.length; j++) {
                var feature = {"type": "Feature", "properties": {},  "geometry": { "type": "Polygon", "coordinates": geojson_features[i].geometry.coordinates[j] }}
                feature.properties = geojson_features[i].properties
                new_features.push(feature)
              }
            }
            else if(geojson_features[i].geometry.type == 'Point') {
              var circle = turf.circle(geojson_features[i].geometry.coordinates, 0.0025)
              circle.properties = geojson_features[i].properties
              new_features.push(circle)
            } else if(geojson_features[i].geometry.type == 'LineString') {
              new_features.push(geojson_features[i])
            }
          }
          var clean_geojson = {"type":"FeatureCollection","features": new_features };
        } else if (new_geojson.type == 'Feature') {
            var clean_geojson = {"type":"FeatureCollection","features": new_geojson };
        } else {
          var clean_geojson = clean_data(new_geojson)
        }
        new_geojsonToParent(clean_geojson, newest_file_key)
        createLayer(newest_file_name, newest_file_key, clean_geojson)
      }
      reader.readAsText(file);
    } else {
      alert("Failed to load file");
    }
  }
readGeoJSONFile ( file_upload ) { var file = file_upload . target . files [ 0 ] ; const newest_file_key = Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ; const newest_file_name = file . name ; if ( file ) { var reader = new FileReader ( ) ; reader . onload = function ( e ) { const new_geojson = JSON . parse ( e . target . result ) if ( new_geojson . type == 'FeatureCollection' ) { var geojson_features = new_geojson . features var new_features = [ ] for ( var i = 0 ; i < geojson_features . length ; i ++ ) { if ( geojson_features [ i ] . geometry . type == 'Polygon' ) { new_features . push ( geojson_features [ i ] ) } else if ( geojson_features [ i ] . geometry . type == 'MultiPolygon' ) { for ( var j = 0 ; j < geojson_features [ i ] . geometry . coordinates . length ; j ++ ) { var feature = { "type" : "Feature" , "properties" : { } , "geometry" : { "type" : "Polygon" , "coordinates" : geojson_features [ i ] . geometry . coordinates [ j ] } } feature . properties = geojson_features [ i ] . properties new_features . push ( feature ) } } else if ( geojson_features [ i ] . geometry . type == 'Point' ) { var circle = turf . circle ( geojson_features [ i ] . geometry . coordinates , 0.0025 ) circle . properties = geojson_features [ i ] . properties new_features . push ( circle ) } else if ( geojson_features [ i ] . geometry . type == 'LineString' ) { new_features . push ( geojson_features [ i ] ) } } var clean_geojson = { "type" : "FeatureCollection" , "features" : new_features } ; } else if ( new_geojson . type == 'Feature' ) { var clean_geojson = { "type" : "FeatureCollection" , "features" : new_geojson } ; } else { var clean_geojson = clean_data ( new_geojson ) } new_geojsonToParent ( clean_geojson , newest_file_key ) createLayer ( newest_file_name , newest_file_key , clean_geojson ) } reader . readAsText ( file ) ; } else { alert ( "Failed to load file" ) ; } }
Adding file reading code in Layers as this is the only place it is used.
Adding file reading code in Layers as this is the only place it is used .
//Retrieving the first (and only!) File from the FileList object //NOTE: Math.random should be unique (with less than 10.000 simulatanious layers) because of its seeding algorithm. // Checks the uploaded file, and cleans the data, making everything on same format. // Sending the JSON extracted from file to Parent component.