Primitive Stand Splitter for Non-spatial Applications

The need to break stands apart is often necessary.  This typically comes from regulatory requirements.  This routine will do this however you will loose spatial reference.

<?php

require_once("FPSOpen.php");

echo "\nFSD FPS Stand Split\n\n";

$MinArea = 25;

$SQL = "SELECT Std_ID, Area_GIS, Area_Net, Area_Rpt FROM ADMIN WHERE Area_GIS > $MinArea AND STD_ID > 0;";

foreach($dbh->query($SQL, PDO::FETCH_ASSOC) as $row) {

  $row['Area25s'] = $row['Area_GIS'] / $MinArea;
  $row['Remainder25'] = $row['Area25s'] - floor($row['Area25s']);
  $row['RemainderAc'] = $row['Remainder25'] * $MinArea;

  for($a = 1; $a <= $row['Area25s'];$a++) {

    $row['Admins'][$a]['Std_ID'] = ((100 + $a) * 10000) + $row['Std_ID'];
    $row['Admins'][$a]['Area_GIS'] = $MinArea;
    $row['Admins'][$a]['Area_Net'] = ($row['Area_Net'] / $row['Area_GIS']) * $MinArea;
    $row['Admins'][$a]['Area_Rpt'] = ($row['Area_Rpt'] / $row['Area_GIS']) * $MinArea;
 
  }
 
  $row['Admins'][0]['Std_ID'] = ((100 + 0) * 10000) + $row['Std_ID'];
  $row['Admins'][0]['Area_GIS'] = $row['RemainderAc'];
  $row['Admins'][0]['Area_Net'] = ($row['Area_Net'] / $row['Area_GIS']) * $row['RemainderAc'];
  $row['Admins'][0]['Area_Rpt'] = ($row['Area_Rpt'] / $row['Area_GIS']) * $row['RemainderAc'];

  $SQLAdmin = "SELECT * FROM ADMIN WHERE Std_ID = {$row['Std_ID']};";

  foreach($dbh->query($SQLAdmin, PDO::FETCH_ASSOC) as $AdminRec){
    $AdminRec = array_change_key_case($AdminRec,CASE_UPPER);
    foreach($AdminRec as $Key=>$Value) {
      if(!is_numeric($Value)) $AdminRec[$Key] = "'$Value'";
    }
    foreach($row['Admins'] as $NewStand) {

      $AdminRec['STD_ID'] = $NewStand['Std_ID'];
      $AdminRec['AREA_GIS'] = $NewStand['Area_GIS'];
      $AdminRec['AREA_NET'] = $NewStand['Area_Net'];
      $AdminRec['AREA_RPT'] = $NewStand['Area_Rpt'];

      $AddAdmin = 'INSERT INTO `ADMIN` (`';
      $AddAdmin .= implode('`,`',array_keys($AdminRec));
      $AddAdmin .= '`) VALUES (';
      $AddAdmin .= implode(',',$AdminRec);
      $AddAdmin .= ');';

      $dbh->query($AddAdmin);
    }
  }

  $SQLStand = "SELECT * FROM STAND WHERE Std_ID = {$row['Std_ID']};";

  foreach($dbh->query($SQLStand, PDO::FETCH_ASSOC) as $StandRec){
    $StandRec = array_change_key_case($StandRec,CASE_UPPER);
    foreach($StandRec as $Key=>$Value) {
      if(!is_numeric($Value)) {
        if(trim($Value) == '') {
          $StandRec[$Key] = 'NULL';
        } else {
          $StandRec[$Key] = "'$Value'";
        }
      }
    }
    
    foreach($row['Admins'] as $NewStand) {
      $StandRec['STD_ID'] = $NewStand['Std_ID'];
      
      $AddStand = 'INSERT INTO `STAND` (`';
      $AddStand .= implode('`,`',array_keys($StandRec));
      $AddStand .= '`) VALUES (';
      $AddStand .= implode(',',$StandRec);
      $AddStand .= ');';

      $dbh->query($AddStand);
    }
      
  }

  $SQLDBH = "SELECT * FROM DBHCLS WHERE Std_ID = {$row['Std_ID']};";

  foreach($dbh->query($SQLDBH, PDO::FETCH_ASSOC) as $DBHRec){
    $DBHRec = array_change_key_case($DBHRec,CASE_UPPER);
    foreach($DBHRec as $Key=>$Value) {
      if(!is_numeric($Value)) {
        if(trim($Value)!='') {
          $DBHRec[$Key] = "'".trim($Value)."'";
        } else {
          $DBHRec[$Key] = 'NULL';
        }
      }
    }
    
    foreach($row['Admins'] as $NewStand) {
      $DBHRec['STD_ID'] = $NewStand['Std_ID'];
      
      $AddDBH = 'INSERT INTO `DBHCLS` (`';
      $AddDBH .= implode('`,`',array_keys($DBHRec));
      $AddDBH .= '`) VALUES (';
      $AddDBH .= implode(',',$DBHRec);
      $AddDBH .= ');';

      $dbh->query($AddDBH);
    }
      
  }

  $AdminClean = "UPDATE ADMIN SET STD_ID = -`STD_ID` WHERE STD_ID = {$row['Std_ID']};";
  $dbh->query($AdminClean);
  echo $row['Std_ID'] . "\n";

}

?>