PHP - 从'Point'形状生成kml

6

我正在使用PHPShapefile库生成KML并将数据显示到Google地图上,但是当涉及到'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个回答

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"
    }
  }

你知道它的任何替代库吗? - Wit Wikky
@Rorschach 不好意思,不是的。 - chrki

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接