TransWikia.com

Getting a GeoServer created style to Leaflet markers via PHP

Geographic Information Systems Asked by WMTS on May 27, 2021

How can I use a point/marker style created on GeoServer within my Leaflet map when I am drawing data from the server using PHP.

I am currently PHP to draw geodata from my GIS server, however I would like to use one of my custom styles created within GeoServer as the marker which shows on Leaflet as Leaflet does not support triangle or cross shaped markers by default. Is it possible to draw my marker type using WMS or PHP and apply it to the data drawn from my database using PHP?

Current marker options

var markerOptions = {
            radius: 4,
            fillColor: '#FF6600',
            color: '#000',
            weight: 1,
            opacity: 1,
            fillOpacity: 0.4 }

GeoServer custom style AKA Pub Style

*{
  mark: symbol(triangle);
  mark-size: 8px;
  :mark {
    fill:#FF0608;
    fill-opacity:0.4;
    stroke:#080;
  }
}

current call using AJAX to get PHP data using markerOptions as the style

var pTable = 'geoupload.pubs';
var pIRETable = 'geoupload.Pubs_and_Clubs_in_Causeway_Coast_and_Glens NI';
var pColumn =["id", "p_name", "address1", "postcode", "local_authority"];
var pGeom = "geom";

$(document).ready(function(){
    $.ajax({
        url: "knn.php",
        data:{table: pTable, inclu: pColumn, geomy: pGeom}
    })
    .done(function(data){
        pubsLay = L.geoJSON(JSON.parse(data),{
            pointToLayer: function (feature, latlng){
                return L.circleMarker(latlng,markerOptions);
        }});
        pub_map.addLayer(pubsLay);
    })
});

KNN.php call to get data from server

//call table and data
    $table = $_GET['table'];
    $inclu = $_GET['inclu'];
    $geomy = $_GET['geomy'];

    //construct substitutions
    $fields = "";
    foreach ($inclu as $i => $col) {
        $fields = $fields . $col . ", ";
    }
    $fields = $fields . $geomy;
    $geom = " - " . $geomy;

    //build query
    $sql1 =
    "SELECT jsonb_build_object(
            'type',     'FeatureCollection',
            'features', jsonb_agg(feature))
    FROM (
        SELECT jsonb_build_object(
            'type',       'Feature',
            'geometry',   ST_AsGeoJSON(st_transform(geom,4326),4)::jsonb,
            'properties', to_jsonb(row) #omits#
        ) AS feature
        FROM (SELECT #fields# FROM #table#) row) features;";

    //replace placeholders
    $sql2 = str_replace("#table#", $table, $sql1);
    $sql3 = str_replace("#fields#", $fields, $sql2);
    $sql4 = str_replace("#omits#", $geom, $sql3);
    ChromePhp::log($sql4);

//execute query
if (!$response = pg_query($conn, $sql4)) {
    ChromePhp::error("*SQL failed*");
    exit;
}
    
//return data
while ($row = pg_fetch_row($response)) {
    foreach ($row as $i => $attr){
        echo $attr;
    }
}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP