2016-01-21 53 views
6

我正在使用PHPShapefile庫生成KML並向Google地圖顯示數據,但是當涉及到'Point'形狀時,它不起作用,並且不會生成相同的KML。 這裏是多邊形形狀的代碼片段幫助我爲點形狀創建。PHP - 從'Point'形狀生成kml

//this shape data i'm fetching from shapefile library.   
$shp_data = $record->getShpData(); 
if (isset($shp_data['parts'])) { 
    $counter1 = 0; 
    if ($shp_data['numparts']) { 
    $polygon_array['polygon']['status'] = 'multi-polygon'; 
    } else { 
    $polygon_array['polygon']['status'] = 'single-polygon'; 
    } 

    $polygon_array['polygon']['total_polygon'] = $shp_data['numparts']; 

    foreach ($shp_data['parts'] as $polygon) { 
    foreach ($polygon as $points) { 
     $counter = 0; 
     $polygon_string = ''; 

     while ($counter < count($points)) { 
     if ($counter == 0) { 
      $polygon_string = $points[count($points) - 1]['x'] . ','; 
      $polygon_string .= $points[$counter]['y'] . ' ' . $points[$counter]['x'] . ','; 
     } else if ($counter == count($points) - 1) { 
      $polygon_string .= $points[$counter]['y']; 
     } else { 
      $polygon_string .= $points[$counter]['y'] . ' ' . $points[$counter]['x'] . ','; 
     } 
     $counter = $counter + 1; 
     } 
     $polygon_single[$counter1] = $polygon_string; 
     $polygon_array['polygon']['view'] = $polygon_single; 
     $counter1 = $counter1 + 1; 
    } 
    } 
    $arr[$i] = $polygon_array; 
    $i++; 
} 

回答

1

這個條件對於點幾何失敗:

if (isset($shp_data['parts'])) { 

不幸的是,它看起來像您正在使用沒有一個適當的方式來識別幾何類型SHAPEFILE PHP庫。

作爲一種解決方法,如果上述檢查失敗,然後你可以檢查幾何有xy協調,像這樣:

if (isset($shp_data['parts'])) { 
    // probably a polygon 
    // ... your code here ... 
} elseif(isset($shp_data['y']) && isset($shp_data['x'])) { 
    // probably a point 
    $point = []; 
    $point["coordinates"] = $shp_data['y'] .' '. $shp_data['x']; 
    $arr[$i]['point'] = $point; 
} 

這將導致一個數組,看起來是這樣的:

[0]=> 
    array(1) { 
    ["point"]=> 
    array(1) { 
     ["coordinates"]=> 
     string(34) "0.75712656784493 -0.99201824401368" 
    } 
    } 
+0

你知道任何替代庫嗎? – Rorschach

+0

@Rorschach不,對不起 – chrki