/** * Upload a shapefile and load data to a WaterRequest * @param string $id string representing the ID of a WaterRequest * @throws CHttpException */ public function actionUpload($id) { $model = $this->loadModel($id); //if(!$model) // throw new CHttpException(400,'Invalid request. Wrong parameters. Model not found.'); if (!Yii::app()->user->checkAccess('updateWaterRequest', array('waterRequest' => $model))) { throw new CHttpException(403, Yii::t('http_status', '403')); } $this->layout = '//layouts/column1'; $model->scenario = 'upload'; //Yii::log('Model loaded: '.print_r($model, true) , CLogger::LEVEL_INFO, 'actionUpload'); // DEBUG if (isset($_POST['WaterRequests'])) { //Yii::log('Sto caricando un file' , CLogger::LEVEL_INFO, 'actionUpload'); // DEBUG $model->shpfile = CUploadedFile::getInstance($model, 'shpfile'); $model->shxfile = CUploadedFile::getInstance($model, 'shxfile'); $model->dbffile = CUploadedFile::getInstance($model, 'dbffile'); $model->fileproj = $_POST['WaterRequests']['fileproj']; //Yii::log('shpfile='.print_r($model->shpfile, true) , CLogger::LEVEL_INFO, 'actionUpload'); // DEBUG $shp_folder = Yii::app()->params['shp_upload_folder']; if ($model->validate()) { $all_saved = $model->shpfile->saveAs(Yii::app()->basePath . $shp_folder . $model->shpfile->getname()) && $model->shxfile != null ? $model->shxfile->saveAs(Yii::app()->basePath . $shp_folder . $model->shxfile->getname()) : true && $model->dbffile != null ? $model->dbffile->saveAs(Yii::app()->basePath . $shp_folder . $model->dbffile->getname()) : true; if ($all_saved) { //Yii::log('CARICATO!' , CLogger::LEVEL_INFO, 'actionUpload'); // DEBUG $shpfile = Yii::app()->basePath . $shp_folder . $model->shpfile->getname(); if (isset($shpfile)) { //echo 'DATI ESTRATTI:<pre>'; //echo "\n Il sistema di riferimento selezionato è: ".$model->fileproj. "\n"; $options = array('noparts' => false); $shp = new ShapeFile($shpfile, $options); // along this file the class will use file.shx and file.dbf $i = 0; while ($record = $shp->getNext()) { $shp_data = $record->getShpData(); if (count($shp_data)) { // read shape data // store number of parts $oneshot = 0; $wkt = 'MULTIPOLYGON('; foreach ($shp_data['parts'] as $part) { $wkt = $wkt . ($oneshot++ == 0 ? '((' : ',(('); $coords = array(); foreach ($part['points'] as $point) { $coords[] = round($point['x'], 2) . ',' . round($point['y'], 2); } $search = array(',', ';'); $replace = array(' ', ','); $subject = implode(';', $coords); $wkt = $wkt . str_replace($search, $replace, $subject); $wkt = $wkt . '))'; } $wkt = $wkt . ')'; $saveres = WaterRequestGeometries::save_geom($wkt, $id, $model->fileproj); if ($saveres['result']) { Yii::log('Salvataggio avvenuto con successo. ID inserito: ' . $saveres['newid'], CLogger::LEVEL_INFO, 'actionUpload'); } else { Yii::log('Salvataggio non riuscito. WR_ID: ' . $id . ' iterazione: ' . $i, CLogger::LEVEL_INFO, 'actionUpload'); } } //echo "\n"; $i++; } //echo '</pre>'; } $this->redirect(array('view', 'id' => $model->id)); /* $this->render('upload',array( 'wr_id'=>$id, 'model'=>$model, 'shpfile'=>Yii::app()->basePath . $shp_folder . $model->shpfile->getname(), )); */ Yii::app()->end(); } // else Yii::log('FAIL!!' , CLogger::LEVEL_INFO, 'actionUpload'); // DEBUG } // else Yii::log('NON VALIDO!!' , CLogger::LEVEL_INFO, 'actionUpload'); // DEBUG } $this->render('upload', array('wr_id' => $id, 'model' => $model)); }
function shp_parse_fh($fh, $more) { # See also: # http://vis4.net/blog/de/2010/04/reading-esri-shapefiles-in-php/ fclose($fh); $args = array('noparts' => true); $shp = new ShapeFile($more['file']['path'], $args); if (!$shp) { return array('ok' => 0, 'error' => 'Failed to parse shapefile'); } $data = array(); $errors = array(); $record = 0; while ($record = $shp->getNext()) { # This is mostly here if/when we break in $parts loop if ($more['max_records'] && $record > $more['max_records']) { break; } # What to do about file specific metadata? $shp_data = $record->getShpData(); $parts = isset($shp_data['parts']) ? $shp_data['parts'] : array($shp_data); foreach ($parts as $pt) { $record++; if ($more['max_records'] && $record > $more['max_records']) { break; } $lat = $pt['y']; $lon = $pt['x']; if (!$lat || !$lon) { continue; } # check $more for reprojection nonsense here # the projection transformation stuff is very # much bleeding edge still and may turn in to # a yak (20110216/straup) # loadlib("geo_proj"); # $from = 'EPSG:26943'; # $pt = array('latitude' => $lat, 'longitude' => $lon); # $pt = geo_proj_transform($pt, $from, 'EPSG:4326'); if (!geo_utils_is_valid_latitude($lat)) { $errors[] = array('record' => $record, 'column' => 'latitude', 'error' => 'Invalid latitude'); continue; } if (!geo_utils_is_valid_longitude($lon)) { $errors[] = array('record' => $record, 'column' => 'longitude', 'error' => 'Invalid longitude'); continue; } $tmp = array('latitude' => $lat, 'longitude' => $lon); $data[] = $tmp; } } if (!count($data)) { return array('ok' => 0, 'error' => ''); } return array('ok' => 1, 'errors' => &$errors, 'data' => &$data); }
function upload() { require_once APPPATH . 'third_party/ShapeFile.php'; require_once APPPATH . 'third_party/Simplify.php'; $options = array('noparts' => false); try { $shp = new ShapeFile($_FILES['shp_file']['tmp_name'], null, $options); $polygons = array(); while ($record = $shp->getNext()) { $shpData = $record->getShpData(); foreach ($shpData['parts'] as $part) { $polygons[] = Simplify::line($part['points'], count($part['points']) / 1000000); } } } catch (Exception $e) { $this->session->set_flashdata('error', $e->getMessage()); } $this->user_polygons_model->save(array('user_id' => $this->tank_auth->get_user_id(), 'name' => $_FILES['shp_file']['name'], 'points' => json_encode($polygons))); redirect('/areas'); }
static function readRecordPoint(&$fp, $create_object = false, $options = null) { $data = array(); $data['x'] = ShapeFile::readAndUnpack('d', fread($fp, 8)); $data['y'] = ShapeFile::readAndUnpack('d', fread($fp, 8)); ////_d("Returning Point shp_data array = ".getArray($data)); return $data; }
<?php require_once '../shapefile.php'; try { echo "Creating Point Shapefile <br/>"; $ShapeFile = new ShapeFile('point.shp'); $ShapeFile->addPoint(array("x" => 3, "y" => 4)); $ShapeFile->addPoint(array("x" => 15, "y" => 15)); $ShapeFile->addPoint(array("x" => -25, "y" => 15)); $ShapeFile->write(); echo "Creating Multipoint Shapefile <br/>"; $ShapeFile = new ShapeFile('multipoint.shp'); $ShapeFile->addMultipoint(array(array("x" => 3, "y" => 4), array("x" => 15, "y" => 15), array("x" => -25, "y" => 13))); $ShapeFile->addMultipoint(array(array("x" => 1, "y" => 5), array("x" => 2, "y" => -9), array("x" => -4, "y" => 4))); $ShapeFile->write(); echo "Creating Polyline Shapefile <br/>"; $ShapeFile = new ShapeFile('polyline.shp'); $ShapeFile->addPolyline(array(array(array("x" => 3, "y" => 4), array("x" => 15, "y" => 15), array("x" => -25, "y" => 13)), array(array("x" => 1, "y" => 5), array("x" => 2, "y" => -9), array("x" => -4, "y" => 4)))); $ShapeFile->addPolyline(array(array(array("x" => 1, "y" => 1), array("x" => 9, "y" => 81), array("x" => 10, "y" => 100)), array(array("x" => -1, "y" => 1), array("x" => -3, "y" => 9), array("x" => -6, "y" => 36)))); $ShapeFile->write(); echo "Creating Polygon Shapefile <br/>"; $ShapeFile = new ShapeFile('polygon.shp'); $ShapeFile->addPolygon(array(array(array("x" => 1, "y" => 1), array("x" => 1, "y" => 10), array("x" => 10, "y" => 10), array("x" => 10, "y" => 1), array("x" => 1, "y" => 1)), array(array("x" => 2, "y" => 2), array("x" => 9, "y" => 2), array("x" => 5, "y" => 9), array("x" => 2, "y" => 2)))); $ShapeFile->write(); } catch (ShapeFileException $e) { exit('Error ' . $e->getCode() . ': ' . $e->getMessage()); } ?> </p> <p>Done. Please go back in your web browser.</p> </body> </html>
<html> <head> <title>Multipoint</title> </head> <body> <h3>Reading Multipoint Shapefile</h3> <?php require_once '../shapefile.php'; try { $ShapeFile = new ShapeFile('multipoint.shp'); echo "<pre>"; while ($record = $ShapeFile->getRecord(SHAPEFILE::GEOMETRY_ARRAY)) { if ($record['dbf']['deleted']) { echo "Deleted\n"; continue; } // Geometry print_r($record['shp']); // DBF Data print_r($record['dbf']); } } catch (ShapeFileException $e) { exit('Error ' . $e->getCode() . ': ' . $e->getMessage()); } ?> </body> </html>
/** * Loads DBF file if supported * * @return boolean */ private function _openDBFFile() { if (!ShapeFile::supports_dbase()) { $this->DBFFile = null; return true; } $dbf_name = $this->_getFilename('.dbf'); if (is_readable($dbf_name)) { $this->DBFFile = @dbase_open($dbf_name, 0); if (!$this->DBFFile) { $this->setError(sprintf('It wasn\'t possible to open the DBase file "%s"', $dbf_name)); return false; } } else { $this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbf_name)); return false; } return true; }
/** * Saves record to files * * @param file &$SHPFile Opened SHP file * @param file &$DBFFile Opened DBF file * @param integer $recordNumber Record number * * @return void */ public function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) { $this->SHPFile = $SHPFile; $this->DBFFile = $DBFFile; $this->recordNumber = $recordNumber; $this->_saveHeaders(); switch ($this->shapeType) { case 0: // Nothing to save break; case 1: $this->_savePointRecord(); break; case 21: $this->_savePointMRecord(); break; case 11: $this->_savePointZRecord(); break; case 3: $this->_savePolyLineRecord(); break; case 23: $this->_savePolyLineMRecord(); break; case 13: $this->_savePolyLineZRecord(); break; case 5: $this->_savePolygonRecord(); break; case 25: $this->_savePolygonMRecord(); break; case 15: $this->_savePolygonZRecord(); break; case 8: $this->_saveMultiPointRecord(); break; case 28: $this->_saveMultiPointMRecord(); break; case 18: $this->_saveMultiPointZRecord(); break; default: $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)); break; } if (ShapeFile::supports_dbase() && !is_null($this->DBFFile)) { $this->_saveDBFData(); } }