1

I'm using Mapbox with Leaflet for drawing, editing and removing polygons etc. I'm also using leaflet.shapefile to upload shapefiles and add them to the map.

For a certain application, I need to convert the layer to a GeoJSON object, which I then convert to a JSON string using this code:

var fcgeojson = layer.toGeoJSON();
var fcgeojsonstring = JSON.stringify(fcgeojson);

I have a working version of this in this jsfiddle. And for most of the shapefiles, it works just fine. However, in some cases, like when I use this shapefile, the JSON string of one of the polygons is actually incomplete. You can check out those strings by clicking on the "Test" button in the jsfiddle.

I haven't been able to figure out why this is happening (my guess is maybe the shapefile has too many vertices).

1 Answer 1

1

The method you are using to test the json string is where the problem arises. The incomplete json string comes from either stringify or alert. For this question, the problem likely lies with the latter.

Javascript alerts truncate messages at 10 000 characters, which coincidentally is exactly the number of characters that appear for me in the alert box when testing in your jsfiddle with your shapefile loaded.

When I used console.log() rather than alert() I could see the entire json (which is nearly 14 000 characters long). Though, there is a max size for console.log that varies with browsers, so you could always set the content of a div or other element to show the full content on screen.

1
  • Thank you very much for your response. My use of the alert was because I was converting the GeoJSON to a WKT string, and then sending it to C# using a Pagemethod to save to the database. But that was giving me an error for this polygon. Seeing your reply, I started looking at whether Pagemethod arguments had any character limit. And it seems that was indeed the reason. So I followed the solution from this post, and that worked for me. Thanks for pointing me in the right direction!
    – Raj
    Jan 30, 2017 at 17:53

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.