From 92f4d2145625faade25dfd0cf90f65eeabafc5e9 Mon Sep 17 00:00:00 2001 From: Madhura Jayaratne Date: Thu, 16 Jun 2011 08:17:16 +0530 Subject: [PATCH] OpenStreetMaps now zoom correctly. --- libraries/gis/pma_gis_geometry.php | 17 +++++++++++++++++ libraries/gis/pma_gis_geometrycollection.php | 13 +++++++------ libraries/gis/pma_gis_linestring.php | 8 ++++++-- libraries/gis/pma_gis_multilinestring.php | 7 +++++-- libraries/gis/pma_gis_multipoint.php | 8 ++++++-- libraries/gis/pma_gis_multipolygon.php | 7 +++++-- libraries/gis/pma_gis_point.php | 8 ++++++-- libraries/gis/pma_gis_polygon.php | 7 +++++-- libraries/gis/pma_gis_visualization.php | 9 +++++++-- tbl_gis_visualization.php | 7 +++++-- 10 files changed, 69 insertions(+), 22 deletions(-) diff --git a/libraries/gis/pma_gis_geometry.php b/libraries/gis/pma_gis_geometry.php index 60b043cfab..b54b756995 100644 --- a/libraries/gis/pma_gis_geometry.php +++ b/libraries/gis/pma_gis_geometry.php @@ -40,6 +40,23 @@ abstract class PMA_GIS_Geometry */ public abstract function scaleRow($spatial); + /** + * Returns OpenLayers.Bounds object that correspond to the bounds of GIS data. + * + * @param string $srid Spatial reference ID + * @param array $scale_data Data related to scaling + * + * @return OpenLayers.Bounds object that correspond to the bounds of GIS data + */ + protected function getBoundsForOl($srid, $scale_data) + { + return 'bound = new OpenLayers.Bounds(); bound.extend(new OpenLayers.LonLat(' + . $scale_data['minX'] . ', ' . $scale_data['minY'] . ').transform(new OpenLayers.Projection("EPSG:' + . $srid . '"), map.getProjectionObject())); bound.extend(new OpenLayers.LonLat(' + . $scale_data['maxX'] . ', ' . $scale_data['maxY'] . ').transform(new OpenLayers.Projection("EPSG:' + . $srid . '"), map.getProjectionObject()));'; + } + /** * Update the min, max values with the given point set. * diff --git a/libraries/gis/pma_gis_geometrycollection.php b/libraries/gis/pma_gis_geometrycollection.php index 76e753b6b3..db7c6ac44b 100644 --- a/libraries/gis/pma_gis_geometrycollection.php +++ b/libraries/gis/pma_gis_geometrycollection.php @@ -167,14 +167,15 @@ class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry /** * Prepares the code related to a row in the GIS dataset to visualize it with OpenLayers. * - * @param string $spatial GIS GEOMETRYCOLLECTION object - * @param int $srid Spatial reference ID - * @param string $label Label for the GIS GEOMETRYCOLLECTION object - * @param string $color Color for the GIS GEOMETRYCOLLECTION object + * @param string $spatial GIS GEOMETRYCOLLECTION object + * @param int $srid Spatial reference ID + * @param string $label Label for the GIS GEOMETRYCOLLECTION object + * @param string $color Color for the GIS GEOMETRYCOLLECTION object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $color) + public function prepareRowAsOl($spatial, $srid, $label, $color, $scale_data) { $row = ''; @@ -188,7 +189,7 @@ class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry $type = substr($sub_part, 0, $type_pos); $gis_obj = PMA_GIS_Factory::factory($type); - $row .= $gis_obj->prepareRowAsOl($sub_part, $srid, $label, $color); + $row .= $gis_obj->prepareRowAsOl($sub_part, $srid, $label, $color, $scale_data); } return $row; } diff --git a/libraries/gis/pma_gis_linestring.php b/libraries/gis/pma_gis_linestring.php index 747ac968ce..c6d4d799d7 100644 --- a/libraries/gis/pma_gis_linestring.php +++ b/libraries/gis/pma_gis_linestring.php @@ -160,10 +160,11 @@ class PMA_GIS_Linestring extends PMA_GIS_Geometry * @param int $srid Spatial reference ID * @param string $label Label for the GIS LINESTRING object * @param string $line_color Color for the GIS LINESTRING object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $line_color) + public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data) { $style_options = array( 'strokeColor' => $line_color, @@ -174,6 +175,8 @@ class PMA_GIS_Linestring extends PMA_GIS_Geometry if ($srid == 0) { $srid = 4326; } + $result = $this->getBoundsForOl($srid, $scale_data); + // Trim to remove leading 'LINESTRING(' and trailing ')' $linesrting = substr($spatial, 11, (strlen($spatial) - 12)); $points_arr = $this->extractPoints($linesrting, null); @@ -186,9 +189,10 @@ class PMA_GIS_Linestring extends PMA_GIS_Geometry $row = substr($row, 0, strlen($row) - 2); $row .= ')'; - return 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' . 'new OpenLayers.Geometry.LineString(' . $row . '), null, ' . json_encode($style_options) . '));'; + return $result; } } ?> diff --git a/libraries/gis/pma_gis_multilinestring.php b/libraries/gis/pma_gis_multilinestring.php index e520897cfe..ac8e44593b 100644 --- a/libraries/gis/pma_gis_multilinestring.php +++ b/libraries/gis/pma_gis_multilinestring.php @@ -185,10 +185,11 @@ class PMA_GIS_Multilinestring extends PMA_GIS_Geometry * @param int $srid Spatial reference ID * @param string $label Label for the GIS MULTILINESTRING object * @param string $line_color Color for the GIS MULTILINESTRING object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $line_color) + public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data) { $style_options = array( 'strokeColor' => $line_color, @@ -199,12 +200,14 @@ class PMA_GIS_Multilinestring extends PMA_GIS_Geometry if ($srid == 0) { $srid = 4326; } + $row = $this->getBoundsForOl($srid, $scale_data); + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19)); // Seperate each linestring $linestirngs = explode("),(", $multilinestirng); - $row = 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' . 'new OpenLayers.Geometry.MultiLineString(new Array('; foreach ($linestirngs as $linestring) { $points_arr = $this->extractPoints($linestring, null); diff --git a/libraries/gis/pma_gis_multipoint.php b/libraries/gis/pma_gis_multipoint.php index ee4a5d800e..b6a9c0c3ce 100644 --- a/libraries/gis/pma_gis_multipoint.php +++ b/libraries/gis/pma_gis_multipoint.php @@ -149,10 +149,11 @@ class PMA_GIS_Multipoint extends PMA_GIS_Geometry * @param int $srid Spatial reference ID * @param string $label Label for the GIS MULTIPOINT object * @param string $point_color Color for the GIS MULTIPOINT object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $point_color) + public function prepareRowAsOl($spatial, $srid, $label, $point_color, $scale_data) { $style_options = array( 'pointRadius' => 3, @@ -166,6 +167,8 @@ class PMA_GIS_Multipoint extends PMA_GIS_Geometry if ($srid == 0) { $srid = 4326; } + $result = $this->getBoundsForOl($srid, $scale_data); + // Trim to remove leading 'MULTIPOINT(' and trailing ')' $multipoint = substr($spatial, 11, (strlen($spatial) - 12)); $points_arr = $this->extractPoints($multipoint, null); @@ -178,9 +181,10 @@ class PMA_GIS_Multipoint extends PMA_GIS_Geometry $row = substr($row, 0, strlen($row) - 2); $row .= ')'; - return 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' . 'new OpenLayers.Geometry.MultiPoint(' . $row . '), null, ' . json_encode($style_options) . '));'; + return $result; } } ?> diff --git a/libraries/gis/pma_gis_multipolygon.php b/libraries/gis/pma_gis_multipolygon.php index 40ad6de372..40394bc060 100644 --- a/libraries/gis/pma_gis_multipolygon.php +++ b/libraries/gis/pma_gis_multipolygon.php @@ -229,10 +229,11 @@ class PMA_GIS_Multipolygon extends PMA_GIS_Geometry * @param int $srid Spatial reference ID * @param string $label Label for the GIS MULTIPOLYGON object * @param string $fill_color Color for the GIS MULTIPOLYGON object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $fill_color) + public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data) { $style_options = array( 'strokeColor' => '#000000', @@ -245,12 +246,14 @@ class PMA_GIS_Multipolygon extends PMA_GIS_Geometry if ($srid == 0) { $srid = 4326; } + $row = $this->getBoundsForOl($srid, $scale_data); + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' $multipolygon = substr($spatial, 15, (strlen($spatial) - 18)); // Seperate each polygon $polygons = explode(")),((", $multipolygon); - $row = 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' . 'new OpenLayers.Geometry.MultiPolygon(new Array('; foreach ($polygons as $polygon) { diff --git a/libraries/gis/pma_gis_point.php b/libraries/gis/pma_gis_point.php index bb255b3b3d..30ed0726fa 100644 --- a/libraries/gis/pma_gis_point.php +++ b/libraries/gis/pma_gis_point.php @@ -142,10 +142,11 @@ class PMA_GIS_Point extends PMA_GIS_Geometry * @param int $srid Spatial reference ID * @param string $label Label for the GIS POINT object * @param string $point_color Color for the GIS POINT object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $point_color) + public function prepareRowAsOl($spatial, $srid, $label, $point_color, $scale_data) { $style_options = array( 'pointRadius' => 3, @@ -159,14 +160,17 @@ class PMA_GIS_Point extends PMA_GIS_Geometry if ($srid == 0) { $srid = 4326; } + $result = $this->getBoundsForOl($srid, $scale_data); + // Trim to remove leading 'POINT(' and trailing ')' $point = substr($spatial, 6, (strlen($spatial) - 7)); $points_arr = $this->extractPoints($point, null); - return 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector((' + $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector((' . 'new OpenLayers.Geometry.Point(' . $points_arr[0][0] . ', ' . $points_arr[0][1] . ')' . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject())),' . ' null, ' . json_encode($style_options) . '));'; + return $result; } } ?> diff --git a/libraries/gis/pma_gis_polygon.php b/libraries/gis/pma_gis_polygon.php index 049155adc1..c4c580ebce 100644 --- a/libraries/gis/pma_gis_polygon.php +++ b/libraries/gis/pma_gis_polygon.php @@ -212,10 +212,11 @@ class PMA_GIS_Polygon extends PMA_GIS_Geometry * @param int $srid Spatial reference ID * @param string $label Label for the GIS POLYGON object * @param string $fill_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling * * @return the code related to a row in the GIS dataset */ - public function prepareRowAsOl($spatial, $srid, $label, $fill_color) + public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data) { $style_options = array( 'strokeColor' => '#000000', @@ -228,10 +229,12 @@ class PMA_GIS_Polygon extends PMA_GIS_Geometry if ($srid == 0) { $srid = 4326; } + $row = $this->getBoundsForOl($srid, $scale_data); + // Trim to remove leading 'POLYGON((' and trailing '))' $polygon = substr($spatial, 9, (strlen($spatial) - 11)); - $row = 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' . 'new OpenLayers.Geometry.Polygon(new Array('; // If the polygon doesnt have an inner polygon if (strpos($polygon, "),(") === false) { diff --git a/libraries/gis/pma_gis_visualization.php b/libraries/gis/pma_gis_visualization.php index db551e48cb..2a971be64a 100644 --- a/libraries/gis/pma_gis_visualization.php +++ b/libraries/gis/pma_gis_visualization.php @@ -251,7 +251,8 @@ class PMA_GIS_Visualization public function asOl() { $this->init(); - $output = $this->_prepareDataSet($this->_data, 0, null, 'ol', ''); + $scale_data = $this->_scaleDataSet($this->_data); + $output = $this->_prepareDataSet($this->_data, 0, $scale_data, 'ol', ''); return $output; } @@ -355,6 +356,10 @@ class PMA_GIS_Visualization 'scale' => $scale, 'x' => $x, 'y' => $y, + 'minX' => $min_max['minX'], + 'maxX' => $min_max['maxX'], + 'minY' => $min_max['minY'], + 'maxY' => $min_max['maxY'], 'height' => $this->_settings['height'], ); } @@ -408,7 +413,7 @@ class PMA_GIS_Visualization } elseif ($format == 'ol') { $results .= $gis_obj->prepareRowAsOl( $row[$this->_settings['spatialColumn']], $row['srid'], - $label, $this->_settings['colors'][$index] + $label, $this->_settings['colors'][$index], $scale_data ); } $color_number++; diff --git a/tbl_gis_visualization.php b/tbl_gis_visualization.php index 5bbe890972..93e02efcfb 100644 --- a/tbl_gis_visualization.php +++ b/tbl_gis_visualization.php @@ -52,7 +52,6 @@ if (PMA_isValid($_REQUEST['visualizationSettings'], 'array')) { $visualizationSettings = $_REQUEST['visualizationSettings']; } -// If label column is not set, use first non-geometric colum as label column if (! isset($visualizationSettings['labelColumn']) && isset($labelCandidates[0])) { $visualizationSettings['labelColumn'] = ''; } @@ -144,10 +143,14 @@ if ($format == 'svg') { // create a vector layer var vectorLayer = new OpenLayers.Layer.Vector("Data"); + var bound; map.addLayer(vectorLayer); - map.setCenter(new OpenLayers.LonLat(0, 0), 2); + map.zoomToExtent(bound); + if (map.getZoom() < 2) { + map.zoomTo(2); + } map.addControl(new OpenLayers.Control.LayerSwitcher()); map.addControl(new OpenLayers.Control.MousePosition()); }