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

Using with React Native #133

Closed leonamjlpaula closed 4 years ago

leonamjlpaula commented 4 years ago

Hi. Could you give a me a clue on how to use this lib to convert shp files to GeoJSON in a React Native App?

calvinmetcalf commented 4 years ago

I've never used react native so I don't know how you'd go about getting the file in but assuming you were able to get the file in as an array buffer there shouldn't be anything preventing you from using this library

calvinmetcalf commented 4 years ago

also it looks like you can just download files just the same as in the browser

leonamjlpaula commented 4 years ago

Thanks for your answer @calvinmetcalf ! I've use a file read lib for react native and now I can get the base64 string of the shp file and convert it to a arrayBuffer. How then should I use your lib to get a GeoJSON? Could please help me?

leonamjlpaula commented 4 years ago

I tried to convert a .zip file and got this:

Error: nodebuffer is not supported by this browser
    at Object.exports.checkSupport (utils.js:280)
    at Object.exports.transformTo (utils.js:242)
    at ZipObject.asNodeBuffer (object.js:140)
    at unzip.js:10
    at Array.forEach (<anonymous>)
    at module.exports (unzip.js:8)
    at Function.shp.parseZip (index.js:65)
    at index.js:138
    at tryToUnwrap (browser.js:166)
    at tryCatch (browser.js:178)
calvinmetcalf commented 4 years ago

looks like the unzip library we use doesn't support buffers in react native

leonamjlpaula commented 4 years ago

But I just need now to convert the shp arrabuffer to GeoJSON. I won't user zip files.

leonamjlpaula commented 4 years ago

But I just need now to convert the shp arrabuffer to GeoJSON. I won't user zip files.

calvinmetcalf commented 4 years ago

you could pass just the .shp to shp.parseShp

leonamjlpaula commented 4 years ago

I've tried this but there is a prj parameter that expects a Buffer or string. What should I pass as prj?

calvinmetcalf commented 4 years ago

if you have a .prj file that went with this .shp pass the contents as a string

leonamjlpaula commented 4 years ago

@calvinmetcalf now it worked! By reading the prj file with ascii was possible to make this work! Thanks a lot for your attention and patience!

leonamjlpaula commented 4 years ago

Hi again. What parameter should I use to specify a default value for cpg on parseDbf? The result of parseShp is not complete to be plotted, so I guess I should combine (using combine function) with the dbf file. I got this trying to pass a simple string 'utf-8' as ArrayBuffer:

RangeError: Failed to construct 'TextDecoder': The encoding label provided ('[object ArrayBuffer]') is invalid.
leonamjlpaula commented 4 years ago

Hi again. What parameter should I use to specify a default value for cpg on parseDbf? The result of parseShp is not complete to be plotted, so I guess I should combine (using combine function) with the dbf file. I got this trying to pass a simple string 'utf-8' as ArrayBuffer:

RangeError: Failed to construct 'TextDecoder': The encoding label provided ('[object ArrayBuffer]') is invalid.

I realized that by passing null the lib uses the default decoder. The problem was that I'm using typescript and it complains about null but runs ok. Also the combine function works fine and now I can plot the GeoJSON on a MapBox Layer.

georgetk commented 2 years ago

Hi @leonamjlpaula, I'm also trying to make this working in RN. I tried reading the .shp file with RNFS but that's giving errors. I would really appreciate if you could share how you made this working!

leonamjlpaula commented 2 years ago

hi @georgetk. I was able to make it work with RN but this was not easy. Send me an email leonamjlpaula@gmail.com and let's schedule a call. I will be glad to present you the solution I found.