2017-05-09 12 views
0

Multilinestring 기하 필드를 데이터베이스에서 geojson 데이터로 변환하는 쿼리를 실행하는 PHP 코드가 있습니다. 이 코드는 다중 다각형 및 점 기하 데이터에 대해서는 잘 작동하지만 다중 선 문자 기하학 필드를 구문 분석하는 동안 약간의 오류가 있습니다. 나는 \를 제거하려고하면ST_asGeoJson for Multilinestring for php 전단지

<?php 
include('../config.php'); // the db config file 
function createFeature() { 
    $feature = new stdClass(); 
    $feature->type = 'Feature'; 
    $feature->geometry = new stdClass(); 
    $feature->geometry->type = 'Multilinestring'; 
    $feature->properties = new stdClass(); 
    return $feature; 
} 

function createCollection() { 
    $collection = new stdClass(); 
    $collection->type = 'FeatureCollection'; 
    $collection->features = array(); 
    return $collection; 
} 


$query = 'SELECT ST_AsGeoJson(geom) as geom,name FROM table_name'; 
if($result = pg_query($query)) { 
      $collection = createCollection(); 
      while($row = pg_fetch_row($result)) 
      { 
      $feature = createFeature(); 
     $feature->geometry = $row[0]; 
     $feature->properties->name=$row[1]; 
     $collection->features[] = $feature; 
      } 

      echo (json_encode($collection,JSON_NUMERIC_CHECK)); 

     } 

내가 코드를 실행에 얻을 응답은

{"type":"FeatureCollection", 
    "features": 
    [ 
    { 
    "type":"Feature", 
    "geometry": 
    "{\"type\":\"MultiLineString\", 
     \"coordinates\":[[[73.9750168196755,15.2410462374959], 
         [73.974612433675,15.2415698937723], 
         [73.9733813019535,15.2431183375569], 
         [73.9727337832775,15.2439091075613]]] 
     }", 
    "properties":{"name":"NH - 17"} 
    } 
    ] 
} 

이다 슬래시 기능을 사용하여 내가 여전히 오류가

echo stripslashes(json_encode($collection,JSON_NUMERIC_CHECK)); 

을 위해서 stripslashes

SyntaxError: Unexpected token t in JSON at position 72 

오류는 주로 기하학 값 앞에 큰 따옴표가 있기 때문에 발생합니다. 그것을 해결하는 방법을 모르십시오.

geojson으로 다중 선 문자 기하 데이터를 가져 오는 다른 방법이 있습니까?

답변

1

귀하의 문제를 줄

echo (json_encode($collection,JSON_NUMERIC_CHECK)); 

를 대체하여 그것을 제거는

$feature->geometry = $row[0]; 

문자열 아닌 사전을 반환하는 것을 (또는 "정렬 된지도"또는 PHP 용어로 "배열"). 문자열은 PostgreSQL이 JSON을 PHP 코드와 통신 할 수있는 유일한 방법입니다.

당신은 같은 것을 수행하여 더 나은 결과가있을 것이다 :

$feature->geometry = json_decode($row[0]); 
0

오류는 추가 따옴표입니다.

다음

$trial=stripslashes(json_encode($collection,JSON_NUMERIC_CHECK)); 
      $trial= str_replace('"{"type"','{"type"',$trial); 
      $trial= str_replace('}","properties"','},"properties"',$trial); 
echo $trial;