Skip to content

Instantly share code, notes, and snippets.

@sanchangon
Created October 12, 2016 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanchangon/24058c407ab051dc5f72fa5fdbadc0ac to your computer and use it in GitHub Desktop.
Save sanchangon/24058c407ab051dc5f72fa5fdbadc0ac to your computer and use it in GitHub Desktop.
leaflet-phpgeojson
<html>
<!--
* Name: Leafletjs connect php-geojson through postgis spatial database
* Purpose: GIST@NU (www.cgistln.nu.ac.th)
* Date: 2016/10/13
* Author: Chingchai Humhong (chingchaih@nu.ac.th)
* Acknowledgement:
!-->
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css"/>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="map" style="width: 100%; height: 800px;"></div>
<script type="text/javascript">
var map = L.map('map');
var OpenStreetMap_BlackAndWhite = L.tileLayer('http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
});
OpenStreetMap_BlackAndWhite.addTo(map);
map.setView([17.05, 100.25], 9);
function addDataToMap(data, map) {
var dataLayer = L.geoJson(data, {
onEachFeature: function(feature, layer) {
var popupText = "hospital name: " + feature.properties.name
+ "<br>hospital code: " + feature.properties.maincode;
//+ "<br><a href='" + feature.properties.url + "'>More info</a>";
layer.bindPopup(popupText); }
});
dataLayer.addTo(map);
}
$.getJSON("healthcenter.php", function(data) { addDataToMap(data,map); });
</script>
</body>
</html>
<?php
//-------------------------------------------------------------
// * Name: PHP-PostGIS2GeoJSON
// * Purpose: GIST@NU (www.cgistln.nu.ac.th)
// * Date: 2016/10/13
// * Author: Chingchai Humhong (chingchaih@nu.ac.th)
// * Acknowledgement:
//-------------------------------------------------------------
// Database connection settings
define("PG_DB" , "database");
define("PG_HOST", "localhost");
define("PG_USER", "postgres");
define("PG_PORT", "port");
define("PG_PASS", "password");
define("TABLE", "table");
// Retrieve start point
// Connect to database
$con = pg_connect("dbname=".PG_DB." host=".PG_HOST." port=".PG_PORT." password=".PG_PASS." user=".PG_USER);
$sql = "select gid, provcode, maincode, bed, name, lat, lon, typecode, ST_AsGeoJSON(geom) AS geojson from ".TABLE." WHERE provcode = 65; ";
// Perform database query
$query = pg_query($con,$sql);
//echo $sql;
// Return route as GeoJSON
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
// Add geom to GeoJSON array
while($edge=pg_fetch_assoc($query)) {
$feature = array(
'type' => 'Feature',
'geometry' => json_decode($edge['geojson'], true),
'crs' => array(
'type' => 'EPSG',
'properties' => array('code' => '4326')
),
'properties' => array(
'gid' => $edge['gid'],
'provcode' => $edge['provcode'],
'maincode' => $edge['maincode'],
'bed' => $edge['bed'],
'name' => $edge['name'],
'lat' => $edge['lat'],
'lon' => $edge['lon'],
'typecode' => $edge['typecode']
)
);
// Add feature array to feature collection array
array_push($geojson['features'], $feature);
}
// Close database connection
pg_close($con);
// Return routing result
// header('Content-type: application/json',true);
echo json_encode($geojson);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment