0

I am trying to programmatically upload a map into GeoServer as a layer. The user requirements as such that this data has to be uploaded from memory directly into GeoServer, using JavaScript. I looked up the REST API documentation and tried many things. I have posted the following posts one after another trying many approaches

Can anyone guide me in any approach in which I can upload to GeoServer using JavaScript REST API?

I am willing to use any data format, as long as it can be done through JavaScript REST API, but not Nodejs.

Here is the latest code and error from GeoServer logs.

 function UploadMap()
 {   
    var shapeFile;  //store the shapefile from url    
    console.log("Retrieving Shapefile to Load");
    var httpRequest2 = new XMLHttpRequest(); // asynchronous request
    httpRequest2.open("GET", "https://docs.mapbox.com/help/data/stations.zip", true);
    httpRequest2.send();
    httpRequest2.addEventListener("readystatechange", function() {
        if (this.readyState === this.DONE) {     
            shapeFile = this.response;
            console.log("shapeFile2 " + shapeFile2);
        }
    });      
 
    console.log('Loading Shapefile: ');  
    var xhr = new XMLHttpRequest();
    console.log("XHR.Open");
    xhr.open('PUT', 'https://DOMAIN/geoserver/rest/workspaces/ws/datastores/my_geonode_data/file.shp', true); 
    xhr.setRequestHeader('Content-Type', 'application/zip');  //;type=geojson   
    xhr.setRequestHeader("Authorization", "Basic " + btoa("USERNAME:PASSWORD"));
    xhr.onload = function () {
        console.log('responseText: ' + this.responseText);
    };

    console.log("Sending file for upload, XHR.Send ");         
    xhr.send(shapeFile);    
    var responseObject = xhr.responseText;
    console.log('responseObject' + responseObject); // ------- Test status of upload
}  

Error returned in console Error returned in console

Error within Geoserver.log file

    2022-02-03 02:03:55,568 ERROR [geoserver.rest] - Error occured unzipping file
    org.geoserver.rest.RestException 500 INTERNAL_SERVER_ERROR: Error occured unzipping file
    at org.geoserver.rest.catalog.AbstractStoreUploadController.handleFileUpload(AbstractStoreUploadController.java:132)    at org.geoserver.rest.catalog.DataStoreFileController.doFileUpload(DataStoreFileController.java:577)
    at org.geoserver.rest.catalog.DataStoreFileController.dataStorePut(DataStoreFileController.java:263)
    at sun.reflect.GeneratedMethodAccessor511.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at 
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at 
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
    at 
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMeth 
    odAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

Caused by: java.util.zip.ZipException: zip file is empty
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:225)
at java.util.zip.ZipFile.<init>(ZipFile.java:155)
at java.util.zip.ZipFile.<init>(ZipFile.java:169)
at org.geoserver.rest.util.RESTUtils.unzipFile(RESTUtils.java:271)
at org.geoserver.rest.catalog.AbstractStoreUploadController.handleFileUpload(AbstractStoreUploadController.java:117)
8
  • Are you sure that the zip file is valid and contains all the files needed? What does the GeoServer log file say
    – Ian Turton
    Feb 2, 2022 at 18:55
  • I have just added another similar post in which I try to upload a shapefile from an online shapefile link. gis.stackexchange.com/questions/422765/…
    – SharmaPN
    Feb 3, 2022 at 2:31
  • you need to break this down in to simpler steps - 1. can you upload that zipped shpaefile to geoserver using curl (or other command line tool)? if you can't then the issue is with the file if you can then the problem is with your javascript.
    – Ian Turton
    Feb 3, 2022 at 9:08
  • Yes, I can upload the zipped shapefile using CURL, as follows curl -v -u USERNAME:PASSWORD -XPUT -H "Content-type: application/zip" --data-binary @D:\curl\bin\roads.zip DOMAIN/geoserver/rest/workspaces/geonode/datastores/…
    – SharmaPN
    Feb 3, 2022 at 9:45
  • then this is a javascript issue - more of the geoserver log file might help, you are looking for the section starting caused by
    – Ian Turton
    Feb 3, 2022 at 10:46

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.