calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
MIT License
735 stars 230 forks source link

RangeError: TextDecoder constructor: The given encoding is not supported. #136

Closed bcastagna closed 4 years ago

bcastagna commented 4 years ago

Hi

First, thanks for this package, it's very handy :+1: :100:

Recently we encountered an issue which appears only on our built for prod with AOT enabled angular app :

shp('assets/shape-files/' + this.placeName).then((geojson: Object) => { console.log("Geojson retrieved " + geojson); this.serviceParams['placeName'] = this.placeName; this.placeGeoJson = geojson; this.loadMap(); }, (error: any) => { this.nbToastrService.show( 'Error while retrieving shape file ! Check console.', 'Error', {status: 'danger'} ); console.log(error); });

image

We have searched for an explanation for hours but still can find one. Few info:

Please help. What could it be ?? :(

calvinmetcalf commented 4 years ago

I believe the issue is that you are not properly returning 404 errors on your server, I believe you are just returning the app page for all other routes when in reality you are going to want to give 404s for anything in the assets folder that you cant' find.

stepping back whats happening is that you pass it a path and it looks for the files with .shp, .dbf, .prj, .cpg extensions, the last 2 being optional and we assume we didn't get them if we get a 400 or higher status cope, but your server is just returning a 200 and the web app page for all requests it doesn't know about. So as a stop gap, just put a file with your shapefile called {shapefileName}.cpg that contains the text utf8 and longer term I can update this library to just ignore invalid text encoding

bcastagna commented 4 years ago

In deed that's the only thing that could differ from between our local ng serve and staging's apache. Ok im going to build with the latest release and dig your hints and update this thread if other people run into this issue. Thanks a lot, much appreciated !! :+1:

bcastagna commented 4 years ago

I confirm it works with last release 3.6.0 (without having put the missing {shapefileName}.cpg) ... and in deed I had a 404 with the CPG file in dev i didnt notice... Thanks again ! :+1: