Reputation: 1036
I am trying to export some shape files to a a MySQL database.
I can read the shp features dbf file using PHP but I want to be able to get the WKT spatial data for each row that I am looping through. I have thought to add a calculated field (geomToWKT) to the shp file fields in qgis, but unfortunately the length of the string field attributes in the dbf file is limited to 254 characters. How can I can get the corresponding wkt for each row?
function process_dbf($dbf_path) {
$db = dbase_open($dbf_path, 0);
if($db) {
$record_numbers = dbase_numrecords($db);
for ($i = 1; $i <= $record_numbers; $i++) {
$row = dbase_get_record_with_names($db, $i);
$name= $row['name'];
$name=$row['area'];
$x=$row['x'];
$y=$row['y'];
$notes=$row['notes'];
$owner=$row['owner'];
$id=$row['ID'];
$tel=$row['tel_'];
$wkt=geomToWKT(geometry); //what I am trying to achieve in PHP
}
}
}}
Upvotes: 0
Views: 1610
Reputation: 2013
I am posting this late answer just as a service for the community, since this is one of the questions that pop up when looking for "converting ESRI Shapefiles to WKT".
My PHP Shapefile library can read and write any ESRI Shapefile and convert it natively from/to WKT and GeoJSON.
It does not require any third party dependency nor the dBase extension (not available in most modern PHP environments) and it is actively maintained.
Upvotes: 1
Reputation: 1036
OK. Finally managed to get it to work.
You will need this class. Also, if you are using Windows you will need this dbase extension on PHP 5.4
include 'PHP5-Shape-File-Parser-master/shpParser.php';
function process_shp($shp_location,$dbf_location) {
$parser=new shpParser();
$parser->load($shp_location);
$db = dbase_open($dbf_location, 0);
$data=$parser->getShapeData();
$i=1;
foreach($data as $shprow) {
$datarow=dbase_get_record_with_names($db, $i);
$i++;
//print_r($shprow); for shp geo spatial data (shp)
//print_r($datarow); for shp feature data (dbf)
}
}
process_shp('Jerusalem/BeitHanina/entrances/New_Shapefile-new.shp';
'Jerusalem/BeitHanina/entrances/New_Shapefile-new.dbf'); //for example
Upvotes: 1