0

As GeoServer does not support GeoJSON upload through REST API, I have tried to convert the GeoJSON to shapefile and try to insert the shapefile to GeoServer. But it does not work. Attached in the image is the error from geoserver.log. Maybe one of you can point out any issues. Here is the JavaScript code.

function postGeoJSON(){
    // geonode upload layer     
    console.log("Uploading geonode layer using REST API");  
    
    var object;      
    var httpRequest2 = new XMLHttpRequest(); // asynchronous request

    httpRequest2.open("GET", "https://openlayers.org/en/v4.6.5/examples/data/geojson/countries.geojson", true);
  
    httpRequest2.send();
    httpRequest2.addEventListener("readystatechange", function() {
        if (this.readyState === this.DONE) {
            // when the request has completed
            object = JSON.parse(this.response);
            console.log("geojson " + object);
            console.log("geojson " + JSON.stringify(object));
             body = JSON.stringify(object)                    
         }
    });
  
  //download GEOJSON
  console.log("Downloading GEOJSON as Shapefile");  
  // (optional) set names for feature types and zipped folder
  var options2 = {
        folder: 'myshapes',
        types: {
            point: 'mypoints',
            polygon: 'mypolygons',
            line: 'mylines'
        }
  }
             
  //convert geojson to shapefile
  var shpfile = shpwrite.zip(object);

  // triggers a download of a zip file with shapefiles contained within.
  console.log('shpfile' + JSON.stringify(shpfile) );
  
        
  var xhr = new XMLHttpRequest();   
  xhr.open('POST', 'https://<<DOMAIN>>/geoserver/rest/workspaces/geonode/datastores/my_geonode_data/featuretypes', true);

  xhr.onload = function () {
    // do something to response
    console.log('responseText: ' + this.responseText);
  };
  
  xhr.onerror = function () {
    console.log("** An error occurred during the transaction");
  };
  
  xhr.setRequestHeader('Content-Type', 'application/zip');  //;type=geojson   application/json;charset=UTF-8
            
  //xhr.setRequestHeader('Content-Length', body.length);
        xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
  console.log(" response " + xhr.responseText);
        
  xhr.onload = function () {
          console.log(xhr.responseText)
        };
        
  xhr.send(shpfile); //body);      
  var responseObject = xhr.responseText;
  console.log('responseObject' + responseObject); // ------- Test status of upload
  

}

enter image description here

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.