1

Is it possible to limit the records retrieved from a GeoJSON file?

I have a data file in GeoJSON format that my Leaflet page loads from a local text file and displays as a series of markers

But it's a lot of points, and I only want the few that should be displayed in the current bounding box

2

I do not believe it's possible to limit the records retrieved from a geojson file. A complete geojson data structure is always an object, so if you only fetched a part of it (for example using an http range request) then you would get an invalid json syntax - missing either the opening or closing brace and the json would not be parseable by the browser.

I only want the few that should be displayed in the current bounding box

OK so this necessitates having a spatial index on the server side, and generate the geojson on the fly to include only the relevant search results. In a PostGIS context, it can be done with a SQL query on point data something like this:

SELECT ...
WHERE ST_Contains(
  ST_MakeEnvelope(%(minx)s, %(miny)s, %(maxx)s, %(maxy)s, %(srid)s),
   geographic_coord::geometry
)

Where the minx, miny, etc. parameters are from the leaflet.js getBounds() function which returns the current extent of the viewer. But the implementation would depend on your server-side resources.

  • 1
    Also, this approach should only be considered if the performance of downloading the entire dataset is actually prohibitive. For example in a web app I wrote, there are > 100K records, plus a lots of metadata, so downloading the entire set was was actually too slow. Is IvanSanchez noted well, there may be other solutions with clustering or selectively adding data to leaflet. – Alex G Rice Jun 7 '16 at 15:59
  • Thanks for clarifying that the GeoJSON is an all or nothing solution – SteveC Jun 8 '16 at 15:04
1

I only want the few that should be displayed in the current bounding box

Why?

Unless there's a performance hit, you shouldn't filter the results - when you pan the map, you'll have to manually recalculate the visible points again.

If there's a performance hit due to a large number of points, use some clustering/decluttering leaflet plugin first (these will prevent prematurely adding elements to the DOM, which is usually the cause of slowdowns)

If the reason is "the points in this area are thematically aligned" then the answer is process them first and show the results.

0

With javascript you could write a filter, here is an example using jquery: https://stackoverflow.com/questions/23720988/how-to-filter-json-data-in-javascript-or-jquery

  • Won't this require loading all of the points? I'm trying to avoid loading all of the records – SteveC Jun 7 '16 at 12:47

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.