【问题标题】:PHP - Generate kml from 'Point' shapePHP - 从“点”形状生成 kml
【发布时间】:2016-01-21 13:06:51
【问题描述】:

我正在使用PHPShapefile 库来生成 KML 并将数据显示到谷歌地图,但是当涉及到“点”形状时,它无法正常工作并且无法生成相同的 KML。 这是多边形形状的代码 sn-p 帮助我创建点形状。

//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++;
} 

【问题讨论】:

    标签: php google-maps kml shapefile


    【解决方案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"
        }
      }
    

    【讨论】:

    • 你知道任何替代库吗?
    • @Rorschach 不,对不起
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多