Skip to content Skip to sidebar Skip to footer

Convert .shp Files To An Excel Spreadsheet Programmatically

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.

Solution 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 :)

Post a Comment for "Convert .shp Files To An Excel Spreadsheet Programmatically"