I have a string of properly formatted JSON from a VB.NET function that I am trying to pass to JavaScript where I can convert it into an OpenLayers vector layer. The string can be variable length, sometimes as small as 5,000 characters and sometimes as long as 40,000,000 characters.

Here is a small example of the JSON:


If the string is less than 100,000 characters I can successfully send it to my JavaScript function as a normal string variable OR as a serialized variable, can successfully read it as GeoJSON, and add the features to my OpenLayers map using the normal OpenLayers GeoJSON methods. The following JavaScript takes the VB.NET serialized string and adds it to the map;

            var geojson_format = new OpenLayers.Format.GeoJSON(); 
            var vector_layer = new OpenLayers.Layer.Vector("query_POSTGIS");
            var parsedJSON = JSON.parse(outputStr);
            alert(geojson_format.isValidType(parsedJSON, "FeatureCollection")); 

            var geoJsonStr = geojson_format.read(parsedJSON, "FeatureCollection"); 

This code works great until I get to the 100,000 character limit. Any suggestions on how to make this work with strings over 100,000 characters?

  • Does it fail server-side or client side? If it's server-side, I recall there being some sort of setting to change the maximum length of the json response. Other thought: isn't that a lot of data to be shoving into a vector layer? Could you simplify the polygons / use a WMS layer then only send over the features a user wants to work with or otherwise change the workflow? – mlinth Nov 20 '15 at 14:21
  • The fail appears to be on the client side but I'm not sure. It's really not a lot of vector data though. The 20,000,000 character files are only about 2,500 polygons. The vectors are served dynamically based on user queries and the geo data is joined to other tables of data that constantly change so there is no way to prepare a WMS in my GeoServer for each potential query. – Todd Krueger Nov 20 '15 at 14:35
  • This seems like it should be something simple and painfully obvious because this is exactly what PostGres/PostGIS is supposed to do, serve query-able geographic data. But I can't seem to figure out what is wrong. :-( – Todd Krueger Nov 20 '15 at 14:39
  • Try using st_simplify. That should significantly reduce the number of characters returned, although probably not sufficiently for your largest queries. I don't think the limitation is postgis - it's doing exactly what you ask. But I'd suggest you find the root cause of the problem. Is the server sending what you expect, or is OpenLayers falling over? Any error messages? – mlinth Nov 20 '15 at 14:51
  • st_simplify didn't solve this particular problem but I will definitely be using that command in other places!! Here is the error from the Google Chrome console --- "throw Sys.Net.WebServiceProxy._createFailedError(methodName, String.format(Sys.Res.webServiceFailed, methodName, error));" – Todd Krueger Nov 20 '15 at 15:02

I was able to solve the issue. It appears that in .NET there is a limit to how long a string that is serialized can be and you must explicitly declare a maxJsonLength in your Web.config. I was able to solve the problem by adding the following to my Web.config:

                <jsonSerialization maxJsonLength="50000000"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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