/** * 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'); }