0

I'm trying to create a shapefile with the PhpMyAdmin\ShapeFile library.

I followed the example in the Github project repository, but I would like to create a shapefile within polygon elements, not within point elements.

I also wonder if shapefile coordinates could be decimals lat long (EPSG:4326 WGS84) or if I have to convert them.

The file that I create doesn't work in QGIS (no errors, but it is not displayed).

Here is my PHP code:

$xmin=0;
$xmax=0;
$ymin=0;
$ymax=0;

$sql = "SELECT poli FROM poligons WHERE ID = '$id'";

$res = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($res)) {
    $poli = json_decode($row[0],true);
    foreach ($poli["geometry"][0] as $vert) {
        if ($xmin==0 || $vert[1]< $xmin) $xmin = $vert[1];
        if ($xmax==0 || $vert[1]> $xmax) $xmax = $vert[1];
        if ($ymin==0 || $vert[0]< $ymin) $ymin = $vert[0];
        if ($ymax==0 || $vert[0]> $ymax) $ymax = $vert[0];
    }
}
$shp = new ShapeFile(5, [
    'xmin' => $xmin,
    'ymin' => $ymin,
    'xmax' => $xmax,
    'ymax' => $ymax,
]);


$res = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_array($res)) {
    $poli = json_decode($row[0],true);
    // I don't know if I should pass 1 or 5 to create new shape record
    // in next line: 5 is for polygons 1 for points...
    $record = new ShapeRecord(5);
    $firstVertex=true;
    $record_to_repeat=null;
    foreach ($poli["geometry"][0] as $vert) {
        $record->addPoint([
            'x' => $vert[1],
            'y' => $vert[0],
        ]);
        if ($firstVertex) {
            $record_to_repeat = $record;
            $firstVertex = false;
        }
        $shp->addRecord($record);
    }
    $shp->addRecord($record_to_repeat);
}
$shp->saveToFile("extraction_$id.*");

Here is the JSON extracted from one record of my table (the $poli variable content). I confirm that vertex are correctly extracted from the JSON string file shp seems to be generated with them inside (no error occurs on $record->addPoint(...) and $shp->addRecord(...) ):

{
    "type": "POLYGON",
    "id": "1",
    "title": "303",
    "geometry": [
        [
            ["43.91270111368998", "10.763205885887146"],
            ["43.91284216823677", "10.762763321399689"],
            ["43.912703045946344", "10.762830376625061"],
            ["43.912710774970996", "10.762543380260468"],
            ["43.91272623301729", "10.761880874633789"],
            ["43.912737826549396", "10.761044025421143"],
            ["43.912919458257605", "10.760885775089264"],
            ["43.91306244453156", "10.761824548244476"],
            ["43.91320349822197", "10.761883556842804"],
            ["43.91304698657257", "10.762304663658142"],
            ["43.91313586978198", "10.762417316436768"],
            ["43.913207362701925", "10.76258897781372"],
            ["43.91293491624973", "10.763594806194305"],
            ["43.91279772642933", "10.763554573059082"],
            ["43.91278033614783", "10.763546526432037"],
            ["43.91269531692063", "10.763541162014008"]
        ]
    ]
}
  • 1
    Seems like you did not take into account the requirement from esri.com/library/whitepapers/pdfs/shapefile.pdf "The rings are closed (the first and last vertex of a ring MUST be the same" – user30184 Jan 23 at 13:23
  • @user30184 Yes, I do. I have the variable $record_to_repeat that I use to close the ring repeating the first vertex. I know it is not in GeoJSON, but I repeat the first when creating the shape. – kiks73 Jan 23 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.