For example, in my database I've some points with geographic coordinates. I want to display them on Leaflet JS layer. Which instruments should I use? Maybe there is some API...

  • Welcome to GIS SE? Is your question really "How to display PostGIS points using Leaflet?" If so, can you edit your question to make that clearer because I am uncertain what you mean by "instruments" in this context. – PolyGeo Nov 3 '13 at 21:28
up vote 22 down vote accepted

In order to get data from a database into a web browser, you need to use a server-side script. This can be tricky to do because you need to be aware of vulnerabilities, like SQL injection. Some best practice important things to note is that you should use parameterised queries and ensure that your database user is unprivileged, having only read access to the tables that you want being public.

The easy way to get API access to a database is to use a bit of software like GeoServer, but that is a very big application to use for potentially small problem.

Another method would be to have a server-side script dump the data out into a GeoJSON file periodically, depending on how often your data changes, this could be hourly, daily, weekly. keep in mind that your website might not function while the file is writing.

Another method is to use PHP or Python as a CGI script to build your own API.

Examples:

Once you have GeoJSON, whether dynamic or static, you can load it very easily using Leaflet: http://leafletjs.com/examples/geojson.html.

  • 1
    Thanks for answer. I incline to use GeoJSON. Now I want to use CSV file instead of Postgis, convert it to geoJSON and then display data with Leaflet. – John Smith Nov 4 '13 at 10:45
  • 1
    use then ogr2ogr reading csv to convert easily to geoJSON – Jorge Sanz Nov 4 '13 at 10:52
  • 1
    If your data is too big and takes too long to load as JSON, take a look at TopoJSON. github.com/mbostock/topojson – Alex Leith Dec 16 '13 at 5:02

You could try out this leaflet plugin

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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