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