calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
715 stars 228 forks source link

Loading shp witouth cpg #125

Closed jpchenel closed 2 years ago

jpchenel commented 4 years ago

Hi, Using Apache Cordova, I'm trying to load shp file without .cpg file.

The error is: Failed to load resource: net::ERR_FILE_NOT_FOUND [file:///storage/emulated/0/app/test.cpg]

If I zip the content, code is working well.

test.shp test.dbf test.shx test.prj

function shp2geojson(file, callback) { file = file.replace(".shp", ""); shp(file).then(function(geojson){ callback(geojson); }); }

With best regards,

calvinmetcalf commented 4 years ago

are you getting an error in the code or seeing an error in the console ? if it's the latter then that's not an issue, since the code doesn't know if there is a cpg on the server so it has to look and the 404 is the web sever telling us that no such file exists.

jpchenel commented 4 years ago

Dear Calvin, Exactly, the error occurs in the console, saying that the file was not found. Can it have a workaround, setting the encoding in utf-8 if the file was not found? Also, why is the code working well using a zip file that does not contain the cpg file? With best regards,

calvinmetcalf commented 4 years ago

Again it is not an error, we are looking to see if a cpg file exists and if it doesn't we assume utf8.

The 'error' you are seeing is not an error the browser prints all 404 level http responses like they are errors even if they are not related to code misbehaving.

On Mon, May 4, 2020, 11:53 AM jpchenel notifications@github.com wrote:

Dear Calvin, Exactly, the error occurs in the console, saying that the file was not found. Can it have a workaround, setting the encoding in utf-8 if the file was not found? Also, why is the code working well using a zip file that does not contain the cpg file? With best regards,

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/calvinmetcalf/shapefile-js/issues/125#issuecomment-623547480, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAITRH4ANZEZLGOBTMN2CDLRP3QHDANCNFSM4MYZQWUQ .

jpchenel commented 4 years ago

Dear Calvin,

Thank for your follow-up.

Maybe it is a misunderstood of my part, but I think that the function binaryAjax in the file in the /dist/shp.js is not the same version as the function in the /lib/binaryajax.js. The first one only has a handler for the load event.

I've only added the file shp.js to my solution (taken from the release 3.4.3) Does I need to include anything else?

With best regards,

calvinmetcalf commented 4 years ago

./lib/binaryajax.js is not the version for use in the browser ./lib/binaryajax-browser.js is the file that ends up in the browser build.

Again if the only problem is that you are getting something in your console that's says there is a 404 then there is no error actually present and I don't think there is an actual issue. Please correct me if I'm wrong and there is an error preventing your app from working correctly.

jpchenel commented 4 years ago

Calvin,

I've updated the file shp.js with this code to add a handler on the error event. Without it, the shp.combine function was never called.

function binaryAjax(url){
    return new Promise(function(resolve,reject){
        var type = url.slice(-3);
        var ajax = new XMLHttpRequest();

        ajax.open('GET',url,true);

        if(type !== 'prj' && type !== 'cpg'){
            ajax.responseType='arraybuffer';
        }

        ajax.addEventListener('load', function (){
            if(ajax.status>399){
                if(type==='prj' || type === 'cpg'){
                    return resolve(false);
                }else{
                    return reject(new Error(ajax.status));
                }
            }
            if(type !== 'prj' && type !== 'cpg'){
                return resolve(new Buffer(ajax.response));
            } else {
                return resolve(ajax.response);
            }
        }, false);

        ajax.addEventListener('error', function (){
            if(type==='prj' || type === 'cpg'){
                return resolve(false);
            } else {
                return reject(new Error(ajax.status));
            }
        }, false);

        ajax.send();
    });
}

Regards,

calvinmetcalf commented 4 years ago

OK that makes more sense, I'll take a look tommorow

On Mon, May 4, 2020, 3:30 PM jpchenel notifications@github.com wrote:

Calvin,

I've updated the file shp.js with this code to add a handler on the error event. Without it, the shp.combine function was never called.

function binaryAjax(url){ return new Promise(function(resolve,reject){ var type = url.slice(-3); var ajax = new XMLHttpRequest();

  ajax.open('GET',url,true);

  if(type !== 'prj' && type !== 'cpg'){
      ajax.responseType='arraybuffer';
  }

  ajax.addEventListener('load', function (){
      if(ajax.status>399){
          if(type==='prj' || type === 'cpg'){
              return resolve(false);
          }else{
              return reject(new Error(ajax.status));
          }
      }
      if(type !== 'prj' && type !== 'cpg'){
          return resolve(new Buffer(ajax.response));
      } else {
          return resolve(ajax.response);
      }
  }, false);

  ajax.addEventListener('error', function (){
      if(type==='prj' || type === 'cpg'){
          return resolve(false);
      } else {
          return reject(new Error(ajax.status));
      }
  }, false);

  ajax.send();

}); }

Regards,

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/calvinmetcalf/shapefile-js/issues/125#issuecomment-623659928, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAITRH3P5WR6ZPLSUKL7UKLRP4JW3ANCNFSM4MYZQWUQ .

calvinmetcalf commented 4 years ago

ok I'm somewhat confused, a 404 not found should not trigger an ajax error event, it should trigger a load event with a status set to 404, sigh is this like weird cordova thing where it's triggering an error instead of a success with 404 status?

jpchenel commented 4 years ago

I think this is something possible, maybe some code in cordova raising another kind of error or manage it differently.

calvinmetcalf commented 4 years ago

ok try this version