20

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...

1
  • 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
    Commented Nov 3, 2013 at 21:28

2 Answers 2

24

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.

3
  • 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
    Commented Nov 4, 2013 at 10:45
  • 1
    use then ogr2ogr reading csv to convert easily to geoJSON
    – Jorge Sanz
    Commented Nov 4, 2013 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
    Commented Dec 16, 2013 at 5:02
0

You could try out this leaflet plugin

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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