Leaflet and PHP

Access

A web application employing PostGIS, PHP, and LeafletJS.

This application uses the PHP-Database-GeoJSON file from https://github.com/bmcbride/PHP-Database-GeoJSON

It can be access via http://domain.com/LeafletPHPDemo.html

../_images/leaflet-geojson-europe.png

Initialize

If you any issues connecting to PostGIS, check line 12 of get-json.phpbelow:

$conn = new PDO(‘pgsql:host=localhost;dbname=postgisftw’,’pgis’,’<YourPgisPassword>’);

Replace <YourPgisPassword> with the password for user pgis.

The password for user pgis is auto-generated and can be found at /home/pgis/.pgpass

Security

This map loads GeoJson generated from PostGIS directly and does not employ the pg_featurserv or GeoServer urls.

As you may have noticed, it is identical to our LeafletGeoJson.html map except for the GeoJson source url.

While urls for pg_featurserv and GeoServer can be secured, the url will always be exposed to your users as it is required for map rendering.

Conversely, the PostGIS PHP application uses the get-json.php file to establish a connection to PostGIS in the background.

You can employ the same method for loading GeoJson from pg_featurserv, GeoServer, or any other json url.

There is performance penalty, however, as the GeoJson is loaded into the end users browser and for large data sets, this can become extremely slow.

Structure

The app is located at:

/vaw/www/html/LeafletPHPDemo.html

This is just a basic Leafletjs map in which we are pulling in geojson from get-json.php:

$.getJSON("get-json.php", function(data) {

Content

The content of the html page is displayed below.

 1     <!doctype html>
 2     <html>
 3     <head>
 4     <style type="text/css">
 5     body {
 6     padding: 0;
 7     margin: 0;
 8     }
 9
10     html, body, #map {
11     height: 100%;
12     }
13
14     </style>
15
16     <link rel="stylesheet" href="https://unpkg.com/leaflet@1.1.0/dist/leaflet.css"
17     integrity="sha512-wcw6ts8Anuw10Mzh9Ytw4pylW8+NAD4ch3lqm9lzAsTxg0GFeJgoAtxuCLREZSC5lUXdVyo/7yfsqFjQ4S+aKw=="
18     crossorigin=""/>
19
20     <script src="https://unpkg.com/leaflet@1.1.0/dist/leaflet.js"
21     integrity="sha512-mNqn2Wg7tSToJhvHcqfzLMU6J4mkOImSPTxVZAdo+lcPlk+GhZmYgACEe0x35K7YzW1zJ7XyJV/TT1MrdXvMcA=="
22     crossorigin=""></script>
23     </head>
24
25     <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
26     </head>
27     <body>
28     <div id="map"></div>
29     <script>
30
31
32     var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
33     var osmAttrib='Data &copy <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
34     var osm = new L.TileLayer(osmUrl, {minZoom: 2, maxZoom: 8, attribution: osmAttrib});
35
36     $.getJSON("get-json.php", function(data) {
37
38     function onEachFeature(feature, layer) {
39       layer.bindPopup("Name: " + feature.properties.name + "<br>" + "Abbreviation: " + feature.properties.abbrev);
40       }
41
42     var geojson = L.geoJson(data, {
43     onEachFeature: onEachFeature
44     });
45
46     var map = L.map('map').fitBounds(geojson.getBounds());
47
48     osm.addTo(map);
49     geojson.addTo(map);
50     });
51     </script>
52
53     </body>
54     </html>

Documentation

https://leafletjs.com/

https://leafletjs.com/examples/geojson/