PHP - 从'Point'形状生成kml

Man*_*nki 6 php google-maps kml shapefile

我正在使用PHPShapefile库来生成KML并将数据显示到谷歌地图,但是当涉及到"点"形状时它不起作用而不生成KML.这是Polygon形状的代码片段,帮助我为Point形状创建.

//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++;
} 
Run Code Online (Sandbox Code Playgroud)

chr*_*rki 1

This condition will fail for point geometries:

if (isset($shp_data['parts'])) {
Run Code Online (Sandbox Code Playgroud)

Unfortunately it looks like that the ShapeFile PHP library you are using does not have a proper way to identify the geometry type.

As a workaround, if the above check fails, you can then check if the geometry has an x and y coordinate like so:

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;
}
Run Code Online (Sandbox Code Playgroud)

This should result in an array that looks something like this:

  [0]=>
  array(1) {
    ["point"]=>
    array(1) {
      ["coordinates"]=>
      string(34) "0.75712656784493 -0.99201824401368"
    }
  }
Run Code Online (Sandbox Code Playgroud)