/** * Handles the whole import logic * * @return void */ public function doImport() { global $db, $error, $finished, $compression, $import_file, $local_import_file, $message; $GLOBALS['finished'] = false; $shp = new ShapeFile(1); // If the zip archive has more than one file, // get the correct content to the buffer from .shp file. if ($compression == 'application/zip' && PMA_getNoOfFilesInZip($import_file) > 1) { $zip_content = PMA_getZipContents($import_file, '/^.*\\.shp$/i'); $GLOBALS['import_text'] = $zip_content['data']; } $temp_dbf_file = false; // We need dbase extension to handle .dbf file if (extension_loaded('dbase')) { // If we can extract the zip archive to 'TempDir' // and use the files in it for import if ($compression == 'application/zip' && !empty($GLOBALS['cfg']['TempDir']) && is_writable($GLOBALS['cfg']['TempDir'])) { $dbf_file_name = PMA_findFileFromZipArchive('/^.*\\.dbf$/i', $import_file); // If the corresponding .dbf file is in the zip archive if ($dbf_file_name) { // Extract the .dbf file and point to it. $extracted = PMA_zipExtract($import_file, realpath($GLOBALS['cfg']['TempDir']), array($dbf_file_name)); if ($extracted) { $dbf_file_path = realpath($GLOBALS['cfg']['TempDir']) . (PMA_IS_WINDOWS ? '\\' : '/') . $dbf_file_name; $temp_dbf_file = true; // Replace the .dbf with .*, as required // by the bsShapeFiles library. $file_name = mb_substr($dbf_file_path, 0, mb_strlen($dbf_file_path) - 4) . '.*'; $shp->FileName = $file_name; } } } elseif (!empty($local_import_file) && !empty($GLOBALS['cfg']['UploadDir']) && $compression == 'none') { // If file is in UploadDir, use .dbf file in the same UploadDir // to load extra data. // Replace the .shp with .*, // so the bsShapeFiles library correctly locates .dbf file. $file_name = mb_substr($import_file, 0, mb_strlen($import_file) - 4) . '.*'; $shp->FileName = $file_name; } } // Load data $shp->loadFromFile(''); if ($shp->lastError != "") { $error = true; $message = PMA\libraries\Message::error(__('There was an error importing the ESRI shape file: "%s".')); $message->addParam($shp->lastError); return; } // Delete the .dbf file extracted to 'TempDir' if ($temp_dbf_file && isset($dbf_file_path) && file_exists($dbf_file_path)) { unlink($dbf_file_path); } $esri_types = array(0 => 'Null Shape', 1 => 'Point', 3 => 'PolyLine', 5 => 'Polygon', 8 => 'MultiPoint', 11 => 'PointZ', 13 => 'PolyLineZ', 15 => 'PolygonZ', 18 => 'MultiPointZ', 21 => 'PointM', 23 => 'PolyLineM', 25 => 'PolygonM', 28 => 'MultiPointM', 31 => 'MultiPatch'); switch ($shp->shapeType) { // ESRI Null Shape case 0: break; // ESRI Point // ESRI Point case 1: $gis_type = 'point'; break; // ESRI PolyLine // ESRI PolyLine case 3: $gis_type = 'multilinestring'; break; // ESRI Polygon // ESRI Polygon case 5: $gis_type = 'multipolygon'; break; // ESRI MultiPoint // ESRI MultiPoint case 8: $gis_type = 'multipoint'; break; default: $error = true; if (!isset($esri_types[$shp->shapeType])) { $message = PMA\libraries\Message::error(__('You tried to import an invalid file or the imported file' . ' contains invalid data!')); } else { $message = PMA\libraries\Message::error(__('MySQL Spatial Extension does not support ESRI type "%s".')); $message->addParam($esri_types[$shp->shapeType]); } return; } if (isset($gis_type)) { /** @var GISMultilinestring|\PMA\libraries\gis\GISMultipoint|\PMA\libraries\gis\GISPoint|GISPolygon $gis_obj */ $gis_obj = GISFactory::factory($gis_type); } else { $gis_obj = null; } $num_rows = count($shp->records); // If .dbf file is loaded, the number of extra data columns $num_data_cols = isset($shp->DBFHeader) ? count($shp->DBFHeader) : 0; $rows = array(); $col_names = array(); if ($num_rows != 0) { foreach ($shp->records as $record) { $tempRow = array(); if ($gis_obj == null) { $tempRow[] = null; } else { $tempRow[] = "GeomFromText('" . $gis_obj->getShape($record->SHPData) . "')"; } if (isset($shp->DBFHeader)) { foreach ($shp->DBFHeader as $c) { $cell = trim($record->DBFData[$c[0]]); if (!strcmp($cell, '')) { $cell = 'NULL'; } $tempRow[] = $cell; } } $rows[] = $tempRow; } } if (count($rows) == 0) { $error = true; $message = PMA\libraries\Message::error(__('The imported file does not contain any data!')); return; } // Column names for spatial column and the rest of the columns, // if they are available $col_names[] = 'SPATIAL'; for ($n = 0; $n < $num_data_cols; $n++) { $col_names[] = $shp->DBFHeader[$n][0]; } // Set table name based on the number of tables if (mb_strlen($db)) { $result = $GLOBALS['dbi']->fetchResult('SHOW TABLES'); $table_name = 'TABLE ' . (count($result) + 1); } else { $table_name = 'TBL_NAME'; } $tables = array(array($table_name, $col_names, $rows)); // Use data from shape file to chose best-fit MySQL types for each column $analyses = array(); $analyses[] = PMA_analyzeTable($tables[0]); $table_no = 0; $spatial_col = 0; $analyses[$table_no][TYPES][$spatial_col] = GEOMETRY; $analyses[$table_no][FORMATTEDSQL][$spatial_col] = true; // Set database name to the currently selected one, if applicable if (mb_strlen($db)) { $db_name = $db; $options = array('create_db' => false); } else { $db_name = 'SHP_DB'; $options = null; } // Created and execute necessary SQL statements from data $null_param = null; PMA_buildSQL($db_name, $tables, $analyses, $null_param, $options); unset($tables); unset($analyses); $finished = true; $error = false; // Commit any possible data in buffers PMA_importRunQuery(); }
/** * Prepares and return the dataset as needed by the visualization. * * @param array $data Raw data * @param array $scale_data Data related to scaling * @param string $format Format of the visualization * @param object $results Image object in the case of png * TCPDF object in the case of pdf * * @return mixed the formatted array of data * @access private */ private function _prepareDataSet($data, $scale_data, $format, $results) { $color_number = 0; // loop through the rows foreach ($data as $row) { $index = $color_number % sizeof($this->_settings['colors']); // Figure out the data type $ref_data = $row[$this->_settings['spatialColumn']]; $type_pos = mb_stripos($ref_data, '('); $type = mb_substr($ref_data, 0, $type_pos); $gis_obj = GISFactory::factory($type); if (!$gis_obj) { continue; } $label = ''; if (isset($this->_settings['labelColumn']) && isset($row[$this->_settings['labelColumn']])) { $label = $row[$this->_settings['labelColumn']]; } if ($format == 'svg') { $results .= $gis_obj->prepareRowAsSvg($row[$this->_settings['spatialColumn']], $label, $this->_settings['colors'][$index], $scale_data); } elseif ($format == 'png') { $results = $gis_obj->prepareRowAsPng($row[$this->_settings['spatialColumn']], $label, $this->_settings['colors'][$index], $scale_data, $results); } elseif ($format == 'pdf') { $results = $gis_obj->prepareRowAsPdf($row[$this->_settings['spatialColumn']], $label, $this->_settings['colors'][$index], $scale_data, $results); } elseif ($format == 'ol') { $results .= $gis_obj->prepareRowAsOl($row[$this->_settings['spatialColumn']], $row['srid'], $label, $this->_settings['colors'][$index], $scale_data); } $color_number++; } return $results; }
/** * Test factory method * * @param string $type geometry type * @param object $geom geometry object * * @dataProvider providerForTestFactory * @return void */ public function testFactory($type, $geom) { $this->assertInstanceOf($geom, GISFactory::factory($type)); }
// Extract from field's values if available, if not use the column type passed. if (!isset($gis_data['gis_type'])) { if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') { $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']); } if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') { $start = substr($_REQUEST['value'], 0, 1) == "'" ? 1 : 0; $gis_data['gis_type'] = mb_substr($_REQUEST['value'], $start, mb_strpos($_REQUEST['value'], "(") - $start); } if (!isset($gis_data['gis_type']) || !in_array($gis_data['gis_type'], $gis_types)) { $gis_data['gis_type'] = $gis_types[0]; } } $geom_type = $gis_data['gis_type']; // Generate parameters from value passed. $gis_obj = GISFactory::factory($geom_type); if (isset($_REQUEST['value'])) { $gis_data = array_merge($gis_data, $gis_obj->generateParams($_REQUEST['value'])); } // Generate Well Known Text $srid = isset($gis_data['srid']) && $gis_data['srid'] != '' ? htmlspecialchars($gis_data['srid']) : 0; $wkt = $gis_obj->generateWkt($gis_data, 0); $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0'); $result = "'" . $wkt . "'," . $srid; // Generate SVG based visualization $visualizationSettings = array('width' => 450, 'height' => 300, 'spatialColumn' => 'wkt'); $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid)); $visualization = GISVisualization::getByData($data, $visualizationSettings)->toImage('svg'); $open_layers = GISVisualization::getByData($data, $visualizationSettings)->asOl(); // If the call is to update the WKT and visualization make an AJAX response if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
/** * Generates parameters for the GIS data editor from the value of the GIS column. * * @param string $value of the GIS column * * @return array parameters for the GIS editor from the value of the GIS column * @access public */ public function generateParams($value) { $params = array(); $data = GISGeometry::generateParams($value); $params['srid'] = $data['srid']; $wkt = $data['wkt']; // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' $goem_col = mb_substr($wkt, 19, mb_strlen($wkt) - 20); // Split the geometry collection object to get its constituents. $sub_parts = $this->_explodeGeomCol($goem_col); $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts); $i = 0; foreach ($sub_parts as $sub_part) { $type_pos = mb_stripos($sub_part, '('); $type = mb_substr($sub_part, 0, $type_pos); $gis_obj = GISFactory::factory($type); if (!$gis_obj) { continue; } $params = array_merge($params, $gis_obj->generateParams($sub_part, $i)); $i++; } return $params; }