Friday, 4 May 2018

What is the most common way of displaying geodata from Postgis on Leaflet?


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



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...