Convert .shp files to an excel spreadsheet programmatically

安稳与你 提交于 2019-12-11 12:46:57

问题


I have a file in .shp format and I need it to convert it to an Excel spreadsheet programmatically. I want to do this using PHP or JavaScript.


回答1:


Once I've used small PHP lib ShapeFile, you can get it in phpclasses.org. Although it is a bit of not so good design, it works.

Here is a little example from my own code:

require_once 'lib/ShapeFile.inc.php';
$shp = new ShapeFile($filename, array('noparts' => false));
if ($shp->getError() !== '')
  print_r($shp->getError());
else
{
  $records = array();
  while ($record = $shp->getNext())
  {
    $dbf_data = $record->getDbfData();
    $shp_data = $record->getShpData();

    //Dump the information
    $obj = array(
      'type' => $shp->getShpTypeName($record->getShpType())
    );

    $obj['shape'] = $shp_data;
    $obj['meta'] = $dbf_data;

    $records[] = $obj;
  }
}

print_r($records);

So, after that $records contain all the data from shapefile. Of course, you will need some time to figure out what shapefile is and what data it can hold (assuming you are not familiar with it). Start from wikipedia. Actually there are bunch of arrays with some labels.

Then use some php excel lib (just seek in so) and you're done :)



来源:https://stackoverflow.com/questions/9029901/convert-shp-files-to-an-excel-spreadsheet-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!