【问题标题】:Adding Linear Feature From PostGIS to Leaflet将 PostGIS 中的线性特征添加到传单
【发布时间】:2020-05-23 04:30:15
【问题描述】:

我设法将 PostGIS 点特征添加到传单,但我无法将 PostGIS 的线性特征添加到传单。当我使用alert(response) 时,我能够在警报窗口中看到关系数据,但我似乎无法让它在地图上绘制。我需要用一种特殊的方式来符号化线要素吗?

以下是我正在使用的代码(适用于 PostGIS 点功能):

//PHP    
<?php
    $dsn = "pgsql:host=xxxxx;dbname=postgres;port=5432";
    $opt = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false
    ];
    $pdo = new PDO($dsn, 'postgres', 'xxxxxx', $opt);

    $result = $pdo->query("SELECT *, ST_AsGeoJSON(geom,5) AS geojson FROM testlyr");
    $features=[];
    foreach($result AS $row) {
        unset($row['geom']);
        $geometry=$row['geojson']=json_decode($row['geojson']);
        unset($row['geojson']);
        $feature=["type"=>"Feature", "geometry"=>$geometry, "properties"=>$row];
        array_push($features, $feature);
    }
$featureCollection=["type"=>"FeatureCollection", "features"=>$features];
echo json_encode($featureCollection);

?>

_

//JS
function mylineStyle(feature) {
  return {
    color: "#3388ff",
    weight: 10,
  };
}

$.ajax({
  url: "load_attractions.php",
  success: function (response) {
    jsnCities = JSON.parse(response);
    lyrCities = L.geoJSON(jsnCities, { style: mylineStyle }).addTo(map);
  },
  error: function (xhr, status, error) {
    alert("ERROR: " + error);
  },
});

-

//Output of console.log() JSON response 
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[-10616750.46022,3451527.81212],[-10616193.63558,3451197.53649],[-10615872.3906,3451772.85539],[-10615154.94348,3451495.84996],[-10615283.44147,3451080.34194],[-10615626.10278,3450696.79619]]]},"properties":{"id":1}}]}

【问题讨论】:

  • 您的browser's console 中是否有任何错误?
  • 感谢您的回复!控制台中没有错误。我使用相同的代码在 Leaflet 地图中成功绘制了 PostGIS 点特征。对于线性层,我更改的只是 PHP 代码中 SQL 语句中的表名。是否需要更改以在 Leaflet 中显示 PostGIS 线性和多边形特征?我是如此接近,但我一定在这里错过了一些东西。
  • 如果没有看到 JSON 响应的 console.log() 输出,很难说。不过,没有明显的错误。
  • 我在上面的原始帖子中添加了 JSON 响应的 console.log() 输出。只有一个功能。
  • 哦,您的 GeoJSON 不符合标准,因为坐标不是(未投影的)经纬度,正如规范所说。

标签: php ajax leaflet postgis


【解决方案1】:

Ivan Sanchez 发现坐标不是规格要求的纬度/经度。现在完美运行!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2017-11-26
    • 2022-09-28
    • 2020-02-01
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多