2012-04-23 98 views
1

我堅持完全通過使用openlayers的腳本。我有postgis中的數據庫與座標和高度值,甚至每行的幾何列。我使用提交按鈕創建表單,僅根據用戶輸入的值檢索數據。當我按下提交按鈕時,PHP正在獲取正確的數據並將其轉換爲JSON格式,我已將其顯示爲結果。有人知道如何將這些結果加載到openlayers圖層並顯示這些點? 那是主頁:PHP,geojson和openlayers

`<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Welcome to my maps</title> 
<script src="http://www.openlayers.org/api/OpenLayers.js"></script> 
<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script> 
<link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css" type="text/css" /> 


     <style type="text/css"> 
         #bmap { 
          width:83%; 
          height:90%; 
          border:2px solid black; 


          position:absolute; 
          top:10px; 
          left:200px; 
          } 
          body{ 
          background:yellow; 
          } 
      </style> 

      <script> 

        var mapoptions = { 
           theme: null, 
           maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), 
           maxResolution: 156543.0399, 
           numZoomLevels: 20, 
           units: 'm', 
           projection: new OpenLayers.Projection("EPSG:900913"), 
           displayProjection: new OpenLayers.Projection("EPSG:4326"), 

        controls:[ 
            new OpenLayers.Control.PanZoomBar(), 
            new OpenLayers.Control.Navigation(), 
            new OpenLayers.Control.LayerSwitcher(), 
            new OpenLayers.Control.MousePosition(), 
            new OpenLayers.Control.ScaleLine(), 
            new OpenLayers.Control.Scale() 
           ] 
        }; 



          function init() { 
          map = new OpenLayers.Map("bmap", mapoptions); 

          var mapnik = new OpenLayers.Layer.OSM("OSM Mapnik"); 
          map.addLayer(mapnik); 

          var cyclemap = new OpenLayers.Layer.OSM("OSM CycleMap"); 
          map.addLayer(cyclemap); 



          var wmslayer = new OpenLayers.Layer.WMS(
             "Altitude points", 
             "http://192.168.56.101:8080/geoserver/wms", 
             {'layers': 'dublin_flooding:dublin', 'format':'image/png', 'transparent':'true'}, 
             {'opacity': 1.0, 'isBaseLayer': false, 'visibility': false} 
            ); 
           map.addLayer(wmslayer); 

var veclayer=new OpenLayers.Layer.Vector("geojson",{ 

        strategies: [new OpenLayers.Strategy.Fixed()], 
        protocol: new OpenLayers.Protocol.HTTP({ 
      url: "query5.php", 
         format: new OpenLayers.Format.GeoJSON(), 
        internalProjection: new OpenLayers.Projection("EPSG:900913"), 
          externalProjection: new OpenLayers.Projection("EPSG:4326") 
        }), 

       }); 
       map.addLayer(veclayer); 




          map.setCenter(new OpenLayers.LonLat(-6.26555,53.34590) // Center of the map 
           .transform(
           new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984 
           new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection 
          ), 12 // Zoom level 
          ); 
          } 
      </script> 


</head> 

<body> 
<h3>Flooding projection</h3> 
<form action="query5.php" method="POST" name="form"> 


     <table cellpadding="0"> 
     <tr> 
     <td> 
     <p>Meters:</p> 
     </td> 
     <td> 
     <input name="sliderValue" id="sliderValue" type="Text" size="3"> 
     </td> 
     </tr> 
     <tr> 
     <td> 
<input name="Submit" type="Submit" value="Submit"> 
</td> 
</tr> 
</table> 

</form> 
<body onload="init();"> 
<div id="bmap"></div> 
</body> 
</html> 
` 

和PHP查詢看起來像這樣:

`<?php 
$db = pg_connect("host=localhost port=5432 dbname=firstSpatialDB user=postgres password=postgres"); 
$query = "SELECT* FROM dublin where alt<='$_POST[sliderValue]'"; 
     $result = pg_query($query); 
// Return route as GeoJSON 
    $geojson = array(
     'type'  => 'FeatureCollection', 
     'features' => array() 
    ); 
    // Add edges to GeoJSON array 
    while($row=pg_fetch_array($result)) { 
    $feature = array(
      'type' => 'Feature', 
      'geometry' => array(
      'type' => 'Point', 
      'coordinates' => array($row[1], $row[2]) 

     ), 
      'properties' => array(
      'gid' => $row[0], 
      'alt' => $row[3] 
      ) 
     ); 
     // Add feature array to feature collection array 
     array_push($geojson['features'], $feature); 
    } 
    pg_close($dbconn); 
    // Return routing result 
    header("Content-Type:application/json",true); 
    //header("Location:map.html"); 
    echo json_encode($geojson); 
?> ` 

在我看來應該是工作,但不是在所有。 也許有人知道什麼是錯的。感謝您的任何建議,因爲我真的有足夠的自己的。

回答

1

我從來沒有使用PHP,所以我不知道這是否是你的問題。 比較你的代碼,這對我有用,也許你的錯誤是在JavaScript中。

var map; 

     function init(){ 
      map = new OpenLayers.Map('map'); 
      var options = {numZoomLevels: 3} 
      var floorplan = new OpenLayers.Layer.Image(
      'Floorplan Map', 
      '../../temp_photos/sample-floor-plan.jpg', 
      new OpenLayers.Bounds(-300, -188.759, 300, 188.759), 
      new OpenLayers.Size(580, 288), 
      options 
     ); 
      map.addLayer(floorplan); 
      //Create a Format object 
    var vector_format = new OpenLayers.Format.GeoJSON({}); 

    //Create a Protocol object using the format object just created 
    var vector_protocol = new OpenLayers.Protocol.HTTP({ 
     url: 'ex5_data.json', 
     format: vector_format 
    }); 

    //Create an array of strategy objects 
    var vector_strategies = [new OpenLayers.Strategy.Fixed()]; 

    //Create a vector layer that contains a Format, Protocol, and Strategy class 
    var vector_layer = new OpenLayers.Layer.Vector('More Advanced Vector Layer',{ 
     protocol: vector_protocol, 
     strategies: vector_strategies 
    }); 

    map.addLayer(vector_layer); 

    if(!map.getCenter()){ 
     map.zoomToMaxExtent(); 
    } 
     } 
+0

謝謝!我會試試這個。 – user1351769 2012-05-29 11:03:14

+0

當我嘗試從世界地圖上的服務器顯示數據(OpenStreetMaps)而不是圖像地圖時,我一直有一些麻煩。我想我已經搞砸了預測,你可能也想和這個混在一起 – 2012-05-30 12:16:22

1

我剛剛試過你的代碼(chri_chri)。

我試圖加載圖像,但似乎是錯誤的... 還林現在的OpenLayers

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Floorplan test</title> 
<script src="http://www.openlayers.org/api/OpenLayers.js"></script> 
<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js"></script> 
<link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css"  type="text/css" /> 


     <style type="text/css"> 
         #bmap { 
          width:83%; 
          height:90%; 
          border:2px solid black; 


          position:absolute; 
          top:10px; 
          left:200px; 
          } 
          body{ 
          background:yellow; 
          } 
      </style> 

      <script> 

        var map; 

     function init(){ 
      map = new OpenLayers.Map('map'); 
      var options = {numZoomLevels: 3} 
      var floorplan = new OpenLayers.Layer.Image(
      'Floorplan Map', 
      'png_1.jpg', 
      new OpenLayers.Bounds(-300, -188.759, 300, 188.759), 
      new OpenLayers.Size(580, 288), 
      options 
     ); 
      map.addLayer(floorplan); 
      //Create a Format object 
    var vector_format = new OpenLayers.Format.GeoJSON({}); 

    //Create a Protocol object using the format object just created 
    var vector_protocol = new OpenLayers.Protocol.HTTP({ 
     url: 'ex5_data.json', 
     format: vector_format 
    }); 

    //Create an array of strategy objects 
    var vector_strategies = [new OpenLayers.Strategy.Fixed()]; 

    //Create a vector layer that contains a Format, Protocol, and Strategy class 
    var vector_layer = new OpenLayers.Layer.Vector('More Advanced Vector Layer',{  
     protocol: vector_protocol, 
     strategies: vector_strategies 
    }); 

    map.addLayer(vector_layer); 

    if(!map.getCenter()){ 
     map.zoomToMaxExtent(); 
    } 
     } 
      </script> 


</head> 

<body> 
<h3>Floorplan</h3> 

<body onload="init();"> 
<div id="bmap"></div> 
</body> 
</html> 

什麼IM與啓動是加載佈局規劃圖,並嘗試它的規模。

0

你可以看看這個例子postgis to geojson php說明如何使用php腳本通過postgis數據庫獲取geojson數據。

和你在你的geojson圖層url中一樣,你傳遞你的php腳本的url ... 希望它有幫助;

0

我將PostGis與Openlayers 3/4一起使用,沒有GeoServer。我選擇的方式是通過我調用的函數從Postgis數據庫中獲取geojson,該函數返回數據並根據我的設置設置樣式。

在Javascript中,我定義了數據和樣式=> Javascript函數通過GET調用一個php腳本來檢索來自Postgis =>函數樣式的數據以在Openlayers 3中呈現數據。整個腳步可以在Is there an easy way to use Postgis-geojson in Openlayers 3?

請注意,建議的解決方案是不安全的,因爲可以操縱GET字符串(sql注入)。我使用通過https的呼叫,並且服務器端的php腳本檢查是否設置了SESSION。因此,腳本不能在沒有登錄的情況下執行。我們在一個非常小的組中使用這個腳本,但在許多腳本正在訪問數據的環境中使用它可能不是一個好主意。

所以在安全方面的改進會很好。

0

您可以使用PHP

<?php 
    ini_set('display_errors', 1); 

    # Connect to PostgreSQL database 
    $conn = pg_connect("dbname='gisdata' user='username' 
    password='password' host='localhost'") 
    or die ("Could not connect to server\n"); 

    $result = pg_fetch_all(pg_query($conn, "SELECT row_to_json(fc) 
     FROM (SELECT 'FeatureCollection' As type, 
     array_to_json(array_agg(f)) As features 
     FROM (SELECT 'Feature' As type 
     , ST_AsGeoJSON(lg.geom, 4)::json As geometry 
     , row_to_json((SELECT l FROM (SELECT id, designacao) As l 
     )) As properties 
     FROM hidrog As lg) As f) As fc;")); 

    if (!$result) { 
     echo "An error occurred.\n"; 
     exit; 
    } 

    #echo json_encode($result, JSON_NUMERIC_CHECK); 
    $json_data = json_encode($result); 
    file_put_contents('test.json', $json_data); 

    $jsonString = file_get_contents('test.json'); 
    $json_new = substr($jsonString, 17,-2); 
    $json_new = str_ireplace('\"', '"', $json_new); 
    echo $json_new; 

    file_put_contents('test_new.json', $json_new); 
?>